blob: 4c74424c78b049068c0f118de398640781796c1b [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 Viroff01bb482011-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
115static ctl_table raid_table[] = {
116 {
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
133static ctl_table raid_dir_table[] = {
134 {
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
143static ctl_table raid_root_table[] = {
144 {
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
NeilBrownd2eb35a2011-07-28 11:31:48 +1000186void md_trim_bio(struct bio *bio, int offset, int size)
187{
188 /* 'bio' is a cloned bio which we need to trim to match
189 * the given offset and size.
190 * This requires adjusting bi_sector, bi_size, and bi_io_vec
191 */
192 int i;
193 struct bio_vec *bvec;
194 int sofar = 0;
195
196 size <<= 9;
197 if (offset == 0 && size == bio->bi_size)
198 return;
199
200 bio->bi_sector += offset;
201 bio->bi_size = size;
202 offset <<= 9;
203 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
204
205 while (bio->bi_idx < bio->bi_vcnt &&
206 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
207 /* remove this whole bio_vec */
208 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
209 bio->bi_idx++;
210 }
211 if (bio->bi_idx < bio->bi_vcnt) {
212 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
213 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
214 }
215 /* avoid any complications with bi_idx being non-zero*/
216 if (bio->bi_idx) {
217 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
218 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
219 bio->bi_vcnt -= bio->bi_idx;
220 bio->bi_idx = 0;
221 }
222 /* Make sure vcnt and last bv are not too big */
223 bio_for_each_segment(bvec, bio, i) {
224 if (sofar + bvec->bv_len > size)
225 bvec->bv_len = size - sofar;
226 if (bvec->bv_len == 0) {
227 bio->bi_vcnt = i;
228 break;
229 }
230 sofar += bvec->bv_len;
231 }
232}
233EXPORT_SYMBOL_GPL(md_trim_bio);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/*
NeilBrownd7603b72006-01-06 00:20:30 -0800236 * We have a system wide 'event count' that is incremented
237 * on any 'interesting' event, and readers of /proc/mdstat
238 * can use 'poll' or 'select' to find out when the event
239 * count increases.
240 *
241 * Events are:
242 * start array, stop array, error, add device, remove device,
243 * start build, activate spare
244 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800245static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800246static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100247void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800248{
249 atomic_inc(&md_event_count);
250 wake_up(&md_event_waiters);
251}
NeilBrown29269552006-03-27 01:18:10 -0800252EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800253
NeilBrownc331eb02006-05-30 21:27:13 -0700254/* Alternate version that can be called from interrupts
255 * when calling sysfs_notify isn't needed.
256 */
NeilBrownfd01b882011-10-11 16:47:53 +1100257static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700258{
259 atomic_inc(&md_event_count);
260 wake_up(&md_event_waiters);
261}
262
NeilBrownd7603b72006-01-06 00:20:30 -0800263/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 * Enables to iterate over all existing md arrays
265 * all_mddevs_lock protects this list.
266 */
267static LIST_HEAD(all_mddevs);
268static DEFINE_SPINLOCK(all_mddevs_lock);
269
270
271/*
272 * iterates through all used mddevs in the system.
273 * We take care to grab the all_mddevs_lock whenever navigating
274 * the list, and to always hold a refcount when unlocked.
275 * Any code which breaks out of this loop while own
276 * a reference to the current mddev and must mddev_put it.
277 */
NeilBrownfd01b882011-10-11 16:47:53 +1100278#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 \
280 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100281 _tmp = all_mddevs.next; \
282 _mddev = NULL;}); \
283 ({ if (_tmp != &all_mddevs) \
284 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100286 if (_mddev) mddev_put(_mddev); \
287 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
288 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100290 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 )
292
293
NeilBrown409c57f2009-03-31 14:39:39 +1100294/* Rather than calling directly into the personality make_request function,
295 * IO requests come here first so that we can check if the device is
296 * being suspended pending a reconfiguration.
297 * We hold a refcount over the call to ->make_request. By the time that
298 * call has finished, the bio has been linked into some internal structure
299 * and so is visible to ->quiesce(), so we don't need the refcount any more.
300 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200301static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
NeilBrown49077322010-03-25 16:20:56 +1100303 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100304 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100305 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500306 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100307
NeilBrown0ca69882011-01-14 09:14:33 +1100308 if (mddev == NULL || mddev->pers == NULL
309 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100310 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200311 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100312 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100313 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
314 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
315 return;
316 }
NeilBrown0ca69882011-01-14 09:14:33 +1100317 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100318 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200319 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100320 DEFINE_WAIT(__wait);
321 for (;;) {
322 prepare_to_wait(&mddev->sb_wait, &__wait,
323 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200324 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100325 break;
326 rcu_read_unlock();
327 schedule();
328 rcu_read_lock();
329 }
330 finish_wait(&mddev->sb_wait, &__wait);
331 }
332 atomic_inc(&mddev->active_io);
333 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100334
Chris Masone91ece52011-02-07 19:21:48 -0500335 /*
336 * save the sectors now since our bio can
337 * go away inside make_request
338 */
339 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200340 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100341
342 cpu = part_stat_lock();
343 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500344 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100345 part_stat_unlock();
346
NeilBrown409c57f2009-03-31 14:39:39 +1100347 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
348 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
NeilBrown9e35b992010-04-06 14:23:02 +1000351/* mddev_suspend makes sure no new requests are submitted
352 * to the device, and that any requests that have been submitted
353 * are completely handled.
354 * Once ->stop is called and completes, the module will be completely
355 * unused.
356 */
NeilBrownfd01b882011-10-11 16:47:53 +1100357void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100358{
359 BUG_ON(mddev->suspended);
360 mddev->suspended = 1;
361 synchronize_rcu();
362 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
363 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500364
365 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100366}
NeilBrown390ee602010-06-01 19:37:27 +1000367EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100368
NeilBrownfd01b882011-10-11 16:47:53 +1100369void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100370{
371 mddev->suspended = 0;
372 wake_up(&mddev->sb_wait);
373 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500374
Jonathan Brassow47525e52012-05-22 13:55:29 +1000375 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500376 md_wakeup_thread(mddev->thread);
377 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100378}
NeilBrown390ee602010-06-01 19:37:27 +1000379EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100380
NeilBrownfd01b882011-10-11 16:47:53 +1100381int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000382{
383 return mddev->suspended;
384}
385EXPORT_SYMBOL(mddev_congested);
386
NeilBrowna2826aa2009-12-14 12:49:49 +1100387/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200388 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100389 */
390
Tejun Heoe9c74692010-09-03 11:56:18 +0200391static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100392{
NeilBrown3cb03002011-10-11 16:45:26 +1100393 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100394 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100395
396 rdev_dec_pending(rdev, mddev);
397
398 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200399 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200400 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100401 }
402 bio_put(bio);
403}
404
NeilBrowna7a07e62010-12-09 16:04:25 +1100405static void md_submit_flush_data(struct work_struct *ws);
406
NeilBrowna035fc32010-12-09 16:17:51 +1100407static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100408{
NeilBrownfd01b882011-10-11 16:47:53 +1100409 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100410 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100411
NeilBrowna7a07e62010-12-09 16:04:25 +1100412 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
413 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100414 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100415 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100416 if (rdev->raid_disk >= 0 &&
417 !test_bit(Faulty, &rdev->flags)) {
418 /* Take two references, one is dropped
419 * when request finishes, one after
420 * we reclaim rcu_read_lock
421 */
422 struct bio *bi;
423 atomic_inc(&rdev->nr_pending);
424 atomic_inc(&rdev->nr_pending);
425 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000426 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200427 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100428 bi->bi_private = rdev;
429 bi->bi_bdev = rdev->bdev;
430 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200431 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100432 rcu_read_lock();
433 rdev_dec_pending(rdev, mddev);
434 }
435 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100436 if (atomic_dec_and_test(&mddev->flush_pending))
437 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100438}
439
Tejun Heoe9c74692010-09-03 11:56:18 +0200440static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100441{
NeilBrownfd01b882011-10-11 16:47:53 +1100442 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200443 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100444
Tejun Heoe9c74692010-09-03 11:56:18 +0200445 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100446 /* an empty barrier - all done */
447 bio_endio(bio, 0);
448 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200449 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200450 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100451 }
NeilBrown2b74e122010-12-09 15:59:01 +1100452
453 mddev->flush_bio = NULL;
454 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100455}
456
NeilBrownfd01b882011-10-11 16:47:53 +1100457void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100458{
459 spin_lock_irq(&mddev->write_lock);
460 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200461 !mddev->flush_bio,
Lukas Czernereed8c022012-11-30 11:42:40 +0100462 mddev->write_lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200463 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100464 spin_unlock_irq(&mddev->write_lock);
465
NeilBrowna035fc32010-12-09 16:17:51 +1100466 INIT_WORK(&mddev->flush_work, submit_flushes);
467 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100468}
Tejun Heoe9c74692010-09-03 11:56:18 +0200469EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100470
NeilBrown74018dc2012-07-31 09:08:15 +0200471void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000472{
NeilBrown9cbb1752012-07-31 09:08:14 +0200473 struct mddev *mddev = cb->data;
474 md_wakeup_thread(mddev->thread);
475 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000476}
NeilBrown9cbb1752012-07-31 09:08:14 +0200477EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000478
NeilBrownfd01b882011-10-11 16:47:53 +1100479static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 atomic_inc(&mddev->active);
482 return mddev;
483}
484
Dan Williams5fd3a172009-03-04 00:57:25 -0700485static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100486
NeilBrownfd01b882011-10-11 16:47:53 +1100487static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
NeilBrowna167f662010-10-26 18:31:13 +1100489 struct bio_set *bs = NULL;
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
492 return;
NeilBrownd3374822009-01-09 08:31:10 +1100493 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100494 mddev->ctime == 0 && !mddev->hold_active) {
495 /* Array is not configured at all, and not held active,
496 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100497 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100498 bs = mddev->bio_set;
499 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100500 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200501 /* We did a probe so need to clean up. Call
502 * queue_work inside the spinlock so that
503 * flush_workqueue() after mddev_find will
504 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100505 */
506 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200507 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100508 } else
509 kfree(mddev);
510 }
511 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100512 if (bs)
513 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
NeilBrownfd01b882011-10-11 16:47:53 +1100516void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100517{
518 mutex_init(&mddev->open_mutex);
519 mutex_init(&mddev->reconfig_mutex);
520 mutex_init(&mddev->bitmap_info.mutex);
521 INIT_LIST_HEAD(&mddev->disks);
522 INIT_LIST_HEAD(&mddev->all_mddevs);
523 init_timer(&mddev->safemode_timer);
524 atomic_set(&mddev->active, 1);
525 atomic_set(&mddev->openers, 0);
526 atomic_set(&mddev->active_io, 0);
527 spin_lock_init(&mddev->write_lock);
528 atomic_set(&mddev->flush_pending, 0);
529 init_waitqueue_head(&mddev->sb_wait);
530 init_waitqueue_head(&mddev->recovery_wait);
531 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000532 mddev->reshape_backwards = 0;
NeilBrownfafd7fb2010-04-01 15:55:30 +1100533 mddev->resync_min = 0;
534 mddev->resync_max = MaxSector;
535 mddev->level = LEVEL_NONE;
536}
NeilBrown390ee602010-06-01 19:37:27 +1000537EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100538
NeilBrownfd01b882011-10-11 16:47:53 +1100539static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
NeilBrownfd01b882011-10-11 16:47:53 +1100541 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
NeilBrown8f5f02c2011-02-16 13:58:51 +1100543 if (unit && MAJOR(unit) != MD_MAJOR)
544 unit &= ~((1<<MdpMinorShift)-1);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 retry:
547 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
NeilBrownefeb53c2009-01-09 08:31:10 +1100549 if (unit) {
550 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
551 if (mddev->unit == unit) {
552 mddev_get(mddev);
553 spin_unlock(&all_mddevs_lock);
554 kfree(new);
555 return mddev;
556 }
557
558 if (new) {
559 list_add(&new->all_mddevs, &all_mddevs);
560 spin_unlock(&all_mddevs_lock);
561 new->hold_active = UNTIL_IOCTL;
562 return new;
563 }
564 } else if (new) {
565 /* find an unused unit number */
566 static int next_minor = 512;
567 int start = next_minor;
568 int is_free = 0;
569 int dev = 0;
570 while (!is_free) {
571 dev = MKDEV(MD_MAJOR, next_minor);
572 next_minor++;
573 if (next_minor > MINORMASK)
574 next_minor = 0;
575 if (next_minor == start) {
576 /* Oh dear, all in use. */
577 spin_unlock(&all_mddevs_lock);
578 kfree(new);
579 return NULL;
580 }
581
582 is_free = 1;
583 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
584 if (mddev->unit == dev) {
585 is_free = 0;
586 break;
587 }
588 }
589 new->unit = dev;
590 new->md_minor = MINOR(dev);
591 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 list_add(&new->all_mddevs, &all_mddevs);
593 spin_unlock(&all_mddevs_lock);
594 return new;
595 }
596 spin_unlock(&all_mddevs_lock);
597
NeilBrown9ffae0c2006-01-06 00:20:32 -0800598 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (!new)
600 return NULL;
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 new->unit = unit;
603 if (MAJOR(unit) == MD_MAJOR)
604 new->md_minor = MINOR(unit);
605 else
606 new->md_minor = MINOR(unit) >> MdpMinorShift;
607
NeilBrownfafd7fb2010-04-01 15:55:30 +1100608 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 goto retry;
611}
612
NeilBrownfd01b882011-10-11 16:47:53 +1100613static inline int mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800615 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
NeilBrownfd01b882011-10-11 16:47:53 +1100618static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100619{
620 return mutex_is_locked(&mddev->reconfig_mutex);
621}
622
NeilBrownfd01b882011-10-11 16:47:53 +1100623static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800625 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
NeilBrownb6eb1272010-04-15 10:13:47 +1000628static struct attribute_group md_redundancy_group;
629
NeilBrownfd01b882011-10-11 16:47:53 +1100630static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
NeilBrowna64c8762010-04-14 17:15:37 +1000632 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000633 /* These cannot be removed under reconfig_mutex as
634 * an access to the files will try to take reconfig_mutex
635 * while holding the file unremovable, which leads to
636 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000637 * So hold set sysfs_active while the remove in happeing,
638 * and anything else which might set ->to_remove or my
639 * otherwise change the sysfs namespace will fail with
640 * -EBUSY if sysfs_active is still set.
641 * We set sysfs_active under reconfig_mutex and elsewhere
642 * test it under the same mutex to ensure its correct value
643 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000644 */
NeilBrowna64c8762010-04-14 17:15:37 +1000645 struct attribute_group *to_remove = mddev->to_remove;
646 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000647 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000648 mutex_unlock(&mddev->reconfig_mutex);
649
NeilBrown00bcb4a2010-06-01 19:37:23 +1000650 if (mddev->kobj.sd) {
651 if (to_remove != &md_redundancy_group)
652 sysfs_remove_group(&mddev->kobj, to_remove);
653 if (mddev->pers == NULL ||
654 mddev->pers->sync_request == NULL) {
655 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
656 if (mddev->sysfs_action)
657 sysfs_put(mddev->sysfs_action);
658 mddev->sysfs_action = NULL;
659 }
NeilBrowna64c8762010-04-14 17:15:37 +1000660 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000661 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000662 } else
663 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Chris Dunlop751e67c2011-10-19 16:48:26 +1100665 /* As we've dropped the mutex we need a spinlock to
666 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000667 */
668 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700669 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000670 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
NeilBrownfd01b882011-10-11 16:47:53 +1100673static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
NeilBrown3cb03002011-10-11 16:45:26 +1100675 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
NeilBrowndafb20f2012-03-19 12:46:39 +1100677 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (rdev->desc_nr == nr)
679 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return NULL;
682}
683
NeilBrown1ca69c42012-10-11 13:37:33 +1100684static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
685{
686 struct md_rdev *rdev;
687
688 rdev_for_each_rcu(rdev, mddev)
689 if (rdev->desc_nr == nr)
690 return rdev;
691
692 return NULL;
693}
694
695static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
NeilBrown3cb03002011-10-11 16:45:26 +1100697 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
NeilBrowndafb20f2012-03-19 12:46:39 +1100699 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (rdev->bdev->bd_dev == dev)
701 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return NULL;
704}
705
NeilBrown1ca69c42012-10-11 13:37:33 +1100706static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
707{
708 struct md_rdev *rdev;
709
710 rdev_for_each_rcu(rdev, mddev)
711 if (rdev->bdev->bd_dev == dev)
712 return rdev;
713
714 return NULL;
715}
716
NeilBrown84fc4b52011-10-11 16:49:58 +1100717static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800718{
NeilBrown84fc4b52011-10-11 16:49:58 +1100719 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800720 list_for_each_entry(pers, &pers_list, list) {
721 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800722 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800723 if (strcmp(pers->name, clevel)==0)
724 return pers;
725 }
NeilBrown2604b702006-01-06 00:20:36 -0800726 return NULL;
727}
728
Andre Nollb73df2d2008-07-11 22:02:23 +1000729/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100730static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100732 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000733 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
NeilBrown3cb03002011-10-11 16:45:26 +1100736static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 if (rdev->sb_page)
739 MD_BUG();
740
741 rdev->sb_page = alloc_page(GFP_KERNEL);
742 if (!rdev->sb_page) {
743 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000744 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
747 return 0;
748}
749
NeilBrown545c8792012-05-22 13:54:30 +1000750void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800753 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 rdev->sb_loaded = 0;
755 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000756 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100757 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
NeilBrown2699b672011-07-28 11:31:47 +1000759 if (rdev->bb_page) {
760 put_page(rdev->bb_page);
761 rdev->bb_page = NULL;
762 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000763 kfree(rdev->badblocks.page);
764 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
NeilBrown545c8792012-05-22 13:54:30 +1000766EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
NeilBrown6712ecf2007-09-27 12:47:43 +0200768static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700769{
NeilBrown3cb03002011-10-11 16:45:26 +1100770 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100771 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700772
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700773 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
774 printk("md: super_written gets error=%d, uptodate=%d\n",
775 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
776 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800777 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700778 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700779
NeilBrowna9701a32005-11-08 21:39:34 -0800780 if (atomic_dec_and_test(&mddev->pending_writes))
781 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700782 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700783}
784
NeilBrownfd01b882011-10-11 16:47:53 +1100785void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700786 sector_t sector, int size, struct page *page)
787{
788 /* write first size bytes of page to sector of rdev
789 * Increment mddev->pending_writes before returning
790 * and decrement it on completion, waking up sb_wait
791 * if zero is reached.
792 * If an error occurred, call md_error
793 */
NeilBrowna167f662010-10-26 18:31:13 +1100794 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700795
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100796 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700797 bio->bi_sector = sector;
798 bio_add_page(bio, page, size, 0);
799 bio->bi_private = rdev;
800 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800801
NeilBrown7bfa19f2005-06-21 17:17:28 -0700802 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000803 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800804}
805
NeilBrownfd01b882011-10-11 16:47:53 +1100806void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800807{
Tejun Heoe9c74692010-09-03 11:56:18 +0200808 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800809 DEFINE_WAIT(wq);
810 for(;;) {
811 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
812 if (atomic_read(&mddev->pending_writes)==0)
813 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800814 schedule();
815 }
816 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700817}
818
NeilBrown6712ecf2007-09-27 12:47:43 +0200819static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
NeilBrown3cb03002011-10-11 16:45:26 +1100824int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100825 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
NeilBrowna167f662010-10-26 18:31:13 +1100827 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 struct completion event;
829 int ret;
830
Jens Axboe721a9602011-03-09 11:56:30 +0100831 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100833 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
834 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100835 if (metadata_op)
836 bio->bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000837 else if (rdev->mddev->reshape_position != MaxSector &&
838 (rdev->mddev->reshape_backwards ==
839 (sector >= rdev->mddev->reshape_position)))
840 bio->bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100841 else
842 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 bio_add_page(bio, page, size, 0);
844 init_completion(&event);
845 bio->bi_private = &event;
846 bio->bi_end_io = bi_complete;
847 submit_bio(rw, bio);
848 wait_for_completion(&event);
849
850 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
851 bio_put(bio);
852 return ret;
853}
NeilBrowna8745db2006-01-06 00:20:34 -0800854EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
NeilBrown3cb03002011-10-11 16:45:26 +1100856static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 char b[BDEVNAME_SIZE];
859 if (!rdev->sb_page) {
860 MD_BUG();
861 return -EINVAL;
862 }
863 if (rdev->sb_loaded)
864 return 0;
865
866
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100867 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 goto fail;
869 rdev->sb_loaded = 1;
870 return 0;
871
872fail:
873 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
874 bdevname(rdev->bdev,b));
875 return -EINVAL;
876}
877
878static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
879{
Andre Noll05710462008-07-11 22:02:20 +1000880 return sb1->set_uuid0 == sb2->set_uuid0 &&
881 sb1->set_uuid1 == sb2->set_uuid1 &&
882 sb1->set_uuid2 == sb2->set_uuid2 &&
883 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
887{
888 int ret;
889 mdp_super_t *tmp1, *tmp2;
890
891 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
892 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
893
894 if (!tmp1 || !tmp2) {
895 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100896 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto abort;
898 }
899
900 *tmp1 = *sb1;
901 *tmp2 = *sb2;
902
903 /*
904 * nr_disks is not constant
905 */
906 tmp1->nr_disks = 0;
907 tmp2->nr_disks = 0;
908
Andre Nollce0c8e02008-07-11 22:02:20 +1000909 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700911 kfree(tmp1);
912 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return ret;
914}
915
NeilBrown4d167f02007-05-09 02:35:37 -0700916
917static u32 md_csum_fold(u32 csum)
918{
919 csum = (csum & 0xffff) + (csum >> 16);
920 return (csum & 0xffff) + (csum >> 16);
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static unsigned int calc_sb_csum(mdp_super_t * sb)
924{
NeilBrown4d167f02007-05-09 02:35:37 -0700925 u64 newcsum = 0;
926 u32 *sb32 = (u32*)sb;
927 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 unsigned int disk_csum, csum;
929
930 disk_csum = sb->sb_csum;
931 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700932
933 for (i = 0; i < MD_SB_BYTES/4 ; i++)
934 newcsum += sb32[i];
935 csum = (newcsum & 0xffffffff) + (newcsum>>32);
936
937
938#ifdef CONFIG_ALPHA
939 /* This used to use csum_partial, which was wrong for several
940 * reasons including that different results are returned on
941 * different architectures. It isn't critical that we get exactly
942 * the same return value as before (we always csum_fold before
943 * testing, and that removes any differences). However as we
944 * know that csum_partial always returned a 16bit value on
945 * alphas, do a fold to maximise conformity to previous behaviour.
946 */
947 sb->sb_csum = md_csum_fold(disk_csum);
948#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700950#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return csum;
952}
953
954
955/*
956 * Handle superblock details.
957 * We want to be able to handle multiple superblock formats
958 * so we have a common interface to them all, and an array of
959 * different handlers.
960 * We rely on user-space to write the initial superblock, and support
961 * reading and updating of superblocks.
962 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100963 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * loads and validates a superblock on dev.
965 * if refdev != NULL, compare superblocks on both devices
966 * Return:
967 * 0 - dev has a superblock that is compatible with refdev
968 * 1 - dev has a superblock that is compatible and newer than refdev
969 * so dev should be used as the refdev in future
970 * -EINVAL superblock incompatible or invalid
971 * -othererror e.g. -EIO
972 *
NeilBrownfd01b882011-10-11 16:47:53 +1100973 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 * Verify that dev is acceptable into mddev.
975 * The first time, mddev->raid_disks will be 0, and data from
976 * dev should be merged in. Subsequent calls check that dev
977 * is new enough. Return 0 or -EINVAL
978 *
NeilBrownfd01b882011-10-11 16:47:53 +1100979 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * Update the superblock for rdev with data in mddev
981 * This does not write to disc.
982 *
983 */
984
985struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000986 char *name;
987 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000988 int (*load_super)(struct md_rdev *rdev,
989 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000990 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000991 int (*validate_super)(struct mddev *mddev,
992 struct md_rdev *rdev);
993 void (*sync_super)(struct mddev *mddev,
994 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100995 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000996 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000997 int (*allow_new_offset)(struct md_rdev *rdev,
998 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999};
1000
1001/*
Andre Noll0894cc32009-06-18 08:49:23 +10001002 * Check that the given mddev has no bitmap.
1003 *
1004 * This function is called from the run method of all personalities that do not
1005 * support bitmaps. It prints an error message and returns non-zero if mddev
1006 * has a bitmap. Otherwise, it returns 0.
1007 *
1008 */
NeilBrownfd01b882011-10-11 16:47:53 +11001009int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001010{
NeilBrownc3d97142009-12-14 12:49:52 +11001011 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001012 return 0;
1013 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1014 mdname(mddev), mddev->pers->name);
1015 return 1;
1016}
1017EXPORT_SYMBOL(md_check_no_bitmap);
1018
1019/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 * load_super for 0.90.0
1021 */
NeilBrown3cb03002011-10-11 16:45:26 +11001022static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1025 mdp_super_t *sb;
1026 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 /*
Andre Noll0f420352008-07-11 22:02:23 +10001029 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 * it's at the end of the disk.
1031 *
1032 * It also happens to be a multiple of 4Kb.
1033 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001034 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
NeilBrown0002b272005-09-09 16:23:53 -07001036 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (ret) return ret;
1038
1039 ret = -EINVAL;
1040
1041 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001042 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (sb->md_magic != MD_SB_MAGIC) {
1045 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1046 b);
1047 goto abort;
1048 }
1049
1050 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001051 sb->minor_version < 90 ||
1052 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1054 sb->major_version, sb->minor_version,
1055 b);
1056 goto abort;
1057 }
1058
1059 if (sb->raid_disks <= 0)
1060 goto abort;
1061
NeilBrown4d167f02007-05-09 02:35:37 -07001062 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1064 b);
1065 goto abort;
1066 }
1067
1068 rdev->preferred_minor = sb->md_minor;
1069 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001070 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001071 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001072 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 if (sb->level == LEVEL_MULTIPATH)
1075 rdev->desc_nr = -1;
1076 else
1077 rdev->desc_nr = sb->this_disk.number;
1078
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001079 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001081 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001083 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (!uuid_equal(refsb, sb)) {
1085 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1086 b, bdevname(refdev->bdev,b2));
1087 goto abort;
1088 }
1089 if (!sb_equal(refsb, sb)) {
1090 printk(KERN_WARNING "md: %s has same UUID"
1091 " but different superblock to %s\n",
1092 b, bdevname(refdev->bdev, b2));
1093 goto abort;
1094 }
1095 ev1 = md_event(sb);
1096 ev2 = md_event(refsb);
1097 if (ev1 > ev2)
1098 ret = 1;
1099 else
1100 ret = 0;
1101 }
NeilBrown8190e752009-06-18 08:48:58 +10001102 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001103 /* Limit to 4TB as metadata cannot record more than that.
1104 * (not needed for Linear and RAID0 as metadata doesn't
1105 * record this size)
1106 */
1107 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001108 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
NeilBrown27a7b262011-09-10 17:21:28 +10001110 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001111 /* "this cannot possibly happen" ... */
1112 ret = -EINVAL;
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 abort:
1115 return ret;
1116}
1117
1118/*
1119 * validate_super for 0.90.0
1120 */
NeilBrownfd01b882011-10-11 16:47:53 +11001121static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001124 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001125 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
NeilBrown41158c72005-06-21 17:17:25 -07001127 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001128 clear_bit(Faulty, &rdev->flags);
1129 clear_bit(In_sync, &rdev->flags);
1130 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (mddev->raid_disks == 0) {
1133 mddev->major_version = 0;
1134 mddev->minor_version = sb->minor_version;
1135 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001136 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001137 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 mddev->ctime = sb->ctime;
1139 mddev->utime = sb->utime;
1140 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001141 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 mddev->layout = sb->layout;
1143 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001144 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001145 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001146 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001147 mddev->bitmap_info.space = 0;
1148 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001149 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001150 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001151 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
NeilBrownf6705572006-03-27 01:18:11 -08001153 if (mddev->minor_version >= 91) {
1154 mddev->reshape_position = sb->reshape_position;
1155 mddev->delta_disks = sb->delta_disks;
1156 mddev->new_level = sb->new_level;
1157 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001158 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001159 if (mddev->delta_disks < 0)
1160 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001161 } else {
1162 mddev->reshape_position = MaxSector;
1163 mddev->delta_disks = 0;
1164 mddev->new_level = mddev->level;
1165 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001166 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001167 }
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (sb->state & (1<<MD_SB_CLEAN))
1170 mddev->recovery_cp = MaxSector;
1171 else {
1172 if (sb->events_hi == sb->cp_events_hi &&
1173 sb->events_lo == sb->cp_events_lo) {
1174 mddev->recovery_cp = sb->recovery_cp;
1175 } else
1176 mddev->recovery_cp = 0;
1177 }
1178
1179 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1180 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1181 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1182 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1183
1184 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001185
1186 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001187 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001188 mddev->bitmap_info.offset =
1189 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001190 mddev->bitmap_info.space =
1191 mddev->bitmap_info.space;
1192 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001193
NeilBrown41158c72005-06-21 17:17:25 -07001194 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001195 /* Insist on good event counter while assembling, except
1196 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001198 if (sb->disks[rdev->desc_nr].state & (
1199 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1200 if (ev1 < mddev->events)
1201 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001202 } else if (mddev->bitmap) {
1203 /* if adding to array with a bitmap, then we can accept an
1204 * older device ... but not too old.
1205 */
NeilBrown41158c72005-06-21 17:17:25 -07001206 if (ev1 < mddev->bitmap->events_cleared)
1207 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001208 } else {
1209 if (ev1 < mddev->events)
1210 /* just a hot-add of a new device, leave raid_disk at -1 */
1211 return 0;
1212 }
NeilBrown41158c72005-06-21 17:17:25 -07001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 desc = sb->disks + rdev->desc_nr;
1216
1217 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001218 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001219 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1220 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001221 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001223 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1224 /* active but not in sync implies recovery up to
1225 * reshape position. We don't know exactly where
1226 * that is, so set to zero for now */
1227 if (mddev->minor_version >= 91) {
1228 rdev->recovery_offset = 0;
1229 rdev->raid_disk = desc->raid_disk;
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001232 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1233 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001234 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001235 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return 0;
1237}
1238
1239/*
1240 * sync_super for 0.90.0
1241 */
NeilBrownfd01b882011-10-11 16:47:53 +11001242static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001245 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 /* make rdev->sb match mddev data..
1250 *
1251 * 1/ zero out disks
1252 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1253 * 3/ any empty disks < next_spare become removed
1254 *
1255 * disks[0] gets initialised to REMOVED because
1256 * we cannot be sure from other fields if it has
1257 * been initialised or not.
1258 */
1259 int i;
1260 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1261
NeilBrown61181562005-09-09 16:24:02 -07001262 rdev->sb_size = MD_SB_BYTES;
1263
Namhyung Kim65a06f062011-07-27 11:00:36 +10001264 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 memset(sb, 0, sizeof(*sb));
1267
1268 sb->md_magic = MD_SB_MAGIC;
1269 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 sb->patch_version = mddev->patch_version;
1271 sb->gvalid_words = 0; /* ignored */
1272 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1273 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1274 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1275 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1276
1277 sb->ctime = mddev->ctime;
1278 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001279 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 sb->raid_disks = mddev->raid_disks;
1281 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001282 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 sb->utime = mddev->utime;
1284 sb->state = 0;
1285 sb->events_hi = (mddev->events>>32);
1286 sb->events_lo = (u32)mddev->events;
1287
NeilBrownf6705572006-03-27 01:18:11 -08001288 if (mddev->reshape_position == MaxSector)
1289 sb->minor_version = 90;
1290 else {
1291 sb->minor_version = 91;
1292 sb->reshape_position = mddev->reshape_position;
1293 sb->new_level = mddev->new_level;
1294 sb->delta_disks = mddev->delta_disks;
1295 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001296 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001297 }
1298 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (mddev->in_sync)
1300 {
1301 sb->recovery_cp = mddev->recovery_cp;
1302 sb->cp_events_hi = (mddev->events>>32);
1303 sb->cp_events_lo = (u32)mddev->events;
1304 if (mddev->recovery_cp == MaxSector)
1305 sb->state = (1<< MD_SB_CLEAN);
1306 } else
1307 sb->recovery_cp = 0;
1308
1309 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001310 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
NeilBrownc3d97142009-12-14 12:49:52 +11001312 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001313 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001316 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001318 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001319 int is_active = test_bit(In_sync, &rdev2->flags);
1320
1321 if (rdev2->raid_disk >= 0 &&
1322 sb->minor_version >= 91)
1323 /* we have nowhere to store the recovery_offset,
1324 * but if it is not below the reshape_position,
1325 * we can piggy-back on that.
1326 */
1327 is_active = 1;
1328 if (rdev2->raid_disk < 0 ||
1329 test_bit(Faulty, &rdev2->flags))
1330 is_active = 0;
1331 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001332 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001334 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001335 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 d = &sb->disks[rdev2->desc_nr];
1337 nr_disks++;
1338 d->number = rdev2->desc_nr;
1339 d->major = MAJOR(rdev2->bdev->bd_dev);
1340 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001341 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 d->raid_disk = rdev2->raid_disk;
1343 else
1344 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001345 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001347 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001349 if (test_bit(In_sync, &rdev2->flags))
1350 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 active++;
1352 working++;
1353 } else {
1354 d->state = 0;
1355 spare++;
1356 working++;
1357 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001358 if (test_bit(WriteMostly, &rdev2->flags))
1359 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 /* now set the "removed" and "faulty" bits on any missing devices */
1362 for (i=0 ; i < mddev->raid_disks ; i++) {
1363 mdp_disk_t *d = &sb->disks[i];
1364 if (d->state == 0 && d->number == 0) {
1365 d->number = i;
1366 d->raid_disk = i;
1367 d->state = (1<<MD_DISK_REMOVED);
1368 d->state |= (1<<MD_DISK_FAULTY);
1369 failed++;
1370 }
1371 }
1372 sb->nr_disks = nr_disks;
1373 sb->active_disks = active;
1374 sb->working_disks = working;
1375 sb->failed_disks = failed;
1376 sb->spare_disks = spare;
1377
1378 sb->this_disk = sb->disks[rdev->desc_nr];
1379 sb->sb_csum = calc_sb_csum(sb);
1380}
1381
1382/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001383 * rdev_size_change for 0.90.0
1384 */
1385static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001386super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001387{
Andre Noll58c0fed2009-03-31 14:33:13 +11001388 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001389 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001390 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001391 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001392 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001393 if (!num_sectors || num_sectors > rdev->sb_start)
1394 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001395 /* Limit to 4TB as metadata cannot record more than that.
1396 * 4TB == 2^32 KB, or 2*2^32 sectors.
1397 */
NeilBrown667a5312012-08-16 16:46:12 +10001398 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001399 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001400 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001401 rdev->sb_page);
1402 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001403 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001404}
1405
NeilBrownc6563a82012-05-21 09:27:00 +10001406static int
1407super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1408{
1409 /* non-zero offset changes not possible with v0.90 */
1410 return new_offset == 0;
1411}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001412
1413/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * version 1 superblock
1415 */
1416
NeilBrown1c05b4b2006-10-21 10:24:08 -07001417static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001419 __le32 disk_csum;
1420 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 unsigned long long newcsum;
1422 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001423 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 disk_csum = sb->sb_csum;
1426 sb->sb_csum = 0;
1427 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001428 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 newcsum += le32_to_cpu(*isuper++);
1430
1431 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001432 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1435 sb->sb_csum = disk_csum;
1436 return cpu_to_le32(csum);
1437}
1438
NeilBrown2699b672011-07-28 11:31:47 +10001439static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1440 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001441static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 struct mdp_superblock_1 *sb;
1444 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001445 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001446 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001448 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /*
Andre Noll0f420352008-07-11 22:02:23 +10001451 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 * It is always aligned to a 4K boundary and
1453 * depeding on minor_version, it can be:
1454 * 0: At least 8K, but less than 12K, from end of device
1455 * 1: At start of device
1456 * 2: 4K from start of device.
1457 */
1458 switch(minor_version) {
1459 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001460 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001461 sb_start -= 8*2;
1462 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 break;
1464 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001465 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001468 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 break;
1470 default:
1471 return -EINVAL;
1472 }
Andre Noll0f420352008-07-11 22:02:23 +10001473 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
NeilBrown0002b272005-09-09 16:23:53 -07001475 /* superblock is rarely larger than 1K, but it can be larger,
1476 * and it is safe to read 4k, so we do that
1477 */
1478 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (ret) return ret;
1480
1481
Namhyung Kim65a06f062011-07-27 11:00:36 +10001482 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1485 sb->major_version != cpu_to_le32(1) ||
1486 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001487 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001488 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return -EINVAL;
1490
1491 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1492 printk("md: invalid superblock checksum on %s\n",
1493 bdevname(rdev->bdev,b));
1494 return -EINVAL;
1495 }
1496 if (le64_to_cpu(sb->data_size) < 10) {
1497 printk("md: data_size too small on %s\n",
1498 bdevname(rdev->bdev,b));
1499 return -EINVAL;
1500 }
NeilBrownc6563a82012-05-21 09:27:00 +10001501 if (sb->pad0 ||
1502 sb->pad3[0] ||
1503 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1504 /* Some padding is non-zero, might be a new feature */
1505 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 rdev->preferred_minor = 0xffff;
1508 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001509 rdev->new_data_offset = rdev->data_offset;
1510 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1511 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1512 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001513 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
NeilBrown0002b272005-09-09 16:23:53 -07001515 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001516 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001517 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001518 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1519
1520 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001521 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001522 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001523 if (minor_version
1524 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1525 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001526
NeilBrown31b65a02006-07-10 04:44:14 -07001527 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1528 rdev->desc_nr = -1;
1529 else
1530 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1531
NeilBrown2699b672011-07-28 11:31:47 +10001532 if (!rdev->bb_page) {
1533 rdev->bb_page = alloc_page(GFP_KERNEL);
1534 if (!rdev->bb_page)
1535 return -ENOMEM;
1536 }
1537 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1538 rdev->badblocks.count == 0) {
1539 /* need to load the bad block list.
1540 * Currently we limit it to one page.
1541 */
1542 s32 offset;
1543 sector_t bb_sector;
1544 u64 *bbp;
1545 int i;
1546 int sectors = le16_to_cpu(sb->bblog_size);
1547 if (sectors > (PAGE_SIZE / 512))
1548 return -EINVAL;
1549 offset = le32_to_cpu(sb->bblog_offset);
1550 if (offset == 0)
1551 return -EINVAL;
1552 bb_sector = (long long)offset;
1553 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1554 rdev->bb_page, READ, true))
1555 return -EIO;
1556 bbp = (u64 *)page_address(rdev->bb_page);
1557 rdev->badblocks.shift = sb->bblog_shift;
1558 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1559 u64 bb = le64_to_cpu(*bbp);
1560 int count = bb & (0x3ff);
1561 u64 sector = bb >> 10;
1562 sector <<= sb->bblog_shift;
1563 count <<= sb->bblog_shift;
1564 if (bb + 1 == 0)
1565 break;
1566 if (md_set_badblocks(&rdev->badblocks,
1567 sector, count, 1) == 0)
1568 return -EINVAL;
1569 }
NeilBrown486adf72013-04-24 11:42:44 +10001570 } else if (sb->bblog_offset != 0)
1571 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001572
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001573 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001574 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001575 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001577 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1580 sb->level != refsb->level ||
1581 sb->layout != refsb->layout ||
1582 sb->chunksize != refsb->chunksize) {
1583 printk(KERN_WARNING "md: %s has strangely different"
1584 " superblock to %s\n",
1585 bdevname(rdev->bdev,b),
1586 bdevname(refdev->bdev,b2));
1587 return -EINVAL;
1588 }
1589 ev1 = le64_to_cpu(sb->events);
1590 ev2 = le64_to_cpu(refsb->events);
1591
1592 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001593 ret = 1;
1594 else
1595 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
NeilBrownc6563a82012-05-21 09:27:00 +10001597 if (minor_version) {
1598 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1599 sectors -= rdev->data_offset;
1600 } else
1601 sectors = rdev->sb_start;
1602 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001604 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001605 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
NeilBrownfd01b882011-10-11 16:47:53 +11001608static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001610 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001611 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
NeilBrown41158c72005-06-21 17:17:25 -07001613 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001614 clear_bit(Faulty, &rdev->flags);
1615 clear_bit(In_sync, &rdev->flags);
1616 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (mddev->raid_disks == 0) {
1619 mddev->major_version = 1;
1620 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001621 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001622 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1624 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1625 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001626 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 mddev->layout = le32_to_cpu(sb->layout);
1628 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001629 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001630 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001631 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001632 mddev->bitmap_info.space = 0;
1633 /* Default location for bitmap is 1K after superblock
1634 * using 3K - total of 4K
1635 */
NeilBrownc3d97142009-12-14 12:49:52 +11001636 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001637 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001638 mddev->reshape_backwards = 0;
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1641 memcpy(mddev->uuid, sb->set_uuid, 16);
1642
1643 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001644
NeilBrown71c08052005-09-09 16:23:51 -07001645 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001646 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001647 mddev->bitmap_info.offset =
1648 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001649 /* Metadata doesn't record how much space is available.
1650 * For 1.0, we assume we can use up to the superblock
1651 * if before, else to 4K beyond superblock.
1652 * For others, assume no change is possible.
1653 */
1654 if (mddev->minor_version > 0)
1655 mddev->bitmap_info.space = 0;
1656 else if (mddev->bitmap_info.offset > 0)
1657 mddev->bitmap_info.space =
1658 8 - mddev->bitmap_info.offset;
1659 else
1660 mddev->bitmap_info.space =
1661 -mddev->bitmap_info.offset;
1662 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001663
NeilBrownf6705572006-03-27 01:18:11 -08001664 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1665 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1666 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1667 mddev->new_level = le32_to_cpu(sb->new_level);
1668 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001669 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001670 if (mddev->delta_disks < 0 ||
1671 (mddev->delta_disks == 0 &&
1672 (le32_to_cpu(sb->feature_map)
1673 & MD_FEATURE_RESHAPE_BACKWARDS)))
1674 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001675 } else {
1676 mddev->reshape_position = MaxSector;
1677 mddev->delta_disks = 0;
1678 mddev->new_level = mddev->level;
1679 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001680 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001681 }
1682
NeilBrown41158c72005-06-21 17:17:25 -07001683 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001684 /* Insist of good event counter while assembling, except for
1685 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001687 if (rdev->desc_nr >= 0 &&
1688 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1689 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1690 if (ev1 < mddev->events)
1691 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001692 } else if (mddev->bitmap) {
1693 /* If adding to array with a bitmap, then we can accept an
1694 * older device, but not too old.
1695 */
NeilBrown41158c72005-06-21 17:17:25 -07001696 if (ev1 < mddev->bitmap->events_cleared)
1697 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001698 } else {
1699 if (ev1 < mddev->events)
1700 /* just a hot-add of a new device, leave raid_disk at -1 */
1701 return 0;
1702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (mddev->level != LEVEL_MULTIPATH) {
1704 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001705 if (rdev->desc_nr < 0 ||
1706 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1707 role = 0xffff;
1708 rdev->desc_nr = -1;
1709 } else
1710 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 switch(role) {
1712 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 break;
1714 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001715 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 break;
1717 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001718 if ((le32_to_cpu(sb->feature_map) &
1719 MD_FEATURE_RECOVERY_OFFSET))
1720 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1721 else
1722 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 rdev->raid_disk = role;
1724 break;
1725 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001726 if (sb->devflags & WriteMostly1)
1727 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001728 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1729 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001730 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001731 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return 0;
1734}
1735
NeilBrownfd01b882011-10-11 16:47:53 +11001736static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001739 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 int max_dev, i;
1741 /* make rdev->sb match mddev and rdev data. */
1742
Namhyung Kim65a06f062011-07-27 11:00:36 +10001743 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 sb->feature_map = 0;
1746 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001747 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 memset(sb->pad3, 0, sizeof(sb->pad3));
1749
1750 sb->utime = cpu_to_le64((__u64)mddev->utime);
1751 sb->events = cpu_to_le64(mddev->events);
1752 if (mddev->in_sync)
1753 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1754 else
1755 sb->resync_offset = cpu_to_le64(0);
1756
NeilBrown1c05b4b2006-10-21 10:24:08 -07001757 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001758
NeilBrownf0ca3402006-02-02 14:28:04 -08001759 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001760 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001761 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001762 sb->level = cpu_to_le32(mddev->level);
1763 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001764
NeilBrownaeb9b212011-08-25 14:43:08 +10001765 if (test_bit(WriteMostly, &rdev->flags))
1766 sb->devflags |= WriteMostly1;
1767 else
1768 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001769 sb->data_offset = cpu_to_le64(rdev->data_offset);
1770 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001771
NeilBrownc3d97142009-12-14 12:49:52 +11001772 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1773 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001774 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001775 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001776
1777 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001778 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001779 sb->feature_map |=
1780 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1781 sb->recovery_offset =
1782 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001783 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001784 if (test_bit(Replacement, &rdev->flags))
1785 sb->feature_map |=
1786 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001787
NeilBrownf6705572006-03-27 01:18:11 -08001788 if (mddev->reshape_position != MaxSector) {
1789 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1790 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1791 sb->new_layout = cpu_to_le32(mddev->new_layout);
1792 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1793 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001794 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001795 if (mddev->delta_disks == 0 &&
1796 mddev->reshape_backwards)
1797 sb->feature_map
1798 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001799 if (rdev->new_data_offset != rdev->data_offset) {
1800 sb->feature_map
1801 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1802 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1803 - rdev->data_offset));
1804 }
NeilBrownf6705572006-03-27 01:18:11 -08001805 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001806
NeilBrown2699b672011-07-28 11:31:47 +10001807 if (rdev->badblocks.count == 0)
1808 /* Nothing to do for bad blocks*/ ;
1809 else if (sb->bblog_offset == 0)
1810 /* Cannot record bad blocks on this device */
1811 md_error(mddev, rdev);
1812 else {
1813 struct badblocks *bb = &rdev->badblocks;
1814 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1815 u64 *p = bb->page;
1816 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1817 if (bb->changed) {
1818 unsigned seq;
1819
1820retry:
1821 seq = read_seqbegin(&bb->lock);
1822
1823 memset(bbp, 0xff, PAGE_SIZE);
1824
1825 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001826 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001827 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1828 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001829 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001830 }
NeilBrownd0962932012-03-19 12:46:41 +11001831 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001832 if (read_seqretry(&bb->lock, seq))
1833 goto retry;
1834
1835 bb->sector = (rdev->sb_start +
1836 (int)le32_to_cpu(sb->bblog_offset));
1837 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001838 }
1839 }
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001842 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (rdev2->desc_nr+1 > max_dev)
1844 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001845
NeilBrown70471da2009-08-03 10:59:57 +10001846 if (max_dev > le32_to_cpu(sb->max_dev)) {
1847 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001848 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001849 rdev->sb_size = max_dev * 2 + 256;
1850 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1851 if (rdev->sb_size & bmask)
1852 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001853 } else
1854 max_dev = le32_to_cpu(sb->max_dev);
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 for (i=0; i<max_dev;i++)
1857 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1858
NeilBrowndafb20f2012-03-19 12:46:39 +11001859 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001861 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001863 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001865 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001866 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 else
1868 sb->dev_roles[i] = cpu_to_le16(0xffff);
1869 }
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 sb->sb_csum = calc_sb_1_csum(sb);
1872}
1873
Chris Webb0cd17fe2008-06-28 08:31:46 +10001874static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001875super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001876{
1877 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001878 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001879 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001880 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001881 if (rdev->data_offset != rdev->new_data_offset)
1882 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001883 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001884 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001885 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001886 max_sectors -= rdev->data_offset;
1887 if (!num_sectors || num_sectors > max_sectors)
1888 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001889 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001890 /* minor version 0 with bitmap we can't move */
1891 return 0;
1892 } else {
1893 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001894 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001895 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001896 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001897 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001898 if (!num_sectors || num_sectors > max_sectors)
1899 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001900 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001901 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001902 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001903 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001904 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001905 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001906 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001907 rdev->sb_page);
1908 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001909 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001910
1911}
1912
1913static int
1914super_1_allow_new_offset(struct md_rdev *rdev,
1915 unsigned long long new_offset)
1916{
1917 /* All necessary checks on new >= old have been done */
1918 struct bitmap *bitmap;
1919 if (new_offset >= rdev->data_offset)
1920 return 1;
1921
1922 /* with 1.0 metadata, there is no metadata to tread on
1923 * so we can always move back */
1924 if (rdev->mddev->minor_version == 0)
1925 return 1;
1926
1927 /* otherwise we must be sure not to step on
1928 * any metadata, so stay:
1929 * 36K beyond start of superblock
1930 * beyond end of badblocks
1931 * beyond write-intent bitmap
1932 */
1933 if (rdev->sb_start + (32+4)*2 > new_offset)
1934 return 0;
1935 bitmap = rdev->mddev->bitmap;
1936 if (bitmap && !rdev->mddev->bitmap_info.file &&
1937 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001938 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001939 return 0;
1940 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1941 return 0;
1942
1943 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001944}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Adrian Bunk75c96f82005-05-05 16:16:09 -07001946static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 [0] = {
1948 .name = "0.90.0",
1949 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001950 .load_super = super_90_load,
1951 .validate_super = super_90_validate,
1952 .sync_super = super_90_sync,
1953 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001954 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 },
1956 [1] = {
1957 .name = "md-1",
1958 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001959 .load_super = super_1_load,
1960 .validate_super = super_1_validate,
1961 .sync_super = super_1_sync,
1962 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001963 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 },
1965};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
NeilBrownfd01b882011-10-11 16:47:53 +11001967static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001968{
1969 if (mddev->sync_super) {
1970 mddev->sync_super(mddev, rdev);
1971 return;
1972 }
1973
1974 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1975
1976 super_types[mddev->major_version].sync_super(mddev, rdev);
1977}
1978
NeilBrownfd01b882011-10-11 16:47:53 +11001979static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
NeilBrown3cb03002011-10-11 16:45:26 +11001981 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
NeilBrown4b809912008-07-21 17:05:25 +10001983 rcu_read_lock();
1984 rdev_for_each_rcu(rdev, mddev1)
1985 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001986 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001987 rdev2->bdev->bd_contains) {
1988 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001989 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001990 }
1991 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return 0;
1993}
1994
1995static LIST_HEAD(pending_raid_disks);
1996
Andre Nollac5e7112009-08-03 10:59:47 +10001997/*
1998 * Try to register data integrity profile for an mddev
1999 *
2000 * This is called when an array is started and after a disk has been kicked
2001 * from the array. It only succeeds if all working and active component devices
2002 * are integrity capable with matching profiles.
2003 */
NeilBrownfd01b882011-10-11 16:47:53 +11002004int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002005{
NeilBrown3cb03002011-10-11 16:45:26 +11002006 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002007
Andre Nollac5e7112009-08-03 10:59:47 +10002008 if (list_empty(&mddev->disks))
2009 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002010 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2011 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002012 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002013 /* skip spares and non-functional disks */
2014 if (test_bit(Faulty, &rdev->flags))
2015 continue;
2016 if (rdev->raid_disk < 0)
2017 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002018 if (!reference) {
2019 /* Use the first rdev as the reference */
2020 reference = rdev;
2021 continue;
2022 }
2023 /* does this rdev's profile match the reference profile? */
2024 if (blk_integrity_compare(reference->bdev->bd_disk,
2025 rdev->bdev->bd_disk) < 0)
2026 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002027 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002028 if (!reference || !bdev_get_integrity(reference->bdev))
2029 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002030 /*
2031 * All component devices are integrity capable and have matching
2032 * profiles, register the common profile for the md device.
2033 */
2034 if (blk_integrity_register(mddev->gendisk,
2035 bdev_get_integrity(reference->bdev)) != 0) {
2036 printk(KERN_ERR "md: failed to register integrity for %s\n",
2037 mdname(mddev));
2038 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002039 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002040 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2041 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2042 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2043 mdname(mddev));
2044 return -EINVAL;
2045 }
Andre Nollac5e7112009-08-03 10:59:47 +10002046 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002047}
Andre Nollac5e7112009-08-03 10:59:47 +10002048EXPORT_SYMBOL(md_integrity_register);
2049
2050/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002051void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002052{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002053 struct blk_integrity *bi_rdev;
2054 struct blk_integrity *bi_mddev;
2055
2056 if (!mddev->gendisk)
2057 return;
2058
2059 bi_rdev = bdev_get_integrity(rdev->bdev);
2060 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002061
2062 if (!bi_mddev) /* nothing to do */
2063 return;
2064 if (rdev->raid_disk < 0) /* skip spares */
2065 return;
2066 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2067 rdev->bdev->bd_disk) >= 0)
2068 return;
2069 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2070 blk_integrity_unregister(mddev->gendisk);
2071}
2072EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002073
NeilBrownfd01b882011-10-11 16:47:53 +11002074static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002076 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002077 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002078 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002079 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 if (rdev->mddev) {
2082 MD_BUG();
2083 return -EINVAL;
2084 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002085
2086 /* prevent duplicates */
2087 if (find_rdev(mddev, rdev->bdev->bd_dev))
2088 return -EEXIST;
2089
Andre Nolldd8ac332009-03-31 14:33:13 +11002090 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2091 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2092 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002093 if (mddev->pers) {
2094 /* Cannot change size, so fail
2095 * If mddev->level <= 0, then we don't care
2096 * about aligning sizes (e.g. linear)
2097 */
2098 if (mddev->level > 0)
2099 return -ENOSPC;
2100 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002101 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 /* Verify rdev->desc_nr is unique.
2105 * If it is -1, assign a free number, else
2106 * check number is not in use
2107 */
2108 if (rdev->desc_nr < 0) {
2109 int choice = 0;
2110 if (mddev->pers) choice = mddev->raid_disks;
2111 while (find_rdev_nr(mddev, choice))
2112 choice++;
2113 rdev->desc_nr = choice;
2114 } else {
2115 if (find_rdev_nr(mddev, rdev->desc_nr))
2116 return -EBUSY;
2117 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002118 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2119 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2120 mdname(mddev), mddev->max_disks);
2121 return -EBUSY;
2122 }
NeilBrown19133a42005-11-08 21:39:35 -08002123 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002124 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002125 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002128 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002129
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002130 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002131 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002132
Tejun Heo0762b8b2008-08-25 19:56:12 +09002133 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002134 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2135 /* failure here is OK */;
2136 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002137
NeilBrown4b809912008-07-21 17:05:25 +10002138 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002139 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002140
2141 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002142 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002145
2146 fail:
2147 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2148 b, mdname(mddev));
2149 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
NeilBrown177a99b2008-02-06 01:39:56 -08002152static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002153{
NeilBrown3cb03002011-10-11 16:45:26 +11002154 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002155 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002156 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002157}
2158
NeilBrown3cb03002011-10-11 16:45:26 +11002159static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 char b[BDEVNAME_SIZE];
2162 if (!rdev->mddev) {
2163 MD_BUG();
2164 return;
2165 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002166 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002167 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2169 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002170 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002171 sysfs_put(rdev->sysfs_state);
2172 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002173 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002174 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002175 * writing to 'remove' to "dev/state". We also need
2176 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002177 */
NeilBrown4b809912008-07-21 17:05:25 +10002178 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002179 INIT_WORK(&rdev->del_work, md_delayed_delete);
2180 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002181 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
2184/*
2185 * prevent the device from being mounted, repartitioned or
2186 * otherwise reused by a RAID array (or any other kernel
2187 * subsystem), by bd_claiming the device.
2188 */
NeilBrown3cb03002011-10-11 16:45:26 +11002189static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 int err = 0;
2192 struct block_device *bdev;
2193 char b[BDEVNAME_SIZE];
2194
Tejun Heod4d77622010-11-13 11:55:18 +01002195 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002196 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (IS_ERR(bdev)) {
2198 printk(KERN_ERR "md: could not open %s.\n",
2199 __bdevname(dev, b));
2200 return PTR_ERR(bdev);
2201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 rdev->bdev = bdev;
2203 return err;
2204}
2205
NeilBrown3cb03002011-10-11 16:45:26 +11002206static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
2208 struct block_device *bdev = rdev->bdev;
2209 rdev->bdev = NULL;
2210 if (!bdev)
2211 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002212 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
2215void md_autodetect_dev(dev_t dev);
2216
NeilBrown3cb03002011-10-11 16:45:26 +11002217static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 char b[BDEVNAME_SIZE];
2220 printk(KERN_INFO "md: export_rdev(%s)\n",
2221 bdevname(rdev->bdev,b));
2222 if (rdev->mddev)
2223 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002224 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002226 if (test_bit(AutoDetected, &rdev->flags))
2227 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228#endif
2229 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002230 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
NeilBrown3cb03002011-10-11 16:45:26 +11002233static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 unbind_rdev_from_array(rdev);
2236 export_rdev(rdev);
2237}
2238
NeilBrownfd01b882011-10-11 16:47:53 +11002239static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
NeilBrown3cb03002011-10-11 16:45:26 +11002241 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
NeilBrowndafb20f2012-03-19 12:46:39 +11002243 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (!rdev->mddev) {
2245 MD_BUG();
2246 continue;
2247 }
2248 kick_rdev_from_array(rdev);
2249 }
2250 if (!list_empty(&mddev->disks))
2251 MD_BUG();
2252 mddev->raid_disks = 0;
2253 mddev->major_version = 0;
2254}
2255
2256static void print_desc(mdp_disk_t *desc)
2257{
2258 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2259 desc->major,desc->minor,desc->raid_disk,desc->state);
2260}
2261
Cheng Renquancd2ac932009-01-09 08:31:08 +11002262static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 int i;
2265
2266 printk(KERN_INFO
2267 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2268 sb->major_version, sb->minor_version, sb->patch_version,
2269 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2270 sb->ctime);
2271 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2272 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2273 sb->md_minor, sb->layout, sb->chunk_size);
2274 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2275 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2276 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2277 sb->failed_disks, sb->spare_disks,
2278 sb->sb_csum, (unsigned long)sb->events_lo);
2279
2280 printk(KERN_INFO);
2281 for (i = 0; i < MD_SB_DISKS; i++) {
2282 mdp_disk_t *desc;
2283
2284 desc = sb->disks + i;
2285 if (desc->number || desc->major || desc->minor ||
2286 desc->raid_disk || (desc->state && (desc->state != 4))) {
2287 printk(" D %2d: ", i);
2288 print_desc(desc);
2289 }
2290 }
2291 printk(KERN_INFO "md: THIS: ");
2292 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
Cheng Renquancd2ac932009-01-09 08:31:08 +11002295static void print_sb_1(struct mdp_superblock_1 *sb)
2296{
2297 __u8 *uuid;
2298
2299 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002300 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002301 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002302 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002303 le32_to_cpu(sb->major_version),
2304 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002305 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002306 sb->set_name,
2307 (unsigned long long)le64_to_cpu(sb->ctime)
2308 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2309
2310 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002311 printk(KERN_INFO
2312 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002313 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002314 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002315 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2316 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002317 le32_to_cpu(sb->level),
2318 (unsigned long long)le64_to_cpu(sb->size),
2319 le32_to_cpu(sb->raid_disks),
2320 le32_to_cpu(sb->layout),
2321 le32_to_cpu(sb->chunksize),
2322 (unsigned long long)le64_to_cpu(sb->data_offset),
2323 (unsigned long long)le64_to_cpu(sb->data_size),
2324 (unsigned long long)le64_to_cpu(sb->super_offset),
2325 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2326 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002327 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002328 sb->devflags,
2329 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2330 (unsigned long long)le64_to_cpu(sb->events),
2331 (unsigned long long)le64_to_cpu(sb->resync_offset),
2332 le32_to_cpu(sb->sb_csum),
2333 le32_to_cpu(sb->max_dev)
2334 );
2335}
2336
NeilBrown3cb03002011-10-11 16:45:26 +11002337static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
2339 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002340 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2341 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002342 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2343 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002345 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2346 switch (major_version) {
2347 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002348 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002349 break;
2350 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002351 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002352 break;
2353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 } else
2355 printk(KERN_INFO "md: no rdev superblock!\n");
2356}
2357
Adrian Bunk5e563412006-06-26 00:27:42 -07002358static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002360 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002361 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002362 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 char b[BDEVNAME_SIZE];
2364
2365 printk("\n");
2366 printk("md: **********************************\n");
2367 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2368 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002369 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
NeilBrown32a76272005-06-21 17:17:14 -07002371 if (mddev->bitmap)
2372 bitmap_print_sb(mddev->bitmap);
2373 else
2374 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002375 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 printk("<%s>", bdevname(rdev->bdev,b));
2377 printk("\n");
2378
NeilBrowndafb20f2012-03-19 12:46:39 +11002379 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002380 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382 printk("md: **********************************\n");
2383 printk("\n");
2384}
2385
2386
NeilBrownfd01b882011-10-11 16:47:53 +11002387static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
NeilBrown42543762006-06-26 00:27:57 -07002389 /* Update each superblock (in-memory image), but
2390 * if we are allowed to, skip spares which already
2391 * have the right event counter, or have one earlier
2392 * (which would mean they aren't being marked as dirty
2393 * with the rest of the array)
2394 */
NeilBrown3cb03002011-10-11 16:45:26 +11002395 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002396 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002397 if (rdev->sb_events == mddev->events ||
2398 (nospares &&
2399 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002400 rdev->sb_events+1 == mddev->events)) {
2401 /* Don't update this superblock */
2402 rdev->sb_loaded = 2;
2403 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002404 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002405 rdev->sb_loaded = 1;
2406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
2408}
2409
NeilBrownfd01b882011-10-11 16:47:53 +11002410static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
NeilBrown3cb03002011-10-11 16:45:26 +11002412 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002413 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002414 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002415 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
NeilBrownd87f0642013-04-24 11:42:40 +10002417 if (mddev->ro) {
2418 if (force_change)
2419 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2420 return;
2421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002423 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002424 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002425 if (rdev->raid_disk >= 0 &&
2426 mddev->delta_disks >= 0 &&
2427 !test_bit(In_sync, &rdev->flags) &&
2428 mddev->curr_resync_completed > rdev->recovery_offset)
2429 rdev->recovery_offset = mddev->curr_resync_completed;
2430
2431 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002432 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002433 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002434 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002435 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002436 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002437 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002438 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002439 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002440 md_ack_all_badblocks(&rdev->badblocks);
2441 md_error(mddev, rdev);
2442 }
2443 clear_bit(Blocked, &rdev->flags);
2444 clear_bit(BlockedBadBlocks, &rdev->flags);
2445 wake_up(&rdev->blocked_wait);
2446 }
2447 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002448 wake_up(&mddev->sb_wait);
2449 return;
2450 }
2451
NeilBrowna9701a32005-11-08 21:39:34 -08002452 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002453
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002454 mddev->utime = get_seconds();
2455
NeilBrown850b2b42006-10-03 01:15:46 -07002456 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2457 force_change = 1;
2458 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2459 /* just a clean<-> dirty transition, possibly leave spares alone,
2460 * though if events isn't the right even/odd, we will have to do
2461 * spares after all
2462 */
2463 nospares = 1;
2464 if (force_change)
2465 nospares = 0;
2466 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002467 /* If the array is degraded, then skipping spares is both
2468 * dangerous and fairly pointless.
2469 * Dangerous because a device that was removed from the array
2470 * might have a event_count that still looks up-to-date,
2471 * so it can be re-added without a resync.
2472 * Pointless because if there are any spares to skip,
2473 * then a recovery will happen and soon that array won't
2474 * be degraded any more and the spare can go back to sleep then.
2475 */
NeilBrown850b2b42006-10-03 01:15:46 -07002476 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002477
NeilBrown06d91a52005-06-21 17:17:12 -07002478 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002479
2480 /* If this is just a dirty<->clean transition, and the array is clean
2481 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002482 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002483 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002484 && mddev->can_decrease_events
2485 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002486 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002487 mddev->can_decrease_events = 0;
2488 } else {
NeilBrown42543762006-06-26 00:27:57 -07002489 /* otherwise we have to go forward and ... */
2490 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002491 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 if (!mddev->events) {
2495 /*
2496 * oops, this 64-bit counter should never wrap.
2497 * Either we are in around ~1 trillion A.C., assuming
2498 * 1 reboot per second, or we have a bug:
2499 */
2500 MD_BUG();
2501 mddev->events --;
2502 }
NeilBrown2699b672011-07-28 11:31:47 +10002503
NeilBrowndafb20f2012-03-19 12:46:39 +11002504 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002505 if (rdev->badblocks.changed)
2506 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002507 if (test_bit(Faulty, &rdev->flags))
2508 set_bit(FaultRecorded, &rdev->flags);
2509 }
NeilBrown2699b672011-07-28 11:31:47 +10002510
NeilBrowne6910632008-02-06 01:39:51 -08002511 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002512 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
NeilBrown36a4e1f2011-10-07 14:23:17 +11002514 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2515 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
NeilBrown4ad13662007-07-17 04:06:13 -07002517 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002518 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002520
NeilBrown42543762006-06-26 00:27:57 -07002521 if (rdev->sb_loaded != 1)
2522 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002524 if (!test_bit(Faulty, &rdev->flags) &&
2525 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002526 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002527 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002528 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002529 pr_debug("md: (write) %s's sb offset: %llu\n",
2530 bdevname(rdev->bdev, b),
2531 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002532 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002533 if (rdev->badblocks.size) {
2534 md_super_write(mddev, rdev,
2535 rdev->badblocks.sector,
2536 rdev->badblocks.size << 9,
2537 rdev->bb_page);
2538 rdev->badblocks.size = 0;
2539 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002540
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002541 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002542 pr_debug("md: %s (skipping faulty)\n",
2543 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002544 else
2545 pr_debug("(skipping incremental s/r ");
2546
NeilBrown7bfa19f2005-06-21 17:17:28 -07002547 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 /* only need to write one superblock... */
2549 break;
2550 }
NeilBrowna9701a32005-11-08 21:39:34 -08002551 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002552 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002553
NeilBrowna9701a32005-11-08 21:39:34 -08002554 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002555 if (mddev->in_sync != sync_req ||
2556 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002557 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002558 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002559 goto repeat;
2560 }
NeilBrown850b2b42006-10-03 01:15:46 -07002561 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002562 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002563 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002564 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2565 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002566
NeilBrowndafb20f2012-03-19 12:46:39 +11002567 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002568 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2569 clear_bit(Blocked, &rdev->flags);
2570
2571 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002572 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002573 clear_bit(BlockedBadBlocks, &rdev->flags);
2574 wake_up(&rdev->blocked_wait);
2575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Andre Noll7f6ce762008-03-23 18:34:54 +01002578/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002579 * We want to accept with case. For this we use cmd_match.
2580 */
2581static int cmd_match(const char *cmd, const char *str)
2582{
2583 /* See if cmd, written into a sysfs file, matches
2584 * str. They must either be the same, or cmd can
2585 * have a trailing newline
2586 */
2587 while (*cmd && *str && *cmd == *str) {
2588 cmd++;
2589 str++;
2590 }
2591 if (*cmd == '\n')
2592 cmd++;
2593 if (*str || *cmd)
2594 return 0;
2595 return 1;
2596}
2597
NeilBrown86e6ffd2005-11-08 21:39:24 -08002598struct rdev_sysfs_entry {
2599 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002600 ssize_t (*show)(struct md_rdev *, char *);
2601 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002602};
2603
2604static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002605state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002606{
2607 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002608 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002609
NeilBrownde393cd2011-07-28 11:31:48 +10002610 if (test_bit(Faulty, &rdev->flags) ||
2611 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002612 len+= sprintf(page+len, "%sfaulty",sep);
2613 sep = ",";
2614 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002615 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002616 len += sprintf(page+len, "%sin_sync",sep);
2617 sep = ",";
2618 }
NeilBrownf6556752006-06-26 00:28:01 -07002619 if (test_bit(WriteMostly, &rdev->flags)) {
2620 len += sprintf(page+len, "%swrite_mostly",sep);
2621 sep = ",";
2622 }
NeilBrownde393cd2011-07-28 11:31:48 +10002623 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002624 (rdev->badblocks.unacked_exist
2625 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002626 len += sprintf(page+len, "%sblocked", sep);
2627 sep = ",";
2628 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002629 if (!test_bit(Faulty, &rdev->flags) &&
2630 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002631 len += sprintf(page+len, "%sspare", sep);
2632 sep = ",";
2633 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002634 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2635 len += sprintf(page+len, "%swrite_error", sep);
2636 sep = ",";
2637 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002638 if (test_bit(WantReplacement, &rdev->flags)) {
2639 len += sprintf(page+len, "%swant_replacement", sep);
2640 sep = ",";
2641 }
2642 if (test_bit(Replacement, &rdev->flags)) {
2643 len += sprintf(page+len, "%sreplacement", sep);
2644 sep = ",";
2645 }
2646
NeilBrown86e6ffd2005-11-08 21:39:24 -08002647 return len+sprintf(page+len, "\n");
2648}
2649
NeilBrown45dc2de2006-06-26 00:27:58 -07002650static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002651state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002652{
2653 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002654 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002655 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002656 * writemostly - sets write_mostly
2657 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002658 * blocked - sets the Blocked flags
2659 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002660 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002661 * write_error - sets WriteErrorSeen
2662 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002663 */
2664 int err = -EINVAL;
2665 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2666 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002667 if (test_bit(Faulty, &rdev->flags))
2668 err = 0;
2669 else
2670 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002671 } else if (cmd_match(buf, "remove")) {
2672 if (rdev->raid_disk >= 0)
2673 err = -EBUSY;
2674 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002675 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002676 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002677 if (mddev->pers)
2678 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002679 md_new_event(mddev);
2680 err = 0;
2681 }
NeilBrownf6556752006-06-26 00:28:01 -07002682 } else if (cmd_match(buf, "writemostly")) {
2683 set_bit(WriteMostly, &rdev->flags);
2684 err = 0;
2685 } else if (cmd_match(buf, "-writemostly")) {
2686 clear_bit(WriteMostly, &rdev->flags);
2687 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002688 } else if (cmd_match(buf, "blocked")) {
2689 set_bit(Blocked, &rdev->flags);
2690 err = 0;
2691 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002692 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002693 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002694 /* metadata handler doesn't understand badblocks,
2695 * so we need to fail the device
2696 */
2697 md_error(rdev->mddev, rdev);
2698 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002699 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002700 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002701 wake_up(&rdev->blocked_wait);
2702 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2703 md_wakeup_thread(rdev->mddev->thread);
2704
2705 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002706 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2707 set_bit(In_sync, &rdev->flags);
2708 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002709 } else if (cmd_match(buf, "write_error")) {
2710 set_bit(WriteErrorSeen, &rdev->flags);
2711 err = 0;
2712 } else if (cmd_match(buf, "-write_error")) {
2713 clear_bit(WriteErrorSeen, &rdev->flags);
2714 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002715 } else if (cmd_match(buf, "want_replacement")) {
2716 /* Any non-spare device that is not a replacement can
2717 * become want_replacement at any time, but we then need to
2718 * check if recovery is needed.
2719 */
2720 if (rdev->raid_disk >= 0 &&
2721 !test_bit(Replacement, &rdev->flags))
2722 set_bit(WantReplacement, &rdev->flags);
2723 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2724 md_wakeup_thread(rdev->mddev->thread);
2725 err = 0;
2726 } else if (cmd_match(buf, "-want_replacement")) {
2727 /* Clearing 'want_replacement' is always allowed.
2728 * Once replacements starts it is too late though.
2729 */
2730 err = 0;
2731 clear_bit(WantReplacement, &rdev->flags);
2732 } else if (cmd_match(buf, "replacement")) {
2733 /* Can only set a device as a replacement when array has not
2734 * yet been started. Once running, replacement is automatic
2735 * from spares, or by assigning 'slot'.
2736 */
2737 if (rdev->mddev->pers)
2738 err = -EBUSY;
2739 else {
2740 set_bit(Replacement, &rdev->flags);
2741 err = 0;
2742 }
2743 } else if (cmd_match(buf, "-replacement")) {
2744 /* Similarly, can only clear Replacement before start */
2745 if (rdev->mddev->pers)
2746 err = -EBUSY;
2747 else {
2748 clear_bit(Replacement, &rdev->flags);
2749 err = 0;
2750 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002751 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002752 if (!err)
2753 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002754 return err ? err : len;
2755}
NeilBrown80ca3a42006-07-10 04:44:18 -07002756static struct rdev_sysfs_entry rdev_state =
2757__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002758
2759static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002760errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002761{
2762 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2763}
2764
2765static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002766errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002767{
2768 char *e;
2769 unsigned long n = simple_strtoul(buf, &e, 10);
2770 if (*buf && (*e == 0 || *e == '\n')) {
2771 atomic_set(&rdev->corrected_errors, n);
2772 return len;
2773 }
2774 return -EINVAL;
2775}
2776static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002777__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002778
NeilBrown014236d2006-01-06 00:20:55 -08002779static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002780slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002781{
2782 if (rdev->raid_disk < 0)
2783 return sprintf(page, "none\n");
2784 else
2785 return sprintf(page, "%d\n", rdev->raid_disk);
2786}
2787
2788static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002789slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002790{
2791 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002792 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002793 int slot = simple_strtoul(buf, &e, 10);
2794 if (strncmp(buf, "none", 4)==0)
2795 slot = -1;
2796 else if (e==buf || (*e && *e!= '\n'))
2797 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002798 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002799 /* Setting 'slot' on an active array requires also
2800 * updating the 'rd%d' link, and communicating
2801 * with the personality with ->hot_*_disk.
2802 * For now we only support removing
2803 * failed/spare devices. This normally happens automatically,
2804 * but not when the metadata is externally managed.
2805 */
NeilBrownc303da62008-02-06 01:39:51 -08002806 if (rdev->raid_disk == -1)
2807 return -EEXIST;
2808 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002809 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002810 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002811 clear_bit(Blocked, &rdev->flags);
2812 remove_and_add_spares(rdev->mddev, rdev);
2813 if (rdev->raid_disk >= 0)
2814 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002815 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2816 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002817 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002818 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002819 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002820 */
2821
2822 if (rdev->raid_disk != -1)
2823 return -EBUSY;
2824
NeilBrownc6751b22011-02-02 11:57:13 +11002825 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2826 return -EBUSY;
2827
Neil Brown6c2fce22008-06-28 08:31:31 +10002828 if (rdev->mddev->pers->hot_add_disk == NULL)
2829 return -EINVAL;
2830
NeilBrownba1b41b2011-01-14 09:14:34 +11002831 if (slot >= rdev->mddev->raid_disks &&
2832 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2833 return -ENOSPC;
2834
Neil Brown6c2fce22008-06-28 08:31:31 +10002835 rdev->raid_disk = slot;
2836 if (test_bit(In_sync, &rdev->flags))
2837 rdev->saved_raid_disk = slot;
2838 else
2839 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002840 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002841 err = rdev->mddev->pers->
2842 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002843 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002844 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002845 return err;
Neil Brown52664732008-06-28 08:31:44 +10002846 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002847 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002848 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002849 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002850 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002851 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002852 if (slot >= rdev->mddev->raid_disks &&
2853 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002854 return -ENOSPC;
2855 rdev->raid_disk = slot;
2856 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002857 clear_bit(Faulty, &rdev->flags);
2858 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002859 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002860 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002861 }
NeilBrown014236d2006-01-06 00:20:55 -08002862 return len;
2863}
2864
2865
2866static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002867__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002868
NeilBrown93c8cad2006-01-06 00:20:56 -08002869static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002870offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002871{
Andrew Morton6961ece2006-01-06 00:20:59 -08002872 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002873}
2874
2875static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002876offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002877{
NeilBrownc6563a82012-05-21 09:27:00 +10002878 unsigned long long offset;
2879 if (strict_strtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002880 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002881 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002882 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002883 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002884 /* Must set offset before size, so overlap checks
2885 * can be sane */
2886 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002887 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002888 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002889 return len;
2890}
2891
2892static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002893__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002894
NeilBrownc6563a82012-05-21 09:27:00 +10002895static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2896{
2897 return sprintf(page, "%llu\n",
2898 (unsigned long long)rdev->new_data_offset);
2899}
2900
2901static ssize_t new_offset_store(struct md_rdev *rdev,
2902 const char *buf, size_t len)
2903{
2904 unsigned long long new_offset;
2905 struct mddev *mddev = rdev->mddev;
2906
2907 if (strict_strtoull(buf, 10, &new_offset) < 0)
2908 return -EINVAL;
2909
2910 if (mddev->sync_thread)
2911 return -EBUSY;
2912 if (new_offset == rdev->data_offset)
2913 /* reset is always permitted */
2914 ;
2915 else if (new_offset > rdev->data_offset) {
2916 /* must not push array size beyond rdev_sectors */
2917 if (new_offset - rdev->data_offset
2918 + mddev->dev_sectors > rdev->sectors)
2919 return -E2BIG;
2920 }
2921 /* Metadata worries about other space details. */
2922
2923 /* decreasing the offset is inconsistent with a backwards
2924 * reshape.
2925 */
2926 if (new_offset < rdev->data_offset &&
2927 mddev->reshape_backwards)
2928 return -EINVAL;
2929 /* Increasing offset is inconsistent with forwards
2930 * reshape. reshape_direction should be set to
2931 * 'backwards' first.
2932 */
2933 if (new_offset > rdev->data_offset &&
2934 !mddev->reshape_backwards)
2935 return -EINVAL;
2936
2937 if (mddev->pers && mddev->persistent &&
2938 !super_types[mddev->major_version]
2939 .allow_new_offset(rdev, new_offset))
2940 return -E2BIG;
2941 rdev->new_data_offset = new_offset;
2942 if (new_offset > rdev->data_offset)
2943 mddev->reshape_backwards = 1;
2944 else if (new_offset < rdev->data_offset)
2945 mddev->reshape_backwards = 0;
2946
2947 return len;
2948}
2949static struct rdev_sysfs_entry rdev_new_offset =
2950__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2951
NeilBrown83303b62006-01-06 00:21:06 -08002952static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002953rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002954{
Andre Nolldd8ac332009-03-31 14:33:13 +11002955 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002956}
2957
NeilBrownc5d79ad2008-02-06 01:39:54 -08002958static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2959{
2960 /* check if two start/length pairs overlap */
2961 if (s1+l1 <= s2)
2962 return 0;
2963 if (s2+l2 <= s1)
2964 return 0;
2965 return 1;
2966}
2967
Dan Williamsb522adc2009-03-31 15:00:31 +11002968static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2969{
2970 unsigned long long blocks;
2971 sector_t new;
2972
2973 if (strict_strtoull(buf, 10, &blocks) < 0)
2974 return -EINVAL;
2975
2976 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2977 return -EINVAL; /* sector conversion overflow */
2978
2979 new = blocks * 2;
2980 if (new != blocks * 2)
2981 return -EINVAL; /* unsigned long long to sector_t overflow */
2982
2983 *sectors = new;
2984 return 0;
2985}
2986
NeilBrown83303b62006-01-06 00:21:06 -08002987static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002988rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002989{
NeilBrownfd01b882011-10-11 16:47:53 +11002990 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002991 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002992 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002993
Dan Williamsb522adc2009-03-31 15:00:31 +11002994 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002995 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002996 if (rdev->data_offset != rdev->new_data_offset)
2997 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002998 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002999 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003000 sectors = super_types[my_mddev->major_version].
3001 rdev_size_change(rdev, sectors);
3002 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003003 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003004 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003005 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003006 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003007 if (!my_mddev->pers->resize)
3008 /* Cannot change size for RAID0 or Linear etc */
3009 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003010 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003011 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003012 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003013
Andre Nolldd8ac332009-03-31 14:33:13 +11003014 rdev->sectors = sectors;
3015 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003016 /* need to check that all other rdevs with the same ->bdev
3017 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11003018 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08003019 * we have to change it back, we will have the lock again.
3020 */
NeilBrownfd01b882011-10-11 16:47:53 +11003021 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003022 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003023 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003024
NeilBrown27c529b2008-03-04 14:29:33 -08003025 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08003026 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003027 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003028
3029 mddev_lock(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11003030 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003031 if (rdev->bdev == rdev2->bdev &&
3032 rdev != rdev2 &&
3033 overlaps(rdev->data_offset, rdev->sectors,
3034 rdev2->data_offset,
3035 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003036 overlap = 1;
3037 break;
3038 }
3039 mddev_unlock(mddev);
3040 if (overlap) {
3041 mddev_put(mddev);
3042 break;
3043 }
3044 }
NeilBrown27c529b2008-03-04 14:29:33 -08003045 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08003046 if (overlap) {
3047 /* Someone else could have slipped in a size
3048 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003049 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003050 * safe, and trust userspace not to race with
3051 * itself
3052 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003053 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003054 return -EBUSY;
3055 }
3056 }
NeilBrown83303b62006-01-06 00:21:06 -08003057 return len;
3058}
3059
3060static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003061__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003062
Dan Williams06e3c812009-12-12 21:17:12 -07003063
NeilBrown3cb03002011-10-11 16:45:26 +11003064static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003065{
3066 unsigned long long recovery_start = rdev->recovery_offset;
3067
3068 if (test_bit(In_sync, &rdev->flags) ||
3069 recovery_start == MaxSector)
3070 return sprintf(page, "none\n");
3071
3072 return sprintf(page, "%llu\n", recovery_start);
3073}
3074
NeilBrown3cb03002011-10-11 16:45:26 +11003075static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003076{
3077 unsigned long long recovery_start;
3078
3079 if (cmd_match(buf, "none"))
3080 recovery_start = MaxSector;
3081 else if (strict_strtoull(buf, 10, &recovery_start))
3082 return -EINVAL;
3083
3084 if (rdev->mddev->pers &&
3085 rdev->raid_disk >= 0)
3086 return -EBUSY;
3087
3088 rdev->recovery_offset = recovery_start;
3089 if (recovery_start == MaxSector)
3090 set_bit(In_sync, &rdev->flags);
3091 else
3092 clear_bit(In_sync, &rdev->flags);
3093 return len;
3094}
3095
3096static struct rdev_sysfs_entry rdev_recovery_start =
3097__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3098
NeilBrown16c791a2011-07-28 11:31:47 +10003099
3100static ssize_t
3101badblocks_show(struct badblocks *bb, char *page, int unack);
3102static ssize_t
3103badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3104
NeilBrown3cb03002011-10-11 16:45:26 +11003105static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003106{
3107 return badblocks_show(&rdev->badblocks, page, 0);
3108}
NeilBrown3cb03002011-10-11 16:45:26 +11003109static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003110{
NeilBrownde393cd2011-07-28 11:31:48 +10003111 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3112 /* Maybe that ack was all we needed */
3113 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3114 wake_up(&rdev->blocked_wait);
3115 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003116}
3117static struct rdev_sysfs_entry rdev_bad_blocks =
3118__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3119
3120
NeilBrown3cb03002011-10-11 16:45:26 +11003121static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003122{
3123 return badblocks_show(&rdev->badblocks, page, 1);
3124}
NeilBrown3cb03002011-10-11 16:45:26 +11003125static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003126{
3127 return badblocks_store(&rdev->badblocks, page, len, 1);
3128}
3129static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3130__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3131
NeilBrown86e6ffd2005-11-08 21:39:24 -08003132static struct attribute *rdev_default_attrs[] = {
3133 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003134 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003135 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003136 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003137 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003138 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003139 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003140 &rdev_bad_blocks.attr,
3141 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003142 NULL,
3143};
3144static ssize_t
3145rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3146{
3147 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003148 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003149 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003150 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003151
3152 if (!entry->show)
3153 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003154
3155 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3156 if (!rv) {
3157 if (rdev->mddev == NULL)
3158 rv = -EBUSY;
3159 else
3160 rv = entry->show(rdev, page);
3161 mddev_unlock(mddev);
3162 }
3163 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003164}
3165
3166static ssize_t
3167rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3168 const char *page, size_t length)
3169{
3170 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003171 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003172 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003173 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003174
3175 if (!entry->store)
3176 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003177 if (!capable(CAP_SYS_ADMIN))
3178 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003179 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003180 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003181 if (rdev->mddev == NULL)
3182 rv = -EBUSY;
3183 else
3184 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003185 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003186 }
3187 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003188}
3189
3190static void rdev_free(struct kobject *ko)
3191{
NeilBrown3cb03002011-10-11 16:45:26 +11003192 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003193 kfree(rdev);
3194}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003195static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003196 .show = rdev_attr_show,
3197 .store = rdev_attr_store,
3198};
3199static struct kobj_type rdev_ktype = {
3200 .release = rdev_free,
3201 .sysfs_ops = &rdev_sysfs_ops,
3202 .default_attrs = rdev_default_attrs,
3203};
3204
NeilBrown3cb03002011-10-11 16:45:26 +11003205int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003206{
3207 rdev->desc_nr = -1;
3208 rdev->saved_raid_disk = -1;
3209 rdev->raid_disk = -1;
3210 rdev->flags = 0;
3211 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003212 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003213 rdev->sb_events = 0;
3214 rdev->last_read_error.tv_sec = 0;
3215 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003216 rdev->sb_loaded = 0;
3217 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003218 atomic_set(&rdev->nr_pending, 0);
3219 atomic_set(&rdev->read_errors, 0);
3220 atomic_set(&rdev->corrected_errors, 0);
3221
3222 INIT_LIST_HEAD(&rdev->same_set);
3223 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003224
3225 /* Add space to store bad block list.
3226 * This reserves the space even on arrays where it cannot
3227 * be used - I wonder if that matters
3228 */
3229 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003230 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003231 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3232 seqlock_init(&rdev->badblocks.lock);
3233 if (rdev->badblocks.page == NULL)
3234 return -ENOMEM;
3235
3236 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003237}
3238EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239/*
3240 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3241 *
3242 * mark the device faulty if:
3243 *
3244 * - the device is nonexistent (zero size)
3245 * - the device has no valid superblock
3246 *
3247 * a faulty rdev _never_ has rdev->sb set.
3248 */
NeilBrown3cb03002011-10-11 16:45:26 +11003249static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
3251 char b[BDEVNAME_SIZE];
3252 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003253 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 sector_t size;
3255
NeilBrown9ffae0c2006-01-06 00:20:32 -08003256 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (!rdev) {
3258 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3259 return ERR_PTR(-ENOMEM);
3260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
NeilBrown2230dfe2011-07-28 11:31:46 +10003262 err = md_rdev_init(rdev);
3263 if (err)
3264 goto abort_free;
3265 err = alloc_disk_sb(rdev);
3266 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 goto abort_free;
3268
NeilBrownc5d79ad2008-02-06 01:39:54 -08003269 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (err)
3271 goto abort_free;
3272
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003273 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003274
Mike Snitzer77304d22010-11-08 14:39:12 +01003275 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 if (!size) {
3277 printk(KERN_WARNING
3278 "md: %s has zero or unknown size, marking faulty!\n",
3279 bdevname(rdev->bdev,b));
3280 err = -EINVAL;
3281 goto abort_free;
3282 }
3283
3284 if (super_format >= 0) {
3285 err = super_types[super_format].
3286 load_super(rdev, NULL, super_minor);
3287 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003288 printk(KERN_WARNING
3289 "md: %s does not have a valid v%d.%d "
3290 "superblock, not importing!\n",
3291 bdevname(rdev->bdev,b),
3292 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 goto abort_free;
3294 }
3295 if (err < 0) {
3296 printk(KERN_WARNING
3297 "md: could not read %s's sb, not importing!\n",
3298 bdevname(rdev->bdev,b));
3299 goto abort_free;
3300 }
3301 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return rdev;
3304
3305abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003306 if (rdev->bdev)
3307 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003308 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 kfree(rdev);
3310 return ERR_PTR(err);
3311}
3312
3313/*
3314 * Check a full RAID array for plausibility
3315 */
3316
3317
NeilBrownfd01b882011-10-11 16:47:53 +11003318static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
3320 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003321 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 char b[BDEVNAME_SIZE];
3323
3324 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003325 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 switch (super_types[mddev->major_version].
3327 load_super(rdev, freshest, mddev->minor_version)) {
3328 case 1:
3329 freshest = rdev;
3330 break;
3331 case 0:
3332 break;
3333 default:
3334 printk( KERN_ERR \
3335 "md: fatal superblock inconsistency in %s"
3336 " -- removing from array\n",
3337 bdevname(rdev->bdev,b));
3338 kick_rdev_from_array(rdev);
3339 }
3340
3341
3342 super_types[mddev->major_version].
3343 validate_super(mddev, freshest);
3344
3345 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003346 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003347 if (mddev->max_disks &&
3348 (rdev->desc_nr >= mddev->max_disks ||
3349 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003350 printk(KERN_WARNING
3351 "md: %s: %s: only %d devices permitted\n",
3352 mdname(mddev), bdevname(rdev->bdev, b),
3353 mddev->max_disks);
3354 kick_rdev_from_array(rdev);
3355 continue;
3356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (rdev != freshest)
3358 if (super_types[mddev->major_version].
3359 validate_super(mddev, rdev)) {
3360 printk(KERN_WARNING "md: kicking non-fresh %s"
3361 " from array!\n",
3362 bdevname(rdev->bdev,b));
3363 kick_rdev_from_array(rdev);
3364 continue;
3365 }
3366 if (mddev->level == LEVEL_MULTIPATH) {
3367 rdev->desc_nr = i++;
3368 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003369 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003370 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003371 rdev->raid_disk = -1;
3372 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 }
3374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
3376
NeilBrown72e02072009-12-14 12:49:55 +11003377/* Read a fixed-point number.
3378 * Numbers in sysfs attributes should be in "standard" units where
3379 * possible, so time should be in seconds.
3380 * However we internally use a a much smaller unit such as
3381 * milliseconds or jiffies.
3382 * This function takes a decimal number with a possible fractional
3383 * component, and produces an integer which is the result of
3384 * multiplying that number by 10^'scale'.
3385 * all without any floating-point arithmetic.
3386 */
3387int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3388{
3389 unsigned long result = 0;
3390 long decimals = -1;
3391 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3392 if (*cp == '.')
3393 decimals = 0;
3394 else if (decimals < scale) {
3395 unsigned int value;
3396 value = *cp - '0';
3397 result = result * 10 + value;
3398 if (decimals >= 0)
3399 decimals++;
3400 }
3401 cp++;
3402 }
3403 if (*cp == '\n')
3404 cp++;
3405 if (*cp)
3406 return -EINVAL;
3407 if (decimals < 0)
3408 decimals = 0;
3409 while (decimals < scale) {
3410 result *= 10;
3411 decimals ++;
3412 }
3413 *res = result;
3414 return 0;
3415}
3416
3417
NeilBrown19052c02008-08-05 15:54:13 +10003418static void md_safemode_timeout(unsigned long data);
3419
NeilBrowneae17012005-11-08 21:39:23 -08003420static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003421safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003422{
3423 int msec = (mddev->safemode_delay*1000)/HZ;
3424 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3425}
3426static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003427safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003428{
NeilBrown16f17b32006-06-26 00:27:37 -07003429 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003430
NeilBrown72e02072009-12-14 12:49:55 +11003431 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003432 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003433 if (msec == 0)
3434 mddev->safemode_delay = 0;
3435 else {
NeilBrown19052c02008-08-05 15:54:13 +10003436 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003437 mddev->safemode_delay = (msec*HZ)/1000;
3438 if (mddev->safemode_delay == 0)
3439 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003440 if (mddev->safemode_delay < old_delay)
3441 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003442 }
3443 return len;
3444}
3445static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003446__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003447
3448static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003449level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003450{
NeilBrown84fc4b52011-10-11 16:49:58 +11003451 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003452 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003453 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003454 else if (mddev->clevel[0])
3455 return sprintf(page, "%s\n", mddev->clevel);
3456 else if (mddev->level != LEVEL_NONE)
3457 return sprintf(page, "%d\n", mddev->level);
3458 else
3459 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003460}
3461
NeilBrownd9d166c2006-01-06 00:20:51 -08003462static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003463level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003464{
Dan Williamsf2859af2010-05-02 10:04:16 -07003465 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003466 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003467 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003468 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003469 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003470 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003471
3472 if (mddev->pers == NULL) {
3473 if (len == 0)
3474 return 0;
3475 if (len >= sizeof(mddev->clevel))
3476 return -ENOSPC;
3477 strncpy(mddev->clevel, buf, len);
3478 if (mddev->clevel[len-1] == '\n')
3479 len--;
3480 mddev->clevel[len] = 0;
3481 mddev->level = LEVEL_NONE;
3482 return rv;
3483 }
3484
3485 /* request to change the personality. Need to ensure:
3486 * - array is not engaged in resync/recovery/reshape
3487 * - old personality can be suspended
3488 * - new personality will access other array.
3489 */
3490
NeilBrownbb4f1e92010-08-08 21:18:03 +10003491 if (mddev->sync_thread ||
3492 mddev->reshape_position != MaxSector ||
3493 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003494 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003495
3496 if (!mddev->pers->quiesce) {
3497 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3498 mdname(mddev), mddev->pers->name);
3499 return -EINVAL;
3500 }
3501
3502 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003503 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003504 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003505 strncpy(clevel, buf, len);
3506 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003507 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003508 clevel[len] = 0;
3509 if (strict_strtol(clevel, 10, &level))
3510 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003511
Dan Williamsf2859af2010-05-02 10:04:16 -07003512 if (request_module("md-%s", clevel) != 0)
3513 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003514 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003515 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003516 if (!pers || !try_module_get(pers->owner)) {
3517 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003518 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003519 return -EINVAL;
3520 }
3521 spin_unlock(&pers_lock);
3522
3523 if (pers == mddev->pers) {
3524 /* Nothing to do! */
3525 module_put(pers->owner);
3526 return rv;
3527 }
3528 if (!pers->takeover) {
3529 module_put(pers->owner);
3530 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003531 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003532 return -EINVAL;
3533 }
3534
NeilBrowndafb20f2012-03-19 12:46:39 +11003535 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003536 rdev->new_raid_disk = rdev->raid_disk;
3537
NeilBrown245f46c2009-03-31 14:39:39 +11003538 /* ->takeover must set new_* and/or delta_disks
3539 * if it succeeds, and may set them when it fails.
3540 */
3541 priv = pers->takeover(mddev);
3542 if (IS_ERR(priv)) {
3543 mddev->new_level = mddev->level;
3544 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003545 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003546 mddev->raid_disks -= mddev->delta_disks;
3547 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003548 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003549 module_put(pers->owner);
3550 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003551 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003552 return PTR_ERR(priv);
3553 }
3554
3555 /* Looks like we have a winner */
3556 mddev_suspend(mddev);
3557 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003558
3559 if (mddev->pers->sync_request == NULL &&
3560 pers->sync_request != NULL) {
3561 /* need to add the md_redundancy_group */
3562 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3563 printk(KERN_WARNING
3564 "md: cannot register extra attributes for %s\n",
3565 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003566 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003567 }
3568 if (mddev->pers->sync_request != NULL &&
3569 pers->sync_request == NULL) {
3570 /* need to remove the md_redundancy_group */
3571 if (mddev->to_remove == NULL)
3572 mddev->to_remove = &md_redundancy_group;
3573 }
3574
Trela Maciej54071b32010-03-08 16:02:42 +11003575 if (mddev->pers->sync_request == NULL &&
3576 mddev->external) {
3577 /* We are converting from a no-redundancy array
3578 * to a redundancy array and metadata is managed
3579 * externally so we need to be sure that writes
3580 * won't block due to a need to transition
3581 * clean->dirty
3582 * until external management is started.
3583 */
3584 mddev->in_sync = 0;
3585 mddev->safemode_delay = 0;
3586 mddev->safemode = 0;
3587 }
3588
NeilBrowndafb20f2012-03-19 12:46:39 +11003589 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003590 if (rdev->raid_disk < 0)
3591 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003592 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003593 rdev->new_raid_disk = -1;
3594 if (rdev->new_raid_disk == rdev->raid_disk)
3595 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003596 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003597 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003598 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003599 if (rdev->raid_disk < 0)
3600 continue;
3601 if (rdev->new_raid_disk == rdev->raid_disk)
3602 continue;
3603 rdev->raid_disk = rdev->new_raid_disk;
3604 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003605 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003606 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003607 if (sysfs_link_rdev(mddev, rdev))
3608 printk(KERN_WARNING "md: cannot register rd%d"
3609 " for %s after level change\n",
3610 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003611 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003612 }
3613
3614 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003615 mddev->pers = pers;
3616 mddev->private = priv;
3617 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3618 mddev->level = mddev->new_level;
3619 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003620 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003621 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003622 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003623 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003624 if (mddev->pers->sync_request == NULL) {
3625 /* this is now an array without redundancy, so
3626 * it must always be in_sync
3627 */
3628 mddev->in_sync = 1;
3629 del_timer_sync(&mddev->safemode_timer);
3630 }
NeilBrown245f46c2009-03-31 14:39:39 +11003631 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003632 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003633 mddev_resume(mddev);
Maciej Trela5cac78612010-04-14 17:17:39 +10003634 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003635 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003636 return rv;
3637}
3638
3639static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003640__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003641
NeilBrownd4dbd022006-06-26 00:27:59 -07003642
3643static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003644layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003645{
3646 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003647 if (mddev->reshape_position != MaxSector &&
3648 mddev->layout != mddev->new_layout)
3649 return sprintf(page, "%d (%d)\n",
3650 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003651 return sprintf(page, "%d\n", mddev->layout);
3652}
3653
3654static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003655layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003656{
3657 char *e;
3658 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003659
3660 if (!*buf || (*e && *e != '\n'))
3661 return -EINVAL;
3662
NeilBrownb3546032009-03-31 14:56:41 +11003663 if (mddev->pers) {
3664 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003665 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003666 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003667 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003668 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003669 if (err) {
3670 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003671 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003672 }
NeilBrownb3546032009-03-31 14:56:41 +11003673 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003674 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003675 if (mddev->reshape_position == MaxSector)
3676 mddev->layout = n;
3677 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003678 return len;
3679}
3680static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003681__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003682
3683
NeilBrowneae17012005-11-08 21:39:23 -08003684static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003685raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003686{
NeilBrownbb636542005-11-08 21:39:45 -08003687 if (mddev->raid_disks == 0)
3688 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003689 if (mddev->reshape_position != MaxSector &&
3690 mddev->delta_disks != 0)
3691 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3692 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003693 return sprintf(page, "%d\n", mddev->raid_disks);
3694}
3695
NeilBrownfd01b882011-10-11 16:47:53 +11003696static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003697
3698static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003699raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003700{
NeilBrownda943b992006-01-06 00:20:54 -08003701 char *e;
3702 int rv = 0;
3703 unsigned long n = simple_strtoul(buf, &e, 10);
3704
3705 if (!*buf || (*e && *e != '\n'))
3706 return -EINVAL;
3707
3708 if (mddev->pers)
3709 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003710 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003711 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003712 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003713
3714 rdev_for_each(rdev, mddev) {
3715 if (olddisks < n &&
3716 rdev->data_offset < rdev->new_data_offset)
3717 return -EINVAL;
3718 if (olddisks > n &&
3719 rdev->data_offset > rdev->new_data_offset)
3720 return -EINVAL;
3721 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003722 mddev->delta_disks = n - olddisks;
3723 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003724 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003725 } else
NeilBrownda943b992006-01-06 00:20:54 -08003726 mddev->raid_disks = n;
3727 return rv ? rv : len;
3728}
3729static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003730__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003731
NeilBrown24dd4692005-11-08 21:39:26 -08003732static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003733chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003734{
NeilBrown08a02ec2007-05-09 02:35:38 -07003735 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003736 mddev->chunk_sectors != mddev->new_chunk_sectors)
3737 return sprintf(page, "%d (%d)\n",
3738 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003739 mddev->chunk_sectors << 9);
3740 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003741}
3742
3743static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003744chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003745{
NeilBrown3b343802006-01-06 00:20:47 -08003746 char *e;
3747 unsigned long n = simple_strtoul(buf, &e, 10);
3748
NeilBrown3b343802006-01-06 00:20:47 -08003749 if (!*buf || (*e && *e != '\n'))
3750 return -EINVAL;
3751
NeilBrownb3546032009-03-31 14:56:41 +11003752 if (mddev->pers) {
3753 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003754 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003755 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003756 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003757 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003758 if (err) {
3759 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003760 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003761 }
NeilBrownb3546032009-03-31 14:56:41 +11003762 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003763 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003764 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003765 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003766 }
NeilBrown3b343802006-01-06 00:20:47 -08003767 return len;
3768}
3769static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003770__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003771
NeilBrowna94213b2006-06-26 00:28:00 -07003772static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003773resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003774{
NeilBrownd1a7c502009-03-31 15:24:32 +11003775 if (mddev->recovery_cp == MaxSector)
3776 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003777 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3778}
3779
3780static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003781resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003782{
NeilBrowna94213b2006-06-26 00:28:00 -07003783 char *e;
3784 unsigned long long n = simple_strtoull(buf, &e, 10);
3785
NeilBrownb0986362011-05-11 15:52:21 +10003786 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003787 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003788 if (cmd_match(buf, "none"))
3789 n = MaxSector;
3790 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003791 return -EINVAL;
3792
3793 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003794 if (mddev->pers)
3795 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003796 return len;
3797}
3798static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003799__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003800
NeilBrown9e653b62006-06-26 00:27:58 -07003801/*
3802 * The array state can be:
3803 *
3804 * clear
3805 * No devices, no size, no level
3806 * Equivalent to STOP_ARRAY ioctl
3807 * inactive
3808 * May have some settings, but array is not active
3809 * all IO results in error
3810 * When written, doesn't tear down array, but just stops it
3811 * suspended (not supported yet)
3812 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003813 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003814 * readonly
3815 * no resync can happen. no superblocks get written.
3816 * write requests fail
3817 * read-auto
3818 * like readonly, but behaves like 'clean' on a write request.
3819 *
3820 * clean - no pending writes, but otherwise active.
3821 * When written to inactive array, starts without resync
3822 * If a write request arrives then
3823 * if metadata is known, mark 'dirty' and switch to 'active'.
3824 * if not known, block and switch to write-pending
3825 * If written to an active array that has pending writes, then fails.
3826 * active
3827 * fully active: IO and resync can be happening.
3828 * When written to inactive array, starts with resync
3829 *
3830 * write-pending
3831 * clean, but writes are blocked waiting for 'active' to be written.
3832 *
3833 * active-idle
3834 * like active, but no writes have been seen for a while (100msec).
3835 *
3836 */
3837enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3838 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003839static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003840 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3841 "write-pending", "active-idle", NULL };
3842
3843static int match_word(const char *word, char **list)
3844{
3845 int n;
3846 for (n=0; list[n]; n++)
3847 if (cmd_match(word, list[n]))
3848 break;
3849 return n;
3850}
3851
3852static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003853array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003854{
3855 enum array_state st = inactive;
3856
3857 if (mddev->pers)
3858 switch(mddev->ro) {
3859 case 1:
3860 st = readonly;
3861 break;
3862 case 2:
3863 st = read_auto;
3864 break;
3865 case 0:
3866 if (mddev->in_sync)
3867 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003868 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003869 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003870 else if (mddev->safemode)
3871 st = active_idle;
3872 else
3873 st = active;
3874 }
3875 else {
3876 if (list_empty(&mddev->disks) &&
3877 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003878 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003879 st = clear;
3880 else
3881 st = inactive;
3882 }
3883 return sprintf(page, "%s\n", array_states[st]);
3884}
3885
NeilBrowna05b7ea02012-07-19 15:59:18 +10003886static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3887static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003888static int do_md_run(struct mddev * mddev);
3889static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003890
3891static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003892array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003893{
3894 int err = -EINVAL;
3895 enum array_state st = match_word(buf, array_states);
3896 switch(st) {
3897 case bad_word:
3898 break;
3899 case clear:
3900 /* stopping an active array */
NeilBrowna05b7ea02012-07-19 15:59:18 +10003901 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003902 break;
3903 case inactive:
3904 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003905 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003906 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003907 else
NeilBrowne6910632008-02-06 01:39:51 -08003908 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003909 break;
3910 case suspended:
3911 break; /* not supported yet */
3912 case readonly:
3913 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003914 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003915 else {
3916 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003917 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003918 err = do_md_run(mddev);
3919 }
3920 break;
3921 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003922 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003923 if (mddev->ro == 0)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003924 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003925 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003926 err = restart_array(mddev);
3927 if (err == 0) {
3928 mddev->ro = 2;
3929 set_disk_ro(mddev->gendisk, 0);
3930 }
NeilBrown9e653b62006-06-26 00:27:58 -07003931 } else {
3932 mddev->ro = 2;
3933 err = do_md_run(mddev);
3934 }
3935 break;
3936 case clean:
3937 if (mddev->pers) {
3938 restart_array(mddev);
3939 spin_lock_irq(&mddev->write_lock);
3940 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003941 if (mddev->in_sync == 0) {
3942 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003943 if (mddev->safemode == 1)
3944 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003945 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003946 }
3947 err = 0;
3948 } else
3949 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003950 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003951 } else
3952 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003953 break;
3954 case active:
3955 if (mddev->pers) {
3956 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003957 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003958 wake_up(&mddev->sb_wait);
3959 err = 0;
3960 } else {
3961 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003962 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003963 err = do_md_run(mddev);
3964 }
3965 break;
3966 case write_pending:
3967 case active_idle:
3968 /* these cannot be set */
3969 break;
3970 }
3971 if (err)
3972 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003973 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003974 if (mddev->hold_active == UNTIL_IOCTL)
3975 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003976 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003977 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003978 }
NeilBrown9e653b62006-06-26 00:27:58 -07003979}
NeilBrown80ca3a42006-07-10 04:44:18 -07003980static struct md_sysfs_entry md_array_state =
3981__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003982
NeilBrown6d7ff732006-01-06 00:21:16 -08003983static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003984max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003985 return sprintf(page, "%d\n",
3986 atomic_read(&mddev->max_corr_read_errors));
3987}
3988
3989static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003990max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003991{
3992 char *e;
3993 unsigned long n = simple_strtoul(buf, &e, 10);
3994
3995 if (*buf && (*e == 0 || *e == '\n')) {
3996 atomic_set(&mddev->max_corr_read_errors, n);
3997 return len;
3998 }
3999 return -EINVAL;
4000}
4001
4002static struct md_sysfs_entry max_corr_read_errors =
4003__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4004 max_corrected_read_errors_store);
4005
4006static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004007null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08004008{
4009 return -EINVAL;
4010}
4011
4012static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004013new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08004014{
4015 /* buf must be %d:%d\n? giving major and minor numbers */
4016 /* The new device is added to the array.
4017 * If the array has a persistent superblock, we read the
4018 * superblock to initialise info and check validity.
4019 * Otherwise, only checking done is that in bind_rdev_to_array,
4020 * which mainly checks size.
4021 */
4022 char *e;
4023 int major = simple_strtoul(buf, &e, 10);
4024 int minor;
4025 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004026 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08004027 int err;
4028
4029 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4030 return -EINVAL;
4031 minor = simple_strtoul(e+1, &e, 10);
4032 if (*e && *e != '\n')
4033 return -EINVAL;
4034 dev = MKDEV(major, minor);
4035 if (major != MAJOR(dev) ||
4036 minor != MINOR(dev))
4037 return -EOVERFLOW;
4038
4039
4040 if (mddev->persistent) {
4041 rdev = md_import_device(dev, mddev->major_version,
4042 mddev->minor_version);
4043 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004044 struct md_rdev *rdev0
4045 = list_entry(mddev->disks.next,
4046 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08004047 err = super_types[mddev->major_version]
4048 .load_super(rdev, rdev0, mddev->minor_version);
4049 if (err < 0)
4050 goto out;
4051 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004052 } else if (mddev->external)
4053 rdev = md_import_device(dev, -2, -1);
4054 else
NeilBrown6d7ff732006-01-06 00:21:16 -08004055 rdev = md_import_device(dev, -1, -1);
4056
4057 if (IS_ERR(rdev))
4058 return PTR_ERR(rdev);
4059 err = bind_rdev_to_array(rdev, mddev);
4060 out:
4061 if (err)
4062 export_rdev(rdev);
4063 return err ? err : len;
4064}
4065
4066static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004067__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004068
4069static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004070bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004071{
4072 char *end;
4073 unsigned long chunk, end_chunk;
4074
4075 if (!mddev->bitmap)
4076 goto out;
4077 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4078 while (*buf) {
4079 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4080 if (buf == end) break;
4081 if (*end == '-') { /* range */
4082 buf = end + 1;
4083 end_chunk = simple_strtoul(buf, &end, 0);
4084 if (buf == end) break;
4085 }
4086 if (*end && !isspace(*end)) break;
4087 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004088 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004089 }
4090 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4091out:
4092 return len;
4093}
4094
4095static struct md_sysfs_entry md_bitmap =
4096__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4097
4098static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004099size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004100{
Andre Noll58c0fed2009-03-31 14:33:13 +11004101 return sprintf(page, "%llu\n",
4102 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004103}
4104
NeilBrownfd01b882011-10-11 16:47:53 +11004105static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004106
4107static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004108size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004109{
4110 /* If array is inactive, we can reduce the component size, but
4111 * not increase it (except from 0).
4112 * If array is active, we can try an on-line resize
4113 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004114 sector_t sectors;
4115 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004116
Andre Noll58c0fed2009-03-31 14:33:13 +11004117 if (err < 0)
4118 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004119 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004120 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004121 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004122 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004123 if (mddev->dev_sectors == 0 ||
4124 mddev->dev_sectors > sectors)
4125 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004126 else
4127 err = -ENOSPC;
4128 }
4129 return err ? err : len;
4130}
4131
4132static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004133__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004134
NeilBrown8bb93aa2006-01-06 00:20:50 -08004135
Masanari Iida83f0d772012-10-30 00:18:08 +09004136/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004137 * This is one of
4138 * 'none' for arrays with no metadata (good luck...)
4139 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004140 * or N.M for internally known formats
4141 */
4142static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004143metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004144{
4145 if (mddev->persistent)
4146 return sprintf(page, "%d.%d\n",
4147 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004148 else if (mddev->external)
4149 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004150 else
4151 return sprintf(page, "none\n");
4152}
4153
4154static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004155metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004156{
4157 int major, minor;
4158 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004159 /* Changing the details of 'external' metadata is
4160 * always permitted. Otherwise there must be
4161 * no devices attached to the array.
4162 */
4163 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4164 ;
4165 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004166 return -EBUSY;
4167
4168 if (cmd_match(buf, "none")) {
4169 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004170 mddev->external = 0;
4171 mddev->major_version = 0;
4172 mddev->minor_version = 90;
4173 return len;
4174 }
4175 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004176 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004177 if (namelen >= sizeof(mddev->metadata_type))
4178 namelen = sizeof(mddev->metadata_type)-1;
4179 strncpy(mddev->metadata_type, buf+9, namelen);
4180 mddev->metadata_type[namelen] = 0;
4181 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4182 mddev->metadata_type[--namelen] = 0;
4183 mddev->persistent = 0;
4184 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004185 mddev->major_version = 0;
4186 mddev->minor_version = 90;
4187 return len;
4188 }
4189 major = simple_strtoul(buf, &e, 10);
4190 if (e==buf || *e != '.')
4191 return -EINVAL;
4192 buf = e+1;
4193 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004194 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004195 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004196 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004197 return -ENOENT;
4198 mddev->major_version = major;
4199 mddev->minor_version = minor;
4200 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004201 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004202 return len;
4203}
4204
4205static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004206__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004207
NeilBrowna35b0d62006-01-06 00:20:49 -08004208static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004209action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004210{
NeilBrown7eec3142005-11-08 21:39:44 -08004211 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004212 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4213 type = "frozen";
4214 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004215 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004216 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4217 type = "reshape";
4218 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004219 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4220 type = "resync";
4221 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4222 type = "check";
4223 else
4224 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004225 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004226 type = "recover";
4227 }
4228 return sprintf(page, "%s\n", type);
4229}
4230
4231static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004232action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004233{
NeilBrown7eec3142005-11-08 21:39:44 -08004234 if (!mddev->pers || !mddev->pers->sync_request)
4235 return -EINVAL;
4236
NeilBrownb6a9ce62009-05-26 09:41:17 +10004237 if (cmd_match(page, "frozen"))
4238 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4239 else
4240 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4241
4242 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004243 if (mddev->sync_thread) {
4244 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004245 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004246 }
NeilBrown03c902e2006-01-06 00:20:46 -08004247 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4248 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004249 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004250 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004251 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004252 else if (cmd_match(page, "recover")) {
4253 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4254 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4255 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004256 int err;
4257 if (mddev->pers->start_reshape == NULL)
4258 return -EINVAL;
4259 err = mddev->pers->start_reshape(mddev);
4260 if (err)
4261 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004262 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004263 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004264 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004265 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004266 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004267 return -EINVAL;
4268 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4269 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004270 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004271 if (mddev->ro == 2) {
4272 /* A write to sync_action is enough to justify
4273 * canceling read-auto mode
4274 */
4275 mddev->ro = 0;
4276 md_wakeup_thread(mddev->sync_thread);
4277 }
NeilBrown03c902e2006-01-06 00:20:46 -08004278 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004279 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004280 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004281 return len;
4282}
4283
NeilBrown9d888832005-11-08 21:39:26 -08004284static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004285mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004286{
4287 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004288 (unsigned long long)
4289 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004290}
4291
NeilBrown80ca3a42006-07-10 04:44:18 -07004292static struct md_sysfs_entry md_scan_mode =
4293__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004294
NeilBrown96de1e62005-11-08 21:39:39 -08004295
NeilBrown80ca3a42006-07-10 04:44:18 -07004296static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004297
NeilBrown88202a02006-01-06 00:21:36 -08004298static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004299sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004300{
4301 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4302 mddev->sync_speed_min ? "local": "system");
4303}
4304
4305static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004306sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004307{
4308 int min;
4309 char *e;
4310 if (strncmp(buf, "system", 6)==0) {
4311 mddev->sync_speed_min = 0;
4312 return len;
4313 }
4314 min = simple_strtoul(buf, &e, 10);
4315 if (buf == e || (*e && *e != '\n') || min <= 0)
4316 return -EINVAL;
4317 mddev->sync_speed_min = min;
4318 return len;
4319}
4320
4321static struct md_sysfs_entry md_sync_min =
4322__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4323
4324static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004325sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004326{
4327 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4328 mddev->sync_speed_max ? "local": "system");
4329}
4330
4331static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004332sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004333{
4334 int max;
4335 char *e;
4336 if (strncmp(buf, "system", 6)==0) {
4337 mddev->sync_speed_max = 0;
4338 return len;
4339 }
4340 max = simple_strtoul(buf, &e, 10);
4341 if (buf == e || (*e && *e != '\n') || max <= 0)
4342 return -EINVAL;
4343 mddev->sync_speed_max = max;
4344 return len;
4345}
4346
4347static struct md_sysfs_entry md_sync_max =
4348__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4349
Iustin Popd7f3d292007-10-16 23:30:54 -07004350static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004351degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004352{
4353 return sprintf(page, "%d\n", mddev->degraded);
4354}
4355static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004356
4357static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004358sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004359{
4360 return sprintf(page, "%d\n", mddev->parallel_resync);
4361}
4362
4363static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004364sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004365{
4366 long n;
4367
4368 if (strict_strtol(buf, 10, &n))
4369 return -EINVAL;
4370
4371 if (n != 0 && n != 1)
4372 return -EINVAL;
4373
4374 mddev->parallel_resync = n;
4375
4376 if (mddev->sync_thread)
4377 wake_up(&resync_wait);
4378
4379 return len;
4380}
4381
4382/* force parallel resync, even with shared block devices */
4383static struct md_sysfs_entry md_sync_force_parallel =
4384__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4385 sync_force_parallel_show, sync_force_parallel_store);
4386
4387static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004388sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004389{
4390 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004391 if (mddev->curr_resync == 0)
4392 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004393 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4394 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004395 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004396 db = resync - mddev->resync_mark_cnt;
4397 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004398}
4399
NeilBrown80ca3a42006-07-10 04:44:18 -07004400static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004401
4402static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004403sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004404{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004405 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004406
NeilBrownacb180b2009-04-14 16:28:34 +10004407 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4408 return sprintf(page, "none\n");
4409
NeilBrown72f36d52012-10-11 14:25:57 +11004410 if (mddev->curr_resync == 1 ||
4411 mddev->curr_resync == 2)
4412 return sprintf(page, "delayed\n");
4413
NeilBrownc804cde2012-05-21 09:28:33 +10004414 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4415 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004416 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004417 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004418 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004419
NeilBrownacb180b2009-04-14 16:28:34 +10004420 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004421 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004422}
4423
NeilBrown80ca3a42006-07-10 04:44:18 -07004424static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004425
NeilBrowne464eaf2006-03-27 01:18:14 -08004426static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004427min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004428{
4429 return sprintf(page, "%llu\n",
4430 (unsigned long long)mddev->resync_min);
4431}
4432static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004433min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004434{
4435 unsigned long long min;
4436 if (strict_strtoull(buf, 10, &min))
4437 return -EINVAL;
4438 if (min > mddev->resync_max)
4439 return -EINVAL;
4440 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4441 return -EBUSY;
4442
4443 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004444 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004445 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004446 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004447 return -EINVAL;
4448 }
4449 mddev->resync_min = min;
4450
4451 return len;
4452}
4453
4454static struct md_sysfs_entry md_min_sync =
4455__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4456
4457static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004458max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004459{
4460 if (mddev->resync_max == MaxSector)
4461 return sprintf(page, "max\n");
4462 else
4463 return sprintf(page, "%llu\n",
4464 (unsigned long long)mddev->resync_max);
4465}
4466static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004467max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004468{
4469 if (strncmp(buf, "max", 3) == 0)
4470 mddev->resync_max = MaxSector;
4471 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004472 unsigned long long max;
4473 if (strict_strtoull(buf, 10, &max))
4474 return -EINVAL;
4475 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004476 return -EINVAL;
4477 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004478 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004479 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4480 return -EBUSY;
4481
4482 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004483 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004484 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004485 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004486 return -EINVAL;
4487 }
4488 mddev->resync_max = max;
4489 }
4490 wake_up(&mddev->recovery_wait);
4491 return len;
4492}
4493
4494static struct md_sysfs_entry md_max_sync =
4495__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4496
4497static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004498suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004499{
4500 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4501}
4502
4503static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004504suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004505{
4506 char *e;
4507 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004508 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004509
NeilBrownb8d966e2009-07-01 11:14:04 +10004510 if (mddev->pers == NULL ||
4511 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004512 return -EINVAL;
4513 if (buf == e || (*e && *e != '\n'))
4514 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004515
4516 mddev->suspend_lo = new;
4517 if (new >= old)
4518 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004519 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004520 else {
4521 /* Expanding suspended region - need to wait */
4522 mddev->pers->quiesce(mddev, 1);
4523 mddev->pers->quiesce(mddev, 0);
4524 }
4525 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004526}
4527static struct md_sysfs_entry md_suspend_lo =
4528__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4529
4530
4531static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004532suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004533{
4534 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4535}
4536
4537static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004538suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004539{
4540 char *e;
4541 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004542 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004543
NeilBrownb8d966e2009-07-01 11:14:04 +10004544 if (mddev->pers == NULL ||
4545 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004546 return -EINVAL;
4547 if (buf == e || (*e && *e != '\n'))
4548 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004549
4550 mddev->suspend_hi = new;
4551 if (new <= old)
4552 /* Shrinking suspended region */
4553 mddev->pers->quiesce(mddev, 2);
4554 else {
4555 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004556 mddev->pers->quiesce(mddev, 1);
4557 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004558 }
4559 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004560}
4561static struct md_sysfs_entry md_suspend_hi =
4562__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4563
NeilBrown08a02ec2007-05-09 02:35:38 -07004564static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004565reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004566{
4567 if (mddev->reshape_position != MaxSector)
4568 return sprintf(page, "%llu\n",
4569 (unsigned long long)mddev->reshape_position);
4570 strcpy(page, "none\n");
4571 return 5;
4572}
4573
4574static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004575reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004576{
NeilBrownc6563a82012-05-21 09:27:00 +10004577 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004578 char *e;
4579 unsigned long long new = simple_strtoull(buf, &e, 10);
4580 if (mddev->pers)
4581 return -EBUSY;
4582 if (buf == e || (*e && *e != '\n'))
4583 return -EINVAL;
4584 mddev->reshape_position = new;
4585 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004586 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004587 mddev->new_level = mddev->level;
4588 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004589 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004590 rdev_for_each(rdev, mddev)
4591 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004592 return len;
4593}
4594
4595static struct md_sysfs_entry md_reshape_position =
4596__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4597 reshape_position_store);
4598
Dan Williamsb522adc2009-03-31 15:00:31 +11004599static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004600reshape_direction_show(struct mddev *mddev, char *page)
4601{
4602 return sprintf(page, "%s\n",
4603 mddev->reshape_backwards ? "backwards" : "forwards");
4604}
4605
4606static ssize_t
4607reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4608{
4609 int backwards = 0;
4610 if (cmd_match(buf, "forwards"))
4611 backwards = 0;
4612 else if (cmd_match(buf, "backwards"))
4613 backwards = 1;
4614 else
4615 return -EINVAL;
4616 if (mddev->reshape_backwards == backwards)
4617 return len;
4618
4619 /* check if we are allowed to change */
4620 if (mddev->delta_disks)
4621 return -EBUSY;
4622
4623 if (mddev->persistent &&
4624 mddev->major_version == 0)
4625 return -EINVAL;
4626
4627 mddev->reshape_backwards = backwards;
4628 return len;
4629}
4630
4631static struct md_sysfs_entry md_reshape_direction =
4632__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4633 reshape_direction_store);
4634
4635static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004636array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004637{
4638 if (mddev->external_size)
4639 return sprintf(page, "%llu\n",
4640 (unsigned long long)mddev->array_sectors/2);
4641 else
4642 return sprintf(page, "default\n");
4643}
4644
4645static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004646array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004647{
4648 sector_t sectors;
4649
4650 if (strncmp(buf, "default", 7) == 0) {
4651 if (mddev->pers)
4652 sectors = mddev->pers->size(mddev, 0, 0);
4653 else
4654 sectors = mddev->array_sectors;
4655
4656 mddev->external_size = 0;
4657 } else {
4658 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4659 return -EINVAL;
4660 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004661 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004662
4663 mddev->external_size = 1;
4664 }
4665
4666 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004667 if (mddev->pers) {
4668 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004669 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004670 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004671 return len;
4672}
4673
4674static struct md_sysfs_entry md_array_size =
4675__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4676 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004677
NeilBrowneae17012005-11-08 21:39:23 -08004678static struct attribute *md_default_attrs[] = {
4679 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004680 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004681 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004682 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004683 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004684 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004685 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004686 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004687 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004688 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004689 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004690 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004691 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004692 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004693 NULL,
4694};
4695
4696static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004697 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004698 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004699 &md_sync_min.attr,
4700 &md_sync_max.attr,
4701 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004702 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004703 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004704 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004705 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004706 &md_suspend_lo.attr,
4707 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004708 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004709 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004710 NULL,
4711};
NeilBrown411036f2005-11-08 21:39:40 -08004712static struct attribute_group md_redundancy_group = {
4713 .name = NULL,
4714 .attrs = md_redundancy_attrs,
4715};
4716
NeilBrowneae17012005-11-08 21:39:23 -08004717
4718static ssize_t
4719md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4720{
4721 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004722 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004723 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004724
4725 if (!entry->show)
4726 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004727 spin_lock(&all_mddevs_lock);
4728 if (list_empty(&mddev->all_mddevs)) {
4729 spin_unlock(&all_mddevs_lock);
4730 return -EBUSY;
4731 }
4732 mddev_get(mddev);
4733 spin_unlock(&all_mddevs_lock);
4734
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004735 rv = mddev_lock(mddev);
4736 if (!rv) {
4737 rv = entry->show(mddev, page);
4738 mddev_unlock(mddev);
4739 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004740 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004741 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004742}
4743
4744static ssize_t
4745md_attr_store(struct kobject *kobj, struct attribute *attr,
4746 const char *page, size_t length)
4747{
4748 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004749 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004750 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004751
4752 if (!entry->store)
4753 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004754 if (!capable(CAP_SYS_ADMIN))
4755 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004756 spin_lock(&all_mddevs_lock);
4757 if (list_empty(&mddev->all_mddevs)) {
4758 spin_unlock(&all_mddevs_lock);
4759 return -EBUSY;
4760 }
4761 mddev_get(mddev);
4762 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004763 if (entry->store == new_dev_store)
4764 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004765 rv = mddev_lock(mddev);
4766 if (!rv) {
4767 rv = entry->store(mddev, page, length);
4768 mddev_unlock(mddev);
4769 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004770 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004771 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004772}
4773
4774static void md_free(struct kobject *ko)
4775{
NeilBrownfd01b882011-10-11 16:47:53 +11004776 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004777
4778 if (mddev->sysfs_state)
4779 sysfs_put(mddev->sysfs_state);
4780
4781 if (mddev->gendisk) {
4782 del_gendisk(mddev->gendisk);
4783 put_disk(mddev->gendisk);
4784 }
4785 if (mddev->queue)
4786 blk_cleanup_queue(mddev->queue);
4787
NeilBrowneae17012005-11-08 21:39:23 -08004788 kfree(mddev);
4789}
4790
Emese Revfy52cf25d2010-01-19 02:58:23 +01004791static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004792 .show = md_attr_show,
4793 .store = md_attr_store,
4794};
4795static struct kobj_type md_ktype = {
4796 .release = md_free,
4797 .sysfs_ops = &md_sysfs_ops,
4798 .default_attrs = md_default_attrs,
4799};
4800
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801int mdp_major = 0;
4802
Dan Williams5fd3a172009-03-04 00:57:25 -07004803static void mddev_delayed_delete(struct work_struct *ws)
4804{
NeilBrownfd01b882011-10-11 16:47:53 +11004805 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004806
NeilBrown43a70502009-12-14 12:49:55 +11004807 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004808 kobject_del(&mddev->kobj);
4809 kobject_put(&mddev->kobj);
4810}
4811
NeilBrownefeb53c2009-01-09 08:31:10 +11004812static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004814 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004815 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004817 int partitioned;
4818 int shift;
4819 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004820 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
4822 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004823 return -ENODEV;
4824
4825 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4826 shift = partitioned ? MdpMinorShift : 0;
4827 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Tejun Heoe804ac72010-10-15 15:36:08 +02004829 /* wait for any previous instance of this device to be
4830 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004831 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004832 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004833
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004834 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004835 error = -EEXIST;
4836 if (mddev->gendisk)
4837 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004838
4839 if (name) {
4840 /* Need to ensure that 'name' is not a duplicate.
4841 */
NeilBrownfd01b882011-10-11 16:47:53 +11004842 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004843 spin_lock(&all_mddevs_lock);
4844
4845 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4846 if (mddev2->gendisk &&
4847 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4848 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004849 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004850 }
4851 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 }
NeilBrown8b765392009-01-09 08:31:08 +11004853
NeilBrown0909dc42009-07-01 12:27:21 +10004854 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004855 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004856 if (!mddev->queue)
4857 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004858 mddev->queue->queuedata = mddev;
4859
NeilBrown409c57f2009-03-31 14:39:39 +11004860 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004861 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 disk = alloc_disk(1 << shift);
4864 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004865 blk_cleanup_queue(mddev->queue);
4866 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004867 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004869 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004871 if (name)
4872 strcpy(disk->disk_name, name);
4873 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004875 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 disk->fops = &md_fops;
4878 disk->private_data = mddev;
4879 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004880 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004881 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004882 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004883 * remove it now.
4884 */
4885 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004887 /* As soon as we call add_disk(), another thread could get
4888 * through to md_open, so make sure it doesn't get too far
4889 */
4890 mutex_lock(&mddev->open_mutex);
4891 add_disk(disk);
4892
Tejun Heoed9e1982008-08-25 19:56:05 +09004893 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4894 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004895 if (error) {
4896 /* This isn't possible, but as kobject_init_and_add is marked
4897 * __must_check, we must do something with the result
4898 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004899 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4900 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004901 error = 0;
4902 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004903 if (mddev->kobj.sd &&
4904 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004905 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004906 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004907 abort:
4908 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004909 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004910 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004911 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004912 }
NeilBrownd3374822009-01-09 08:31:10 +11004913 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004914 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004915}
4916
4917static struct kobject *md_probe(dev_t dev, int *part, void *data)
4918{
4919 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 return NULL;
4921}
4922
NeilBrownefeb53c2009-01-09 08:31:10 +11004923static int add_named_array(const char *val, struct kernel_param *kp)
4924{
4925 /* val must be "md_*" where * is not all digits.
4926 * We allocate an array with a large free minor number, and
4927 * set the name to val. val must not already be an active name.
4928 */
4929 int len = strlen(val);
4930 char buf[DISK_NAME_LEN];
4931
4932 while (len && val[len-1] == '\n')
4933 len--;
4934 if (len >= DISK_NAME_LEN)
4935 return -E2BIG;
4936 strlcpy(buf, val, len+1);
4937 if (strncmp(buf, "md_", 3) != 0)
4938 return -EINVAL;
4939 return md_alloc(0, buf);
4940}
4941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942static void md_safemode_timeout(unsigned long data)
4943{
NeilBrownfd01b882011-10-11 16:47:53 +11004944 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
Neil Brown0fd62b82008-06-28 08:31:36 +10004946 if (!atomic_read(&mddev->writes_pending)) {
4947 mddev->safemode = 1;
4948 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004949 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 md_wakeup_thread(mddev->thread);
4952}
4953
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004954static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
NeilBrownfd01b882011-10-11 16:47:53 +11004956int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957{
NeilBrown2604b702006-01-06 00:20:36 -08004958 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004959 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004960 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
NeilBrowna757e642005-04-16 15:26:42 -07004962 if (list_empty(&mddev->disks))
4963 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
4966 if (mddev->pers)
4967 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004968 /* Cannot run until previous stop completes properly */
4969 if (mddev->sysfs_active)
4970 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004971
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 /*
4973 * Analyze all RAID superblock(s)
4974 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004975 if (!mddev->raid_disks) {
4976 if (!mddev->persistent)
4977 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004978 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
NeilBrownd9d166c2006-01-06 00:20:51 -08004981 if (mddev->level != LEVEL_NONE)
4982 request_module("md-level-%d", mddev->level);
4983 else if (mddev->clevel[0])
4984 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
4986 /*
4987 * Drop all container device buffers, from now on
4988 * the only valid external interface is through the md
4989 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004991 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004992 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 continue;
4994 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004995 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004996
4997 /* perform some consistency tests on the device.
4998 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004999 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005000 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005001 if (rdev->meta_bdev) {
5002 /* Nothing to check */;
5003 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005004 if (mddev->dev_sectors &&
5005 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005006 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005007 printk("md: %s: data overlaps metadata\n",
5008 mdname(mddev));
5009 return -EINVAL;
5010 }
5011 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005012 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005013 > rdev->data_offset) {
5014 printk("md: %s: metadata overlaps data\n",
5015 mdname(mddev));
5016 return -EINVAL;
5017 }
5018 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005019 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 }
5021
NeilBrowna167f662010-10-26 18:31:13 +11005022 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005023 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005024
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005026 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005027 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005029 if (mddev->level != LEVEL_NONE)
5030 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5031 mddev->level);
5032 else
5033 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5034 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 return -EINVAL;
5036 }
NeilBrown2604b702006-01-06 00:20:36 -08005037 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005039 if (mddev->level != pers->level) {
5040 mddev->level = pers->level;
5041 mddev->new_level = pers->level;
5042 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005043 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
NeilBrownf6705572006-03-27 01:18:11 -08005045 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005046 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005047 /* This personality cannot handle reshaping... */
5048 mddev->pers = NULL;
5049 module_put(pers->owner);
5050 return -EINVAL;
5051 }
5052
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005053 if (pers->sync_request) {
5054 /* Warn if this is a potentially silly
5055 * configuration.
5056 */
5057 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005058 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005059 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005060
NeilBrowndafb20f2012-03-19 12:46:39 +11005061 rdev_for_each(rdev, mddev)
5062 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005063 if (rdev < rdev2 &&
5064 rdev->bdev->bd_contains ==
5065 rdev2->bdev->bd_contains) {
5066 printk(KERN_WARNING
5067 "%s: WARNING: %s appears to be"
5068 " on the same physical disk as"
5069 " %s.\n",
5070 mdname(mddev),
5071 bdevname(rdev->bdev,b),
5072 bdevname(rdev2->bdev,b2));
5073 warned = 1;
5074 }
5075 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005076
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005077 if (warned)
5078 printk(KERN_WARNING
5079 "True protection against single-disk"
5080 " failure might be compromised.\n");
5081 }
5082
NeilBrown657390d2005-08-26 18:34:16 -07005083 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005084 /* may be over-ridden by personality */
5085 mddev->resync_max_sectors = mddev->dev_sectors;
5086
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005087 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
NeilBrown0f9552b2009-12-30 12:08:50 +11005089 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005090 mddev->ro = 2; /* read-only, but switch on first write */
5091
NeilBrownb15c2e52006-01-06 00:20:16 -08005092 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005093 if (err)
5094 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005095 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5096 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5097 " but 'external_size' not in effect?\n", __func__);
5098 printk(KERN_ERR
5099 "md: invalid array_size %llu > default size %llu\n",
5100 (unsigned long long)mddev->array_sectors / 2,
5101 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5102 err = -EINVAL;
5103 mddev->pers->stop(mddev);
5104 }
NeilBrownef99bf42012-05-22 13:55:08 +10005105 if (err == 0 && mddev->pers->sync_request &&
5106 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005107 err = bitmap_create(mddev);
5108 if (err) {
5109 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5110 mdname(mddev), err);
5111 mddev->pers->stop(mddev);
5112 }
5113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 module_put(mddev->pers->owner);
5116 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005117 bitmap_destroy(mddev);
5118 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005120 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005121 if (mddev->kobj.sd &&
5122 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005123 printk(KERN_WARNING
5124 "md: cannot register extra attributes for %s\n",
5125 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005126 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005127 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005128 mddev->ro = 0;
5129
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005131 atomic_set(&mddev->max_corr_read_errors,
5132 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 mddev->safemode = 0;
5134 mddev->safemode_timer.function = md_safemode_timeout;
5135 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005136 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005138 smp_wmb();
5139 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005140 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005141 if (rdev->raid_disk >= 0)
5142 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005143 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
5145 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5146
NeilBrown850b2b42006-10-03 01:15:46 -07005147 if (mddev->flags)
5148 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
NeilBrownd7603b72006-01-06 00:20:30 -08005150 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005151 sysfs_notify_dirent_safe(mddev->sysfs_state);
5152 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005153 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 return 0;
5155}
NeilBrown390ee602010-06-01 19:37:27 +10005156EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157
NeilBrownfd01b882011-10-11 16:47:53 +11005158static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005159{
5160 int err;
5161
5162 err = md_run(mddev);
5163 if (err)
5164 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005165 err = bitmap_load(mddev);
5166 if (err) {
5167 bitmap_destroy(mddev);
5168 goto out;
5169 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005170
5171 md_wakeup_thread(mddev->thread);
5172 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5173
NeilBrownfe60b012010-03-29 11:10:42 +11005174 set_capacity(mddev->gendisk, mddev->array_sectors);
5175 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005176 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005177 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5178out:
5179 return err;
5180}
5181
NeilBrownfd01b882011-10-11 16:47:53 +11005182static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183{
5184 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
Andre Noll80fab1d2008-07-11 22:02:21 +10005186 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005188 return -ENXIO;
5189 if (!mddev->pers)
5190 return -EINVAL;
5191 if (!mddev->ro)
5192 return -EBUSY;
5193 mddev->safemode = 0;
5194 mddev->ro = 0;
5195 set_disk_ro(disk, 0);
5196 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5197 mdname(mddev));
5198 /* Kick recovery or resync if necessary */
5199 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5200 md_wakeup_thread(mddev->thread);
5201 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005202 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204}
5205
NeilBrownacc55e22006-06-26 00:27:47 -07005206/* similar to deny_write_access, but accounts for our holding a reference
5207 * to the file ourselves */
5208static int deny_bitmap_write_access(struct file * file)
5209{
5210 struct inode *inode = file->f_mapping->host;
5211
5212 spin_lock(&inode->i_lock);
5213 if (atomic_read(&inode->i_writecount) > 1) {
5214 spin_unlock(&inode->i_lock);
5215 return -ETXTBSY;
5216 }
5217 atomic_set(&inode->i_writecount, -1);
5218 spin_unlock(&inode->i_lock);
5219
5220 return 0;
5221}
5222
NeilBrown43a70502009-12-14 12:49:55 +11005223void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005224{
5225 struct inode *inode = file->f_mapping->host;
5226
5227 spin_lock(&inode->i_lock);
5228 atomic_set(&inode->i_writecount, 1);
5229 spin_unlock(&inode->i_lock);
5230}
5231
NeilBrownfd01b882011-10-11 16:47:53 +11005232static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005233{
5234 mddev->array_sectors = 0;
5235 mddev->external_size = 0;
5236 mddev->dev_sectors = 0;
5237 mddev->raid_disks = 0;
5238 mddev->recovery_cp = 0;
5239 mddev->resync_min = 0;
5240 mddev->resync_max = MaxSector;
5241 mddev->reshape_position = MaxSector;
5242 mddev->external = 0;
5243 mddev->persistent = 0;
5244 mddev->level = LEVEL_NONE;
5245 mddev->clevel[0] = 0;
5246 mddev->flags = 0;
5247 mddev->ro = 0;
5248 mddev->metadata_type[0] = 0;
5249 mddev->chunk_sectors = 0;
5250 mddev->ctime = mddev->utime = 0;
5251 mddev->layout = 0;
5252 mddev->max_disks = 0;
5253 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005254 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005255 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005256 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005257 mddev->new_level = LEVEL_NONE;
5258 mddev->new_layout = 0;
5259 mddev->new_chunk_sectors = 0;
5260 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005261 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005262 mddev->suspend_lo = mddev->suspend_hi = 0;
5263 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5264 mddev->recovery = 0;
5265 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005266 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005267 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005268 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005269 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005270 mddev->bitmap_info.offset = 0;
5271 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005272 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005273 mddev->bitmap_info.chunksize = 0;
5274 mddev->bitmap_info.daemon_sleep = 0;
5275 mddev->bitmap_info.max_write_behind = 0;
5276}
5277
NeilBrownfd01b882011-10-11 16:47:53 +11005278static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005279{
5280 if (mddev->sync_thread) {
5281 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5282 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005283 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005284 }
5285
5286 del_timer_sync(&mddev->safemode_timer);
5287
5288 bitmap_flush(mddev);
5289 md_super_wait(mddev);
5290
NeilBrownb6d428c2013-04-24 11:42:42 +10005291 if (mddev->ro == 0 &&
5292 (!mddev->in_sync || mddev->flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005293 /* mark array as shutdown cleanly */
5294 mddev->in_sync = 1;
5295 md_update_sb(mddev, 1);
5296 }
5297}
NeilBrowndefad612011-01-14 09:14:33 +11005298
NeilBrownfd01b882011-10-11 16:47:53 +11005299void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005300{
5301 mddev_lock(mddev);
5302 __md_stop_writes(mddev);
5303 mddev_unlock(mddev);
5304}
NeilBrown390ee602010-06-01 19:37:27 +10005305EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005306
NeilBrown5eff3c42012-11-19 10:47:48 +11005307static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005308{
NeilBrown0ca69882011-01-14 09:14:33 +11005309 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005310 mddev->pers->stop(mddev);
5311 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5312 mddev->to_remove = &md_redundancy_group;
5313 module_put(mddev->pers->owner);
5314 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005315 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005316}
NeilBrown5eff3c42012-11-19 10:47:48 +11005317
5318void md_stop(struct mddev *mddev)
5319{
5320 /* stop the array and free an attached data structures.
5321 * This is called from dm-raid
5322 */
5323 __md_stop(mddev);
5324 bitmap_destroy(mddev);
5325 if (mddev->bio_set)
5326 bioset_free(mddev->bio_set);
5327}
5328
NeilBrown390ee602010-06-01 19:37:27 +10005329EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005330
NeilBrowna05b7ea02012-07-19 15:59:18 +10005331static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005332{
5333 int err = 0;
5334 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005335 if (atomic_read(&mddev->openers) > !!bdev) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005336 printk("md: %s still in use.\n",mdname(mddev));
5337 err = -EBUSY;
5338 goto out;
5339 }
NeilBrowna05b7ea02012-07-19 15:59:18 +10005340 if (bdev)
5341 sync_blockdev(bdev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005342 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005343 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005344
5345 err = -ENXIO;
5346 if (mddev->ro==1)
5347 goto out;
5348 mddev->ro = 1;
5349 set_disk_ro(mddev->gendisk, 1);
5350 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005351 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005352 err = 0;
5353 }
5354out:
5355 mutex_unlock(&mddev->open_mutex);
5356 return err;
5357}
5358
NeilBrown9e653b62006-06-26 00:27:58 -07005359/* mode:
5360 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005361 * 2 - stop but do not disassemble array
5362 */
NeilBrowna05b7ea02012-07-19 15:59:18 +10005363static int do_md_stop(struct mddev * mddev, int mode,
5364 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005367 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
NeilBrownc8c00a62009-08-10 12:50:52 +10005369 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005370 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10005371 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005372 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005373 mutex_unlock(&mddev->open_mutex);
5374 return -EBUSY;
5375 }
NeilBrowna05b7ea02012-07-19 15:59:18 +10005376 if (bdev)
5377 /* It is possible IO was issued on some other
5378 * open file which was closed before we took ->open_mutex.
5379 * As that was not the last close __blkdev_put will not
5380 * have called sync_blockdev, so we must.
5381 */
5382 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
NeilBrown6e17b022010-08-07 21:41:19 +10005384 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005385 if (mddev->ro)
5386 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005387
NeilBrowndefad612011-01-14 09:14:33 +11005388 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005389 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005390 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005391 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005392
NeilBrowna4bd82d2010-03-29 13:23:10 +11005393 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005394 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005395
NeilBrowndafb20f2012-03-19 12:46:39 +11005396 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005397 if (rdev->raid_disk >= 0)
5398 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005399
NeilBrowna4bd82d2010-03-29 13:23:10 +11005400 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005401 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005402 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005403 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005404
NeilBrowna4bd82d2010-03-29 13:23:10 +11005405 if (mddev->ro)
5406 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005407 } else
5408 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 /*
5410 * Free resources if final stop
5411 */
NeilBrown9e653b62006-06-26 00:27:58 -07005412 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5414
NeilBrown978f9462006-02-02 14:28:05 -08005415 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005416 if (mddev->bitmap_info.file) {
5417 restore_bitmap_write_access(mddev->bitmap_info.file);
5418 fput(mddev->bitmap_info.file);
5419 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005420 }
NeilBrownc3d97142009-12-14 12:49:52 +11005421 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005422
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 export_array(mddev);
5424
NeilBrown6177b472010-03-29 11:37:13 +11005425 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005426 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005427 if (mddev->hold_active == UNTIL_STOP)
5428 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005429 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005430 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005431 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005432 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434}
5435
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005436#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005437static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438{
NeilBrown3cb03002011-10-11 16:45:26 +11005439 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 int err;
5441
NeilBrowna757e642005-04-16 15:26:42 -07005442 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
5445 printk(KERN_INFO "md: running: ");
5446
NeilBrowndafb20f2012-03-19 12:46:39 +11005447 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 char b[BDEVNAME_SIZE];
5449 printk("<%s>", bdevname(rdev->bdev,b));
5450 }
5451 printk("\n");
5452
NeilBrownd710e132008-10-13 11:55:12 +11005453 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 if (err) {
5455 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005456 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 }
5458}
5459
5460/*
5461 * lets try to run arrays based on all disks that have arrived
5462 * until now. (those are in pending_raid_disks)
5463 *
5464 * the method: pick the first pending disk, collect all disks with
5465 * the same UUID, remove all from the pending list and put them into
5466 * the 'same_array' list. Then order this list based on superblock
5467 * update time (freshest comes first), kick out 'old' disks and
5468 * compare superblocks. If everything's fine then run it.
5469 *
5470 * If "unit" is allocated, then bump its reference count
5471 */
5472static void autorun_devices(int part)
5473{
NeilBrown3cb03002011-10-11 16:45:26 +11005474 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005475 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 char b[BDEVNAME_SIZE];
5477
5478 printk(KERN_INFO "md: autorun ...\n");
5479 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005480 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005482 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005484 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
5486 printk(KERN_INFO "md: considering %s ...\n",
5487 bdevname(rdev0->bdev,b));
5488 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005489 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 if (super_90_load(rdev, rdev0, 0) >= 0) {
5491 printk(KERN_INFO "md: adding %s ...\n",
5492 bdevname(rdev->bdev,b));
5493 list_move(&rdev->same_set, &candidates);
5494 }
5495 /*
5496 * now we have a set of devices, with all of them having
5497 * mostly sane superblocks. It's time to allocate the
5498 * mddev.
5499 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005500 if (part) {
5501 dev = MKDEV(mdp_major,
5502 rdev0->preferred_minor << MdpMinorShift);
5503 unit = MINOR(dev) >> MdpMinorShift;
5504 } else {
5505 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5506 unit = MINOR(dev);
5507 }
5508 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5510 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5511 break;
5512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
5514 md_probe(dev, NULL, NULL);
5515 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005516 if (!mddev || !mddev->gendisk) {
5517 if (mddev)
5518 mddev_put(mddev);
5519 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 "md: cannot allocate memory for md drive.\n");
5521 break;
5522 }
5523 if (mddev_lock(mddev))
5524 printk(KERN_WARNING "md: %s locked, cannot run\n",
5525 mdname(mddev));
5526 else if (mddev->raid_disks || mddev->major_version
5527 || !list_empty(&mddev->disks)) {
5528 printk(KERN_WARNING
5529 "md: %s already running, cannot run %s\n",
5530 mdname(mddev), bdevname(rdev0->bdev,b));
5531 mddev_unlock(mddev);
5532 } else {
5533 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005534 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005535 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 list_del_init(&rdev->same_set);
5537 if (bind_rdev_to_array(rdev, mddev))
5538 export_rdev(rdev);
5539 }
5540 autorun_array(mddev);
5541 mddev_unlock(mddev);
5542 }
5543 /* on success, candidates will be empty, on error
5544 * it won't...
5545 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005546 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005547 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 mddev_put(mddev);
5551 }
5552 printk(KERN_INFO "md: ... autorun DONE.\n");
5553}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005554#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556static int get_version(void __user * arg)
5557{
5558 mdu_version_t ver;
5559
5560 ver.major = MD_MAJOR_VERSION;
5561 ver.minor = MD_MINOR_VERSION;
5562 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5563
5564 if (copy_to_user(arg, &ver, sizeof(ver)))
5565 return -EFAULT;
5566
5567 return 0;
5568}
5569
NeilBrownfd01b882011-10-11 16:47:53 +11005570static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571{
5572 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005573 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005574 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
NeilBrown1ca69c42012-10-11 13:37:33 +11005576 nr = working = insync = failed = spare = 0;
5577 rcu_read_lock();
5578 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005580 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 failed++;
5582 else {
5583 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005584 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005585 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 else
5587 spare++;
5588 }
5589 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005590 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
5592 info.major_version = mddev->major_version;
5593 info.minor_version = mddev->minor_version;
5594 info.patch_version = MD_PATCHLEVEL_VERSION;
5595 info.ctime = mddev->ctime;
5596 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005597 info.size = mddev->dev_sectors / 2;
5598 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005599 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 info.nr_disks = nr;
5601 info.raid_disks = mddev->raid_disks;
5602 info.md_minor = mddev->md_minor;
5603 info.not_persistent= !mddev->persistent;
5604
5605 info.utime = mddev->utime;
5606 info.state = 0;
5607 if (mddev->in_sync)
5608 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005609 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005610 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005611 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 info.working_disks = working;
5613 info.failed_disks = failed;
5614 info.spare_disks = spare;
5615
5616 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005617 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
5619 if (copy_to_user(arg, &info, sizeof(info)))
5620 return -EFAULT;
5621
5622 return 0;
5623}
5624
NeilBrownfd01b882011-10-11 16:47:53 +11005625static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005626{
5627 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5628 char *ptr, *buf = NULL;
5629 int err = -ENOMEM;
5630
Dan Williamsb5470dc2008-06-27 21:44:04 -07005631 if (md_allow_write(mddev))
5632 file = kmalloc(sizeof(*file), GFP_NOIO);
5633 else
5634 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005635
NeilBrown32a76272005-06-21 17:17:14 -07005636 if (!file)
5637 goto out;
5638
5639 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005640 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005641 file->pathname[0] = '\0';
5642 goto copy_out;
5643 }
5644
5645 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5646 if (!buf)
5647 goto out;
5648
NeilBrown1ec885c2012-05-22 13:55:10 +10005649 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5650 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005651 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005652 goto out;
5653
5654 strcpy(file->pathname, ptr);
5655
5656copy_out:
5657 err = 0;
5658 if (copy_to_user(arg, file, sizeof(*file)))
5659 err = -EFAULT;
5660out:
5661 kfree(buf);
5662 kfree(file);
5663 return err;
5664}
5665
NeilBrownfd01b882011-10-11 16:47:53 +11005666static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667{
5668 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005669 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
5671 if (copy_from_user(&info, arg, sizeof(info)))
5672 return -EFAULT;
5673
NeilBrown1ca69c42012-10-11 13:37:33 +11005674 rcu_read_lock();
5675 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 if (rdev) {
5677 info.major = MAJOR(rdev->bdev->bd_dev);
5678 info.minor = MINOR(rdev->bdev->bd_dev);
5679 info.raid_disk = rdev->raid_disk;
5680 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005681 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005683 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 info.state |= (1<<MD_DISK_ACTIVE);
5685 info.state |= (1<<MD_DISK_SYNC);
5686 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005687 if (test_bit(WriteMostly, &rdev->flags))
5688 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 } else {
5690 info.major = info.minor = 0;
5691 info.raid_disk = -1;
5692 info.state = (1<<MD_DISK_REMOVED);
5693 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005694 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
5696 if (copy_to_user(arg, &info, sizeof(info)))
5697 return -EFAULT;
5698
5699 return 0;
5700}
5701
NeilBrownfd01b882011-10-11 16:47:53 +11005702static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703{
5704 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005705 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 dev_t dev = MKDEV(info->major,info->minor);
5707
5708 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5709 return -EOVERFLOW;
5710
5711 if (!mddev->raid_disks) {
5712 int err;
5713 /* expecting a device which has a superblock */
5714 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5715 if (IS_ERR(rdev)) {
5716 printk(KERN_WARNING
5717 "md: md_import_device returned %ld\n",
5718 PTR_ERR(rdev));
5719 return PTR_ERR(rdev);
5720 }
5721 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005722 struct md_rdev *rdev0
5723 = list_entry(mddev->disks.next,
5724 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005725 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 .load_super(rdev, rdev0, mddev->minor_version);
5727 if (err < 0) {
5728 printk(KERN_WARNING
5729 "md: %s has different UUID to %s\n",
5730 bdevname(rdev->bdev,b),
5731 bdevname(rdev0->bdev,b2));
5732 export_rdev(rdev);
5733 return -EINVAL;
5734 }
5735 }
5736 err = bind_rdev_to_array(rdev, mddev);
5737 if (err)
5738 export_rdev(rdev);
5739 return err;
5740 }
5741
5742 /*
5743 * add_new_disk can be used once the array is assembled
5744 * to add "hot spares". They must already have a superblock
5745 * written
5746 */
5747 if (mddev->pers) {
5748 int err;
5749 if (!mddev->pers->hot_add_disk) {
5750 printk(KERN_WARNING
5751 "%s: personality does not support diskops!\n",
5752 mdname(mddev));
5753 return -EINVAL;
5754 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005755 if (mddev->persistent)
5756 rdev = md_import_device(dev, mddev->major_version,
5757 mddev->minor_version);
5758 else
5759 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 if (IS_ERR(rdev)) {
5761 printk(KERN_WARNING
5762 "md: md_import_device returned %ld\n",
5763 PTR_ERR(rdev));
5764 return PTR_ERR(rdev);
5765 }
NeilBrown1a855a02010-12-09 16:36:28 +11005766 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005767 if (!mddev->persistent) {
5768 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005769 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005770 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005771 set_bit(In_sync, &rdev->flags);
5772 } else
NeilBrown41158c72005-06-21 17:17:25 -07005773 rdev->raid_disk = -1;
5774 } else
5775 super_types[mddev->major_version].
5776 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005777 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005778 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005779 /* This was a hot-add request, but events doesn't
5780 * match, so reject it.
5781 */
5782 export_rdev(rdev);
5783 return -EINVAL;
5784 }
5785
NeilBrown1a855a02010-12-09 16:36:28 +11005786 if (test_bit(In_sync, &rdev->flags))
5787 rdev->saved_raid_disk = rdev->raid_disk;
5788 else
5789 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005790
NeilBrownb2d444d2005-11-08 21:39:31 -08005791 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005792 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5793 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005794 else
5795 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005796
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 rdev->raid_disk = -1;
5798 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005799 if (!err && !mddev->pers->hot_remove_disk) {
5800 /* If there is hot_add_disk but no hot_remove_disk
5801 * then added disks for geometry changes,
5802 * and should be added immediately.
5803 */
5804 super_types[mddev->major_version].
5805 validate_super(mddev, rdev);
5806 err = mddev->pers->hot_add_disk(mddev, rdev);
5807 if (err)
5808 unbind_rdev_from_array(rdev);
5809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 if (err)
5811 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005812 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005813 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005814
NeilBrown7ceb17e2013-04-24 11:42:42 +10005815 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005816 if (mddev->degraded)
5817 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005818 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005819 if (!err)
5820 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005821 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 return err;
5823 }
5824
5825 /* otherwise, add_new_disk is only allowed
5826 * for major_version==0 superblocks
5827 */
5828 if (mddev->major_version != 0) {
5829 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5830 mdname(mddev));
5831 return -EINVAL;
5832 }
5833
5834 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5835 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005836 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 if (IS_ERR(rdev)) {
5838 printk(KERN_WARNING
5839 "md: error, md_import_device() returned %ld\n",
5840 PTR_ERR(rdev));
5841 return PTR_ERR(rdev);
5842 }
5843 rdev->desc_nr = info->number;
5844 if (info->raid_disk < mddev->raid_disks)
5845 rdev->raid_disk = info->raid_disk;
5846 else
5847 rdev->raid_disk = -1;
5848
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005850 if (info->state & (1<<MD_DISK_SYNC))
5851 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005853 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5854 set_bit(WriteMostly, &rdev->flags);
5855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 if (!mddev->persistent) {
5857 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005858 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5859 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005860 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005861 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
NeilBrown2bf071b2006-01-06 00:20:55 -08005863 err = bind_rdev_to_array(rdev, mddev);
5864 if (err) {
5865 export_rdev(rdev);
5866 return err;
5867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 }
5869
5870 return 0;
5871}
5872
NeilBrownfd01b882011-10-11 16:47:53 +11005873static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874{
5875 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005876 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 rdev = find_rdev(mddev, dev);
5879 if (!rdev)
5880 return -ENXIO;
5881
NeilBrown3ea8929d2013-04-24 11:42:41 +10005882 clear_bit(Blocked, &rdev->flags);
5883 remove_and_add_spares(mddev, rdev);
5884
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 if (rdev->raid_disk >= 0)
5886 goto busy;
5887
5888 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005889 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005890 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
5892 return 0;
5893busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005894 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 bdevname(rdev->bdev,b), mdname(mddev));
5896 return -EBUSY;
5897}
5898
NeilBrownfd01b882011-10-11 16:47:53 +11005899static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900{
5901 char b[BDEVNAME_SIZE];
5902 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005903 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
5905 if (!mddev->pers)
5906 return -ENODEV;
5907
5908 if (mddev->major_version != 0) {
5909 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5910 " version-0 superblocks.\n",
5911 mdname(mddev));
5912 return -EINVAL;
5913 }
5914 if (!mddev->pers->hot_add_disk) {
5915 printk(KERN_WARNING
5916 "%s: personality does not support diskops!\n",
5917 mdname(mddev));
5918 return -EINVAL;
5919 }
5920
NeilBrownd710e132008-10-13 11:55:12 +11005921 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 if (IS_ERR(rdev)) {
5923 printk(KERN_WARNING
5924 "md: error, md_import_device() returned %ld\n",
5925 PTR_ERR(rdev));
5926 return -EINVAL;
5927 }
5928
5929 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005930 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005932 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
NeilBrown8190e752009-06-18 08:48:58 +10005934 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
NeilBrownb2d444d2005-11-08 21:39:31 -08005936 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 printk(KERN_WARNING
5938 "md: can not hot-add faulty %s disk to %s!\n",
5939 bdevname(rdev->bdev,b), mdname(mddev));
5940 err = -EINVAL;
5941 goto abort_export;
5942 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005943 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005945 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005946 err = bind_rdev_to_array(rdev, mddev);
5947 if (err)
5948 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949
5950 /*
5951 * The rest should better be atomic, we can have disk failures
5952 * noticed in interrupt contexts ...
5953 */
5954
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 rdev->raid_disk = -1;
5956
NeilBrown850b2b42006-10-03 01:15:46 -07005957 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958
5959 /*
5960 * Kick recovery, maybe this spare has to be added to the
5961 * array immediately.
5962 */
5963 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5964 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005965 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 return 0;
5967
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968abort_export:
5969 export_rdev(rdev);
5970 return err;
5971}
5972
NeilBrownfd01b882011-10-11 16:47:53 +11005973static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005974{
5975 int err;
5976
NeilBrown36fa3062005-09-09 16:23:45 -07005977 if (mddev->pers) {
5978 if (!mddev->pers->quiesce)
5979 return -EBUSY;
5980 if (mddev->recovery || mddev->sync_thread)
5981 return -EBUSY;
5982 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005983 }
5984
NeilBrown36fa3062005-09-09 16:23:45 -07005985
5986 if (fd >= 0) {
5987 if (mddev->bitmap)
5988 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005989 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005990
NeilBrownc3d97142009-12-14 12:49:52 +11005991 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005992 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5993 mdname(mddev));
5994 return -EBADF;
5995 }
5996
NeilBrownc3d97142009-12-14 12:49:52 +11005997 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005998 if (err) {
5999 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6000 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11006001 fput(mddev->bitmap_info.file);
6002 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006003 return err;
6004 }
NeilBrownc3d97142009-12-14 12:49:52 +11006005 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006006 } else if (mddev->bitmap == NULL)
6007 return -ENOENT; /* cannot remove what isn't there */
6008 err = 0;
6009 if (mddev->pers) {
6010 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006011 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07006012 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006013 if (!err)
6014 err = bitmap_load(mddev);
6015 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006016 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006017 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006018 fd = -1; /* make sure to put the file */
6019 }
NeilBrown36fa3062005-09-09 16:23:45 -07006020 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006021 }
6022 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11006023 if (mddev->bitmap_info.file) {
6024 restore_bitmap_write_access(mddev->bitmap_info.file);
6025 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07006026 }
NeilBrownc3d97142009-12-14 12:49:52 +11006027 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006028 }
6029
NeilBrown32a76272005-06-21 17:17:14 -07006030 return err;
6031}
6032
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033/*
6034 * set_array_info is used two different ways
6035 * The original usage is when creating a new array.
6036 * In this usage, raid_disks is > 0 and it together with
6037 * level, size, not_persistent,layout,chunksize determine the
6038 * shape of the array.
6039 * This will always create an array with a type-0.90.0 superblock.
6040 * The newer usage is when assembling an array.
6041 * In this case raid_disks will be 0, and the major_version field is
6042 * use to determine which style super-blocks are to be found on the devices.
6043 * The minor and patch _version numbers are also kept incase the
6044 * super_block handler wishes to interpret them.
6045 */
NeilBrownfd01b882011-10-11 16:47:53 +11006046static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047{
6048
6049 if (info->raid_disks == 0) {
6050 /* just setting version number for superblock loading */
6051 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006052 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 super_types[info->major_version].name == NULL) {
6054 /* maybe try to auto-load a module? */
6055 printk(KERN_INFO
6056 "md: superblock version %d not known\n",
6057 info->major_version);
6058 return -EINVAL;
6059 }
6060 mddev->major_version = info->major_version;
6061 mddev->minor_version = info->minor_version;
6062 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006063 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006064 /* ensure mddev_put doesn't delete this now that there
6065 * is some minimal configuration.
6066 */
6067 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 return 0;
6069 }
6070 mddev->major_version = MD_MAJOR_VERSION;
6071 mddev->minor_version = MD_MINOR_VERSION;
6072 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6073 mddev->ctime = get_seconds();
6074
6075 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006076 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006077 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 mddev->raid_disks = info->raid_disks;
6079 /* don't set md_minor, it is determined by which /dev/md* was
6080 * openned
6081 */
6082 if (info->state & (1<<MD_SB_CLEAN))
6083 mddev->recovery_cp = MaxSector;
6084 else
6085 mddev->recovery_cp = 0;
6086 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006087 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
6089 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006090 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
6092 mddev->max_disks = MD_SB_DISKS;
6093
NeilBrowne6910632008-02-06 01:39:51 -08006094 if (mddev->persistent)
6095 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006096 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
NeilBrownc3d97142009-12-14 12:49:52 +11006098 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006099 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006100 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006101
NeilBrownf6705572006-03-27 01:18:11 -08006102 mddev->reshape_position = MaxSector;
6103
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 /*
6105 * Generate a 128 bit UUID
6106 */
6107 get_random_bytes(mddev->uuid, 16);
6108
NeilBrownf6705572006-03-27 01:18:11 -08006109 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006110 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006111 mddev->new_layout = mddev->layout;
6112 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006113 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006114
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 return 0;
6116}
6117
NeilBrownfd01b882011-10-11 16:47:53 +11006118void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006119{
Dan Williamsb522adc2009-03-31 15:00:31 +11006120 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6121
6122 if (mddev->external_size)
6123 return;
6124
Dan Williams1f403622009-03-31 14:59:03 +11006125 mddev->array_sectors = array_sectors;
6126}
6127EXPORT_SYMBOL(md_set_array_sectors);
6128
NeilBrownfd01b882011-10-11 16:47:53 +11006129static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006130{
NeilBrown3cb03002011-10-11 16:45:26 +11006131 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006132 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006133 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006134
6135 if (mddev->pers->resize == NULL)
6136 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006137 /* The "num_sectors" is the number of sectors of each device that
6138 * is used. This can only make sense for arrays with redundancy.
6139 * linear and raid0 always use whatever space is available. We can only
6140 * consider changing this number if no resync or reconstruction is
6141 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006142 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006143 * of each device. If num_sectors is zero, we find the largest size
6144 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006145 */
6146 if (mddev->sync_thread)
6147 return -EBUSY;
NeilBrowna4a61252012-05-22 13:55:27 +10006148
NeilBrowndafb20f2012-03-19 12:46:39 +11006149 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006150 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006151
Andre Nolld71f9f82008-07-11 22:02:22 +10006152 if (fit && (num_sectors == 0 || num_sectors > avail))
6153 num_sectors = avail;
6154 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006155 return -ENOSPC;
6156 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006157 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006158 if (!rv)
6159 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006160 return rv;
6161}
6162
NeilBrownfd01b882011-10-11 16:47:53 +11006163static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006164{
6165 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006166 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006167 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006168 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006169 return -EINVAL;
6170 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006171 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006172 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006173 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006174 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006175
6176 rdev_for_each(rdev, mddev) {
6177 if (mddev->raid_disks < raid_disks &&
6178 rdev->data_offset < rdev->new_data_offset)
6179 return -EINVAL;
6180 if (mddev->raid_disks > raid_disks &&
6181 rdev->data_offset > rdev->new_data_offset)
6182 return -EINVAL;
6183 }
6184
NeilBrown63c70c42006-03-27 01:18:13 -08006185 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006186 if (mddev->delta_disks < 0)
6187 mddev->reshape_backwards = 1;
6188 else if (mddev->delta_disks > 0)
6189 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006190
6191 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006192 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006193 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006194 mddev->reshape_backwards = 0;
6195 }
NeilBrownda943b992006-01-06 00:20:54 -08006196 return rv;
6197}
6198
6199
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200/*
6201 * update_array_info is used to change the configuration of an
6202 * on-line array.
6203 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6204 * fields in the info are checked against the array.
6205 * Any differences that cannot be handled will cause an error.
6206 * Normally, only one change can be managed at a time.
6207 */
NeilBrownfd01b882011-10-11 16:47:53 +11006208static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209{
6210 int rv = 0;
6211 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006212 int state = 0;
6213
6214 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006215 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006216 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
6218 if (mddev->major_version != info->major_version ||
6219 mddev->minor_version != info->minor_version ||
6220/* mddev->patch_version != info->patch_version || */
6221 mddev->ctime != info->ctime ||
6222 mddev->level != info->level ||
6223/* mddev->layout != info->layout || */
6224 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006225 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006226 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6227 ((state^info->state) & 0xfffffe00)
6228 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 return -EINVAL;
6230 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006231 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6232 cnt++;
6233 if (mddev->raid_disks != info->raid_disks)
6234 cnt++;
6235 if (mddev->layout != info->layout)
6236 cnt++;
6237 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6238 cnt++;
6239 if (cnt == 0)
6240 return 0;
6241 if (cnt > 1)
6242 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
6244 if (mddev->layout != info->layout) {
6245 /* Change layout
6246 * we don't need to do anything at the md level, the
6247 * personality will take care of it all.
6248 */
NeilBrown50ac1682009-06-18 08:47:55 +10006249 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006251 else {
6252 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006253 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006254 if (rv)
6255 mddev->new_layout = mddev->layout;
6256 return rv;
6257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006259 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006260 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
NeilBrownda943b992006-01-06 00:20:54 -08006262 if (mddev->raid_disks != info->raid_disks)
6263 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264
NeilBrown36fa3062005-09-09 16:23:45 -07006265 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6266 if (mddev->pers->quiesce == NULL)
6267 return -EINVAL;
6268 if (mddev->recovery || mddev->sync_thread)
6269 return -EBUSY;
6270 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6271 /* add the bitmap */
6272 if (mddev->bitmap)
6273 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006274 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006275 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006276 mddev->bitmap_info.offset =
6277 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006278 mddev->bitmap_info.space =
6279 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006280 mddev->pers->quiesce(mddev, 1);
6281 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006282 if (!rv)
6283 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006284 if (rv)
6285 bitmap_destroy(mddev);
6286 mddev->pers->quiesce(mddev, 0);
6287 } else {
6288 /* remove the bitmap */
6289 if (!mddev->bitmap)
6290 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006291 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006292 return -EINVAL;
6293 mddev->pers->quiesce(mddev, 1);
6294 bitmap_destroy(mddev);
6295 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006296 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006297 }
6298 }
NeilBrown850b2b42006-10-03 01:15:46 -07006299 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 return rv;
6301}
6302
NeilBrownfd01b882011-10-11 16:47:53 +11006303static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304{
NeilBrown3cb03002011-10-11 16:45:26 +11006305 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006306 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307
6308 if (mddev->pers == NULL)
6309 return -ENODEV;
6310
NeilBrown1ca69c42012-10-11 13:37:33 +11006311 rcu_read_lock();
6312 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006314 err = -ENODEV;
6315 else {
6316 md_error(mddev, rdev);
6317 if (!test_bit(Faulty, &rdev->flags))
6318 err = -EBUSY;
6319 }
6320 rcu_read_unlock();
6321 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322}
6323
Andre Noll2f9618c2008-04-25 18:57:58 +02006324/*
6325 * We have a problem here : there is no easy way to give a CHS
6326 * virtual geometry. We currently pretend that we have a 2 heads
6327 * 4 sectors (with a BIG number of cylinders...). This drives
6328 * dosfs just mad... ;-)
6329 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006330static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6331{
NeilBrownfd01b882011-10-11 16:47:53 +11006332 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006333
6334 geo->heads = 2;
6335 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006336 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006337 return 0;
6338}
6339
Al Viroa39907f2008-03-02 10:31:15 -05006340static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 unsigned int cmd, unsigned long arg)
6342{
6343 int err = 0;
6344 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006345 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006346 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347
NeilBrown506c9e42011-12-23 10:17:26 +11006348 switch (cmd) {
6349 case RAID_VERSION:
6350 case GET_ARRAY_INFO:
6351 case GET_DISK_INFO:
6352 break;
6353 default:
6354 if (!capable(CAP_SYS_ADMIN))
6355 return -EACCES;
6356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
6358 /*
6359 * Commands dealing with the RAID driver but not any
6360 * particular array:
6361 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006362 switch (cmd) {
6363 case RAID_VERSION:
6364 err = get_version(argp);
6365 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366
NeilBrownc02c0ae2012-12-11 13:39:21 +11006367 case PRINT_RAID_DEBUG:
6368 err = 0;
6369 md_print_devices();
6370 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
6372#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006373 case RAID_AUTORUN:
6374 err = 0;
6375 autostart_arrays(arg);
6376 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006378 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 }
6380
6381 /*
6382 * Commands creating/starting a new array:
6383 */
6384
Al Viroa39907f2008-03-02 10:31:15 -05006385 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386
6387 if (!mddev) {
6388 BUG();
6389 goto abort;
6390 }
6391
NeilBrown1ca69c42012-10-11 13:37:33 +11006392 /* Some actions do not requires the mutex */
6393 switch (cmd) {
6394 case GET_ARRAY_INFO:
6395 if (!mddev->raid_disks && !mddev->external)
6396 err = -ENODEV;
6397 else
6398 err = get_array_info(mddev, argp);
6399 goto abort;
6400
6401 case GET_DISK_INFO:
6402 if (!mddev->raid_disks && !mddev->external)
6403 err = -ENODEV;
6404 else
6405 err = get_disk_info(mddev, argp);
6406 goto abort;
6407
6408 case SET_DISK_FAULTY:
6409 err = set_disk_faulty(mddev, new_decode_dev(arg));
6410 goto abort;
6411 }
6412
NeilBrowna7a3f082012-12-11 13:35:54 +11006413 if (cmd == ADD_NEW_DISK)
6414 /* need to ensure md_delayed_delete() has completed */
6415 flush_workqueue(md_misc_wq);
6416
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 err = mddev_lock(mddev);
6418 if (err) {
6419 printk(KERN_INFO
6420 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6421 err, cmd);
6422 goto abort;
6423 }
6424
NeilBrownc02c0ae2012-12-11 13:39:21 +11006425 if (cmd == SET_ARRAY_INFO) {
6426 mdu_array_info_t info;
6427 if (!arg)
6428 memset(&info, 0, sizeof(info));
6429 else if (copy_from_user(&info, argp, sizeof(info))) {
6430 err = -EFAULT;
6431 goto abort_unlock;
6432 }
6433 if (mddev->pers) {
6434 err = update_array_info(mddev, &info);
6435 if (err) {
6436 printk(KERN_WARNING "md: couldn't update"
6437 " array info. %d\n", err);
6438 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 }
6440 goto done_unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006441 }
6442 if (!list_empty(&mddev->disks)) {
6443 printk(KERN_WARNING
6444 "md: array %s already has disks!\n",
6445 mdname(mddev));
6446 err = -EBUSY;
6447 goto abort_unlock;
6448 }
6449 if (mddev->raid_disks) {
6450 printk(KERN_WARNING
6451 "md: array %s already initialised!\n",
6452 mdname(mddev));
6453 err = -EBUSY;
6454 goto abort_unlock;
6455 }
6456 err = set_array_info(mddev, &info);
6457 if (err) {
6458 printk(KERN_WARNING "md: couldn't set"
6459 " array info. %d\n", err);
6460 goto abort_unlock;
6461 }
6462 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 }
6464
6465 /*
6466 * Commands querying/configuring an existing array:
6467 */
NeilBrown32a76272005-06-21 17:17:14 -07006468 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006469 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006470 if ((!mddev->raid_disks && !mddev->external)
6471 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6472 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6473 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 err = -ENODEV;
6475 goto abort_unlock;
6476 }
6477
6478 /*
6479 * Commands even a read-only array can execute:
6480 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006481 switch (cmd) {
6482 case GET_BITMAP_FILE:
6483 err = get_bitmap_file(mddev, argp);
6484 goto done_unlock;
6485
6486 case RESTART_ARRAY_RW:
6487 err = restart_array(mddev);
6488 goto done_unlock;
6489
6490 case STOP_ARRAY:
6491 err = do_md_stop(mddev, 0, bdev);
6492 goto done_unlock;
6493
6494 case STOP_ARRAY_RO:
6495 err = md_set_readonly(mddev, bdev);
6496 goto done_unlock;
6497
NeilBrown3ea8929d2013-04-24 11:42:41 +10006498 case HOT_REMOVE_DISK:
6499 err = hot_remove_disk(mddev, new_decode_dev(arg));
6500 goto done_unlock;
6501
NeilBrown7ceb17e2013-04-24 11:42:42 +10006502 case ADD_NEW_DISK:
6503 /* We can support ADD_NEW_DISK on read-only arrays
6504 * on if we are re-adding a preexisting device.
6505 * So require mddev->pers and MD_DISK_SYNC.
6506 */
6507 if (mddev->pers) {
6508 mdu_disk_info_t info;
6509 if (copy_from_user(&info, argp, sizeof(info)))
6510 err = -EFAULT;
6511 else if (!(info.state & (1<<MD_DISK_SYNC)))
6512 /* Need to clear read-only for this */
6513 break;
6514 else
6515 err = add_new_disk(mddev, &info);
6516 goto done_unlock;
6517 }
6518 break;
6519
NeilBrownc02c0ae2012-12-11 13:39:21 +11006520 case BLKROSET:
6521 if (get_user(ro, (int __user *)(arg))) {
6522 err = -EFAULT;
6523 goto done_unlock;
6524 }
6525 err = -EINVAL;
6526
6527 /* if the bdev is going readonly the value of mddev->ro
6528 * does not matter, no writes are coming
6529 */
6530 if (ro)
NeilBrown32a76272005-06-21 17:17:14 -07006531 goto done_unlock;
6532
NeilBrownc02c0ae2012-12-11 13:39:21 +11006533 /* are we are already prepared for writes? */
6534 if (mddev->ro != 1)
6535 goto done_unlock;
6536
6537 /* transitioning to readauto need only happen for
6538 * arrays that call md_write_start
6539 */
6540 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006542 if (err == 0) {
6543 mddev->ro = 2;
6544 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006545 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006546 }
6547 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548 }
6549
6550 /*
6551 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006552 * superblock, so we do not allow them on read-only arrays.
6553 * However non-MD ioctls (e.g. get-size) will still come through
6554 * here and hit the 'default' below, so only disallow
6555 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006557 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006558 if (mddev->ro == 2) {
6559 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006560 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006561 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006562 /* mddev_unlock will wake thread */
6563 /* If a device failed while we were read-only, we
6564 * need to make sure the metadata is updated now.
6565 */
6566 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6567 mddev_unlock(mddev);
6568 wait_event(mddev->sb_wait,
6569 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6570 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6571 mddev_lock(mddev);
6572 }
NeilBrownf91de922005-11-08 21:39:36 -08006573 } else {
6574 err = -EROFS;
6575 goto abort_unlock;
6576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 }
6578
NeilBrownc02c0ae2012-12-11 13:39:21 +11006579 switch (cmd) {
6580 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006582 mdu_disk_info_t info;
6583 if (copy_from_user(&info, argp, sizeof(info)))
6584 err = -EFAULT;
6585 else
6586 err = add_new_disk(mddev, &info);
6587 goto done_unlock;
6588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589
NeilBrownc02c0ae2012-12-11 13:39:21 +11006590 case HOT_ADD_DISK:
6591 err = hot_add_disk(mddev, new_decode_dev(arg));
6592 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593
NeilBrownc02c0ae2012-12-11 13:39:21 +11006594 case RUN_ARRAY:
6595 err = do_md_run(mddev);
6596 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597
NeilBrownc02c0ae2012-12-11 13:39:21 +11006598 case SET_BITMAP_FILE:
6599 err = set_bitmap_file(mddev, (int)arg);
6600 goto done_unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006601
NeilBrownc02c0ae2012-12-11 13:39:21 +11006602 default:
6603 err = -EINVAL;
6604 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 }
6606
6607done_unlock:
6608abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006609 if (mddev->hold_active == UNTIL_IOCTL &&
6610 err != -EINVAL)
6611 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 mddev_unlock(mddev);
6613
6614 return err;
6615done:
6616 if (err)
6617 MD_BUG();
6618abort:
6619 return err;
6620}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006621#ifdef CONFIG_COMPAT
6622static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6623 unsigned int cmd, unsigned long arg)
6624{
6625 switch (cmd) {
6626 case HOT_REMOVE_DISK:
6627 case HOT_ADD_DISK:
6628 case SET_DISK_FAULTY:
6629 case SET_BITMAP_FILE:
6630 /* These take in integer arg, do not convert */
6631 break;
6632 default:
6633 arg = (unsigned long)compat_ptr(arg);
6634 break;
6635 }
6636
6637 return md_ioctl(bdev, mode, cmd, arg);
6638}
6639#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640
Al Viroa39907f2008-03-02 10:31:15 -05006641static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642{
6643 /*
6644 * Succeed if we can lock the mddev, which confirms that
6645 * it isn't being stopped right now.
6646 */
NeilBrownfd01b882011-10-11 16:47:53 +11006647 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 int err;
6649
Yuanhan Liu0c098222012-05-22 13:55:32 +10006650 if (!mddev)
6651 return -ENODEV;
6652
NeilBrownd3374822009-01-09 08:31:10 +11006653 if (mddev->gendisk != bdev->bd_disk) {
6654 /* we are racing with mddev_put which is discarding this
6655 * bd_disk.
6656 */
6657 mddev_put(mddev);
6658 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006659 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006660 /* Then retry the open from the top */
6661 return -ERESTARTSYS;
6662 }
6663 BUG_ON(mddev != bdev->bd_disk->private_data);
6664
NeilBrownc8c00a62009-08-10 12:50:52 +10006665 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 goto out;
6667
6668 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006669 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006670 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006672 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673 out:
6674 return err;
6675}
6676
Al Viroa39907f2008-03-02 10:31:15 -05006677static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678{
NeilBrownfd01b882011-10-11 16:47:53 +11006679 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006681 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006682 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 mddev_put(mddev);
6684
6685 return 0;
6686}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006687
6688static int md_media_changed(struct gendisk *disk)
6689{
NeilBrownfd01b882011-10-11 16:47:53 +11006690 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006691
6692 return mddev->changed;
6693}
6694
6695static int md_revalidate(struct gendisk *disk)
6696{
NeilBrownfd01b882011-10-11 16:47:53 +11006697 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006698
6699 mddev->changed = 0;
6700 return 0;
6701}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006702static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703{
6704 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006705 .open = md_open,
6706 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006707 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006708#ifdef CONFIG_COMPAT
6709 .compat_ioctl = md_compat_ioctl,
6710#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006711 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006712 .media_changed = md_media_changed,
6713 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714};
6715
Adrian Bunk75c96f82005-05-05 16:16:09 -07006716static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717{
NeilBrown2b8bf342011-10-11 16:48:23 +11006718 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 /*
6721 * md_thread is a 'system-thread', it's priority should be very
6722 * high. We avoid resource deadlocks individually in each
6723 * raid personality. (RAID5 does preallocation) We also use RR and
6724 * the very same RT priority as kswapd, thus we will never get
6725 * into a priority inversion deadlock.
6726 *
6727 * we definitely have to have equal or higher priority than
6728 * bdflush, otherwise bdflush will deadlock if there are too
6729 * many dirty RAID5 blocks.
6730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731
NeilBrown6985c432005-10-19 21:23:47 -07006732 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006733 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734
NeilBrown93588e22005-11-15 00:09:12 -08006735 /* We need to wait INTERRUPTIBLE so that
6736 * we don't add to the load-average.
6737 * That means we need to be sure no signals are
6738 * pending
6739 */
6740 if (signal_pending(current))
6741 flush_signals(current);
6742
6743 wait_event_interruptible_timeout
6744 (thread->wqueue,
6745 test_bit(THREAD_WAKEUP, &thread->flags)
6746 || kthread_should_stop(),
6747 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748
NeilBrown6c987912011-01-14 09:13:53 +11006749 clear_bit(THREAD_WAKEUP, &thread->flags);
6750 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006751 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006753
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 return 0;
6755}
6756
NeilBrown2b8bf342011-10-11 16:48:23 +11006757void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758{
6759 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006760 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 set_bit(THREAD_WAKEUP, &thread->flags);
6762 wake_up(&thread->wqueue);
6763 }
6764}
6765
Shaohua Li4ed87312012-10-11 13:34:00 +11006766struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6767 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768{
NeilBrown2b8bf342011-10-11 16:48:23 +11006769 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770
NeilBrown2b8bf342011-10-11 16:48:23 +11006771 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 if (!thread)
6773 return NULL;
6774
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 init_waitqueue_head(&thread->wqueue);
6776
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 thread->run = run;
6778 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006779 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006780 thread->tsk = kthread_run(md_thread, thread,
6781 "%s_%s",
6782 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006783 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006784 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 kfree(thread);
6786 return NULL;
6787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 return thread;
6789}
6790
NeilBrown2b8bf342011-10-11 16:48:23 +11006791void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792{
NeilBrown2b8bf342011-10-11 16:48:23 +11006793 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006794 if (!thread)
6795 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006796 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006797 /* Locking ensures that mddev_unlock does not wake_up a
6798 * non-existent thread
6799 */
6800 spin_lock(&pers_lock);
6801 *threadp = NULL;
6802 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006803
6804 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 kfree(thread);
6806}
6807
NeilBrownfd01b882011-10-11 16:47:53 +11006808void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809{
6810 if (!mddev) {
6811 MD_BUG();
6812 return;
6813 }
6814
NeilBrownb2d444d2005-11-08 21:39:31 -08006815 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006817
NeilBrownde393cd2011-07-28 11:31:48 +10006818 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 return;
6820 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006821 if (mddev->degraded)
6822 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006823 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6825 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6826 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006827 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006828 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006829 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830}
6831
6832/* seq_file implementation /proc/mdstat */
6833
6834static void status_unused(struct seq_file *seq)
6835{
6836 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006837 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
6839 seq_printf(seq, "unused devices: ");
6840
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006841 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 char b[BDEVNAME_SIZE];
6843 i++;
6844 seq_printf(seq, "%s ",
6845 bdevname(rdev->bdev,b));
6846 }
6847 if (!i)
6848 seq_printf(seq, "<none>");
6849
6850 seq_printf(seq, "\n");
6851}
6852
6853
NeilBrownfd01b882011-10-11 16:47:53 +11006854static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855{
NeilBrowndd71cf62009-05-07 12:49:35 +10006856 sector_t max_sectors, resync, res;
6857 unsigned long dt, db;
6858 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006859 int scale;
6860 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861
NeilBrown72f36d52012-10-11 14:25:57 +11006862 if (mddev->curr_resync <= 3)
6863 resync = 0;
6864 else
6865 resync = mddev->curr_resync
6866 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867
NeilBrownc804cde2012-05-21 09:28:33 +10006868 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6869 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006870 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006872 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873
6874 /*
6875 * Should not happen.
6876 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006877 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 MD_BUG();
6879 return;
6880 }
NeilBrown4588b422006-03-27 01:18:04 -08006881 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006882 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006883 * u32, as those are the requirements for sector_div.
6884 * Thus 'scale' must be at least 10
6885 */
6886 scale = 10;
6887 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006888 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006889 scale++;
6890 }
6891 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006892 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006893
6894 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 {
NeilBrown4588b422006-03-27 01:18:04 -08006896 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 seq_printf(seq, "[");
6898 for (i = 0; i < x; i++)
6899 seq_printf(seq, "=");
6900 seq_printf(seq, ">");
6901 for (i = 0; i < y; i++)
6902 seq_printf(seq, ".");
6903 seq_printf(seq, "] ");
6904 }
NeilBrown4588b422006-03-27 01:18:04 -08006905 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006906 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6907 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006908 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6909 "check" :
6910 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6911 "resync" : "recovery"))),
6912 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006913 (unsigned long long) resync/2,
6914 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915
6916 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 * dt: time from mark until now
6918 * db: blocks written from mark until now
6919 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006920 *
6921 * rt is a sector_t, so could be 32bit or 64bit.
6922 * So we divide before multiply in case it is 32bit and close
6923 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006924 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006925 * near the end of resync when the number of remaining sectors
6926 * is close to 'db'.
6927 * We then divide rt by 32 after multiplying by db to compensate.
6928 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 */
6930 dt = ((jiffies - mddev->resync_mark) / HZ);
6931 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006932 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6933 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934
NeilBrowndd71cf62009-05-07 12:49:35 +10006935 rt = max_sectors - resync; /* number of remaining sectors */
6936 sector_div(rt, db/32+1);
6937 rt *= dt;
6938 rt >>= 5;
6939
6940 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6941 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942
NeilBrownff4e8d92006-07-10 04:44:16 -07006943 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944}
6945
6946static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6947{
6948 struct list_head *tmp;
6949 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006950 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951
6952 if (l >= 0x10000)
6953 return NULL;
6954 if (!l--)
6955 /* header */
6956 return (void*)1;
6957
6958 spin_lock(&all_mddevs_lock);
6959 list_for_each(tmp,&all_mddevs)
6960 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006961 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962 mddev_get(mddev);
6963 spin_unlock(&all_mddevs_lock);
6964 return mddev;
6965 }
6966 spin_unlock(&all_mddevs_lock);
6967 if (!l--)
6968 return (void*)2;/* tail */
6969 return NULL;
6970}
6971
6972static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6973{
6974 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006975 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976
6977 ++*pos;
6978 if (v == (void*)2)
6979 return NULL;
6980
6981 spin_lock(&all_mddevs_lock);
6982 if (v == (void*)1)
6983 tmp = all_mddevs.next;
6984 else
6985 tmp = mddev->all_mddevs.next;
6986 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006987 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988 else {
6989 next_mddev = (void*)2;
6990 *pos = 0x10000;
6991 }
6992 spin_unlock(&all_mddevs_lock);
6993
6994 if (v != (void*)1)
6995 mddev_put(mddev);
6996 return next_mddev;
6997
6998}
6999
7000static void md_seq_stop(struct seq_file *seq, void *v)
7001{
NeilBrownfd01b882011-10-11 16:47:53 +11007002 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003
7004 if (mddev && v != (void*)1 && v != (void*)2)
7005 mddev_put(mddev);
7006}
7007
7008static int md_seq_show(struct seq_file *seq, void *v)
7009{
NeilBrownfd01b882011-10-11 16:47:53 +11007010 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007011 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007012 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013
7014 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007015 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 seq_printf(seq, "Personalities : ");
7017 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007018 list_for_each_entry(pers, &pers_list, list)
7019 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020
7021 spin_unlock(&pers_lock);
7022 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007023 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 return 0;
7025 }
7026 if (v == (void*)2) {
7027 status_unused(seq);
7028 return 0;
7029 }
7030
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007031 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007033
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7035 seq_printf(seq, "%s : %sactive", mdname(mddev),
7036 mddev->pers ? "" : "in");
7037 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007038 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007040 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007041 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 seq_printf(seq, " %s", mddev->pers->name);
7043 }
7044
Andre Nolldd8ac332009-03-31 14:33:13 +11007045 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007046 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047 char b[BDEVNAME_SIZE];
7048 seq_printf(seq, " %s[%d]",
7049 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007050 if (test_bit(WriteMostly, &rdev->flags))
7051 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007052 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053 seq_printf(seq, "(F)");
7054 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007055 }
7056 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007057 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007058 if (test_bit(Replacement, &rdev->flags))
7059 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007060 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 }
7062
7063 if (!list_empty(&mddev->disks)) {
7064 if (mddev->pers)
7065 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007066 (unsigned long long)
7067 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068 else
7069 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007070 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007072 if (mddev->persistent) {
7073 if (mddev->major_version != 0 ||
7074 mddev->minor_version != 90) {
7075 seq_printf(seq," super %d.%d",
7076 mddev->major_version,
7077 mddev->minor_version);
7078 }
NeilBrowne6910632008-02-06 01:39:51 -08007079 } else if (mddev->external)
7080 seq_printf(seq, " super external:%s",
7081 mddev->metadata_type);
7082 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007083 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084
7085 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007086 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007088 if (mddev->pers->sync_request) {
7089 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007090 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007091 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007092 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007093 seq_printf(seq, "\tresync=DELAYED\n ");
7094 else if (mddev->recovery_cp < MaxSector)
7095 seq_printf(seq, "\tresync=PENDING\n ");
7096 }
NeilBrown32a76272005-06-21 17:17:14 -07007097 } else
7098 seq_printf(seq, "\n ");
7099
NeilBrown57148962012-03-19 12:46:40 +11007100 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101
7102 seq_printf(seq, "\n");
7103 }
7104 mddev_unlock(mddev);
7105
7106 return 0;
7107}
7108
Jan Engelhardt110518b2009-05-07 12:49:37 +10007109static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110 .start = md_seq_start,
7111 .next = md_seq_next,
7112 .stop = md_seq_stop,
7113 .show = md_seq_show,
7114};
7115
7116static int md_seq_open(struct inode *inode, struct file *file)
7117{
Kay Sieversf1514632011-07-12 20:48:39 +02007118 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 int error;
7120
7121 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007122 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007123 return error;
7124
7125 seq = file->private_data;
7126 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 return error;
7128}
7129
NeilBrownd7603b72006-01-06 00:20:30 -08007130static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7131{
Kay Sieversf1514632011-07-12 20:48:39 +02007132 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007133 int mask;
7134
7135 poll_wait(filp, &md_event_waiters, wait);
7136
7137 /* always allow read */
7138 mask = POLLIN | POLLRDNORM;
7139
Kay Sieversf1514632011-07-12 20:48:39 +02007140 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007141 mask |= POLLERR | POLLPRI;
7142 return mask;
7143}
7144
Arjan van de Venfa027c22007-02-12 00:55:33 -08007145static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007146 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 .open = md_seq_open,
7148 .read = seq_read,
7149 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007150 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007151 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152};
7153
NeilBrown84fc4b52011-10-11 16:49:58 +11007154int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007157 list_add_tail(&p->list, &pers_list);
7158 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 spin_unlock(&pers_lock);
7160 return 0;
7161}
7162
NeilBrown84fc4b52011-10-11 16:49:58 +11007163int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164{
NeilBrown2604b702006-01-06 00:20:36 -08007165 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007167 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 spin_unlock(&pers_lock);
7169 return 0;
7170}
7171
NeilBrownfd01b882011-10-11 16:47:53 +11007172static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173{
NeilBrown3cb03002011-10-11 16:45:26 +11007174 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007176 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177
7178 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007179 rcu_read_lock();
7180 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007182 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7183 (int)part_stat_read(&disk->part0, sectors[1]) -
7184 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007185 /* sync IO will cause sync_io to increase before the disk_stats
7186 * as sync_io is counted when a request starts, and
7187 * disk_stats is counted when it completes.
7188 * So resync activity will cause curr_events to be smaller than
7189 * when there was no such activity.
7190 * non-sync IO will cause disk_stat to increase without
7191 * increasing sync_io so curr_events will (eventually)
7192 * be larger than it was before. Once it becomes
7193 * substantially larger, the test below will cause
7194 * the array to appear non-idle, and resync will slow
7195 * down.
7196 * If there is a lot of outstanding resync activity when
7197 * we set last_event to curr_events, then all that activity
7198 * completing might cause the array to appear non-idle
7199 * and resync will be slowed down even though there might
7200 * not have been non-resync activity. This will only
7201 * happen once though. 'last_events' will soon reflect
7202 * the state where there is little or no outstanding
7203 * resync requests, and further resync activity will
7204 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007205 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007207 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 rdev->last_events = curr_events;
7209 idle = 0;
7210 }
7211 }
NeilBrown4b809912008-07-21 17:05:25 +10007212 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 return idle;
7214}
7215
NeilBrownfd01b882011-10-11 16:47:53 +11007216void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217{
7218 /* another "blocks" (512byte) blocks have been synced */
7219 atomic_sub(blocks, &mddev->recovery_active);
7220 wake_up(&mddev->recovery_wait);
7221 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007222 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007223 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 md_wakeup_thread(mddev->thread);
7225 // stop recovery, signal do_sync ....
7226 }
7227}
7228
7229
NeilBrown06d91a52005-06-21 17:17:12 -07007230/* md_write_start(mddev, bi)
7231 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007232 * in superblock) before writing, schedule a superblock update
7233 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007234 */
NeilBrownfd01b882011-10-11 16:47:53 +11007235void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236{
Neil Brown0fd62b82008-06-28 08:31:36 +10007237 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007238 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007239 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007240
NeilBrownf91de922005-11-08 21:39:36 -08007241 BUG_ON(mddev->ro == 1);
7242 if (mddev->ro == 2) {
7243 /* need to switch to read/write */
7244 mddev->ro = 0;
7245 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7246 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007247 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007248 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007249 }
NeilBrown06d91a52005-06-21 17:17:12 -07007250 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007251 if (mddev->safemode == 1)
7252 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007253 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007254 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007255 if (mddev->in_sync) {
7256 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007257 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007258 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007259 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007260 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007261 }
NeilBrowna9701a32005-11-08 21:39:34 -08007262 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007263 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007264 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007265 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007266 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007267 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268}
7269
NeilBrownfd01b882011-10-11 16:47:53 +11007270void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271{
7272 if (atomic_dec_and_test(&mddev->writes_pending)) {
7273 if (mddev->safemode == 2)
7274 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007275 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7277 }
7278}
7279
NeilBrown2a2275d2007-01-26 00:57:11 -08007280/* md_allow_write(mddev)
7281 * Calling this ensures that the array is marked 'active' so that writes
7282 * may proceed without blocking. It is important to call this before
7283 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7284 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007285 *
7286 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7287 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007288 */
NeilBrownfd01b882011-10-11 16:47:53 +11007289int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007290{
7291 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007292 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007293 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007294 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007295 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007296 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007297
7298 spin_lock_irq(&mddev->write_lock);
7299 if (mddev->in_sync) {
7300 mddev->in_sync = 0;
7301 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007302 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007303 if (mddev->safemode_delay &&
7304 mddev->safemode == 0)
7305 mddev->safemode = 1;
7306 spin_unlock_irq(&mddev->write_lock);
7307 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007308 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007309 } else
7310 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007311
NeilBrown070dc6d2010-08-30 17:33:34 +10007312 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007313 return -EAGAIN;
7314 else
7315 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007316}
7317EXPORT_SYMBOL_GPL(md_allow_write);
7318
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319#define SYNC_MARKS 10
7320#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007321#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007322void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323{
Shaohua Li4ed87312012-10-11 13:34:00 +11007324 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007325 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326 unsigned int currspeed = 0,
7327 window;
NeilBrown57afd892005-06-21 17:17:13 -07007328 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007330 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 sector_t mark_cnt[SYNC_MARKS];
7332 int last_mark,m;
7333 struct list_head *tmp;
7334 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007335 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007336 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007337 char *desc;
majianpeng7c2c57c2012-07-03 12:12:26 +10007338 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339
7340 /* just incase thread restarts... */
7341 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7342 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007343 if (mddev->ro) /* never try to sync a read-only array */
7344 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345
NeilBrown61df9d92006-10-03 01:15:57 -07007346 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7347 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7348 desc = "data-check";
7349 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7350 desc = "requested-resync";
7351 else
7352 desc = "resync";
7353 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7354 desc = "reshape";
7355 else
7356 desc = "recovery";
7357
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 /* we overload curr_resync somewhat here.
7359 * 0 == not engaged in resync at all
7360 * 2 == checking that there is no conflict with another sync
7361 * 1 == like 2, but have yielded to allow conflicting resync to
7362 * commense
7363 * other == active in resync - this many blocks
7364 *
7365 * Before starting a resync we must have set curr_resync to
7366 * 2, and then checked that every "conflicting" array has curr_resync
7367 * less than ours. When we find one that is the same or higher
7368 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7369 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7370 * This will mean we have to start checking from the beginning again.
7371 *
7372 */
7373
7374 do {
7375 mddev->curr_resync = 2;
7376
7377 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007378 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007379 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007380
7381 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007383 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384 if (mddev2 == mddev)
7385 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007386 if (!mddev->parallel_resync
7387 && mddev2->curr_resync
7388 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 DEFINE_WAIT(wq);
7390 if (mddev < mddev2 && mddev->curr_resync == 2) {
7391 /* arbitrarily yield */
7392 mddev->curr_resync = 1;
7393 wake_up(&resync_wait);
7394 }
7395 if (mddev > mddev2 && mddev->curr_resync == 1)
7396 /* no need to wait here, we can wait the next
7397 * time 'round when curr_resync == 2
7398 */
7399 continue;
NeilBrown97441972008-09-19 11:49:54 +10007400 /* We need to wait 'interruptible' so as not to
7401 * contribute to the load average, and not to
7402 * be caught by 'softlockup'
7403 */
7404 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007405 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007406 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007407 printk(KERN_INFO "md: delaying %s of %s"
7408 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007410 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007412 if (signal_pending(current))
7413 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 schedule();
7415 finish_wait(&resync_wait, &wq);
7416 goto try_again;
7417 }
7418 finish_wait(&resync_wait, &wq);
7419 }
7420 }
7421 } while (mddev->curr_resync < 2);
7422
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007423 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007424 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007426 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 */
7428 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007429 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007430 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007431 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7432 j = mddev->resync_min;
7433 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007434 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007435
NeilBrownccfcc3c2006-03-27 01:18:09 -08007436 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007437 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007438 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007440 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007441 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007442 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007443 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007444 if (rdev->raid_disk >= 0 &&
7445 !test_bit(Faulty, &rdev->flags) &&
7446 !test_bit(In_sync, &rdev->flags) &&
7447 rdev->recovery_offset < j)
7448 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007449 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451
NeilBrown61df9d92006-10-03 01:15:57 -07007452 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7453 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7454 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007455 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007456 "(but not more than %d KB/sec) for %s.\n",
7457 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458
NeilBrowneea1bf32009-03-31 14:27:02 +11007459 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007460
NeilBrown57afd892005-06-21 17:17:13 -07007461 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 for (m = 0; m < SYNC_MARKS; m++) {
7463 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007464 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 }
7466 last_mark = 0;
7467 mddev->resync_mark = mark[last_mark];
7468 mddev->resync_mark_cnt = mark_cnt[last_mark];
7469
7470 /*
7471 * Tune reconstruction:
7472 */
7473 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007474 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7475 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476
7477 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 last_check = 0;
7479
7480 if (j>2) {
7481 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007482 "md: resuming %s of %s from checkpoint.\n",
7483 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007485 } else
7486 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007487 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007488 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7489 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007490 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491
majianpeng7c2c57c2012-07-03 12:12:26 +10007492 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007494 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495
NeilBrown57afd892005-06-21 17:17:13 -07007496 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007497
NeilBrown7a91ee12009-05-26 12:57:21 +10007498 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7499 ((mddev->curr_resync > mddev->curr_resync_completed &&
7500 (mddev->curr_resync - mddev->curr_resync_completed)
7501 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007502 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007503 (j - mddev->curr_resync_completed)*2
7504 >= mddev->resync_max - mddev->curr_resync_completed
7505 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007506 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007507 wait_event(mddev->recovery_wait,
7508 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007509 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007510 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7511 j > mddev->recovery_cp)
7512 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007513 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007514 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007515 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007516 }
NeilBrownacb180b2009-04-14 16:28:34 +10007517
NeilBrowne62e58a2009-07-01 13:15:35 +10007518 while (j >= mddev->resync_max && !kthread_should_stop()) {
7519 /* As this condition is controlled by user-space,
7520 * we can block indefinitely, so use '_interruptible'
7521 * to avoid triggering warnings.
7522 */
7523 flush_signals(current); /* just in case */
7524 wait_event_interruptible(mddev->recovery_wait,
7525 mddev->resync_max > j
7526 || kthread_should_stop());
7527 }
NeilBrownacb180b2009-04-14 16:28:34 +10007528
7529 if (kthread_should_stop())
7530 goto interrupted;
7531
NeilBrown57afd892005-06-21 17:17:13 -07007532 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007533 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007534 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007535 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 goto out;
7537 }
NeilBrown57afd892005-06-21 17:17:13 -07007538
7539 if (!skipped) { /* actual IO requested */
7540 io_sectors += sectors;
7541 atomic_add(sectors, &mddev->recovery_active);
7542 }
7543
NeilBrowne875ece2011-07-28 11:39:24 +10007544 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7545 break;
7546
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007548 if (j > 2)
7549 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007550 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007551 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007552 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007553 * visible in /proc/mdstat
7554 */
7555 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007556
7557 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558 continue;
7559
NeilBrown57afd892005-06-21 17:17:13 -07007560 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 repeat:
7562 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7563 /* step marks */
7564 int next = (last_mark+1) % SYNC_MARKS;
7565
7566 mddev->resync_mark = mark[next];
7567 mddev->resync_mark_cnt = mark_cnt[next];
7568 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007569 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 last_mark = next;
7571 }
7572
7573
NeilBrownc6207272008-02-06 01:39:52 -08007574 if (kthread_should_stop())
7575 goto interrupted;
7576
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577
7578 /*
7579 * this loop exits only if either when we are slower than
7580 * the 'hard' speed limit, or the system was IO-idle for
7581 * a jiffy.
7582 * the system might be non-idle CPU-wise, but we only care
7583 * about not overloading the IO subsystem. (things like an
7584 * e2fsck being done on the RAID array should execute fast)
7585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 cond_resched();
7587
NeilBrown57afd892005-06-21 17:17:13 -07007588 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7589 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590
NeilBrown88202a02006-01-06 00:21:36 -08007591 if (currspeed > speed_min(mddev)) {
7592 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007593 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007594 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 goto repeat;
7596 }
7597 }
7598 }
NeilBrown61df9d92006-10-03 01:15:57 -07007599 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600 /*
7601 * this also signals 'finished resyncing' to md_stop
7602 */
7603 out:
majianpeng7c2c57c2012-07-03 12:12:26 +10007604 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7606
7607 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007608 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609
NeilBrowndfc70642008-05-23 13:04:39 -07007610 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007611 mddev->curr_resync > 2) {
7612 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7613 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7614 if (mddev->curr_resync >= mddev->recovery_cp) {
7615 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007616 "md: checkpointing %s of %s.\n",
7617 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007618 if (test_bit(MD_RECOVERY_ERROR,
7619 &mddev->recovery))
7620 mddev->recovery_cp =
7621 mddev->curr_resync_completed;
7622 else
7623 mddev->recovery_cp =
7624 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007625 }
7626 } else
7627 mddev->recovery_cp = MaxSector;
7628 } else {
7629 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7630 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007631 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007632 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007633 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007634 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007635 !test_bit(Faulty, &rdev->flags) &&
7636 !test_bit(In_sync, &rdev->flags) &&
7637 rdev->recovery_offset < mddev->curr_resync)
7638 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007639 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007642 skip:
NeilBrown17571282006-12-10 02:20:52 -08007643 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644
NeilBrownc07b70a2009-12-14 12:49:48 +11007645 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7646 /* We completed so min/max setting can be forgotten if used. */
7647 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7648 mddev->resync_min = 0;
7649 mddev->resync_max = MaxSector;
7650 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7651 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652 mddev->curr_resync = 0;
7653 wake_up(&resync_wait);
7654 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7655 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007656 return;
7657
7658 interrupted:
7659 /*
7660 * got a signal, exit.
7661 */
7662 printk(KERN_INFO
7663 "md: md_do_sync() got signal ... exiting\n");
7664 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7665 goto out;
7666
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667}
NeilBrown29269552006-03-27 01:18:10 -08007668EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669
NeilBrown746d3202013-04-24 11:42:41 +10007670static int remove_and_add_spares(struct mddev *mddev,
7671 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007672{
NeilBrown3cb03002011-10-11 16:45:26 +11007673 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007674 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007675 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007676
NeilBrowndafb20f2012-03-19 12:46:39 +11007677 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007678 if ((this == NULL || rdev == this) &&
7679 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007680 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007681 (test_bit(Faulty, &rdev->flags) ||
7682 ! test_bit(In_sync, &rdev->flags)) &&
7683 atomic_read(&rdev->nr_pending)==0) {
7684 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007685 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007686 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007687 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007688 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007689 }
7690 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007691 if (removed && mddev->kobj.sd)
7692 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007693
NeilBrown746d3202013-04-24 11:42:41 +10007694 if (this)
7695 goto no_add;
7696
NeilBrowndafb20f2012-03-19 12:46:39 +11007697 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007698 if (rdev->raid_disk >= 0 &&
7699 !test_bit(In_sync, &rdev->flags) &&
7700 !test_bit(Faulty, &rdev->flags))
7701 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007702 if (rdev->raid_disk >= 0)
7703 continue;
7704 if (test_bit(Faulty, &rdev->flags))
7705 continue;
7706 if (mddev->ro &&
7707 rdev->saved_raid_disk < 0)
7708 continue;
7709
7710 rdev->recovery_offset = 0;
7711 if (rdev->saved_raid_disk >= 0 && mddev->in_sync) {
7712 spin_lock_irq(&mddev->write_lock);
7713 if (mddev->in_sync)
7714 /* OK, this device, which is in_sync,
7715 * will definitely be noticed before
7716 * the next write, so recovery isn't
7717 * needed.
7718 */
7719 rdev->recovery_offset = mddev->recovery_cp;
7720 spin_unlock_irq(&mddev->write_lock);
7721 }
7722 if (mddev->ro && rdev->recovery_offset != MaxSector)
7723 /* not safe to add this disk now */
7724 continue;
7725 if (mddev->pers->
7726 hot_add_disk(mddev, rdev) == 0) {
7727 if (sysfs_link_rdev(mddev, rdev))
7728 /* failure here is OK */;
7729 spares++;
7730 md_new_event(mddev);
7731 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007732 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007733 }
NeilBrown746d3202013-04-24 11:42:41 +10007734no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007735 if (removed)
7736 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007737 return spares;
7738}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007739
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740/*
7741 * This routine is regularly called by all per-raid-array threads to
7742 * deal with generic issues like resync and super-block update.
7743 * Raid personalities that don't have a thread (linear/raid0) do not
7744 * need this as they never do any recovery or update the superblock.
7745 *
7746 * It does not do any resync itself, but rather "forks" off other threads
7747 * to do that as needed.
7748 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7749 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007750 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751 * and wakeups up this thread which will reap the thread and finish up.
7752 * This thread also removes any faulty devices (with nr_pending == 0).
7753 *
7754 * The overall approach is:
7755 * 1/ if the superblock needs updating, update it.
7756 * 2/ If a recovery thread is running, don't do anything else.
7757 * 3/ If recovery has finished, clean up, possibly marking spares active.
7758 * 4/ If there are any faulty devices, remove them.
7759 * 5/ If array is degraded, try to add spares devices
7760 * 6/ If array has spares or is not in-sync, start a resync thread.
7761 */
NeilBrownfd01b882011-10-11 16:47:53 +11007762void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007763{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007764 if (mddev->suspended)
7765 return;
7766
NeilBrown5f404022005-06-21 17:17:16 -07007767 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007768 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769
NeilBrownfca4d842005-06-21 17:17:11 -07007770 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007771 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007772 printk(KERN_INFO "md: %s in immediate safe mode\n",
7773 mdname(mddev));
7774 mddev->safemode = 2;
7775 }
7776 flush_signals(current);
7777 }
7778
NeilBrownc89a8ee2008-08-05 15:54:13 +10007779 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7780 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007782 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007784 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007785 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007786 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7787 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788 ))
7789 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007790
NeilBrowndf5b89b2006-03-27 01:18:20 -08007791 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007792 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007793
NeilBrownc89a8ee2008-08-05 15:54:13 +10007794 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007795 /* On a read-only array we can:
7796 * - remove failed devices
7797 * - add already-in_sync devices if the array itself
7798 * is in-sync.
7799 * As we only add devices that are already in-sync,
7800 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007801 */
NeilBrownc89a8ee2008-08-05 15:54:13 +10007802 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown7ceb17e2013-04-24 11:42:42 +10007803 remove_and_add_spares(mddev, NULL);
7804 mddev->pers->spare_active(mddev);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007805 goto unlock;
7806 }
7807
NeilBrown31a59e32008-04-30 00:52:30 -07007808 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007809 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007810 spin_lock_irq(&mddev->write_lock);
7811 if (mddev->safemode &&
7812 !atomic_read(&mddev->writes_pending) &&
7813 !mddev->in_sync &&
7814 mddev->recovery_cp == MaxSector) {
7815 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007816 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007817 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007818 }
7819 if (mddev->safemode == 1)
7820 mddev->safemode = 0;
7821 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007822 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007823 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007824 }
NeilBrownfca4d842005-06-21 17:17:11 -07007825
NeilBrown850b2b42006-10-03 01:15:46 -07007826 if (mddev->flags)
7827 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007828
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7830 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7831 /* resync/recovery still happening */
7832 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7833 goto unlock;
7834 }
7835 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007836 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 goto unlock;
7838 }
Neil Brown72a23c22008-06-28 08:31:41 +10007839 /* Set RUNNING before clearing NEEDED to avoid
7840 * any transients in the value of "sync_action".
7841 */
NeilBrown72f36d52012-10-11 14:25:57 +11007842 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007843 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007844 /* Clear some bits that don't mean anything, but
7845 * might be left set
7846 */
NeilBrown24dd4692005-11-08 21:39:26 -08007847 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7848 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849
NeilBrowned209582012-04-24 10:23:14 +10007850 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7851 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007852 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 /* no recovery is running.
7854 * remove any failed drives, then
7855 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007856 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 * the personality to fail the re-add.
7858 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007860 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007861 if (mddev->pers->check_reshape == NULL ||
7862 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007863 /* Cannot proceed */
7864 goto unlock;
7865 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007866 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007867 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007868 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7869 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007870 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007871 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007872 } else if (mddev->recovery_cp < MaxSector) {
7873 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007874 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007875 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7876 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007878
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007880 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007881 /* We are adding a device or devices to an array
7882 * which has the bitmap stored on all devices.
7883 * So make sure all bitmap pages get written
7884 */
7885 bitmap_write_all(mddev->bitmap);
7886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887 mddev->sync_thread = md_register_thread(md_do_sync,
7888 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007889 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 if (!mddev->sync_thread) {
7891 printk(KERN_ERR "%s: could not start resync"
7892 " thread...\n",
7893 mdname(mddev));
7894 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007895 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7896 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7897 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7898 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7899 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007900 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007902 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007903 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904 }
7905 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007906 if (!mddev->sync_thread) {
7907 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7908 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7909 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007910 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007911 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 mddev_unlock(mddev);
7914 }
7915}
7916
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007917void md_reap_sync_thread(struct mddev *mddev)
7918{
7919 struct md_rdev *rdev;
7920
7921 /* resync has finished, collect result */
7922 md_unregister_thread(&mddev->sync_thread);
7923 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7924 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7925 /* success...*/
7926 /* activate any spares */
7927 if (mddev->pers->spare_active(mddev)) {
7928 sysfs_notify(&mddev->kobj, NULL,
7929 "degraded");
7930 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7931 }
7932 }
7933 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7934 mddev->pers->finish_reshape)
7935 mddev->pers->finish_reshape(mddev);
7936
7937 /* If array is no-longer degraded, then any saved_raid_disk
7938 * information must be scrapped. Also if any device is now
7939 * In_sync we must scrape the saved_raid_disk for that device
7940 * do the superblock for an incrementally recovered device
7941 * written out.
7942 */
7943 rdev_for_each(rdev, mddev)
7944 if (!mddev->degraded ||
7945 test_bit(In_sync, &rdev->flags))
7946 rdev->saved_raid_disk = -1;
7947
7948 md_update_sb(mddev, 1);
7949 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7950 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7951 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7952 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7953 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7954 /* flag recovery needed just to double check */
7955 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7956 sysfs_notify_dirent_safe(mddev->sysfs_action);
7957 md_new_event(mddev);
7958 if (mddev->event_work.func)
7959 queue_work(md_misc_wq, &mddev->event_work);
7960}
7961
NeilBrownfd01b882011-10-11 16:47:53 +11007962void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007963{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007964 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007965 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007966 !test_bit(Blocked, &rdev->flags) &&
7967 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007968 msecs_to_jiffies(5000));
7969 rdev_dec_pending(rdev, mddev);
7970}
7971EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7972
NeilBrownc6563a82012-05-21 09:27:00 +10007973void md_finish_reshape(struct mddev *mddev)
7974{
7975 /* called be personality module when reshape completes. */
7976 struct md_rdev *rdev;
7977
7978 rdev_for_each(rdev, mddev) {
7979 if (rdev->data_offset > rdev->new_data_offset)
7980 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7981 else
7982 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7983 rdev->data_offset = rdev->new_data_offset;
7984 }
7985}
7986EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007987
7988/* Bad block management.
7989 * We can record which blocks on each device are 'bad' and so just
7990 * fail those blocks, or that stripe, rather than the whole device.
7991 * Entries in the bad-block table are 64bits wide. This comprises:
7992 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7993 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7994 * A 'shift' can be set so that larger blocks are tracked and
7995 * consequently larger devices can be covered.
7996 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7997 *
7998 * Locking of the bad-block table uses a seqlock so md_is_badblock
7999 * might need to retry if it is very unlucky.
8000 * We will sometimes want to check for bad blocks in a bi_end_io function,
8001 * so we use the write_seqlock_irq variant.
8002 *
8003 * When looking for a bad block we specify a range and want to
8004 * know if any block in the range is bad. So we binary-search
8005 * to the last range that starts at-or-before the given endpoint,
8006 * (or "before the sector after the target range")
8007 * then see if it ends after the given start.
8008 * We return
8009 * 0 if there are no known bad blocks in the range
8010 * 1 if there are known bad block which are all acknowledged
8011 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8012 * plus the start/length of the first bad section we overlap.
8013 */
8014int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8015 sector_t *first_bad, int *bad_sectors)
8016{
8017 int hi;
majianpengab056132012-11-06 17:13:44 +08008018 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008019 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008020 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008021 sector_t target = s + sectors;
8022 unsigned seq;
8023
8024 if (bb->shift > 0) {
8025 /* round the start down, and the end up */
8026 s >>= bb->shift;
8027 target += (1<<bb->shift) - 1;
8028 target >>= bb->shift;
8029 sectors = target - s;
8030 }
8031 /* 'target' is now the first block after the bad range */
8032
8033retry:
8034 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008035 lo = 0;
8036 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008037 hi = bb->count;
8038
8039 /* Binary search between lo and hi for 'target'
8040 * i.e. for the last range that starts before 'target'
8041 */
8042 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8043 * are known not to be the last range before target.
8044 * VARIANT: hi-lo is the number of possible
8045 * ranges, and decreases until it reaches 1
8046 */
8047 while (hi - lo > 1) {
8048 int mid = (lo + hi) / 2;
8049 sector_t a = BB_OFFSET(p[mid]);
8050 if (a < target)
8051 /* This could still be the one, earlier ranges
8052 * could not. */
8053 lo = mid;
8054 else
8055 /* This and later ranges are definitely out. */
8056 hi = mid;
8057 }
8058 /* 'lo' might be the last that started before target, but 'hi' isn't */
8059 if (hi > lo) {
8060 /* need to check all range that end after 's' to see if
8061 * any are unacknowledged.
8062 */
8063 while (lo >= 0 &&
8064 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8065 if (BB_OFFSET(p[lo]) < target) {
8066 /* starts before the end, and finishes after
8067 * the start, so they must overlap
8068 */
8069 if (rv != -1 && BB_ACK(p[lo]))
8070 rv = 1;
8071 else
8072 rv = -1;
8073 *first_bad = BB_OFFSET(p[lo]);
8074 *bad_sectors = BB_LEN(p[lo]);
8075 }
8076 lo--;
8077 }
8078 }
8079
8080 if (read_seqretry(&bb->lock, seq))
8081 goto retry;
8082
8083 return rv;
8084}
8085EXPORT_SYMBOL_GPL(md_is_badblock);
8086
8087/*
8088 * Add a range of bad blocks to the table.
8089 * This might extend the table, or might contract it
8090 * if two adjacent ranges can be merged.
8091 * We binary-search to find the 'insertion' point, then
8092 * decide how best to handle it.
8093 */
8094static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8095 int acknowledged)
8096{
8097 u64 *p;
8098 int lo, hi;
8099 int rv = 1;
8100
8101 if (bb->shift < 0)
8102 /* badblocks are disabled */
8103 return 0;
8104
8105 if (bb->shift) {
8106 /* round the start down, and the end up */
8107 sector_t next = s + sectors;
8108 s >>= bb->shift;
8109 next += (1<<bb->shift) - 1;
8110 next >>= bb->shift;
8111 sectors = next - s;
8112 }
8113
8114 write_seqlock_irq(&bb->lock);
8115
8116 p = bb->page;
8117 lo = 0;
8118 hi = bb->count;
8119 /* Find the last range that starts at-or-before 's' */
8120 while (hi - lo > 1) {
8121 int mid = (lo + hi) / 2;
8122 sector_t a = BB_OFFSET(p[mid]);
8123 if (a <= s)
8124 lo = mid;
8125 else
8126 hi = mid;
8127 }
8128 if (hi > lo && BB_OFFSET(p[lo]) > s)
8129 hi = lo;
8130
8131 if (hi > lo) {
8132 /* we found a range that might merge with the start
8133 * of our new range
8134 */
8135 sector_t a = BB_OFFSET(p[lo]);
8136 sector_t e = a + BB_LEN(p[lo]);
8137 int ack = BB_ACK(p[lo]);
8138 if (e >= s) {
8139 /* Yes, we can merge with a previous range */
8140 if (s == a && s + sectors >= e)
8141 /* new range covers old */
8142 ack = acknowledged;
8143 else
8144 ack = ack && acknowledged;
8145
8146 if (e < s + sectors)
8147 e = s + sectors;
8148 if (e - a <= BB_MAX_LEN) {
8149 p[lo] = BB_MAKE(a, e-a, ack);
8150 s = e;
8151 } else {
8152 /* does not all fit in one range,
8153 * make p[lo] maximal
8154 */
8155 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8156 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8157 s = a + BB_MAX_LEN;
8158 }
8159 sectors = e - s;
8160 }
8161 }
8162 if (sectors && hi < bb->count) {
8163 /* 'hi' points to the first range that starts after 's'.
8164 * Maybe we can merge with the start of that range */
8165 sector_t a = BB_OFFSET(p[hi]);
8166 sector_t e = a + BB_LEN(p[hi]);
8167 int ack = BB_ACK(p[hi]);
8168 if (a <= s + sectors) {
8169 /* merging is possible */
8170 if (e <= s + sectors) {
8171 /* full overlap */
8172 e = s + sectors;
8173 ack = acknowledged;
8174 } else
8175 ack = ack && acknowledged;
8176
8177 a = s;
8178 if (e - a <= BB_MAX_LEN) {
8179 p[hi] = BB_MAKE(a, e-a, ack);
8180 s = e;
8181 } else {
8182 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8183 s = a + BB_MAX_LEN;
8184 }
8185 sectors = e - s;
8186 lo = hi;
8187 hi++;
8188 }
8189 }
8190 if (sectors == 0 && hi < bb->count) {
8191 /* we might be able to combine lo and hi */
8192 /* Note: 's' is at the end of 'lo' */
8193 sector_t a = BB_OFFSET(p[hi]);
8194 int lolen = BB_LEN(p[lo]);
8195 int hilen = BB_LEN(p[hi]);
8196 int newlen = lolen + hilen - (s - a);
8197 if (s >= a && newlen < BB_MAX_LEN) {
8198 /* yes, we can combine them */
8199 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8200 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8201 memmove(p + hi, p + hi + 1,
8202 (bb->count - hi - 1) * 8);
8203 bb->count--;
8204 }
8205 }
8206 while (sectors) {
8207 /* didn't merge (it all).
8208 * Need to add a range just before 'hi' */
8209 if (bb->count >= MD_MAX_BADBLOCKS) {
8210 /* No room for more */
8211 rv = 0;
8212 break;
8213 } else {
8214 int this_sectors = sectors;
8215 memmove(p + hi + 1, p + hi,
8216 (bb->count - hi) * 8);
8217 bb->count++;
8218
8219 if (this_sectors > BB_MAX_LEN)
8220 this_sectors = BB_MAX_LEN;
8221 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8222 sectors -= this_sectors;
8223 s += this_sectors;
8224 }
8225 }
8226
8227 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008228 if (!acknowledged)
8229 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10008230 write_sequnlock_irq(&bb->lock);
8231
8232 return rv;
8233}
8234
NeilBrown3cb03002011-10-11 16:45:26 +11008235int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008236 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008237{
NeilBrownc6563a82012-05-21 09:27:00 +10008238 int rv;
8239 if (is_new)
8240 s += rdev->new_data_offset;
8241 else
8242 s += rdev->data_offset;
8243 rv = md_set_badblocks(&rdev->badblocks,
8244 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008245 if (rv) {
8246 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008247 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008248 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8249 md_wakeup_thread(rdev->mddev->thread);
8250 }
8251 return rv;
8252}
8253EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8254
8255/*
8256 * Remove a range of bad blocks from the table.
8257 * This may involve extending the table if we spilt a region,
8258 * but it must not fail. So if the table becomes full, we just
8259 * drop the remove request.
8260 */
8261static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8262{
8263 u64 *p;
8264 int lo, hi;
8265 sector_t target = s + sectors;
8266 int rv = 0;
8267
8268 if (bb->shift > 0) {
8269 /* When clearing we round the start up and the end down.
8270 * This should not matter as the shift should align with
8271 * the block size and no rounding should ever be needed.
8272 * However it is better the think a block is bad when it
8273 * isn't than to think a block is not bad when it is.
8274 */
8275 s += (1<<bb->shift) - 1;
8276 s >>= bb->shift;
8277 target >>= bb->shift;
8278 sectors = target - s;
8279 }
8280
8281 write_seqlock_irq(&bb->lock);
8282
8283 p = bb->page;
8284 lo = 0;
8285 hi = bb->count;
8286 /* Find the last range that starts before 'target' */
8287 while (hi - lo > 1) {
8288 int mid = (lo + hi) / 2;
8289 sector_t a = BB_OFFSET(p[mid]);
8290 if (a < target)
8291 lo = mid;
8292 else
8293 hi = mid;
8294 }
8295 if (hi > lo) {
8296 /* p[lo] is the last range that could overlap the
8297 * current range. Earlier ranges could also overlap,
8298 * but only this one can overlap the end of the range.
8299 */
8300 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8301 /* Partial overlap, leave the tail of this range */
8302 int ack = BB_ACK(p[lo]);
8303 sector_t a = BB_OFFSET(p[lo]);
8304 sector_t end = a + BB_LEN(p[lo]);
8305
8306 if (a < s) {
8307 /* we need to split this range */
8308 if (bb->count >= MD_MAX_BADBLOCKS) {
8309 rv = 0;
8310 goto out;
8311 }
8312 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8313 bb->count++;
8314 p[lo] = BB_MAKE(a, s-a, ack);
8315 lo++;
8316 }
8317 p[lo] = BB_MAKE(target, end - target, ack);
8318 /* there is no longer an overlap */
8319 hi = lo;
8320 lo--;
8321 }
8322 while (lo >= 0 &&
8323 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8324 /* This range does overlap */
8325 if (BB_OFFSET(p[lo]) < s) {
8326 /* Keep the early parts of this range. */
8327 int ack = BB_ACK(p[lo]);
8328 sector_t start = BB_OFFSET(p[lo]);
8329 p[lo] = BB_MAKE(start, s - start, ack);
8330 /* now low doesn't overlap, so.. */
8331 break;
8332 }
8333 lo--;
8334 }
8335 /* 'lo' is strictly before, 'hi' is strictly after,
8336 * anything between needs to be discarded
8337 */
8338 if (hi - lo > 1) {
8339 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8340 bb->count -= (hi - lo - 1);
8341 }
8342 }
8343
8344 bb->changed = 1;
8345out:
8346 write_sequnlock_irq(&bb->lock);
8347 return rv;
8348}
8349
NeilBrownc6563a82012-05-21 09:27:00 +10008350int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8351 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008352{
NeilBrownc6563a82012-05-21 09:27:00 +10008353 if (is_new)
8354 s += rdev->new_data_offset;
8355 else
8356 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008357 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008358 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008359}
8360EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8361
8362/*
8363 * Acknowledge all bad blocks in a list.
8364 * This only succeeds if ->changed is clear. It is used by
8365 * in-kernel metadata updates
8366 */
8367void md_ack_all_badblocks(struct badblocks *bb)
8368{
8369 if (bb->page == NULL || bb->changed)
8370 /* no point even trying */
8371 return;
8372 write_seqlock_irq(&bb->lock);
8373
majianpengecb178b2012-03-19 12:46:42 +11008374 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008375 u64 *p = bb->page;
8376 int i;
8377 for (i = 0; i < bb->count ; i++) {
8378 if (!BB_ACK(p[i])) {
8379 sector_t start = BB_OFFSET(p[i]);
8380 int len = BB_LEN(p[i]);
8381 p[i] = BB_MAKE(start, len, 1);
8382 }
8383 }
NeilBrownde393cd2011-07-28 11:31:48 +10008384 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008385 }
8386 write_sequnlock_irq(&bb->lock);
8387}
8388EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8389
NeilBrown16c791a2011-07-28 11:31:47 +10008390/* sysfs access to bad-blocks list.
8391 * We present two files.
8392 * 'bad-blocks' lists sector numbers and lengths of ranges that
8393 * are recorded as bad. The list is truncated to fit within
8394 * the one-page limit of sysfs.
8395 * Writing "sector length" to this file adds an acknowledged
8396 * bad block list.
8397 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8398 * been acknowledged. Writing to this file adds bad blocks
8399 * without acknowledging them. This is largely for testing.
8400 */
8401
8402static ssize_t
8403badblocks_show(struct badblocks *bb, char *page, int unack)
8404{
8405 size_t len;
8406 int i;
8407 u64 *p = bb->page;
8408 unsigned seq;
8409
8410 if (bb->shift < 0)
8411 return 0;
8412
8413retry:
8414 seq = read_seqbegin(&bb->lock);
8415
8416 len = 0;
8417 i = 0;
8418
8419 while (len < PAGE_SIZE && i < bb->count) {
8420 sector_t s = BB_OFFSET(p[i]);
8421 unsigned int length = BB_LEN(p[i]);
8422 int ack = BB_ACK(p[i]);
8423 i++;
8424
8425 if (unack && ack)
8426 continue;
8427
8428 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8429 (unsigned long long)s << bb->shift,
8430 length << bb->shift);
8431 }
NeilBrownde393cd2011-07-28 11:31:48 +10008432 if (unack && len == 0)
8433 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008434
8435 if (read_seqretry(&bb->lock, seq))
8436 goto retry;
8437
8438 return len;
8439}
8440
8441#define DO_DEBUG 1
8442
8443static ssize_t
8444badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8445{
8446 unsigned long long sector;
8447 int length;
8448 char newline;
8449#ifdef DO_DEBUG
8450 /* Allow clearing via sysfs *only* for testing/debugging.
8451 * Normally only a successful write may clear a badblock
8452 */
8453 int clear = 0;
8454 if (page[0] == '-') {
8455 clear = 1;
8456 page++;
8457 }
8458#endif /* DO_DEBUG */
8459
8460 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8461 case 3:
8462 if (newline != '\n')
8463 return -EINVAL;
8464 case 2:
8465 if (length <= 0)
8466 return -EINVAL;
8467 break;
8468 default:
8469 return -EINVAL;
8470 }
8471
8472#ifdef DO_DEBUG
8473 if (clear) {
8474 md_clear_badblocks(bb, sector, length);
8475 return len;
8476 }
8477#endif /* DO_DEBUG */
8478 if (md_set_badblocks(bb, sector, length, !unack))
8479 return len;
8480 else
8481 return -ENOSPC;
8482}
8483
Adrian Bunk75c96f82005-05-05 16:16:09 -07008484static int md_notify_reboot(struct notifier_block *this,
8485 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008486{
8487 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008488 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008489 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490
NeilBrownc744a652012-03-19 12:46:37 +11008491 for_each_mddev(mddev, tmp) {
8492 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008493 if (mddev->pers)
8494 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008495 mddev->safemode = 2;
8496 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008497 }
NeilBrownc744a652012-03-19 12:46:37 +11008498 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499 }
NeilBrownc744a652012-03-19 12:46:37 +11008500 /*
8501 * certain more exotic SCSI devices are known to be
8502 * volatile wrt too early system reboots. While the
8503 * right place to handle this issue is the given
8504 * driver, we do want to have a safe RAID driver ...
8505 */
8506 if (need_delay)
8507 mdelay(1000*1);
8508
Linus Torvalds1da177e2005-04-16 15:20:36 -07008509 return NOTIFY_DONE;
8510}
8511
Adrian Bunk75c96f82005-05-05 16:16:09 -07008512static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 .notifier_call = md_notify_reboot,
8514 .next = NULL,
8515 .priority = INT_MAX, /* before any real devices */
8516};
8517
8518static void md_geninit(void)
8519{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008520 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008522 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523}
8524
Adrian Bunk75c96f82005-05-05 16:16:09 -07008525static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526{
Tejun Heoe804ac72010-10-15 15:36:08 +02008527 int ret = -ENOMEM;
8528
Tejun Heoada609e2011-01-25 14:35:54 +01008529 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008530 if (!md_wq)
8531 goto err_wq;
8532
8533 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8534 if (!md_misc_wq)
8535 goto err_misc_wq;
8536
8537 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8538 goto err_md;
8539
8540 if ((ret = register_blkdev(0, "mdp")) < 0)
8541 goto err_mdp;
8542 mdp_major = ret;
8543
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008544 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008545 md_probe, NULL, NULL);
8546 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008547 md_probe, NULL, NULL);
8548
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008550 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551
8552 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554
Tejun Heoe804ac72010-10-15 15:36:08 +02008555err_mdp:
8556 unregister_blkdev(MD_MAJOR, "md");
8557err_md:
8558 destroy_workqueue(md_misc_wq);
8559err_misc_wq:
8560 destroy_workqueue(md_wq);
8561err_wq:
8562 return ret;
8563}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008564
8565#ifndef MODULE
8566
8567/*
8568 * Searches all registered partitions for autorun RAID arrays
8569 * at boot time.
8570 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008571
8572static LIST_HEAD(all_detected_devices);
8573struct detected_devices_node {
8574 struct list_head list;
8575 dev_t dev;
8576};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577
8578void md_autodetect_dev(dev_t dev)
8579{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008580 struct detected_devices_node *node_detected_dev;
8581
8582 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8583 if (node_detected_dev) {
8584 node_detected_dev->dev = dev;
8585 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8586 } else {
8587 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8588 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590}
8591
8592
8593static void autostart_arrays(int part)
8594{
NeilBrown3cb03002011-10-11 16:45:26 +11008595 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008596 struct detected_devices_node *node_detected_dev;
8597 dev_t dev;
8598 int i_scanned, i_passed;
8599
8600 i_scanned = 0;
8601 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602
8603 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8604
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008605 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8606 i_scanned++;
8607 node_detected_dev = list_entry(all_detected_devices.next,
8608 struct detected_devices_node, list);
8609 list_del(&node_detected_dev->list);
8610 dev = node_detected_dev->dev;
8611 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008612 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613 if (IS_ERR(rdev))
8614 continue;
8615
NeilBrownb2d444d2005-11-08 21:39:31 -08008616 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617 MD_BUG();
8618 continue;
8619 }
NeilBrownd0fae182008-03-04 14:29:31 -08008620 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008622 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008623 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008624
8625 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8626 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008627
8628 autorun_devices(part);
8629}
8630
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008631#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008632
8633static __exit void md_exit(void)
8634{
NeilBrownfd01b882011-10-11 16:47:53 +11008635 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008637
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008638 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008639 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008641 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642 unregister_blkdev(mdp_major, "mdp");
8643 unregister_reboot_notifier(&md_notifier);
8644 unregister_sysctl_table(raid_table_header);
8645 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008646 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008648 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008650 destroy_workqueue(md_misc_wq);
8651 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008652}
8653
Dan Williams685784a2007-07-09 11:56:42 -07008654subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008655module_exit(md_exit)
8656
NeilBrownf91de922005-11-08 21:39:36 -08008657static int get_ro(char *buffer, struct kernel_param *kp)
8658{
8659 return sprintf(buffer, "%d", start_readonly);
8660}
8661static int set_ro(const char *val, struct kernel_param *kp)
8662{
8663 char *e;
8664 int num = simple_strtoul(val, &e, 10);
8665 if (*val && (*e == '\0' || *e == '\n')) {
8666 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008667 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008668 }
8669 return -EINVAL;
8670}
8671
NeilBrown80ca3a42006-07-10 04:44:18 -07008672module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8673module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008674
NeilBrownefeb53c2009-01-09 08:31:10 +11008675module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008676
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677EXPORT_SYMBOL(register_md_personality);
8678EXPORT_SYMBOL(unregister_md_personality);
8679EXPORT_SYMBOL(md_error);
8680EXPORT_SYMBOL(md_done_sync);
8681EXPORT_SYMBOL(md_write_start);
8682EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008683EXPORT_SYMBOL(md_register_thread);
8684EXPORT_SYMBOL(md_unregister_thread);
8685EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008686EXPORT_SYMBOL(md_check_recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008687EXPORT_SYMBOL(md_reap_sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008688MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008689MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008690MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008691MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);