blob: 491afda21fd9faf30a8ae311a070a68a6931c773 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
NeilBrown746d3202013-04-24 11:42:41 +100075static int remove_and_add_spares(struct mddev *mddev,
76 struct md_rdev *this);
77
Adrian Bunk5e563412006-06-26 00:27:42 -070078#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
Robert Becker1e509152009-12-14 12:49:58 +110081 * Default number of read corrections we'll attempt on an rdev
82 * before ejecting it from the array. We divide the read error
83 * count by 2 for every hour elapsed between read errors.
84 */
85#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
86/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
88 * is 1000 KB/sec, so the extra system load does not show up that much.
89 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070090 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * subsystem is idle. There is also an 'absolute maximum' reconstruction
92 * speed limit - in case reconstruction slows down your system despite
93 * idle IO detection.
94 *
95 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080096 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98
99static int sysctl_speed_limit_min = 1000;
100static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100101static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800102{
103 return mddev->sync_speed_min ?
104 mddev->sync_speed_min : sysctl_speed_limit_min;
105}
106
NeilBrownfd01b882011-10-11 16:47:53 +1100107static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800108{
109 return mddev->sync_speed_max ?
110 mddev->sync_speed_max : sysctl_speed_limit_max;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static struct ctl_table_header *raid_table_header;
114
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 }
1570 } else if (sb->bblog_offset == 0)
1571 rdev->badblocks.shift = -1;
1572
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
NeilBrownaeb9b2112011-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);
NeilBrownaeb9b2112011-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
NeilBrown850b2b422006-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 */
NeilBrown850b2b422006-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 */
NeilBrown850b2b422006-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);
NeilBrown850b2b422006-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);
NeilBrown850b2b422006-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 }
NeilBrown850b2b422006-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;
3230 rdev->badblocks.shift = 0;
3231 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 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003302 if (super_format == -1)
3303 /* hot-add for 0.90, or non-persistent: so no badblocks */
3304 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return rdev;
3307
3308abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003309 if (rdev->bdev)
3310 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003311 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 kfree(rdev);
3313 return ERR_PTR(err);
3314}
3315
3316/*
3317 * Check a full RAID array for plausibility
3318 */
3319
3320
NeilBrownfd01b882011-10-11 16:47:53 +11003321static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
3323 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003324 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 char b[BDEVNAME_SIZE];
3326
3327 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003328 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 switch (super_types[mddev->major_version].
3330 load_super(rdev, freshest, mddev->minor_version)) {
3331 case 1:
3332 freshest = rdev;
3333 break;
3334 case 0:
3335 break;
3336 default:
3337 printk( KERN_ERR \
3338 "md: fatal superblock inconsistency in %s"
3339 " -- removing from array\n",
3340 bdevname(rdev->bdev,b));
3341 kick_rdev_from_array(rdev);
3342 }
3343
3344
3345 super_types[mddev->major_version].
3346 validate_super(mddev, freshest);
3347
3348 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003349 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003350 if (mddev->max_disks &&
3351 (rdev->desc_nr >= mddev->max_disks ||
3352 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003353 printk(KERN_WARNING
3354 "md: %s: %s: only %d devices permitted\n",
3355 mdname(mddev), bdevname(rdev->bdev, b),
3356 mddev->max_disks);
3357 kick_rdev_from_array(rdev);
3358 continue;
3359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 if (rdev != freshest)
3361 if (super_types[mddev->major_version].
3362 validate_super(mddev, rdev)) {
3363 printk(KERN_WARNING "md: kicking non-fresh %s"
3364 " from array!\n",
3365 bdevname(rdev->bdev,b));
3366 kick_rdev_from_array(rdev);
3367 continue;
3368 }
3369 if (mddev->level == LEVEL_MULTIPATH) {
3370 rdev->desc_nr = i++;
3371 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003372 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003373 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003374 rdev->raid_disk = -1;
3375 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 }
3377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
3379
NeilBrown72e02072009-12-14 12:49:55 +11003380/* Read a fixed-point number.
3381 * Numbers in sysfs attributes should be in "standard" units where
3382 * possible, so time should be in seconds.
3383 * However we internally use a a much smaller unit such as
3384 * milliseconds or jiffies.
3385 * This function takes a decimal number with a possible fractional
3386 * component, and produces an integer which is the result of
3387 * multiplying that number by 10^'scale'.
3388 * all without any floating-point arithmetic.
3389 */
3390int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3391{
3392 unsigned long result = 0;
3393 long decimals = -1;
3394 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3395 if (*cp == '.')
3396 decimals = 0;
3397 else if (decimals < scale) {
3398 unsigned int value;
3399 value = *cp - '0';
3400 result = result * 10 + value;
3401 if (decimals >= 0)
3402 decimals++;
3403 }
3404 cp++;
3405 }
3406 if (*cp == '\n')
3407 cp++;
3408 if (*cp)
3409 return -EINVAL;
3410 if (decimals < 0)
3411 decimals = 0;
3412 while (decimals < scale) {
3413 result *= 10;
3414 decimals ++;
3415 }
3416 *res = result;
3417 return 0;
3418}
3419
3420
NeilBrown19052c02008-08-05 15:54:13 +10003421static void md_safemode_timeout(unsigned long data);
3422
NeilBrowneae17012005-11-08 21:39:23 -08003423static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003424safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003425{
3426 int msec = (mddev->safemode_delay*1000)/HZ;
3427 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3428}
3429static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003430safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003431{
NeilBrown16f17b32006-06-26 00:27:37 -07003432 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003433
NeilBrown72e02072009-12-14 12:49:55 +11003434 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003435 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003436 if (msec == 0)
3437 mddev->safemode_delay = 0;
3438 else {
NeilBrown19052c02008-08-05 15:54:13 +10003439 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003440 mddev->safemode_delay = (msec*HZ)/1000;
3441 if (mddev->safemode_delay == 0)
3442 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003443 if (mddev->safemode_delay < old_delay)
3444 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003445 }
3446 return len;
3447}
3448static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003449__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003450
3451static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003452level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003453{
NeilBrown84fc4b52011-10-11 16:49:58 +11003454 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003455 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003456 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003457 else if (mddev->clevel[0])
3458 return sprintf(page, "%s\n", mddev->clevel);
3459 else if (mddev->level != LEVEL_NONE)
3460 return sprintf(page, "%d\n", mddev->level);
3461 else
3462 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003463}
3464
NeilBrownd9d166c2006-01-06 00:20:51 -08003465static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003466level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003467{
Dan Williamsf2859af2010-05-02 10:04:16 -07003468 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003469 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003470 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003471 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003472 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003473 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003474
3475 if (mddev->pers == NULL) {
3476 if (len == 0)
3477 return 0;
3478 if (len >= sizeof(mddev->clevel))
3479 return -ENOSPC;
3480 strncpy(mddev->clevel, buf, len);
3481 if (mddev->clevel[len-1] == '\n')
3482 len--;
3483 mddev->clevel[len] = 0;
3484 mddev->level = LEVEL_NONE;
3485 return rv;
3486 }
3487
3488 /* request to change the personality. Need to ensure:
3489 * - array is not engaged in resync/recovery/reshape
3490 * - old personality can be suspended
3491 * - new personality will access other array.
3492 */
3493
NeilBrownbb4f1e92010-08-08 21:18:03 +10003494 if (mddev->sync_thread ||
3495 mddev->reshape_position != MaxSector ||
3496 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003497 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003498
3499 if (!mddev->pers->quiesce) {
3500 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3501 mdname(mddev), mddev->pers->name);
3502 return -EINVAL;
3503 }
3504
3505 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003506 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003507 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003508 strncpy(clevel, buf, len);
3509 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003510 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003511 clevel[len] = 0;
3512 if (strict_strtol(clevel, 10, &level))
3513 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003514
Dan Williamsf2859af2010-05-02 10:04:16 -07003515 if (request_module("md-%s", clevel) != 0)
3516 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003517 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003518 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003519 if (!pers || !try_module_get(pers->owner)) {
3520 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003521 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003522 return -EINVAL;
3523 }
3524 spin_unlock(&pers_lock);
3525
3526 if (pers == mddev->pers) {
3527 /* Nothing to do! */
3528 module_put(pers->owner);
3529 return rv;
3530 }
3531 if (!pers->takeover) {
3532 module_put(pers->owner);
3533 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003534 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003535 return -EINVAL;
3536 }
3537
NeilBrowndafb20f2012-03-19 12:46:39 +11003538 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003539 rdev->new_raid_disk = rdev->raid_disk;
3540
NeilBrown245f46c2009-03-31 14:39:39 +11003541 /* ->takeover must set new_* and/or delta_disks
3542 * if it succeeds, and may set them when it fails.
3543 */
3544 priv = pers->takeover(mddev);
3545 if (IS_ERR(priv)) {
3546 mddev->new_level = mddev->level;
3547 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003548 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003549 mddev->raid_disks -= mddev->delta_disks;
3550 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003551 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003552 module_put(pers->owner);
3553 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003554 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003555 return PTR_ERR(priv);
3556 }
3557
3558 /* Looks like we have a winner */
3559 mddev_suspend(mddev);
3560 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003561
3562 if (mddev->pers->sync_request == NULL &&
3563 pers->sync_request != NULL) {
3564 /* need to add the md_redundancy_group */
3565 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3566 printk(KERN_WARNING
3567 "md: cannot register extra attributes for %s\n",
3568 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003569 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003570 }
3571 if (mddev->pers->sync_request != NULL &&
3572 pers->sync_request == NULL) {
3573 /* need to remove the md_redundancy_group */
3574 if (mddev->to_remove == NULL)
3575 mddev->to_remove = &md_redundancy_group;
3576 }
3577
Trela Maciej54071b32010-03-08 16:02:42 +11003578 if (mddev->pers->sync_request == NULL &&
3579 mddev->external) {
3580 /* We are converting from a no-redundancy array
3581 * to a redundancy array and metadata is managed
3582 * externally so we need to be sure that writes
3583 * won't block due to a need to transition
3584 * clean->dirty
3585 * until external management is started.
3586 */
3587 mddev->in_sync = 0;
3588 mddev->safemode_delay = 0;
3589 mddev->safemode = 0;
3590 }
3591
NeilBrowndafb20f2012-03-19 12:46:39 +11003592 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003593 if (rdev->raid_disk < 0)
3594 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003595 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003596 rdev->new_raid_disk = -1;
3597 if (rdev->new_raid_disk == rdev->raid_disk)
3598 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003599 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003600 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003601 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003602 if (rdev->raid_disk < 0)
3603 continue;
3604 if (rdev->new_raid_disk == rdev->raid_disk)
3605 continue;
3606 rdev->raid_disk = rdev->new_raid_disk;
3607 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003608 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003609 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003610 if (sysfs_link_rdev(mddev, rdev))
3611 printk(KERN_WARNING "md: cannot register rd%d"
3612 " for %s after level change\n",
3613 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003614 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003615 }
3616
3617 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003618 mddev->pers = pers;
3619 mddev->private = priv;
3620 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3621 mddev->level = mddev->new_level;
3622 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003623 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003624 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003625 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003626 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003627 if (mddev->pers->sync_request == NULL) {
3628 /* this is now an array without redundancy, so
3629 * it must always be in_sync
3630 */
3631 mddev->in_sync = 1;
3632 del_timer_sync(&mddev->safemode_timer);
3633 }
NeilBrown245f46c2009-03-31 14:39:39 +11003634 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003635 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003636 mddev_resume(mddev);
Maciej Trela5cac78612010-04-14 17:17:39 +10003637 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003638 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003639 return rv;
3640}
3641
3642static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003643__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003644
NeilBrownd4dbd022006-06-26 00:27:59 -07003645
3646static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003647layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003648{
3649 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003650 if (mddev->reshape_position != MaxSector &&
3651 mddev->layout != mddev->new_layout)
3652 return sprintf(page, "%d (%d)\n",
3653 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003654 return sprintf(page, "%d\n", mddev->layout);
3655}
3656
3657static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003658layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003659{
3660 char *e;
3661 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003662
3663 if (!*buf || (*e && *e != '\n'))
3664 return -EINVAL;
3665
NeilBrownb3546032009-03-31 14:56:41 +11003666 if (mddev->pers) {
3667 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003668 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003669 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003670 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003671 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003672 if (err) {
3673 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003674 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003675 }
NeilBrownb3546032009-03-31 14:56:41 +11003676 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003677 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003678 if (mddev->reshape_position == MaxSector)
3679 mddev->layout = n;
3680 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003681 return len;
3682}
3683static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003684__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003685
3686
NeilBrowneae17012005-11-08 21:39:23 -08003687static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003688raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003689{
NeilBrownbb636542005-11-08 21:39:45 -08003690 if (mddev->raid_disks == 0)
3691 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003692 if (mddev->reshape_position != MaxSector &&
3693 mddev->delta_disks != 0)
3694 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3695 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003696 return sprintf(page, "%d\n", mddev->raid_disks);
3697}
3698
NeilBrownfd01b882011-10-11 16:47:53 +11003699static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003700
3701static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003702raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003703{
NeilBrownda943b992006-01-06 00:20:54 -08003704 char *e;
3705 int rv = 0;
3706 unsigned long n = simple_strtoul(buf, &e, 10);
3707
3708 if (!*buf || (*e && *e != '\n'))
3709 return -EINVAL;
3710
3711 if (mddev->pers)
3712 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003713 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003714 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003715 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003716
3717 rdev_for_each(rdev, mddev) {
3718 if (olddisks < n &&
3719 rdev->data_offset < rdev->new_data_offset)
3720 return -EINVAL;
3721 if (olddisks > n &&
3722 rdev->data_offset > rdev->new_data_offset)
3723 return -EINVAL;
3724 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003725 mddev->delta_disks = n - olddisks;
3726 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003727 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003728 } else
NeilBrownda943b992006-01-06 00:20:54 -08003729 mddev->raid_disks = n;
3730 return rv ? rv : len;
3731}
3732static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003733__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003734
NeilBrown24dd4692005-11-08 21:39:26 -08003735static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003736chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003737{
NeilBrown08a02ec2007-05-09 02:35:38 -07003738 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003739 mddev->chunk_sectors != mddev->new_chunk_sectors)
3740 return sprintf(page, "%d (%d)\n",
3741 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003742 mddev->chunk_sectors << 9);
3743 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003744}
3745
3746static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003747chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003748{
NeilBrown3b343802006-01-06 00:20:47 -08003749 char *e;
3750 unsigned long n = simple_strtoul(buf, &e, 10);
3751
NeilBrown3b343802006-01-06 00:20:47 -08003752 if (!*buf || (*e && *e != '\n'))
3753 return -EINVAL;
3754
NeilBrownb3546032009-03-31 14:56:41 +11003755 if (mddev->pers) {
3756 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003757 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003758 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003759 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003760 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003761 if (err) {
3762 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003763 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003764 }
NeilBrownb3546032009-03-31 14:56:41 +11003765 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003766 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003767 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003768 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003769 }
NeilBrown3b343802006-01-06 00:20:47 -08003770 return len;
3771}
3772static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003773__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003774
NeilBrowna94213b2006-06-26 00:28:00 -07003775static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003776resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003777{
NeilBrownd1a7c502009-03-31 15:24:32 +11003778 if (mddev->recovery_cp == MaxSector)
3779 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003780 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3781}
3782
3783static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003784resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003785{
NeilBrowna94213b2006-06-26 00:28:00 -07003786 char *e;
3787 unsigned long long n = simple_strtoull(buf, &e, 10);
3788
NeilBrownb0986362011-05-11 15:52:21 +10003789 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003790 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003791 if (cmd_match(buf, "none"))
3792 n = MaxSector;
3793 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003794 return -EINVAL;
3795
3796 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003797 if (mddev->pers)
3798 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003799 return len;
3800}
3801static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003802__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003803
NeilBrown9e653b62006-06-26 00:27:58 -07003804/*
3805 * The array state can be:
3806 *
3807 * clear
3808 * No devices, no size, no level
3809 * Equivalent to STOP_ARRAY ioctl
3810 * inactive
3811 * May have some settings, but array is not active
3812 * all IO results in error
3813 * When written, doesn't tear down array, but just stops it
3814 * suspended (not supported yet)
3815 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003816 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003817 * readonly
3818 * no resync can happen. no superblocks get written.
3819 * write requests fail
3820 * read-auto
3821 * like readonly, but behaves like 'clean' on a write request.
3822 *
3823 * clean - no pending writes, but otherwise active.
3824 * When written to inactive array, starts without resync
3825 * If a write request arrives then
3826 * if metadata is known, mark 'dirty' and switch to 'active'.
3827 * if not known, block and switch to write-pending
3828 * If written to an active array that has pending writes, then fails.
3829 * active
3830 * fully active: IO and resync can be happening.
3831 * When written to inactive array, starts with resync
3832 *
3833 * write-pending
3834 * clean, but writes are blocked waiting for 'active' to be written.
3835 *
3836 * active-idle
3837 * like active, but no writes have been seen for a while (100msec).
3838 *
3839 */
3840enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3841 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003842static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003843 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3844 "write-pending", "active-idle", NULL };
3845
3846static int match_word(const char *word, char **list)
3847{
3848 int n;
3849 for (n=0; list[n]; n++)
3850 if (cmd_match(word, list[n]))
3851 break;
3852 return n;
3853}
3854
3855static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003856array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003857{
3858 enum array_state st = inactive;
3859
3860 if (mddev->pers)
3861 switch(mddev->ro) {
3862 case 1:
3863 st = readonly;
3864 break;
3865 case 2:
3866 st = read_auto;
3867 break;
3868 case 0:
3869 if (mddev->in_sync)
3870 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003871 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003872 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003873 else if (mddev->safemode)
3874 st = active_idle;
3875 else
3876 st = active;
3877 }
3878 else {
3879 if (list_empty(&mddev->disks) &&
3880 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003881 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003882 st = clear;
3883 else
3884 st = inactive;
3885 }
3886 return sprintf(page, "%s\n", array_states[st]);
3887}
3888
NeilBrowna05b7ea2012-07-19 15:59:18 +10003889static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3890static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003891static int do_md_run(struct mddev * mddev);
3892static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003893
3894static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003895array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003896{
3897 int err = -EINVAL;
3898 enum array_state st = match_word(buf, array_states);
3899 switch(st) {
3900 case bad_word:
3901 break;
3902 case clear:
3903 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003904 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003905 break;
3906 case inactive:
3907 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003908 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003909 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003910 else
NeilBrowne6910632008-02-06 01:39:51 -08003911 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003912 break;
3913 case suspended:
3914 break; /* not supported yet */
3915 case readonly:
3916 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003917 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003918 else {
3919 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003920 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003921 err = do_md_run(mddev);
3922 }
3923 break;
3924 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003925 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003926 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003927 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003928 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003929 err = restart_array(mddev);
3930 if (err == 0) {
3931 mddev->ro = 2;
3932 set_disk_ro(mddev->gendisk, 0);
3933 }
NeilBrown9e653b62006-06-26 00:27:58 -07003934 } else {
3935 mddev->ro = 2;
3936 err = do_md_run(mddev);
3937 }
3938 break;
3939 case clean:
3940 if (mddev->pers) {
3941 restart_array(mddev);
3942 spin_lock_irq(&mddev->write_lock);
3943 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003944 if (mddev->in_sync == 0) {
3945 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003946 if (mddev->safemode == 1)
3947 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003948 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003949 }
3950 err = 0;
3951 } else
3952 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003953 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003954 } else
3955 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003956 break;
3957 case active:
3958 if (mddev->pers) {
3959 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003960 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003961 wake_up(&mddev->sb_wait);
3962 err = 0;
3963 } else {
3964 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003965 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003966 err = do_md_run(mddev);
3967 }
3968 break;
3969 case write_pending:
3970 case active_idle:
3971 /* these cannot be set */
3972 break;
3973 }
3974 if (err)
3975 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003976 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003977 if (mddev->hold_active == UNTIL_IOCTL)
3978 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003979 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003980 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003981 }
NeilBrown9e653b62006-06-26 00:27:58 -07003982}
NeilBrown80ca3a42006-07-10 04:44:18 -07003983static struct md_sysfs_entry md_array_state =
3984__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003985
NeilBrown6d7ff7382006-01-06 00:21:16 -08003986static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003987max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003988 return sprintf(page, "%d\n",
3989 atomic_read(&mddev->max_corr_read_errors));
3990}
3991
3992static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003993max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003994{
3995 char *e;
3996 unsigned long n = simple_strtoul(buf, &e, 10);
3997
3998 if (*buf && (*e == 0 || *e == '\n')) {
3999 atomic_set(&mddev->max_corr_read_errors, n);
4000 return len;
4001 }
4002 return -EINVAL;
4003}
4004
4005static struct md_sysfs_entry max_corr_read_errors =
4006__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4007 max_corrected_read_errors_store);
4008
4009static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004010null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004011{
4012 return -EINVAL;
4013}
4014
4015static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004016new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004017{
4018 /* buf must be %d:%d\n? giving major and minor numbers */
4019 /* The new device is added to the array.
4020 * If the array has a persistent superblock, we read the
4021 * superblock to initialise info and check validity.
4022 * Otherwise, only checking done is that in bind_rdev_to_array,
4023 * which mainly checks size.
4024 */
4025 char *e;
4026 int major = simple_strtoul(buf, &e, 10);
4027 int minor;
4028 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004029 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004030 int err;
4031
4032 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4033 return -EINVAL;
4034 minor = simple_strtoul(e+1, &e, 10);
4035 if (*e && *e != '\n')
4036 return -EINVAL;
4037 dev = MKDEV(major, minor);
4038 if (major != MAJOR(dev) ||
4039 minor != MINOR(dev))
4040 return -EOVERFLOW;
4041
4042
4043 if (mddev->persistent) {
4044 rdev = md_import_device(dev, mddev->major_version,
4045 mddev->minor_version);
4046 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004047 struct md_rdev *rdev0
4048 = list_entry(mddev->disks.next,
4049 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004050 err = super_types[mddev->major_version]
4051 .load_super(rdev, rdev0, mddev->minor_version);
4052 if (err < 0)
4053 goto out;
4054 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004055 } else if (mddev->external)
4056 rdev = md_import_device(dev, -2, -1);
4057 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004058 rdev = md_import_device(dev, -1, -1);
4059
4060 if (IS_ERR(rdev))
4061 return PTR_ERR(rdev);
4062 err = bind_rdev_to_array(rdev, mddev);
4063 out:
4064 if (err)
4065 export_rdev(rdev);
4066 return err ? err : len;
4067}
4068
4069static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004070__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004071
4072static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004073bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004074{
4075 char *end;
4076 unsigned long chunk, end_chunk;
4077
4078 if (!mddev->bitmap)
4079 goto out;
4080 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4081 while (*buf) {
4082 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4083 if (buf == end) break;
4084 if (*end == '-') { /* range */
4085 buf = end + 1;
4086 end_chunk = simple_strtoul(buf, &end, 0);
4087 if (buf == end) break;
4088 }
4089 if (*end && !isspace(*end)) break;
4090 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004091 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004092 }
4093 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4094out:
4095 return len;
4096}
4097
4098static struct md_sysfs_entry md_bitmap =
4099__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4100
4101static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004102size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004103{
Andre Noll58c0fed2009-03-31 14:33:13 +11004104 return sprintf(page, "%llu\n",
4105 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004106}
4107
NeilBrownfd01b882011-10-11 16:47:53 +11004108static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004109
4110static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004111size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004112{
4113 /* If array is inactive, we can reduce the component size, but
4114 * not increase it (except from 0).
4115 * If array is active, we can try an on-line resize
4116 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004117 sector_t sectors;
4118 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004119
Andre Noll58c0fed2009-03-31 14:33:13 +11004120 if (err < 0)
4121 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004122 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004123 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004124 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004125 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004126 if (mddev->dev_sectors == 0 ||
4127 mddev->dev_sectors > sectors)
4128 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004129 else
4130 err = -ENOSPC;
4131 }
4132 return err ? err : len;
4133}
4134
4135static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004136__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004137
NeilBrown8bb93aa2006-01-06 00:20:50 -08004138
Masanari Iida83f0d772012-10-30 00:18:08 +09004139/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004140 * This is one of
4141 * 'none' for arrays with no metadata (good luck...)
4142 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004143 * or N.M for internally known formats
4144 */
4145static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004146metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004147{
4148 if (mddev->persistent)
4149 return sprintf(page, "%d.%d\n",
4150 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004151 else if (mddev->external)
4152 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004153 else
4154 return sprintf(page, "none\n");
4155}
4156
4157static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004158metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004159{
4160 int major, minor;
4161 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004162 /* Changing the details of 'external' metadata is
4163 * always permitted. Otherwise there must be
4164 * no devices attached to the array.
4165 */
4166 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4167 ;
4168 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004169 return -EBUSY;
4170
4171 if (cmd_match(buf, "none")) {
4172 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004173 mddev->external = 0;
4174 mddev->major_version = 0;
4175 mddev->minor_version = 90;
4176 return len;
4177 }
4178 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004179 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004180 if (namelen >= sizeof(mddev->metadata_type))
4181 namelen = sizeof(mddev->metadata_type)-1;
4182 strncpy(mddev->metadata_type, buf+9, namelen);
4183 mddev->metadata_type[namelen] = 0;
4184 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4185 mddev->metadata_type[--namelen] = 0;
4186 mddev->persistent = 0;
4187 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004188 mddev->major_version = 0;
4189 mddev->minor_version = 90;
4190 return len;
4191 }
4192 major = simple_strtoul(buf, &e, 10);
4193 if (e==buf || *e != '.')
4194 return -EINVAL;
4195 buf = e+1;
4196 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004197 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004198 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004199 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004200 return -ENOENT;
4201 mddev->major_version = major;
4202 mddev->minor_version = minor;
4203 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004204 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004205 return len;
4206}
4207
4208static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004209__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004210
NeilBrowna35b0d62006-01-06 00:20:49 -08004211static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004212action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004213{
NeilBrown7eec3142005-11-08 21:39:44 -08004214 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004215 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4216 type = "frozen";
4217 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004218 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004219 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4220 type = "reshape";
4221 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004222 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4223 type = "resync";
4224 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4225 type = "check";
4226 else
4227 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004228 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004229 type = "recover";
4230 }
4231 return sprintf(page, "%s\n", type);
4232}
4233
NeilBrownfd01b882011-10-11 16:47:53 +11004234static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004235
NeilBrown24dd4692005-11-08 21:39:26 -08004236static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004237action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004238{
NeilBrown7eec3142005-11-08 21:39:44 -08004239 if (!mddev->pers || !mddev->pers->sync_request)
4240 return -EINVAL;
4241
NeilBrownb6a9ce62009-05-26 09:41:17 +10004242 if (cmd_match(page, "frozen"))
4243 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4244 else
4245 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4246
4247 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004248 if (mddev->sync_thread) {
4249 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004250 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004251 }
NeilBrown03c902e2006-01-06 00:20:46 -08004252 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4253 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004254 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004255 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004256 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004257 else if (cmd_match(page, "recover")) {
4258 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4259 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4260 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004261 int err;
4262 if (mddev->pers->start_reshape == NULL)
4263 return -EINVAL;
4264 err = mddev->pers->start_reshape(mddev);
4265 if (err)
4266 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004267 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004268 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004269 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004270 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004271 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004272 return -EINVAL;
4273 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4274 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004275 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004276 if (mddev->ro == 2) {
4277 /* A write to sync_action is enough to justify
4278 * canceling read-auto mode
4279 */
4280 mddev->ro = 0;
4281 md_wakeup_thread(mddev->sync_thread);
4282 }
NeilBrown03c902e2006-01-06 00:20:46 -08004283 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004284 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004285 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004286 return len;
4287}
4288
NeilBrown9d888832005-11-08 21:39:26 -08004289static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004290mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004291{
4292 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004293 (unsigned long long)
4294 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004295}
4296
NeilBrown80ca3a42006-07-10 04:44:18 -07004297static struct md_sysfs_entry md_scan_mode =
4298__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004299
NeilBrown96de1e62005-11-08 21:39:39 -08004300
NeilBrown80ca3a42006-07-10 04:44:18 -07004301static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004302
NeilBrown88202a02006-01-06 00:21:36 -08004303static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004304sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004305{
4306 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4307 mddev->sync_speed_min ? "local": "system");
4308}
4309
4310static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004311sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004312{
4313 int min;
4314 char *e;
4315 if (strncmp(buf, "system", 6)==0) {
4316 mddev->sync_speed_min = 0;
4317 return len;
4318 }
4319 min = simple_strtoul(buf, &e, 10);
4320 if (buf == e || (*e && *e != '\n') || min <= 0)
4321 return -EINVAL;
4322 mddev->sync_speed_min = min;
4323 return len;
4324}
4325
4326static struct md_sysfs_entry md_sync_min =
4327__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4328
4329static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004330sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004331{
4332 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4333 mddev->sync_speed_max ? "local": "system");
4334}
4335
4336static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004337sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004338{
4339 int max;
4340 char *e;
4341 if (strncmp(buf, "system", 6)==0) {
4342 mddev->sync_speed_max = 0;
4343 return len;
4344 }
4345 max = simple_strtoul(buf, &e, 10);
4346 if (buf == e || (*e && *e != '\n') || max <= 0)
4347 return -EINVAL;
4348 mddev->sync_speed_max = max;
4349 return len;
4350}
4351
4352static struct md_sysfs_entry md_sync_max =
4353__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4354
Iustin Popd7f3d292007-10-16 23:30:54 -07004355static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004356degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004357{
4358 return sprintf(page, "%d\n", mddev->degraded);
4359}
4360static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004361
4362static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004363sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004364{
4365 return sprintf(page, "%d\n", mddev->parallel_resync);
4366}
4367
4368static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004369sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004370{
4371 long n;
4372
4373 if (strict_strtol(buf, 10, &n))
4374 return -EINVAL;
4375
4376 if (n != 0 && n != 1)
4377 return -EINVAL;
4378
4379 mddev->parallel_resync = n;
4380
4381 if (mddev->sync_thread)
4382 wake_up(&resync_wait);
4383
4384 return len;
4385}
4386
4387/* force parallel resync, even with shared block devices */
4388static struct md_sysfs_entry md_sync_force_parallel =
4389__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4390 sync_force_parallel_show, sync_force_parallel_store);
4391
4392static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004393sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004394{
4395 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004396 if (mddev->curr_resync == 0)
4397 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004398 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4399 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004400 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004401 db = resync - mddev->resync_mark_cnt;
4402 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004403}
4404
NeilBrown80ca3a42006-07-10 04:44:18 -07004405static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004406
4407static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004408sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004409{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004410 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004411
NeilBrownacb180b2009-04-14 16:28:34 +10004412 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4413 return sprintf(page, "none\n");
4414
NeilBrown72f36d52012-10-11 14:25:57 +11004415 if (mddev->curr_resync == 1 ||
4416 mddev->curr_resync == 2)
4417 return sprintf(page, "delayed\n");
4418
NeilBrownc804cde2012-05-21 09:28:33 +10004419 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4420 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004421 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004422 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004423 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004424
NeilBrownacb180b2009-04-14 16:28:34 +10004425 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004426 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004427}
4428
NeilBrown80ca3a42006-07-10 04:44:18 -07004429static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004430
NeilBrowne464eaf2006-03-27 01:18:14 -08004431static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004432min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004433{
4434 return sprintf(page, "%llu\n",
4435 (unsigned long long)mddev->resync_min);
4436}
4437static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004438min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004439{
4440 unsigned long long min;
4441 if (strict_strtoull(buf, 10, &min))
4442 return -EINVAL;
4443 if (min > mddev->resync_max)
4444 return -EINVAL;
4445 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4446 return -EBUSY;
4447
4448 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004449 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004450 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004451 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004452 return -EINVAL;
4453 }
4454 mddev->resync_min = min;
4455
4456 return len;
4457}
4458
4459static struct md_sysfs_entry md_min_sync =
4460__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4461
4462static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004463max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004464{
4465 if (mddev->resync_max == MaxSector)
4466 return sprintf(page, "max\n");
4467 else
4468 return sprintf(page, "%llu\n",
4469 (unsigned long long)mddev->resync_max);
4470}
4471static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004472max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004473{
4474 if (strncmp(buf, "max", 3) == 0)
4475 mddev->resync_max = MaxSector;
4476 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004477 unsigned long long max;
4478 if (strict_strtoull(buf, 10, &max))
4479 return -EINVAL;
4480 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004481 return -EINVAL;
4482 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004483 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004484 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4485 return -EBUSY;
4486
4487 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004488 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004489 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004490 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004491 return -EINVAL;
4492 }
4493 mddev->resync_max = max;
4494 }
4495 wake_up(&mddev->recovery_wait);
4496 return len;
4497}
4498
4499static struct md_sysfs_entry md_max_sync =
4500__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4501
4502static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004503suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004504{
4505 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4506}
4507
4508static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004509suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004510{
4511 char *e;
4512 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004513 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004514
NeilBrownb8d966e2009-07-01 11:14:04 +10004515 if (mddev->pers == NULL ||
4516 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004517 return -EINVAL;
4518 if (buf == e || (*e && *e != '\n'))
4519 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004520
4521 mddev->suspend_lo = new;
4522 if (new >= old)
4523 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004524 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004525 else {
4526 /* Expanding suspended region - need to wait */
4527 mddev->pers->quiesce(mddev, 1);
4528 mddev->pers->quiesce(mddev, 0);
4529 }
4530 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004531}
4532static struct md_sysfs_entry md_suspend_lo =
4533__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4534
4535
4536static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004537suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004538{
4539 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4540}
4541
4542static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004543suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004544{
4545 char *e;
4546 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004547 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004548
NeilBrownb8d966e2009-07-01 11:14:04 +10004549 if (mddev->pers == NULL ||
4550 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004551 return -EINVAL;
4552 if (buf == e || (*e && *e != '\n'))
4553 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004554
4555 mddev->suspend_hi = new;
4556 if (new <= old)
4557 /* Shrinking suspended region */
4558 mddev->pers->quiesce(mddev, 2);
4559 else {
4560 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004561 mddev->pers->quiesce(mddev, 1);
4562 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004563 }
4564 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004565}
4566static struct md_sysfs_entry md_suspend_hi =
4567__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4568
NeilBrown08a02ec2007-05-09 02:35:38 -07004569static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004570reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004571{
4572 if (mddev->reshape_position != MaxSector)
4573 return sprintf(page, "%llu\n",
4574 (unsigned long long)mddev->reshape_position);
4575 strcpy(page, "none\n");
4576 return 5;
4577}
4578
4579static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004580reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004581{
NeilBrownc6563a82012-05-21 09:27:00 +10004582 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004583 char *e;
4584 unsigned long long new = simple_strtoull(buf, &e, 10);
4585 if (mddev->pers)
4586 return -EBUSY;
4587 if (buf == e || (*e && *e != '\n'))
4588 return -EINVAL;
4589 mddev->reshape_position = new;
4590 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004591 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004592 mddev->new_level = mddev->level;
4593 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004594 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004595 rdev_for_each(rdev, mddev)
4596 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004597 return len;
4598}
4599
4600static struct md_sysfs_entry md_reshape_position =
4601__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4602 reshape_position_store);
4603
Dan Williamsb522adc2009-03-31 15:00:31 +11004604static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004605reshape_direction_show(struct mddev *mddev, char *page)
4606{
4607 return sprintf(page, "%s\n",
4608 mddev->reshape_backwards ? "backwards" : "forwards");
4609}
4610
4611static ssize_t
4612reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4613{
4614 int backwards = 0;
4615 if (cmd_match(buf, "forwards"))
4616 backwards = 0;
4617 else if (cmd_match(buf, "backwards"))
4618 backwards = 1;
4619 else
4620 return -EINVAL;
4621 if (mddev->reshape_backwards == backwards)
4622 return len;
4623
4624 /* check if we are allowed to change */
4625 if (mddev->delta_disks)
4626 return -EBUSY;
4627
4628 if (mddev->persistent &&
4629 mddev->major_version == 0)
4630 return -EINVAL;
4631
4632 mddev->reshape_backwards = backwards;
4633 return len;
4634}
4635
4636static struct md_sysfs_entry md_reshape_direction =
4637__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4638 reshape_direction_store);
4639
4640static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004641array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004642{
4643 if (mddev->external_size)
4644 return sprintf(page, "%llu\n",
4645 (unsigned long long)mddev->array_sectors/2);
4646 else
4647 return sprintf(page, "default\n");
4648}
4649
4650static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004651array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004652{
4653 sector_t sectors;
4654
4655 if (strncmp(buf, "default", 7) == 0) {
4656 if (mddev->pers)
4657 sectors = mddev->pers->size(mddev, 0, 0);
4658 else
4659 sectors = mddev->array_sectors;
4660
4661 mddev->external_size = 0;
4662 } else {
4663 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4664 return -EINVAL;
4665 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004666 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004667
4668 mddev->external_size = 1;
4669 }
4670
4671 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004672 if (mddev->pers) {
4673 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004674 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004675 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004676 return len;
4677}
4678
4679static struct md_sysfs_entry md_array_size =
4680__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4681 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004682
NeilBrowneae17012005-11-08 21:39:23 -08004683static struct attribute *md_default_attrs[] = {
4684 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004685 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004686 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004687 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004688 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004689 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004690 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004691 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004692 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004693 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004694 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004695 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004696 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004697 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004698 NULL,
4699};
4700
4701static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004702 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004703 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004704 &md_sync_min.attr,
4705 &md_sync_max.attr,
4706 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004707 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004708 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004709 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004710 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004711 &md_suspend_lo.attr,
4712 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004713 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004714 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004715 NULL,
4716};
NeilBrown411036f2005-11-08 21:39:40 -08004717static struct attribute_group md_redundancy_group = {
4718 .name = NULL,
4719 .attrs = md_redundancy_attrs,
4720};
4721
NeilBrowneae17012005-11-08 21:39:23 -08004722
4723static ssize_t
4724md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4725{
4726 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004727 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004728 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004729
4730 if (!entry->show)
4731 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004732 spin_lock(&all_mddevs_lock);
4733 if (list_empty(&mddev->all_mddevs)) {
4734 spin_unlock(&all_mddevs_lock);
4735 return -EBUSY;
4736 }
4737 mddev_get(mddev);
4738 spin_unlock(&all_mddevs_lock);
4739
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004740 rv = mddev_lock(mddev);
4741 if (!rv) {
4742 rv = entry->show(mddev, page);
4743 mddev_unlock(mddev);
4744 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004745 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004746 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004747}
4748
4749static ssize_t
4750md_attr_store(struct kobject *kobj, struct attribute *attr,
4751 const char *page, size_t length)
4752{
4753 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004754 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004755 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004756
4757 if (!entry->store)
4758 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004759 if (!capable(CAP_SYS_ADMIN))
4760 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004761 spin_lock(&all_mddevs_lock);
4762 if (list_empty(&mddev->all_mddevs)) {
4763 spin_unlock(&all_mddevs_lock);
4764 return -EBUSY;
4765 }
4766 mddev_get(mddev);
4767 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004768 if (entry->store == new_dev_store)
4769 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004770 rv = mddev_lock(mddev);
4771 if (!rv) {
4772 rv = entry->store(mddev, page, length);
4773 mddev_unlock(mddev);
4774 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004775 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004776 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004777}
4778
4779static void md_free(struct kobject *ko)
4780{
NeilBrownfd01b882011-10-11 16:47:53 +11004781 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004782
4783 if (mddev->sysfs_state)
4784 sysfs_put(mddev->sysfs_state);
4785
4786 if (mddev->gendisk) {
4787 del_gendisk(mddev->gendisk);
4788 put_disk(mddev->gendisk);
4789 }
4790 if (mddev->queue)
4791 blk_cleanup_queue(mddev->queue);
4792
NeilBrowneae17012005-11-08 21:39:23 -08004793 kfree(mddev);
4794}
4795
Emese Revfy52cf25d2010-01-19 02:58:23 +01004796static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004797 .show = md_attr_show,
4798 .store = md_attr_store,
4799};
4800static struct kobj_type md_ktype = {
4801 .release = md_free,
4802 .sysfs_ops = &md_sysfs_ops,
4803 .default_attrs = md_default_attrs,
4804};
4805
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806int mdp_major = 0;
4807
Dan Williams5fd3a172009-03-04 00:57:25 -07004808static void mddev_delayed_delete(struct work_struct *ws)
4809{
NeilBrownfd01b882011-10-11 16:47:53 +11004810 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004811
NeilBrown43a70502009-12-14 12:49:55 +11004812 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004813 kobject_del(&mddev->kobj);
4814 kobject_put(&mddev->kobj);
4815}
4816
NeilBrownefeb53c2009-01-09 08:31:10 +11004817static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004819 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004820 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004822 int partitioned;
4823 int shift;
4824 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004825 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
4827 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004828 return -ENODEV;
4829
4830 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4831 shift = partitioned ? MdpMinorShift : 0;
4832 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Tejun Heoe804ac72010-10-15 15:36:08 +02004834 /* wait for any previous instance of this device to be
4835 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004836 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004837 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004838
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004839 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004840 error = -EEXIST;
4841 if (mddev->gendisk)
4842 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004843
4844 if (name) {
4845 /* Need to ensure that 'name' is not a duplicate.
4846 */
NeilBrownfd01b882011-10-11 16:47:53 +11004847 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004848 spin_lock(&all_mddevs_lock);
4849
4850 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4851 if (mddev2->gendisk &&
4852 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4853 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004854 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004855 }
4856 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 }
NeilBrown8b765392009-01-09 08:31:08 +11004858
NeilBrown0909dc42009-07-01 12:27:21 +10004859 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004860 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004861 if (!mddev->queue)
4862 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004863 mddev->queue->queuedata = mddev;
4864
NeilBrown409c57f2009-03-31 14:39:39 +11004865 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004866 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004867
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 disk = alloc_disk(1 << shift);
4869 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004870 blk_cleanup_queue(mddev->queue);
4871 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004872 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004874 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004876 if (name)
4877 strcpy(disk->disk_name, name);
4878 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004880 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 disk->fops = &md_fops;
4883 disk->private_data = mddev;
4884 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004885 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004886 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004887 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004888 * remove it now.
4889 */
4890 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004892 /* As soon as we call add_disk(), another thread could get
4893 * through to md_open, so make sure it doesn't get too far
4894 */
4895 mutex_lock(&mddev->open_mutex);
4896 add_disk(disk);
4897
Tejun Heoed9e1982008-08-25 19:56:05 +09004898 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4899 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004900 if (error) {
4901 /* This isn't possible, but as kobject_init_and_add is marked
4902 * __must_check, we must do something with the result
4903 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004904 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4905 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004906 error = 0;
4907 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004908 if (mddev->kobj.sd &&
4909 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004910 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004911 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004912 abort:
4913 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004914 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004915 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004916 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004917 }
NeilBrownd3374822009-01-09 08:31:10 +11004918 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004919 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004920}
4921
4922static struct kobject *md_probe(dev_t dev, int *part, void *data)
4923{
4924 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 return NULL;
4926}
4927
NeilBrownefeb53c2009-01-09 08:31:10 +11004928static int add_named_array(const char *val, struct kernel_param *kp)
4929{
4930 /* val must be "md_*" where * is not all digits.
4931 * We allocate an array with a large free minor number, and
4932 * set the name to val. val must not already be an active name.
4933 */
4934 int len = strlen(val);
4935 char buf[DISK_NAME_LEN];
4936
4937 while (len && val[len-1] == '\n')
4938 len--;
4939 if (len >= DISK_NAME_LEN)
4940 return -E2BIG;
4941 strlcpy(buf, val, len+1);
4942 if (strncmp(buf, "md_", 3) != 0)
4943 return -EINVAL;
4944 return md_alloc(0, buf);
4945}
4946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947static void md_safemode_timeout(unsigned long data)
4948{
NeilBrownfd01b882011-10-11 16:47:53 +11004949 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950
Neil Brown0fd62b82008-06-28 08:31:36 +10004951 if (!atomic_read(&mddev->writes_pending)) {
4952 mddev->safemode = 1;
4953 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004954 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 md_wakeup_thread(mddev->thread);
4957}
4958
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004959static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
NeilBrownfd01b882011-10-11 16:47:53 +11004961int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962{
NeilBrown2604b702006-01-06 00:20:36 -08004963 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004964 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004965 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
NeilBrowna757e642005-04-16 15:26:42 -07004967 if (list_empty(&mddev->disks))
4968 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
4971 if (mddev->pers)
4972 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004973 /* Cannot run until previous stop completes properly */
4974 if (mddev->sysfs_active)
4975 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004976
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 /*
4978 * Analyze all RAID superblock(s)
4979 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004980 if (!mddev->raid_disks) {
4981 if (!mddev->persistent)
4982 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004983 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
NeilBrownd9d166c2006-01-06 00:20:51 -08004986 if (mddev->level != LEVEL_NONE)
4987 request_module("md-level-%d", mddev->level);
4988 else if (mddev->clevel[0])
4989 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
4991 /*
4992 * Drop all container device buffers, from now on
4993 * the only valid external interface is through the md
4994 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004996 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004997 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 continue;
4999 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005000 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005001
5002 /* perform some consistency tests on the device.
5003 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005004 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005005 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005006 if (rdev->meta_bdev) {
5007 /* Nothing to check */;
5008 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005009 if (mddev->dev_sectors &&
5010 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005011 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005012 printk("md: %s: data overlaps metadata\n",
5013 mdname(mddev));
5014 return -EINVAL;
5015 }
5016 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005017 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005018 > rdev->data_offset) {
5019 printk("md: %s: metadata overlaps data\n",
5020 mdname(mddev));
5021 return -EINVAL;
5022 }
5023 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005024 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 }
5026
NeilBrowna167f662010-10-26 18:31:13 +11005027 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005028 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005029
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005031 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005032 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005034 if (mddev->level != LEVEL_NONE)
5035 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5036 mddev->level);
5037 else
5038 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5039 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 return -EINVAL;
5041 }
NeilBrown2604b702006-01-06 00:20:36 -08005042 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005044 if (mddev->level != pers->level) {
5045 mddev->level = pers->level;
5046 mddev->new_level = pers->level;
5047 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005048 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049
NeilBrownf6705572006-03-27 01:18:11 -08005050 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005051 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005052 /* This personality cannot handle reshaping... */
5053 mddev->pers = NULL;
5054 module_put(pers->owner);
5055 return -EINVAL;
5056 }
5057
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005058 if (pers->sync_request) {
5059 /* Warn if this is a potentially silly
5060 * configuration.
5061 */
5062 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005063 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005064 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005065
NeilBrowndafb20f2012-03-19 12:46:39 +11005066 rdev_for_each(rdev, mddev)
5067 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005068 if (rdev < rdev2 &&
5069 rdev->bdev->bd_contains ==
5070 rdev2->bdev->bd_contains) {
5071 printk(KERN_WARNING
5072 "%s: WARNING: %s appears to be"
5073 " on the same physical disk as"
5074 " %s.\n",
5075 mdname(mddev),
5076 bdevname(rdev->bdev,b),
5077 bdevname(rdev2->bdev,b2));
5078 warned = 1;
5079 }
5080 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005081
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005082 if (warned)
5083 printk(KERN_WARNING
5084 "True protection against single-disk"
5085 " failure might be compromised.\n");
5086 }
5087
NeilBrown657390d2005-08-26 18:34:16 -07005088 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005089 /* may be over-ridden by personality */
5090 mddev->resync_max_sectors = mddev->dev_sectors;
5091
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005092 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
NeilBrown0f9552b52009-12-30 12:08:50 +11005094 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005095 mddev->ro = 2; /* read-only, but switch on first write */
5096
NeilBrownb15c2e52006-01-06 00:20:16 -08005097 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005098 if (err)
5099 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005100 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5101 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5102 " but 'external_size' not in effect?\n", __func__);
5103 printk(KERN_ERR
5104 "md: invalid array_size %llu > default size %llu\n",
5105 (unsigned long long)mddev->array_sectors / 2,
5106 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5107 err = -EINVAL;
5108 mddev->pers->stop(mddev);
5109 }
NeilBrownef99bf42012-05-22 13:55:08 +10005110 if (err == 0 && mddev->pers->sync_request &&
5111 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005112 err = bitmap_create(mddev);
5113 if (err) {
5114 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5115 mdname(mddev), err);
5116 mddev->pers->stop(mddev);
5117 }
5118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 module_put(mddev->pers->owner);
5121 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005122 bitmap_destroy(mddev);
5123 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005125 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005126 if (mddev->kobj.sd &&
5127 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005128 printk(KERN_WARNING
5129 "md: cannot register extra attributes for %s\n",
5130 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005131 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005132 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005133 mddev->ro = 0;
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005136 atomic_set(&mddev->max_corr_read_errors,
5137 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 mddev->safemode = 0;
5139 mddev->safemode_timer.function = md_safemode_timeout;
5140 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005141 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005143 smp_wmb();
5144 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005145 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005146 if (rdev->raid_disk >= 0)
5147 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005148 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
5150 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5151
NeilBrown850b2b422006-10-03 01:15:46 -07005152 if (mddev->flags)
5153 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154
NeilBrownd7603b72006-01-06 00:20:30 -08005155 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005156 sysfs_notify_dirent_safe(mddev->sysfs_state);
5157 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005158 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 return 0;
5160}
NeilBrown390ee602010-06-01 19:37:27 +10005161EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
NeilBrownfd01b882011-10-11 16:47:53 +11005163static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005164{
5165 int err;
5166
5167 err = md_run(mddev);
5168 if (err)
5169 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005170 err = bitmap_load(mddev);
5171 if (err) {
5172 bitmap_destroy(mddev);
5173 goto out;
5174 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005175
5176 md_wakeup_thread(mddev->thread);
5177 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5178
NeilBrownfe60b012010-03-29 11:10:42 +11005179 set_capacity(mddev->gendisk, mddev->array_sectors);
5180 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005181 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005182 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5183out:
5184 return err;
5185}
5186
NeilBrownfd01b882011-10-11 16:47:53 +11005187static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188{
5189 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
Andre Noll80fab1d2008-07-11 22:02:21 +10005191 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005193 return -ENXIO;
5194 if (!mddev->pers)
5195 return -EINVAL;
5196 if (!mddev->ro)
5197 return -EBUSY;
5198 mddev->safemode = 0;
5199 mddev->ro = 0;
5200 set_disk_ro(disk, 0);
5201 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5202 mdname(mddev));
5203 /* Kick recovery or resync if necessary */
5204 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5205 md_wakeup_thread(mddev->thread);
5206 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005207 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209}
5210
NeilBrownacc55e22006-06-26 00:27:47 -07005211/* similar to deny_write_access, but accounts for our holding a reference
5212 * to the file ourselves */
5213static int deny_bitmap_write_access(struct file * file)
5214{
5215 struct inode *inode = file->f_mapping->host;
5216
5217 spin_lock(&inode->i_lock);
5218 if (atomic_read(&inode->i_writecount) > 1) {
5219 spin_unlock(&inode->i_lock);
5220 return -ETXTBSY;
5221 }
5222 atomic_set(&inode->i_writecount, -1);
5223 spin_unlock(&inode->i_lock);
5224
5225 return 0;
5226}
5227
NeilBrown43a70502009-12-14 12:49:55 +11005228void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005229{
5230 struct inode *inode = file->f_mapping->host;
5231
5232 spin_lock(&inode->i_lock);
5233 atomic_set(&inode->i_writecount, 1);
5234 spin_unlock(&inode->i_lock);
5235}
5236
NeilBrownfd01b882011-10-11 16:47:53 +11005237static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005238{
5239 mddev->array_sectors = 0;
5240 mddev->external_size = 0;
5241 mddev->dev_sectors = 0;
5242 mddev->raid_disks = 0;
5243 mddev->recovery_cp = 0;
5244 mddev->resync_min = 0;
5245 mddev->resync_max = MaxSector;
5246 mddev->reshape_position = MaxSector;
5247 mddev->external = 0;
5248 mddev->persistent = 0;
5249 mddev->level = LEVEL_NONE;
5250 mddev->clevel[0] = 0;
5251 mddev->flags = 0;
5252 mddev->ro = 0;
5253 mddev->metadata_type[0] = 0;
5254 mddev->chunk_sectors = 0;
5255 mddev->ctime = mddev->utime = 0;
5256 mddev->layout = 0;
5257 mddev->max_disks = 0;
5258 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005259 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005260 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005261 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005262 mddev->new_level = LEVEL_NONE;
5263 mddev->new_layout = 0;
5264 mddev->new_chunk_sectors = 0;
5265 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005266 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005267 mddev->suspend_lo = mddev->suspend_hi = 0;
5268 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5269 mddev->recovery = 0;
5270 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005271 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005272 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005273 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005274 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005275 mddev->bitmap_info.offset = 0;
5276 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005277 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005278 mddev->bitmap_info.chunksize = 0;
5279 mddev->bitmap_info.daemon_sleep = 0;
5280 mddev->bitmap_info.max_write_behind = 0;
5281}
5282
NeilBrownfd01b882011-10-11 16:47:53 +11005283static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005284{
5285 if (mddev->sync_thread) {
5286 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5287 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005288 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005289 }
5290
5291 del_timer_sync(&mddev->safemode_timer);
5292
5293 bitmap_flush(mddev);
5294 md_super_wait(mddev);
5295
5296 if (!mddev->in_sync || mddev->flags) {
5297 /* mark array as shutdown cleanly */
5298 mddev->in_sync = 1;
5299 md_update_sb(mddev, 1);
5300 }
5301}
NeilBrowndefad612011-01-14 09:14:33 +11005302
NeilBrownfd01b882011-10-11 16:47:53 +11005303void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005304{
5305 mddev_lock(mddev);
5306 __md_stop_writes(mddev);
5307 mddev_unlock(mddev);
5308}
NeilBrown390ee602010-06-01 19:37:27 +10005309EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005310
NeilBrown5eff3c42012-11-19 10:47:48 +11005311static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005312{
NeilBrown0ca69882011-01-14 09:14:33 +11005313 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005314 mddev->pers->stop(mddev);
5315 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5316 mddev->to_remove = &md_redundancy_group;
5317 module_put(mddev->pers->owner);
5318 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005319 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005320}
NeilBrown5eff3c42012-11-19 10:47:48 +11005321
5322void md_stop(struct mddev *mddev)
5323{
5324 /* stop the array and free an attached data structures.
5325 * This is called from dm-raid
5326 */
5327 __md_stop(mddev);
5328 bitmap_destroy(mddev);
5329 if (mddev->bio_set)
5330 bioset_free(mddev->bio_set);
5331}
5332
NeilBrown390ee602010-06-01 19:37:27 +10005333EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005334
NeilBrowna05b7ea2012-07-19 15:59:18 +10005335static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005336{
5337 int err = 0;
5338 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005339 if (atomic_read(&mddev->openers) > !!bdev) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005340 printk("md: %s still in use.\n",mdname(mddev));
5341 err = -EBUSY;
5342 goto out;
5343 }
NeilBrowna05b7ea2012-07-19 15:59:18 +10005344 if (bdev)
5345 sync_blockdev(bdev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005346 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005347 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005348
5349 err = -ENXIO;
5350 if (mddev->ro==1)
5351 goto out;
5352 mddev->ro = 1;
5353 set_disk_ro(mddev->gendisk, 1);
5354 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005355 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005356 err = 0;
5357 }
5358out:
5359 mutex_unlock(&mddev->open_mutex);
5360 return err;
5361}
5362
NeilBrown9e653b62006-06-26 00:27:58 -07005363/* mode:
5364 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005365 * 2 - stop but do not disassemble array
5366 */
NeilBrowna05b7ea2012-07-19 15:59:18 +10005367static int do_md_stop(struct mddev * mddev, int mode,
5368 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005371 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372
NeilBrownc8c00a62009-08-10 12:50:52 +10005373 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005374 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10005375 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005376 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005377 mutex_unlock(&mddev->open_mutex);
5378 return -EBUSY;
5379 }
NeilBrowna05b7ea2012-07-19 15:59:18 +10005380 if (bdev)
5381 /* It is possible IO was issued on some other
5382 * open file which was closed before we took ->open_mutex.
5383 * As that was not the last close __blkdev_put will not
5384 * have called sync_blockdev, so we must.
5385 */
5386 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
NeilBrown6e17b022010-08-07 21:41:19 +10005388 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005389 if (mddev->ro)
5390 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005391
NeilBrowndefad612011-01-14 09:14:33 +11005392 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005393 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005394 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005395 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005396
NeilBrowna4bd82d2010-03-29 13:23:10 +11005397 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005398 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005399
NeilBrowndafb20f2012-03-19 12:46:39 +11005400 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005401 if (rdev->raid_disk >= 0)
5402 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005403
NeilBrowna4bd82d2010-03-29 13:23:10 +11005404 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005405 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005406 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005407 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005408
NeilBrowna4bd82d2010-03-29 13:23:10 +11005409 if (mddev->ro)
5410 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005411 } else
5412 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 /*
5414 * Free resources if final stop
5415 */
NeilBrown9e653b62006-06-26 00:27:58 -07005416 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5418
NeilBrown978f9462006-02-02 14:28:05 -08005419 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005420 if (mddev->bitmap_info.file) {
5421 restore_bitmap_write_access(mddev->bitmap_info.file);
5422 fput(mddev->bitmap_info.file);
5423 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005424 }
NeilBrownc3d97142009-12-14 12:49:52 +11005425 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005426
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 export_array(mddev);
5428
NeilBrown6177b472010-03-29 11:37:13 +11005429 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005430 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005431 if (mddev->hold_active == UNTIL_STOP)
5432 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005433 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005434 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005435 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005436 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438}
5439
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005440#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005441static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442{
NeilBrown3cb03002011-10-11 16:45:26 +11005443 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 int err;
5445
NeilBrowna757e642005-04-16 15:26:42 -07005446 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
5449 printk(KERN_INFO "md: running: ");
5450
NeilBrowndafb20f2012-03-19 12:46:39 +11005451 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 char b[BDEVNAME_SIZE];
5453 printk("<%s>", bdevname(rdev->bdev,b));
5454 }
5455 printk("\n");
5456
NeilBrownd710e132008-10-13 11:55:12 +11005457 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 if (err) {
5459 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005460 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 }
5462}
5463
5464/*
5465 * lets try to run arrays based on all disks that have arrived
5466 * until now. (those are in pending_raid_disks)
5467 *
5468 * the method: pick the first pending disk, collect all disks with
5469 * the same UUID, remove all from the pending list and put them into
5470 * the 'same_array' list. Then order this list based on superblock
5471 * update time (freshest comes first), kick out 'old' disks and
5472 * compare superblocks. If everything's fine then run it.
5473 *
5474 * If "unit" is allocated, then bump its reference count
5475 */
5476static void autorun_devices(int part)
5477{
NeilBrown3cb03002011-10-11 16:45:26 +11005478 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005479 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 char b[BDEVNAME_SIZE];
5481
5482 printk(KERN_INFO "md: autorun ...\n");
5483 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005484 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005486 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005488 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
5490 printk(KERN_INFO "md: considering %s ...\n",
5491 bdevname(rdev0->bdev,b));
5492 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005493 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 if (super_90_load(rdev, rdev0, 0) >= 0) {
5495 printk(KERN_INFO "md: adding %s ...\n",
5496 bdevname(rdev->bdev,b));
5497 list_move(&rdev->same_set, &candidates);
5498 }
5499 /*
5500 * now we have a set of devices, with all of them having
5501 * mostly sane superblocks. It's time to allocate the
5502 * mddev.
5503 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005504 if (part) {
5505 dev = MKDEV(mdp_major,
5506 rdev0->preferred_minor << MdpMinorShift);
5507 unit = MINOR(dev) >> MdpMinorShift;
5508 } else {
5509 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5510 unit = MINOR(dev);
5511 }
5512 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5514 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5515 break;
5516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
5518 md_probe(dev, NULL, NULL);
5519 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005520 if (!mddev || !mddev->gendisk) {
5521 if (mddev)
5522 mddev_put(mddev);
5523 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 "md: cannot allocate memory for md drive.\n");
5525 break;
5526 }
5527 if (mddev_lock(mddev))
5528 printk(KERN_WARNING "md: %s locked, cannot run\n",
5529 mdname(mddev));
5530 else if (mddev->raid_disks || mddev->major_version
5531 || !list_empty(&mddev->disks)) {
5532 printk(KERN_WARNING
5533 "md: %s already running, cannot run %s\n",
5534 mdname(mddev), bdevname(rdev0->bdev,b));
5535 mddev_unlock(mddev);
5536 } else {
5537 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005538 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005539 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 list_del_init(&rdev->same_set);
5541 if (bind_rdev_to_array(rdev, mddev))
5542 export_rdev(rdev);
5543 }
5544 autorun_array(mddev);
5545 mddev_unlock(mddev);
5546 }
5547 /* on success, candidates will be empty, on error
5548 * it won't...
5549 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005550 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005551 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 mddev_put(mddev);
5555 }
5556 printk(KERN_INFO "md: ... autorun DONE.\n");
5557}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005558#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560static int get_version(void __user * arg)
5561{
5562 mdu_version_t ver;
5563
5564 ver.major = MD_MAJOR_VERSION;
5565 ver.minor = MD_MINOR_VERSION;
5566 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5567
5568 if (copy_to_user(arg, &ver, sizeof(ver)))
5569 return -EFAULT;
5570
5571 return 0;
5572}
5573
NeilBrownfd01b882011-10-11 16:47:53 +11005574static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575{
5576 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005577 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005578 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
NeilBrown1ca69c42012-10-11 13:37:33 +11005580 nr = working = insync = failed = spare = 0;
5581 rcu_read_lock();
5582 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005584 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 failed++;
5586 else {
5587 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005588 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005589 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 else
5591 spare++;
5592 }
5593 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005594 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
5596 info.major_version = mddev->major_version;
5597 info.minor_version = mddev->minor_version;
5598 info.patch_version = MD_PATCHLEVEL_VERSION;
5599 info.ctime = mddev->ctime;
5600 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005601 info.size = mddev->dev_sectors / 2;
5602 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005603 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 info.nr_disks = nr;
5605 info.raid_disks = mddev->raid_disks;
5606 info.md_minor = mddev->md_minor;
5607 info.not_persistent= !mddev->persistent;
5608
5609 info.utime = mddev->utime;
5610 info.state = 0;
5611 if (mddev->in_sync)
5612 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005613 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005614 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005615 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 info.working_disks = working;
5617 info.failed_disks = failed;
5618 info.spare_disks = spare;
5619
5620 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005621 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
5623 if (copy_to_user(arg, &info, sizeof(info)))
5624 return -EFAULT;
5625
5626 return 0;
5627}
5628
NeilBrownfd01b882011-10-11 16:47:53 +11005629static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005630{
5631 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5632 char *ptr, *buf = NULL;
5633 int err = -ENOMEM;
5634
Dan Williamsb5470dc2008-06-27 21:44:04 -07005635 if (md_allow_write(mddev))
5636 file = kmalloc(sizeof(*file), GFP_NOIO);
5637 else
5638 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005639
NeilBrown32a76272005-06-21 17:17:14 -07005640 if (!file)
5641 goto out;
5642
5643 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005644 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005645 file->pathname[0] = '\0';
5646 goto copy_out;
5647 }
5648
5649 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5650 if (!buf)
5651 goto out;
5652
NeilBrown1ec885c2012-05-22 13:55:10 +10005653 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5654 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005655 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005656 goto out;
5657
5658 strcpy(file->pathname, ptr);
5659
5660copy_out:
5661 err = 0;
5662 if (copy_to_user(arg, file, sizeof(*file)))
5663 err = -EFAULT;
5664out:
5665 kfree(buf);
5666 kfree(file);
5667 return err;
5668}
5669
NeilBrownfd01b882011-10-11 16:47:53 +11005670static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671{
5672 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005673 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
5675 if (copy_from_user(&info, arg, sizeof(info)))
5676 return -EFAULT;
5677
NeilBrown1ca69c42012-10-11 13:37:33 +11005678 rcu_read_lock();
5679 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 if (rdev) {
5681 info.major = MAJOR(rdev->bdev->bd_dev);
5682 info.minor = MINOR(rdev->bdev->bd_dev);
5683 info.raid_disk = rdev->raid_disk;
5684 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005685 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005687 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 info.state |= (1<<MD_DISK_ACTIVE);
5689 info.state |= (1<<MD_DISK_SYNC);
5690 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005691 if (test_bit(WriteMostly, &rdev->flags))
5692 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 } else {
5694 info.major = info.minor = 0;
5695 info.raid_disk = -1;
5696 info.state = (1<<MD_DISK_REMOVED);
5697 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005698 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699
5700 if (copy_to_user(arg, &info, sizeof(info)))
5701 return -EFAULT;
5702
5703 return 0;
5704}
5705
NeilBrownfd01b882011-10-11 16:47:53 +11005706static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707{
5708 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005709 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 dev_t dev = MKDEV(info->major,info->minor);
5711
5712 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5713 return -EOVERFLOW;
5714
5715 if (!mddev->raid_disks) {
5716 int err;
5717 /* expecting a device which has a superblock */
5718 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5719 if (IS_ERR(rdev)) {
5720 printk(KERN_WARNING
5721 "md: md_import_device returned %ld\n",
5722 PTR_ERR(rdev));
5723 return PTR_ERR(rdev);
5724 }
5725 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005726 struct md_rdev *rdev0
5727 = list_entry(mddev->disks.next,
5728 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005729 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 .load_super(rdev, rdev0, mddev->minor_version);
5731 if (err < 0) {
5732 printk(KERN_WARNING
5733 "md: %s has different UUID to %s\n",
5734 bdevname(rdev->bdev,b),
5735 bdevname(rdev0->bdev,b2));
5736 export_rdev(rdev);
5737 return -EINVAL;
5738 }
5739 }
5740 err = bind_rdev_to_array(rdev, mddev);
5741 if (err)
5742 export_rdev(rdev);
5743 return err;
5744 }
5745
5746 /*
5747 * add_new_disk can be used once the array is assembled
5748 * to add "hot spares". They must already have a superblock
5749 * written
5750 */
5751 if (mddev->pers) {
5752 int err;
5753 if (!mddev->pers->hot_add_disk) {
5754 printk(KERN_WARNING
5755 "%s: personality does not support diskops!\n",
5756 mdname(mddev));
5757 return -EINVAL;
5758 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005759 if (mddev->persistent)
5760 rdev = md_import_device(dev, mddev->major_version,
5761 mddev->minor_version);
5762 else
5763 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 if (IS_ERR(rdev)) {
5765 printk(KERN_WARNING
5766 "md: md_import_device returned %ld\n",
5767 PTR_ERR(rdev));
5768 return PTR_ERR(rdev);
5769 }
NeilBrown1a855a02010-12-09 16:36:28 +11005770 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005771 if (!mddev->persistent) {
5772 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005773 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005774 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005775 set_bit(In_sync, &rdev->flags);
5776 } else
NeilBrown41158c72005-06-21 17:17:25 -07005777 rdev->raid_disk = -1;
5778 } else
5779 super_types[mddev->major_version].
5780 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005781 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005782 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005783 /* This was a hot-add request, but events doesn't
5784 * match, so reject it.
5785 */
5786 export_rdev(rdev);
5787 return -EINVAL;
5788 }
5789
NeilBrown1a855a02010-12-09 16:36:28 +11005790 if (test_bit(In_sync, &rdev->flags))
5791 rdev->saved_raid_disk = rdev->raid_disk;
5792 else
5793 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005794
NeilBrownb2d444d2005-11-08 21:39:31 -08005795 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005796 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5797 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005798 else
5799 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005800
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 rdev->raid_disk = -1;
5802 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005803 if (!err && !mddev->pers->hot_remove_disk) {
5804 /* If there is hot_add_disk but no hot_remove_disk
5805 * then added disks for geometry changes,
5806 * and should be added immediately.
5807 */
5808 super_types[mddev->major_version].
5809 validate_super(mddev, rdev);
5810 err = mddev->pers->hot_add_disk(mddev, rdev);
5811 if (err)
5812 unbind_rdev_from_array(rdev);
5813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 if (err)
5815 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005816 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005817 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005818
NeilBrown7ceb17e2013-04-24 11:42:42 +10005819 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005820 if (mddev->degraded)
5821 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005822 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005823 if (!err)
5824 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005825 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 return err;
5827 }
5828
5829 /* otherwise, add_new_disk is only allowed
5830 * for major_version==0 superblocks
5831 */
5832 if (mddev->major_version != 0) {
5833 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5834 mdname(mddev));
5835 return -EINVAL;
5836 }
5837
5838 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5839 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005840 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 if (IS_ERR(rdev)) {
5842 printk(KERN_WARNING
5843 "md: error, md_import_device() returned %ld\n",
5844 PTR_ERR(rdev));
5845 return PTR_ERR(rdev);
5846 }
5847 rdev->desc_nr = info->number;
5848 if (info->raid_disk < mddev->raid_disks)
5849 rdev->raid_disk = info->raid_disk;
5850 else
5851 rdev->raid_disk = -1;
5852
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005854 if (info->state & (1<<MD_DISK_SYNC))
5855 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005857 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5858 set_bit(WriteMostly, &rdev->flags);
5859
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 if (!mddev->persistent) {
5861 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005862 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5863 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005864 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005865 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
NeilBrown2bf071b2006-01-06 00:20:55 -08005867 err = bind_rdev_to_array(rdev, mddev);
5868 if (err) {
5869 export_rdev(rdev);
5870 return err;
5871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 }
5873
5874 return 0;
5875}
5876
NeilBrownfd01b882011-10-11 16:47:53 +11005877static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878{
5879 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005880 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 rdev = find_rdev(mddev, dev);
5883 if (!rdev)
5884 return -ENXIO;
5885
NeilBrown3ea8929d2013-04-24 11:42:41 +10005886 clear_bit(Blocked, &rdev->flags);
5887 remove_and_add_spares(mddev, rdev);
5888
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 if (rdev->raid_disk >= 0)
5890 goto busy;
5891
5892 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005893 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005894 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
5896 return 0;
5897busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005898 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 bdevname(rdev->bdev,b), mdname(mddev));
5900 return -EBUSY;
5901}
5902
NeilBrownfd01b882011-10-11 16:47:53 +11005903static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904{
5905 char b[BDEVNAME_SIZE];
5906 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005907 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
5909 if (!mddev->pers)
5910 return -ENODEV;
5911
5912 if (mddev->major_version != 0) {
5913 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5914 " version-0 superblocks.\n",
5915 mdname(mddev));
5916 return -EINVAL;
5917 }
5918 if (!mddev->pers->hot_add_disk) {
5919 printk(KERN_WARNING
5920 "%s: personality does not support diskops!\n",
5921 mdname(mddev));
5922 return -EINVAL;
5923 }
5924
NeilBrownd710e132008-10-13 11:55:12 +11005925 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 if (IS_ERR(rdev)) {
5927 printk(KERN_WARNING
5928 "md: error, md_import_device() returned %ld\n",
5929 PTR_ERR(rdev));
5930 return -EINVAL;
5931 }
5932
5933 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005934 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005936 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
NeilBrown8190e752009-06-18 08:48:58 +10005938 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
NeilBrownb2d444d2005-11-08 21:39:31 -08005940 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 printk(KERN_WARNING
5942 "md: can not hot-add faulty %s disk to %s!\n",
5943 bdevname(rdev->bdev,b), mdname(mddev));
5944 err = -EINVAL;
5945 goto abort_export;
5946 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005947 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005949 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005950 err = bind_rdev_to_array(rdev, mddev);
5951 if (err)
5952 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
5954 /*
5955 * The rest should better be atomic, we can have disk failures
5956 * noticed in interrupt contexts ...
5957 */
5958
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 rdev->raid_disk = -1;
5960
NeilBrown850b2b422006-10-03 01:15:46 -07005961 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962
5963 /*
5964 * Kick recovery, maybe this spare has to be added to the
5965 * array immediately.
5966 */
5967 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5968 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005969 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 return 0;
5971
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972abort_export:
5973 export_rdev(rdev);
5974 return err;
5975}
5976
NeilBrownfd01b882011-10-11 16:47:53 +11005977static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005978{
5979 int err;
5980
NeilBrown36fa3062005-09-09 16:23:45 -07005981 if (mddev->pers) {
5982 if (!mddev->pers->quiesce)
5983 return -EBUSY;
5984 if (mddev->recovery || mddev->sync_thread)
5985 return -EBUSY;
5986 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005987 }
5988
NeilBrown36fa3062005-09-09 16:23:45 -07005989
5990 if (fd >= 0) {
5991 if (mddev->bitmap)
5992 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005993 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005994
NeilBrownc3d97142009-12-14 12:49:52 +11005995 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005996 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5997 mdname(mddev));
5998 return -EBADF;
5999 }
6000
NeilBrownc3d97142009-12-14 12:49:52 +11006001 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07006002 if (err) {
6003 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6004 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11006005 fput(mddev->bitmap_info.file);
6006 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006007 return err;
6008 }
NeilBrownc3d97142009-12-14 12:49:52 +11006009 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006010 } else if (mddev->bitmap == NULL)
6011 return -ENOENT; /* cannot remove what isn't there */
6012 err = 0;
6013 if (mddev->pers) {
6014 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006015 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07006016 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006017 if (!err)
6018 err = bitmap_load(mddev);
6019 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006020 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006021 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006022 fd = -1; /* make sure to put the file */
6023 }
NeilBrown36fa3062005-09-09 16:23:45 -07006024 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006025 }
6026 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11006027 if (mddev->bitmap_info.file) {
6028 restore_bitmap_write_access(mddev->bitmap_info.file);
6029 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07006030 }
NeilBrownc3d97142009-12-14 12:49:52 +11006031 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006032 }
6033
NeilBrown32a76272005-06-21 17:17:14 -07006034 return err;
6035}
6036
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037/*
6038 * set_array_info is used two different ways
6039 * The original usage is when creating a new array.
6040 * In this usage, raid_disks is > 0 and it together with
6041 * level, size, not_persistent,layout,chunksize determine the
6042 * shape of the array.
6043 * This will always create an array with a type-0.90.0 superblock.
6044 * The newer usage is when assembling an array.
6045 * In this case raid_disks will be 0, and the major_version field is
6046 * use to determine which style super-blocks are to be found on the devices.
6047 * The minor and patch _version numbers are also kept incase the
6048 * super_block handler wishes to interpret them.
6049 */
NeilBrownfd01b882011-10-11 16:47:53 +11006050static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051{
6052
6053 if (info->raid_disks == 0) {
6054 /* just setting version number for superblock loading */
6055 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006056 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 super_types[info->major_version].name == NULL) {
6058 /* maybe try to auto-load a module? */
6059 printk(KERN_INFO
6060 "md: superblock version %d not known\n",
6061 info->major_version);
6062 return -EINVAL;
6063 }
6064 mddev->major_version = info->major_version;
6065 mddev->minor_version = info->minor_version;
6066 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006067 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006068 /* ensure mddev_put doesn't delete this now that there
6069 * is some minimal configuration.
6070 */
6071 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 return 0;
6073 }
6074 mddev->major_version = MD_MAJOR_VERSION;
6075 mddev->minor_version = MD_MINOR_VERSION;
6076 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6077 mddev->ctime = get_seconds();
6078
6079 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006080 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006081 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 mddev->raid_disks = info->raid_disks;
6083 /* don't set md_minor, it is determined by which /dev/md* was
6084 * openned
6085 */
6086 if (info->state & (1<<MD_SB_CLEAN))
6087 mddev->recovery_cp = MaxSector;
6088 else
6089 mddev->recovery_cp = 0;
6090 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006091 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
6093 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006094 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
6096 mddev->max_disks = MD_SB_DISKS;
6097
NeilBrowne6910632008-02-06 01:39:51 -08006098 if (mddev->persistent)
6099 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006100 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
NeilBrownc3d97142009-12-14 12:49:52 +11006102 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006103 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006104 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006105
NeilBrownf6705572006-03-27 01:18:11 -08006106 mddev->reshape_position = MaxSector;
6107
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 /*
6109 * Generate a 128 bit UUID
6110 */
6111 get_random_bytes(mddev->uuid, 16);
6112
NeilBrownf6705572006-03-27 01:18:11 -08006113 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006114 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006115 mddev->new_layout = mddev->layout;
6116 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006117 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006118
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 return 0;
6120}
6121
NeilBrownfd01b882011-10-11 16:47:53 +11006122void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006123{
Dan Williamsb522adc2009-03-31 15:00:31 +11006124 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6125
6126 if (mddev->external_size)
6127 return;
6128
Dan Williams1f403622009-03-31 14:59:03 +11006129 mddev->array_sectors = array_sectors;
6130}
6131EXPORT_SYMBOL(md_set_array_sectors);
6132
NeilBrownfd01b882011-10-11 16:47:53 +11006133static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006134{
NeilBrown3cb03002011-10-11 16:45:26 +11006135 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006136 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006137 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006138
6139 if (mddev->pers->resize == NULL)
6140 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006141 /* The "num_sectors" is the number of sectors of each device that
6142 * is used. This can only make sense for arrays with redundancy.
6143 * linear and raid0 always use whatever space is available. We can only
6144 * consider changing this number if no resync or reconstruction is
6145 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006146 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006147 * of each device. If num_sectors is zero, we find the largest size
6148 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006149 */
6150 if (mddev->sync_thread)
6151 return -EBUSY;
NeilBrowna4a61252012-05-22 13:55:27 +10006152
NeilBrowndafb20f2012-03-19 12:46:39 +11006153 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006154 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006155
Andre Nolld71f9f82008-07-11 22:02:22 +10006156 if (fit && (num_sectors == 0 || num_sectors > avail))
6157 num_sectors = avail;
6158 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006159 return -ENOSPC;
6160 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006161 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006162 if (!rv)
6163 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006164 return rv;
6165}
6166
NeilBrownfd01b882011-10-11 16:47:53 +11006167static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006168{
6169 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006170 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006171 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006172 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006173 return -EINVAL;
6174 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006175 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006176 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006177 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006178 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006179
6180 rdev_for_each(rdev, mddev) {
6181 if (mddev->raid_disks < raid_disks &&
6182 rdev->data_offset < rdev->new_data_offset)
6183 return -EINVAL;
6184 if (mddev->raid_disks > raid_disks &&
6185 rdev->data_offset > rdev->new_data_offset)
6186 return -EINVAL;
6187 }
6188
NeilBrown63c70c42006-03-27 01:18:13 -08006189 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006190 if (mddev->delta_disks < 0)
6191 mddev->reshape_backwards = 1;
6192 else if (mddev->delta_disks > 0)
6193 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006194
6195 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006196 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006197 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006198 mddev->reshape_backwards = 0;
6199 }
NeilBrownda943b992006-01-06 00:20:54 -08006200 return rv;
6201}
6202
6203
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204/*
6205 * update_array_info is used to change the configuration of an
6206 * on-line array.
6207 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6208 * fields in the info are checked against the array.
6209 * Any differences that cannot be handled will cause an error.
6210 * Normally, only one change can be managed at a time.
6211 */
NeilBrownfd01b882011-10-11 16:47:53 +11006212static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213{
6214 int rv = 0;
6215 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006216 int state = 0;
6217
6218 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006219 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006220 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
6222 if (mddev->major_version != info->major_version ||
6223 mddev->minor_version != info->minor_version ||
6224/* mddev->patch_version != info->patch_version || */
6225 mddev->ctime != info->ctime ||
6226 mddev->level != info->level ||
6227/* mddev->layout != info->layout || */
6228 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006229 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006230 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6231 ((state^info->state) & 0xfffffe00)
6232 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 return -EINVAL;
6234 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006235 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6236 cnt++;
6237 if (mddev->raid_disks != info->raid_disks)
6238 cnt++;
6239 if (mddev->layout != info->layout)
6240 cnt++;
6241 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6242 cnt++;
6243 if (cnt == 0)
6244 return 0;
6245 if (cnt > 1)
6246 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247
6248 if (mddev->layout != info->layout) {
6249 /* Change layout
6250 * we don't need to do anything at the md level, the
6251 * personality will take care of it all.
6252 */
NeilBrown50ac1682009-06-18 08:47:55 +10006253 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006255 else {
6256 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006257 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006258 if (rv)
6259 mddev->new_layout = mddev->layout;
6260 return rv;
6261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006263 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006264 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
NeilBrownda943b992006-01-06 00:20:54 -08006266 if (mddev->raid_disks != info->raid_disks)
6267 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268
NeilBrown36fa3062005-09-09 16:23:45 -07006269 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6270 if (mddev->pers->quiesce == NULL)
6271 return -EINVAL;
6272 if (mddev->recovery || mddev->sync_thread)
6273 return -EBUSY;
6274 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6275 /* add the bitmap */
6276 if (mddev->bitmap)
6277 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006278 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006279 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006280 mddev->bitmap_info.offset =
6281 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006282 mddev->bitmap_info.space =
6283 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006284 mddev->pers->quiesce(mddev, 1);
6285 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006286 if (!rv)
6287 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006288 if (rv)
6289 bitmap_destroy(mddev);
6290 mddev->pers->quiesce(mddev, 0);
6291 } else {
6292 /* remove the bitmap */
6293 if (!mddev->bitmap)
6294 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006295 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006296 return -EINVAL;
6297 mddev->pers->quiesce(mddev, 1);
6298 bitmap_destroy(mddev);
6299 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006300 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006301 }
6302 }
NeilBrown850b2b422006-10-03 01:15:46 -07006303 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 return rv;
6305}
6306
NeilBrownfd01b882011-10-11 16:47:53 +11006307static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308{
NeilBrown3cb03002011-10-11 16:45:26 +11006309 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006310 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311
6312 if (mddev->pers == NULL)
6313 return -ENODEV;
6314
NeilBrown1ca69c42012-10-11 13:37:33 +11006315 rcu_read_lock();
6316 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006318 err = -ENODEV;
6319 else {
6320 md_error(mddev, rdev);
6321 if (!test_bit(Faulty, &rdev->flags))
6322 err = -EBUSY;
6323 }
6324 rcu_read_unlock();
6325 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326}
6327
Andre Noll2f9618c2008-04-25 18:57:58 +02006328/*
6329 * We have a problem here : there is no easy way to give a CHS
6330 * virtual geometry. We currently pretend that we have a 2 heads
6331 * 4 sectors (with a BIG number of cylinders...). This drives
6332 * dosfs just mad... ;-)
6333 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006334static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6335{
NeilBrownfd01b882011-10-11 16:47:53 +11006336 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006337
6338 geo->heads = 2;
6339 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006340 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006341 return 0;
6342}
6343
Al Viroa39907f2008-03-02 10:31:15 -05006344static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 unsigned int cmd, unsigned long arg)
6346{
6347 int err = 0;
6348 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006349 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006350 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351
NeilBrown506c9e42011-12-23 10:17:26 +11006352 switch (cmd) {
6353 case RAID_VERSION:
6354 case GET_ARRAY_INFO:
6355 case GET_DISK_INFO:
6356 break;
6357 default:
6358 if (!capable(CAP_SYS_ADMIN))
6359 return -EACCES;
6360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361
6362 /*
6363 * Commands dealing with the RAID driver but not any
6364 * particular array:
6365 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006366 switch (cmd) {
6367 case RAID_VERSION:
6368 err = get_version(argp);
6369 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370
NeilBrownc02c0ae2012-12-11 13:39:21 +11006371 case PRINT_RAID_DEBUG:
6372 err = 0;
6373 md_print_devices();
6374 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375
6376#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006377 case RAID_AUTORUN:
6378 err = 0;
6379 autostart_arrays(arg);
6380 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006382 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 }
6384
6385 /*
6386 * Commands creating/starting a new array:
6387 */
6388
Al Viroa39907f2008-03-02 10:31:15 -05006389 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
6391 if (!mddev) {
6392 BUG();
6393 goto abort;
6394 }
6395
NeilBrown1ca69c42012-10-11 13:37:33 +11006396 /* Some actions do not requires the mutex */
6397 switch (cmd) {
6398 case GET_ARRAY_INFO:
6399 if (!mddev->raid_disks && !mddev->external)
6400 err = -ENODEV;
6401 else
6402 err = get_array_info(mddev, argp);
6403 goto abort;
6404
6405 case GET_DISK_INFO:
6406 if (!mddev->raid_disks && !mddev->external)
6407 err = -ENODEV;
6408 else
6409 err = get_disk_info(mddev, argp);
6410 goto abort;
6411
6412 case SET_DISK_FAULTY:
6413 err = set_disk_faulty(mddev, new_decode_dev(arg));
6414 goto abort;
6415 }
6416
NeilBrowna7a3f082012-12-11 13:35:54 +11006417 if (cmd == ADD_NEW_DISK)
6418 /* need to ensure md_delayed_delete() has completed */
6419 flush_workqueue(md_misc_wq);
6420
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 err = mddev_lock(mddev);
6422 if (err) {
6423 printk(KERN_INFO
6424 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6425 err, cmd);
6426 goto abort;
6427 }
6428
NeilBrownc02c0ae2012-12-11 13:39:21 +11006429 if (cmd == SET_ARRAY_INFO) {
6430 mdu_array_info_t info;
6431 if (!arg)
6432 memset(&info, 0, sizeof(info));
6433 else if (copy_from_user(&info, argp, sizeof(info))) {
6434 err = -EFAULT;
6435 goto abort_unlock;
6436 }
6437 if (mddev->pers) {
6438 err = update_array_info(mddev, &info);
6439 if (err) {
6440 printk(KERN_WARNING "md: couldn't update"
6441 " array info. %d\n", err);
6442 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 }
6444 goto done_unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006445 }
6446 if (!list_empty(&mddev->disks)) {
6447 printk(KERN_WARNING
6448 "md: array %s already has disks!\n",
6449 mdname(mddev));
6450 err = -EBUSY;
6451 goto abort_unlock;
6452 }
6453 if (mddev->raid_disks) {
6454 printk(KERN_WARNING
6455 "md: array %s already initialised!\n",
6456 mdname(mddev));
6457 err = -EBUSY;
6458 goto abort_unlock;
6459 }
6460 err = set_array_info(mddev, &info);
6461 if (err) {
6462 printk(KERN_WARNING "md: couldn't set"
6463 " array info. %d\n", err);
6464 goto abort_unlock;
6465 }
6466 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 }
6468
6469 /*
6470 * Commands querying/configuring an existing array:
6471 */
NeilBrown32a76272005-06-21 17:17:14 -07006472 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006473 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006474 if ((!mddev->raid_disks && !mddev->external)
6475 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6476 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6477 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 err = -ENODEV;
6479 goto abort_unlock;
6480 }
6481
6482 /*
6483 * Commands even a read-only array can execute:
6484 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006485 switch (cmd) {
6486 case GET_BITMAP_FILE:
6487 err = get_bitmap_file(mddev, argp);
6488 goto done_unlock;
6489
6490 case RESTART_ARRAY_RW:
6491 err = restart_array(mddev);
6492 goto done_unlock;
6493
6494 case STOP_ARRAY:
6495 err = do_md_stop(mddev, 0, bdev);
6496 goto done_unlock;
6497
6498 case STOP_ARRAY_RO:
6499 err = md_set_readonly(mddev, bdev);
6500 goto done_unlock;
6501
NeilBrown3ea8929d2013-04-24 11:42:41 +10006502 case HOT_REMOVE_DISK:
6503 err = hot_remove_disk(mddev, new_decode_dev(arg));
6504 goto done_unlock;
6505
NeilBrown7ceb17e2013-04-24 11:42:42 +10006506 case ADD_NEW_DISK:
6507 /* We can support ADD_NEW_DISK on read-only arrays
6508 * on if we are re-adding a preexisting device.
6509 * So require mddev->pers and MD_DISK_SYNC.
6510 */
6511 if (mddev->pers) {
6512 mdu_disk_info_t info;
6513 if (copy_from_user(&info, argp, sizeof(info)))
6514 err = -EFAULT;
6515 else if (!(info.state & (1<<MD_DISK_SYNC)))
6516 /* Need to clear read-only for this */
6517 break;
6518 else
6519 err = add_new_disk(mddev, &info);
6520 goto done_unlock;
6521 }
6522 break;
6523
NeilBrownc02c0ae2012-12-11 13:39:21 +11006524 case BLKROSET:
6525 if (get_user(ro, (int __user *)(arg))) {
6526 err = -EFAULT;
6527 goto done_unlock;
6528 }
6529 err = -EINVAL;
6530
6531 /* if the bdev is going readonly the value of mddev->ro
6532 * does not matter, no writes are coming
6533 */
6534 if (ro)
NeilBrown32a76272005-06-21 17:17:14 -07006535 goto done_unlock;
6536
NeilBrownc02c0ae2012-12-11 13:39:21 +11006537 /* are we are already prepared for writes? */
6538 if (mddev->ro != 1)
6539 goto done_unlock;
6540
6541 /* transitioning to readauto need only happen for
6542 * arrays that call md_write_start
6543 */
6544 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006546 if (err == 0) {
6547 mddev->ro = 2;
6548 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006549 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006550 }
6551 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 }
6553
6554 /*
6555 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006556 * superblock, so we do not allow them on read-only arrays.
6557 * However non-MD ioctls (e.g. get-size) will still come through
6558 * here and hit the 'default' below, so only disallow
6559 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006561 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006562 if (mddev->ro == 2) {
6563 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006564 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006565 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006566 /* mddev_unlock will wake thread */
6567 /* If a device failed while we were read-only, we
6568 * need to make sure the metadata is updated now.
6569 */
6570 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6571 mddev_unlock(mddev);
6572 wait_event(mddev->sb_wait,
6573 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6574 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6575 mddev_lock(mddev);
6576 }
NeilBrownf91de922005-11-08 21:39:36 -08006577 } else {
6578 err = -EROFS;
6579 goto abort_unlock;
6580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 }
6582
NeilBrownc02c0ae2012-12-11 13:39:21 +11006583 switch (cmd) {
6584 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006586 mdu_disk_info_t info;
6587 if (copy_from_user(&info, argp, sizeof(info)))
6588 err = -EFAULT;
6589 else
6590 err = add_new_disk(mddev, &info);
6591 goto done_unlock;
6592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593
NeilBrownc02c0ae2012-12-11 13:39:21 +11006594 case HOT_ADD_DISK:
6595 err = hot_add_disk(mddev, new_decode_dev(arg));
6596 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597
NeilBrownc02c0ae2012-12-11 13:39:21 +11006598 case RUN_ARRAY:
6599 err = do_md_run(mddev);
6600 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601
NeilBrownc02c0ae2012-12-11 13:39:21 +11006602 case SET_BITMAP_FILE:
6603 err = set_bitmap_file(mddev, (int)arg);
6604 goto done_unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006605
NeilBrownc02c0ae2012-12-11 13:39:21 +11006606 default:
6607 err = -EINVAL;
6608 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 }
6610
6611done_unlock:
6612abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006613 if (mddev->hold_active == UNTIL_IOCTL &&
6614 err != -EINVAL)
6615 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 mddev_unlock(mddev);
6617
6618 return err;
6619done:
6620 if (err)
6621 MD_BUG();
6622abort:
6623 return err;
6624}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006625#ifdef CONFIG_COMPAT
6626static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6627 unsigned int cmd, unsigned long arg)
6628{
6629 switch (cmd) {
6630 case HOT_REMOVE_DISK:
6631 case HOT_ADD_DISK:
6632 case SET_DISK_FAULTY:
6633 case SET_BITMAP_FILE:
6634 /* These take in integer arg, do not convert */
6635 break;
6636 default:
6637 arg = (unsigned long)compat_ptr(arg);
6638 break;
6639 }
6640
6641 return md_ioctl(bdev, mode, cmd, arg);
6642}
6643#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644
Al Viroa39907f2008-03-02 10:31:15 -05006645static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646{
6647 /*
6648 * Succeed if we can lock the mddev, which confirms that
6649 * it isn't being stopped right now.
6650 */
NeilBrownfd01b882011-10-11 16:47:53 +11006651 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 int err;
6653
Yuanhan Liu0c098222012-05-22 13:55:32 +10006654 if (!mddev)
6655 return -ENODEV;
6656
NeilBrownd3374822009-01-09 08:31:10 +11006657 if (mddev->gendisk != bdev->bd_disk) {
6658 /* we are racing with mddev_put which is discarding this
6659 * bd_disk.
6660 */
6661 mddev_put(mddev);
6662 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006663 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006664 /* Then retry the open from the top */
6665 return -ERESTARTSYS;
6666 }
6667 BUG_ON(mddev != bdev->bd_disk->private_data);
6668
NeilBrownc8c00a62009-08-10 12:50:52 +10006669 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 goto out;
6671
6672 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006673 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006674 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006676 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 out:
6678 return err;
6679}
6680
Al Viroa39907f2008-03-02 10:31:15 -05006681static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682{
NeilBrownfd01b882011-10-11 16:47:53 +11006683 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006685 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006686 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 mddev_put(mddev);
6688
6689 return 0;
6690}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006691
6692static int md_media_changed(struct gendisk *disk)
6693{
NeilBrownfd01b882011-10-11 16:47:53 +11006694 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006695
6696 return mddev->changed;
6697}
6698
6699static int md_revalidate(struct gendisk *disk)
6700{
NeilBrownfd01b882011-10-11 16:47:53 +11006701 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006702
6703 mddev->changed = 0;
6704 return 0;
6705}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006706static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707{
6708 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006709 .open = md_open,
6710 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006711 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006712#ifdef CONFIG_COMPAT
6713 .compat_ioctl = md_compat_ioctl,
6714#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006715 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006716 .media_changed = md_media_changed,
6717 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718};
6719
Adrian Bunk75c96f82005-05-05 16:16:09 -07006720static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721{
NeilBrown2b8bf342011-10-11 16:48:23 +11006722 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 /*
6725 * md_thread is a 'system-thread', it's priority should be very
6726 * high. We avoid resource deadlocks individually in each
6727 * raid personality. (RAID5 does preallocation) We also use RR and
6728 * the very same RT priority as kswapd, thus we will never get
6729 * into a priority inversion deadlock.
6730 *
6731 * we definitely have to have equal or higher priority than
6732 * bdflush, otherwise bdflush will deadlock if there are too
6733 * many dirty RAID5 blocks.
6734 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735
NeilBrown6985c432005-10-19 21:23:47 -07006736 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006737 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
NeilBrown93588e22005-11-15 00:09:12 -08006739 /* We need to wait INTERRUPTIBLE so that
6740 * we don't add to the load-average.
6741 * That means we need to be sure no signals are
6742 * pending
6743 */
6744 if (signal_pending(current))
6745 flush_signals(current);
6746
6747 wait_event_interruptible_timeout
6748 (thread->wqueue,
6749 test_bit(THREAD_WAKEUP, &thread->flags)
6750 || kthread_should_stop(),
6751 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752
NeilBrown6c987912011-01-14 09:13:53 +11006753 clear_bit(THREAD_WAKEUP, &thread->flags);
6754 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006755 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006757
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 return 0;
6759}
6760
NeilBrown2b8bf342011-10-11 16:48:23 +11006761void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762{
6763 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006764 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 set_bit(THREAD_WAKEUP, &thread->flags);
6766 wake_up(&thread->wqueue);
6767 }
6768}
6769
Shaohua Li4ed87312012-10-11 13:34:00 +11006770struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6771 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772{
NeilBrown2b8bf342011-10-11 16:48:23 +11006773 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774
NeilBrown2b8bf342011-10-11 16:48:23 +11006775 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 if (!thread)
6777 return NULL;
6778
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 init_waitqueue_head(&thread->wqueue);
6780
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 thread->run = run;
6782 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006783 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006784 thread->tsk = kthread_run(md_thread, thread,
6785 "%s_%s",
6786 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006787 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006788 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 kfree(thread);
6790 return NULL;
6791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 return thread;
6793}
6794
NeilBrown2b8bf342011-10-11 16:48:23 +11006795void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796{
NeilBrown2b8bf342011-10-11 16:48:23 +11006797 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006798 if (!thread)
6799 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006800 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006801 /* Locking ensures that mddev_unlock does not wake_up a
6802 * non-existent thread
6803 */
6804 spin_lock(&pers_lock);
6805 *threadp = NULL;
6806 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006807
6808 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 kfree(thread);
6810}
6811
NeilBrownfd01b882011-10-11 16:47:53 +11006812void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813{
6814 if (!mddev) {
6815 MD_BUG();
6816 return;
6817 }
6818
NeilBrownb2d444d2005-11-08 21:39:31 -08006819 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006821
NeilBrownde393cd2011-07-28 11:31:48 +10006822 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 return;
6824 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006825 if (mddev->degraded)
6826 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006827 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6829 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6830 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006831 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006832 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006833 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834}
6835
6836/* seq_file implementation /proc/mdstat */
6837
6838static void status_unused(struct seq_file *seq)
6839{
6840 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006841 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842
6843 seq_printf(seq, "unused devices: ");
6844
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006845 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 char b[BDEVNAME_SIZE];
6847 i++;
6848 seq_printf(seq, "%s ",
6849 bdevname(rdev->bdev,b));
6850 }
6851 if (!i)
6852 seq_printf(seq, "<none>");
6853
6854 seq_printf(seq, "\n");
6855}
6856
6857
NeilBrownfd01b882011-10-11 16:47:53 +11006858static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859{
NeilBrowndd71cf62009-05-07 12:49:35 +10006860 sector_t max_sectors, resync, res;
6861 unsigned long dt, db;
6862 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006863 int scale;
6864 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865
NeilBrown72f36d52012-10-11 14:25:57 +11006866 if (mddev->curr_resync <= 3)
6867 resync = 0;
6868 else
6869 resync = mddev->curr_resync
6870 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871
NeilBrownc804cde2012-05-21 09:28:33 +10006872 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6873 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006874 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006876 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
6878 /*
6879 * Should not happen.
6880 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006881 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 MD_BUG();
6883 return;
6884 }
NeilBrown4588b422006-03-27 01:18:04 -08006885 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006886 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006887 * u32, as those are the requirements for sector_div.
6888 * Thus 'scale' must be at least 10
6889 */
6890 scale = 10;
6891 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006892 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006893 scale++;
6894 }
6895 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006896 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006897
6898 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 {
NeilBrown4588b422006-03-27 01:18:04 -08006900 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 seq_printf(seq, "[");
6902 for (i = 0; i < x; i++)
6903 seq_printf(seq, "=");
6904 seq_printf(seq, ">");
6905 for (i = 0; i < y; i++)
6906 seq_printf(seq, ".");
6907 seq_printf(seq, "] ");
6908 }
NeilBrown4588b422006-03-27 01:18:04 -08006909 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006910 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6911 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006912 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6913 "check" :
6914 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6915 "resync" : "recovery"))),
6916 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006917 (unsigned long long) resync/2,
6918 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919
6920 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 * dt: time from mark until now
6922 * db: blocks written from mark until now
6923 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006924 *
6925 * rt is a sector_t, so could be 32bit or 64bit.
6926 * So we divide before multiply in case it is 32bit and close
6927 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006928 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006929 * near the end of resync when the number of remaining sectors
6930 * is close to 'db'.
6931 * We then divide rt by 32 after multiplying by db to compensate.
6932 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 */
6934 dt = ((jiffies - mddev->resync_mark) / HZ);
6935 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006936 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6937 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938
NeilBrowndd71cf62009-05-07 12:49:35 +10006939 rt = max_sectors - resync; /* number of remaining sectors */
6940 sector_div(rt, db/32+1);
6941 rt *= dt;
6942 rt >>= 5;
6943
6944 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6945 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946
NeilBrownff4e8d92006-07-10 04:44:16 -07006947 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948}
6949
6950static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6951{
6952 struct list_head *tmp;
6953 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006954 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955
6956 if (l >= 0x10000)
6957 return NULL;
6958 if (!l--)
6959 /* header */
6960 return (void*)1;
6961
6962 spin_lock(&all_mddevs_lock);
6963 list_for_each(tmp,&all_mddevs)
6964 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006965 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 mddev_get(mddev);
6967 spin_unlock(&all_mddevs_lock);
6968 return mddev;
6969 }
6970 spin_unlock(&all_mddevs_lock);
6971 if (!l--)
6972 return (void*)2;/* tail */
6973 return NULL;
6974}
6975
6976static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6977{
6978 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006979 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980
6981 ++*pos;
6982 if (v == (void*)2)
6983 return NULL;
6984
6985 spin_lock(&all_mddevs_lock);
6986 if (v == (void*)1)
6987 tmp = all_mddevs.next;
6988 else
6989 tmp = mddev->all_mddevs.next;
6990 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006991 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 else {
6993 next_mddev = (void*)2;
6994 *pos = 0x10000;
6995 }
6996 spin_unlock(&all_mddevs_lock);
6997
6998 if (v != (void*)1)
6999 mddev_put(mddev);
7000 return next_mddev;
7001
7002}
7003
7004static void md_seq_stop(struct seq_file *seq, void *v)
7005{
NeilBrownfd01b882011-10-11 16:47:53 +11007006 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007
7008 if (mddev && v != (void*)1 && v != (void*)2)
7009 mddev_put(mddev);
7010}
7011
7012static int md_seq_show(struct seq_file *seq, void *v)
7013{
NeilBrownfd01b882011-10-11 16:47:53 +11007014 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007015 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007016 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017
7018 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007019 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020 seq_printf(seq, "Personalities : ");
7021 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007022 list_for_each_entry(pers, &pers_list, list)
7023 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024
7025 spin_unlock(&pers_lock);
7026 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007027 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 return 0;
7029 }
7030 if (v == (void*)2) {
7031 status_unused(seq);
7032 return 0;
7033 }
7034
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007035 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007037
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7039 seq_printf(seq, "%s : %sactive", mdname(mddev),
7040 mddev->pers ? "" : "in");
7041 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007042 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007044 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007045 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046 seq_printf(seq, " %s", mddev->pers->name);
7047 }
7048
Andre Nolldd8ac332009-03-31 14:33:13 +11007049 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007050 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 char b[BDEVNAME_SIZE];
7052 seq_printf(seq, " %s[%d]",
7053 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007054 if (test_bit(WriteMostly, &rdev->flags))
7055 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007056 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 seq_printf(seq, "(F)");
7058 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007059 }
7060 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007061 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007062 if (test_bit(Replacement, &rdev->flags))
7063 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007064 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 }
7066
7067 if (!list_empty(&mddev->disks)) {
7068 if (mddev->pers)
7069 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007070 (unsigned long long)
7071 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 else
7073 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007074 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007076 if (mddev->persistent) {
7077 if (mddev->major_version != 0 ||
7078 mddev->minor_version != 90) {
7079 seq_printf(seq," super %d.%d",
7080 mddev->major_version,
7081 mddev->minor_version);
7082 }
NeilBrowne6910632008-02-06 01:39:51 -08007083 } else if (mddev->external)
7084 seq_printf(seq, " super external:%s",
7085 mddev->metadata_type);
7086 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007087 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088
7089 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007090 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007092 if (mddev->pers->sync_request) {
7093 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007094 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007095 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007096 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007097 seq_printf(seq, "\tresync=DELAYED\n ");
7098 else if (mddev->recovery_cp < MaxSector)
7099 seq_printf(seq, "\tresync=PENDING\n ");
7100 }
NeilBrown32a76272005-06-21 17:17:14 -07007101 } else
7102 seq_printf(seq, "\n ");
7103
NeilBrown57148962012-03-19 12:46:40 +11007104 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105
7106 seq_printf(seq, "\n");
7107 }
7108 mddev_unlock(mddev);
7109
7110 return 0;
7111}
7112
Jan Engelhardt110518b2009-05-07 12:49:37 +10007113static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114 .start = md_seq_start,
7115 .next = md_seq_next,
7116 .stop = md_seq_stop,
7117 .show = md_seq_show,
7118};
7119
7120static int md_seq_open(struct inode *inode, struct file *file)
7121{
Kay Sieversf1514632011-07-12 20:48:39 +02007122 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 int error;
7124
7125 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007126 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007127 return error;
7128
7129 seq = file->private_data;
7130 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 return error;
7132}
7133
NeilBrownd7603b72006-01-06 00:20:30 -08007134static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7135{
Kay Sieversf1514632011-07-12 20:48:39 +02007136 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007137 int mask;
7138
7139 poll_wait(filp, &md_event_waiters, wait);
7140
7141 /* always allow read */
7142 mask = POLLIN | POLLRDNORM;
7143
Kay Sieversf1514632011-07-12 20:48:39 +02007144 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007145 mask |= POLLERR | POLLPRI;
7146 return mask;
7147}
7148
Arjan van de Venfa027c22007-02-12 00:55:33 -08007149static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007150 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 .open = md_seq_open,
7152 .read = seq_read,
7153 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007154 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007155 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156};
7157
NeilBrown84fc4b52011-10-11 16:49:58 +11007158int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007161 list_add_tail(&p->list, &pers_list);
7162 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 spin_unlock(&pers_lock);
7164 return 0;
7165}
7166
NeilBrown84fc4b52011-10-11 16:49:58 +11007167int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168{
NeilBrown2604b702006-01-06 00:20:36 -08007169 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007171 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 spin_unlock(&pers_lock);
7173 return 0;
7174}
7175
NeilBrownfd01b882011-10-11 16:47:53 +11007176static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177{
NeilBrown3cb03002011-10-11 16:45:26 +11007178 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007180 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181
7182 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007183 rcu_read_lock();
7184 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007186 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7187 (int)part_stat_read(&disk->part0, sectors[1]) -
7188 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007189 /* sync IO will cause sync_io to increase before the disk_stats
7190 * as sync_io is counted when a request starts, and
7191 * disk_stats is counted when it completes.
7192 * So resync activity will cause curr_events to be smaller than
7193 * when there was no such activity.
7194 * non-sync IO will cause disk_stat to increase without
7195 * increasing sync_io so curr_events will (eventually)
7196 * be larger than it was before. Once it becomes
7197 * substantially larger, the test below will cause
7198 * the array to appear non-idle, and resync will slow
7199 * down.
7200 * If there is a lot of outstanding resync activity when
7201 * we set last_event to curr_events, then all that activity
7202 * completing might cause the array to appear non-idle
7203 * and resync will be slowed down even though there might
7204 * not have been non-resync activity. This will only
7205 * happen once though. 'last_events' will soon reflect
7206 * the state where there is little or no outstanding
7207 * resync requests, and further resync activity will
7208 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007209 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007211 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 rdev->last_events = curr_events;
7213 idle = 0;
7214 }
7215 }
NeilBrown4b809912008-07-21 17:05:25 +10007216 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 return idle;
7218}
7219
NeilBrownfd01b882011-10-11 16:47:53 +11007220void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221{
7222 /* another "blocks" (512byte) blocks have been synced */
7223 atomic_sub(blocks, &mddev->recovery_active);
7224 wake_up(&mddev->recovery_wait);
7225 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007226 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007227 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 md_wakeup_thread(mddev->thread);
7229 // stop recovery, signal do_sync ....
7230 }
7231}
7232
7233
NeilBrown06d91a52005-06-21 17:17:12 -07007234/* md_write_start(mddev, bi)
7235 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007236 * in superblock) before writing, schedule a superblock update
7237 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007238 */
NeilBrownfd01b882011-10-11 16:47:53 +11007239void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240{
Neil Brown0fd62b82008-06-28 08:31:36 +10007241 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007242 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007243 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007244
NeilBrownf91de922005-11-08 21:39:36 -08007245 BUG_ON(mddev->ro == 1);
7246 if (mddev->ro == 2) {
7247 /* need to switch to read/write */
7248 mddev->ro = 0;
7249 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7250 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007251 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007252 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007253 }
NeilBrown06d91a52005-06-21 17:17:12 -07007254 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007255 if (mddev->safemode == 1)
7256 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007257 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007258 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007259 if (mddev->in_sync) {
7260 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007261 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007262 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007263 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007264 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007265 }
NeilBrowna9701a32005-11-08 21:39:34 -08007266 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007267 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007268 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007269 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007270 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007271 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272}
7273
NeilBrownfd01b882011-10-11 16:47:53 +11007274void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275{
7276 if (atomic_dec_and_test(&mddev->writes_pending)) {
7277 if (mddev->safemode == 2)
7278 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007279 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7281 }
7282}
7283
NeilBrown2a2275d2007-01-26 00:57:11 -08007284/* md_allow_write(mddev)
7285 * Calling this ensures that the array is marked 'active' so that writes
7286 * may proceed without blocking. It is important to call this before
7287 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7288 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007289 *
7290 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7291 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007292 */
NeilBrownfd01b882011-10-11 16:47:53 +11007293int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007294{
7295 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007296 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007297 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007298 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007299 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007300 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007301
7302 spin_lock_irq(&mddev->write_lock);
7303 if (mddev->in_sync) {
7304 mddev->in_sync = 0;
7305 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007306 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007307 if (mddev->safemode_delay &&
7308 mddev->safemode == 0)
7309 mddev->safemode = 1;
7310 spin_unlock_irq(&mddev->write_lock);
7311 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007312 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007313 } else
7314 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007315
NeilBrown070dc6d2010-08-30 17:33:34 +10007316 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007317 return -EAGAIN;
7318 else
7319 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007320}
7321EXPORT_SYMBOL_GPL(md_allow_write);
7322
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323#define SYNC_MARKS 10
7324#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007325#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007326void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327{
Shaohua Li4ed87312012-10-11 13:34:00 +11007328 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007329 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330 unsigned int currspeed = 0,
7331 window;
NeilBrown57afd892005-06-21 17:17:13 -07007332 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007334 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 sector_t mark_cnt[SYNC_MARKS];
7336 int last_mark,m;
7337 struct list_head *tmp;
7338 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007339 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007340 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007341 char *desc;
majianpeng7c2c57c2012-07-03 12:12:26 +10007342 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343
7344 /* just incase thread restarts... */
7345 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7346 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007347 if (mddev->ro) /* never try to sync a read-only array */
7348 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349
NeilBrown61df9d92006-10-03 01:15:57 -07007350 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7351 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7352 desc = "data-check";
7353 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7354 desc = "requested-resync";
7355 else
7356 desc = "resync";
7357 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7358 desc = "reshape";
7359 else
7360 desc = "recovery";
7361
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 /* we overload curr_resync somewhat here.
7363 * 0 == not engaged in resync at all
7364 * 2 == checking that there is no conflict with another sync
7365 * 1 == like 2, but have yielded to allow conflicting resync to
7366 * commense
7367 * other == active in resync - this many blocks
7368 *
7369 * Before starting a resync we must have set curr_resync to
7370 * 2, and then checked that every "conflicting" array has curr_resync
7371 * less than ours. When we find one that is the same or higher
7372 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7373 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7374 * This will mean we have to start checking from the beginning again.
7375 *
7376 */
7377
7378 do {
7379 mddev->curr_resync = 2;
7380
7381 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007382 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007383 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007384
7385 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007387 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388 if (mddev2 == mddev)
7389 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007390 if (!mddev->parallel_resync
7391 && mddev2->curr_resync
7392 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 DEFINE_WAIT(wq);
7394 if (mddev < mddev2 && mddev->curr_resync == 2) {
7395 /* arbitrarily yield */
7396 mddev->curr_resync = 1;
7397 wake_up(&resync_wait);
7398 }
7399 if (mddev > mddev2 && mddev->curr_resync == 1)
7400 /* no need to wait here, we can wait the next
7401 * time 'round when curr_resync == 2
7402 */
7403 continue;
NeilBrown97441972008-09-19 11:49:54 +10007404 /* We need to wait 'interruptible' so as not to
7405 * contribute to the load average, and not to
7406 * be caught by 'softlockup'
7407 */
7408 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007409 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007410 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007411 printk(KERN_INFO "md: delaying %s of %s"
7412 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007414 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007416 if (signal_pending(current))
7417 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 schedule();
7419 finish_wait(&resync_wait, &wq);
7420 goto try_again;
7421 }
7422 finish_wait(&resync_wait, &wq);
7423 }
7424 }
7425 } while (mddev->curr_resync < 2);
7426
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007427 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007428 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007430 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431 */
7432 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007433 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007434 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007435 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7436 j = mddev->resync_min;
7437 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007438 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007439
NeilBrownccfcc3c2006-03-27 01:18:09 -08007440 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007441 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007442 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007444 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007445 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007446 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007447 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007448 if (rdev->raid_disk >= 0 &&
7449 !test_bit(Faulty, &rdev->flags) &&
7450 !test_bit(In_sync, &rdev->flags) &&
7451 rdev->recovery_offset < j)
7452 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007453 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455
NeilBrown61df9d92006-10-03 01:15:57 -07007456 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7457 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7458 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007459 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007460 "(but not more than %d KB/sec) for %s.\n",
7461 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462
NeilBrowneea1bf32009-03-31 14:27:02 +11007463 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007464
NeilBrown57afd892005-06-21 17:17:13 -07007465 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 for (m = 0; m < SYNC_MARKS; m++) {
7467 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007468 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 }
7470 last_mark = 0;
7471 mddev->resync_mark = mark[last_mark];
7472 mddev->resync_mark_cnt = mark_cnt[last_mark];
7473
7474 /*
7475 * Tune reconstruction:
7476 */
7477 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007478 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7479 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480
7481 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 last_check = 0;
7483
7484 if (j>2) {
7485 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007486 "md: resuming %s of %s from checkpoint.\n",
7487 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007489 } else
7490 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007491 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007492 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7493 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007494 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495
majianpeng7c2c57c2012-07-03 12:12:26 +10007496 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007498 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499
NeilBrown57afd892005-06-21 17:17:13 -07007500 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007501
NeilBrown7a91ee12009-05-26 12:57:21 +10007502 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7503 ((mddev->curr_resync > mddev->curr_resync_completed &&
7504 (mddev->curr_resync - mddev->curr_resync_completed)
7505 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007506 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007507 (j - mddev->curr_resync_completed)*2
7508 >= mddev->resync_max - mddev->curr_resync_completed
7509 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007510 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007511 wait_event(mddev->recovery_wait,
7512 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007513 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007514 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7515 j > mddev->recovery_cp)
7516 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007517 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007518 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007519 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007520 }
NeilBrownacb180b2009-04-14 16:28:34 +10007521
NeilBrowne62e58a2009-07-01 13:15:35 +10007522 while (j >= mddev->resync_max && !kthread_should_stop()) {
7523 /* As this condition is controlled by user-space,
7524 * we can block indefinitely, so use '_interruptible'
7525 * to avoid triggering warnings.
7526 */
7527 flush_signals(current); /* just in case */
7528 wait_event_interruptible(mddev->recovery_wait,
7529 mddev->resync_max > j
7530 || kthread_should_stop());
7531 }
NeilBrownacb180b2009-04-14 16:28:34 +10007532
7533 if (kthread_should_stop())
7534 goto interrupted;
7535
NeilBrown57afd892005-06-21 17:17:13 -07007536 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007537 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007538 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007539 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540 goto out;
7541 }
NeilBrown57afd892005-06-21 17:17:13 -07007542
7543 if (!skipped) { /* actual IO requested */
7544 io_sectors += sectors;
7545 atomic_add(sectors, &mddev->recovery_active);
7546 }
7547
NeilBrowne875ece2011-07-28 11:39:24 +10007548 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7549 break;
7550
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007552 if (j > 2)
7553 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007554 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007555 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007556 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007557 * visible in /proc/mdstat
7558 */
7559 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007560
7561 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 continue;
7563
NeilBrown57afd892005-06-21 17:17:13 -07007564 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565 repeat:
7566 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7567 /* step marks */
7568 int next = (last_mark+1) % SYNC_MARKS;
7569
7570 mddev->resync_mark = mark[next];
7571 mddev->resync_mark_cnt = mark_cnt[next];
7572 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007573 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 last_mark = next;
7575 }
7576
7577
NeilBrownc6207272008-02-06 01:39:52 -08007578 if (kthread_should_stop())
7579 goto interrupted;
7580
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581
7582 /*
7583 * this loop exits only if either when we are slower than
7584 * the 'hard' speed limit, or the system was IO-idle for
7585 * a jiffy.
7586 * the system might be non-idle CPU-wise, but we only care
7587 * about not overloading the IO subsystem. (things like an
7588 * e2fsck being done on the RAID array should execute fast)
7589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590 cond_resched();
7591
NeilBrown57afd892005-06-21 17:17:13 -07007592 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7593 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594
NeilBrown88202a02006-01-06 00:21:36 -08007595 if (currspeed > speed_min(mddev)) {
7596 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007597 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007598 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 goto repeat;
7600 }
7601 }
7602 }
NeilBrown61df9d92006-10-03 01:15:57 -07007603 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 /*
7605 * this also signals 'finished resyncing' to md_stop
7606 */
7607 out:
majianpeng7c2c57c2012-07-03 12:12:26 +10007608 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7610
7611 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007612 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613
NeilBrowndfc70642008-05-23 13:04:39 -07007614 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007615 mddev->curr_resync > 2) {
7616 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7617 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7618 if (mddev->curr_resync >= mddev->recovery_cp) {
7619 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007620 "md: checkpointing %s of %s.\n",
7621 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007622 if (test_bit(MD_RECOVERY_ERROR,
7623 &mddev->recovery))
7624 mddev->recovery_cp =
7625 mddev->curr_resync_completed;
7626 else
7627 mddev->recovery_cp =
7628 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007629 }
7630 } else
7631 mddev->recovery_cp = MaxSector;
7632 } else {
7633 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7634 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007635 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007636 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007637 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007638 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007639 !test_bit(Faulty, &rdev->flags) &&
7640 !test_bit(In_sync, &rdev->flags) &&
7641 rdev->recovery_offset < mddev->curr_resync)
7642 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007643 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007646 skip:
NeilBrown17571282006-12-10 02:20:52 -08007647 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648
NeilBrownc07b70a2009-12-14 12:49:48 +11007649 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7650 /* We completed so min/max setting can be forgotten if used. */
7651 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7652 mddev->resync_min = 0;
7653 mddev->resync_max = MaxSector;
7654 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7655 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656 mddev->curr_resync = 0;
7657 wake_up(&resync_wait);
7658 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7659 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007660 return;
7661
7662 interrupted:
7663 /*
7664 * got a signal, exit.
7665 */
7666 printk(KERN_INFO
7667 "md: md_do_sync() got signal ... exiting\n");
7668 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7669 goto out;
7670
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671}
NeilBrown29269552006-03-27 01:18:10 -08007672EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673
NeilBrown746d3202013-04-24 11:42:41 +10007674static int remove_and_add_spares(struct mddev *mddev,
7675 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007676{
NeilBrown3cb03002011-10-11 16:45:26 +11007677 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007678 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007679 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007680
NeilBrowndafb20f2012-03-19 12:46:39 +11007681 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007682 if ((this == NULL || rdev == this) &&
7683 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007684 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007685 (test_bit(Faulty, &rdev->flags) ||
7686 ! test_bit(In_sync, &rdev->flags)) &&
7687 atomic_read(&rdev->nr_pending)==0) {
7688 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007689 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007690 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007691 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007692 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007693 }
7694 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007695 if (removed && mddev->kobj.sd)
7696 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007697
NeilBrown746d3202013-04-24 11:42:41 +10007698 if (this)
7699 goto no_add;
7700
NeilBrowndafb20f2012-03-19 12:46:39 +11007701 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007702 if (rdev->raid_disk >= 0 &&
7703 !test_bit(In_sync, &rdev->flags) &&
7704 !test_bit(Faulty, &rdev->flags))
7705 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007706 if (rdev->raid_disk >= 0)
7707 continue;
7708 if (test_bit(Faulty, &rdev->flags))
7709 continue;
7710 if (mddev->ro &&
7711 rdev->saved_raid_disk < 0)
7712 continue;
7713
7714 rdev->recovery_offset = 0;
7715 if (rdev->saved_raid_disk >= 0 && mddev->in_sync) {
7716 spin_lock_irq(&mddev->write_lock);
7717 if (mddev->in_sync)
7718 /* OK, this device, which is in_sync,
7719 * will definitely be noticed before
7720 * the next write, so recovery isn't
7721 * needed.
7722 */
7723 rdev->recovery_offset = mddev->recovery_cp;
7724 spin_unlock_irq(&mddev->write_lock);
7725 }
7726 if (mddev->ro && rdev->recovery_offset != MaxSector)
7727 /* not safe to add this disk now */
7728 continue;
7729 if (mddev->pers->
7730 hot_add_disk(mddev, rdev) == 0) {
7731 if (sysfs_link_rdev(mddev, rdev))
7732 /* failure here is OK */;
7733 spares++;
7734 md_new_event(mddev);
7735 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007736 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007737 }
NeilBrown746d3202013-04-24 11:42:41 +10007738no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007739 if (removed)
7740 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007741 return spares;
7742}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007743
NeilBrownfd01b882011-10-11 16:47:53 +11007744static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007745{
NeilBrown3cb03002011-10-11 16:45:26 +11007746 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007747
7748 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007749 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007750 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7751 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7752 /* success...*/
7753 /* activate any spares */
NeilBrown6dafab62012-09-19 12:54:22 +10007754 if (mddev->pers->spare_active(mddev)) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007755 sysfs_notify(&mddev->kobj, NULL,
7756 "degraded");
NeilBrown6dafab62012-09-19 12:54:22 +10007757 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7758 }
NeilBrown7ebc0be2011-01-14 09:14:33 +11007759 }
7760 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7761 mddev->pers->finish_reshape)
7762 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007763
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007764 /* If array is no-longer degraded, then any saved_raid_disk
7765 * information must be scrapped. Also if any device is now
7766 * In_sync we must scrape the saved_raid_disk for that device
7767 * do the superblock for an incrementally recovered device
7768 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007769 */
NeilBrowndafb20f2012-03-19 12:46:39 +11007770 rdev_for_each(rdev, mddev)
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007771 if (!mddev->degraded ||
7772 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007773 rdev->saved_raid_disk = -1;
7774
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007775 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007776 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7777 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7778 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7779 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7780 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7781 /* flag recovery needed just to double check */
7782 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7783 sysfs_notify_dirent_safe(mddev->sysfs_action);
7784 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007785 if (mddev->event_work.func)
7786 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007787}
7788
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789/*
7790 * This routine is regularly called by all per-raid-array threads to
7791 * deal with generic issues like resync and super-block update.
7792 * Raid personalities that don't have a thread (linear/raid0) do not
7793 * need this as they never do any recovery or update the superblock.
7794 *
7795 * It does not do any resync itself, but rather "forks" off other threads
7796 * to do that as needed.
7797 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7798 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007799 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800 * and wakeups up this thread which will reap the thread and finish up.
7801 * This thread also removes any faulty devices (with nr_pending == 0).
7802 *
7803 * The overall approach is:
7804 * 1/ if the superblock needs updating, update it.
7805 * 2/ If a recovery thread is running, don't do anything else.
7806 * 3/ If recovery has finished, clean up, possibly marking spares active.
7807 * 4/ If there are any faulty devices, remove them.
7808 * 5/ If array is degraded, try to add spares devices
7809 * 6/ If array has spares or is not in-sync, start a resync thread.
7810 */
NeilBrownfd01b882011-10-11 16:47:53 +11007811void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007813 if (mddev->suspended)
7814 return;
7815
NeilBrown5f404022005-06-21 17:17:16 -07007816 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007817 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818
NeilBrownfca4d842005-06-21 17:17:11 -07007819 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007820 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007821 printk(KERN_INFO "md: %s in immediate safe mode\n",
7822 mdname(mddev));
7823 mddev->safemode = 2;
7824 }
7825 flush_signals(current);
7826 }
7827
NeilBrownc89a8ee2008-08-05 15:54:13 +10007828 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7829 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007831 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007833 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007834 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007835 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7836 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 ))
7838 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007839
NeilBrowndf5b89b2006-03-27 01:18:20 -08007840 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007841 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007842
NeilBrownc89a8ee2008-08-05 15:54:13 +10007843 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007844 /* On a read-only array we can:
7845 * - remove failed devices
7846 * - add already-in_sync devices if the array itself
7847 * is in-sync.
7848 * As we only add devices that are already in-sync,
7849 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007850 */
NeilBrownc89a8ee2008-08-05 15:54:13 +10007851 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown7ceb17e2013-04-24 11:42:42 +10007852 remove_and_add_spares(mddev, NULL);
7853 mddev->pers->spare_active(mddev);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007854 goto unlock;
7855 }
7856
NeilBrown31a59e32008-04-30 00:52:30 -07007857 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007858 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007859 spin_lock_irq(&mddev->write_lock);
7860 if (mddev->safemode &&
7861 !atomic_read(&mddev->writes_pending) &&
7862 !mddev->in_sync &&
7863 mddev->recovery_cp == MaxSector) {
7864 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007865 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007866 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007867 }
7868 if (mddev->safemode == 1)
7869 mddev->safemode = 0;
7870 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007871 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007872 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007873 }
NeilBrownfca4d842005-06-21 17:17:11 -07007874
NeilBrown850b2b422006-10-03 01:15:46 -07007875 if (mddev->flags)
7876 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007877
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7879 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7880 /* resync/recovery still happening */
7881 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7882 goto unlock;
7883 }
7884 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007885 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886 goto unlock;
7887 }
Neil Brown72a23c22008-06-28 08:31:41 +10007888 /* Set RUNNING before clearing NEEDED to avoid
7889 * any transients in the value of "sync_action".
7890 */
NeilBrown72f36d52012-10-11 14:25:57 +11007891 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007892 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007893 /* Clear some bits that don't mean anything, but
7894 * might be left set
7895 */
NeilBrown24dd4692005-11-08 21:39:26 -08007896 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7897 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898
NeilBrowned209582012-04-24 10:23:14 +10007899 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7900 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007901 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 /* no recovery is running.
7903 * remove any failed drives, then
7904 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007905 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906 * the personality to fail the re-add.
7907 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007909 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007910 if (mddev->pers->check_reshape == NULL ||
7911 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007912 /* Cannot proceed */
7913 goto unlock;
7914 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007915 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007916 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007917 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7918 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007919 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007920 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007921 } else if (mddev->recovery_cp < MaxSector) {
7922 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007923 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007924 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7925 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007927
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007929 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007930 /* We are adding a device or devices to an array
7931 * which has the bitmap stored on all devices.
7932 * So make sure all bitmap pages get written
7933 */
7934 bitmap_write_all(mddev->bitmap);
7935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936 mddev->sync_thread = md_register_thread(md_do_sync,
7937 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007938 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007939 if (!mddev->sync_thread) {
7940 printk(KERN_ERR "%s: could not start resync"
7941 " thread...\n",
7942 mdname(mddev));
7943 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007944 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7945 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7946 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7947 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7948 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007949 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007951 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007952 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953 }
7954 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007955 if (!mddev->sync_thread) {
7956 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7957 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7958 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007959 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007960 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962 mddev_unlock(mddev);
7963 }
7964}
7965
NeilBrownfd01b882011-10-11 16:47:53 +11007966void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007967{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007968 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007969 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007970 !test_bit(Blocked, &rdev->flags) &&
7971 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007972 msecs_to_jiffies(5000));
7973 rdev_dec_pending(rdev, mddev);
7974}
7975EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7976
NeilBrownc6563a82012-05-21 09:27:00 +10007977void md_finish_reshape(struct mddev *mddev)
7978{
7979 /* called be personality module when reshape completes. */
7980 struct md_rdev *rdev;
7981
7982 rdev_for_each(rdev, mddev) {
7983 if (rdev->data_offset > rdev->new_data_offset)
7984 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7985 else
7986 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7987 rdev->data_offset = rdev->new_data_offset;
7988 }
7989}
7990EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007991
7992/* Bad block management.
7993 * We can record which blocks on each device are 'bad' and so just
7994 * fail those blocks, or that stripe, rather than the whole device.
7995 * Entries in the bad-block table are 64bits wide. This comprises:
7996 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7997 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7998 * A 'shift' can be set so that larger blocks are tracked and
7999 * consequently larger devices can be covered.
8000 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8001 *
8002 * Locking of the bad-block table uses a seqlock so md_is_badblock
8003 * might need to retry if it is very unlucky.
8004 * We will sometimes want to check for bad blocks in a bi_end_io function,
8005 * so we use the write_seqlock_irq variant.
8006 *
8007 * When looking for a bad block we specify a range and want to
8008 * know if any block in the range is bad. So we binary-search
8009 * to the last range that starts at-or-before the given endpoint,
8010 * (or "before the sector after the target range")
8011 * then see if it ends after the given start.
8012 * We return
8013 * 0 if there are no known bad blocks in the range
8014 * 1 if there are known bad block which are all acknowledged
8015 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8016 * plus the start/length of the first bad section we overlap.
8017 */
8018int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8019 sector_t *first_bad, int *bad_sectors)
8020{
8021 int hi;
majianpengab056132012-11-06 17:13:44 +08008022 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008023 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008024 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008025 sector_t target = s + sectors;
8026 unsigned seq;
8027
8028 if (bb->shift > 0) {
8029 /* round the start down, and the end up */
8030 s >>= bb->shift;
8031 target += (1<<bb->shift) - 1;
8032 target >>= bb->shift;
8033 sectors = target - s;
8034 }
8035 /* 'target' is now the first block after the bad range */
8036
8037retry:
8038 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008039 lo = 0;
8040 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008041 hi = bb->count;
8042
8043 /* Binary search between lo and hi for 'target'
8044 * i.e. for the last range that starts before 'target'
8045 */
8046 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8047 * are known not to be the last range before target.
8048 * VARIANT: hi-lo is the number of possible
8049 * ranges, and decreases until it reaches 1
8050 */
8051 while (hi - lo > 1) {
8052 int mid = (lo + hi) / 2;
8053 sector_t a = BB_OFFSET(p[mid]);
8054 if (a < target)
8055 /* This could still be the one, earlier ranges
8056 * could not. */
8057 lo = mid;
8058 else
8059 /* This and later ranges are definitely out. */
8060 hi = mid;
8061 }
8062 /* 'lo' might be the last that started before target, but 'hi' isn't */
8063 if (hi > lo) {
8064 /* need to check all range that end after 's' to see if
8065 * any are unacknowledged.
8066 */
8067 while (lo >= 0 &&
8068 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8069 if (BB_OFFSET(p[lo]) < target) {
8070 /* starts before the end, and finishes after
8071 * the start, so they must overlap
8072 */
8073 if (rv != -1 && BB_ACK(p[lo]))
8074 rv = 1;
8075 else
8076 rv = -1;
8077 *first_bad = BB_OFFSET(p[lo]);
8078 *bad_sectors = BB_LEN(p[lo]);
8079 }
8080 lo--;
8081 }
8082 }
8083
8084 if (read_seqretry(&bb->lock, seq))
8085 goto retry;
8086
8087 return rv;
8088}
8089EXPORT_SYMBOL_GPL(md_is_badblock);
8090
8091/*
8092 * Add a range of bad blocks to the table.
8093 * This might extend the table, or might contract it
8094 * if two adjacent ranges can be merged.
8095 * We binary-search to find the 'insertion' point, then
8096 * decide how best to handle it.
8097 */
8098static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8099 int acknowledged)
8100{
8101 u64 *p;
8102 int lo, hi;
8103 int rv = 1;
8104
8105 if (bb->shift < 0)
8106 /* badblocks are disabled */
8107 return 0;
8108
8109 if (bb->shift) {
8110 /* round the start down, and the end up */
8111 sector_t next = s + sectors;
8112 s >>= bb->shift;
8113 next += (1<<bb->shift) - 1;
8114 next >>= bb->shift;
8115 sectors = next - s;
8116 }
8117
8118 write_seqlock_irq(&bb->lock);
8119
8120 p = bb->page;
8121 lo = 0;
8122 hi = bb->count;
8123 /* Find the last range that starts at-or-before 's' */
8124 while (hi - lo > 1) {
8125 int mid = (lo + hi) / 2;
8126 sector_t a = BB_OFFSET(p[mid]);
8127 if (a <= s)
8128 lo = mid;
8129 else
8130 hi = mid;
8131 }
8132 if (hi > lo && BB_OFFSET(p[lo]) > s)
8133 hi = lo;
8134
8135 if (hi > lo) {
8136 /* we found a range that might merge with the start
8137 * of our new range
8138 */
8139 sector_t a = BB_OFFSET(p[lo]);
8140 sector_t e = a + BB_LEN(p[lo]);
8141 int ack = BB_ACK(p[lo]);
8142 if (e >= s) {
8143 /* Yes, we can merge with a previous range */
8144 if (s == a && s + sectors >= e)
8145 /* new range covers old */
8146 ack = acknowledged;
8147 else
8148 ack = ack && acknowledged;
8149
8150 if (e < s + sectors)
8151 e = s + sectors;
8152 if (e - a <= BB_MAX_LEN) {
8153 p[lo] = BB_MAKE(a, e-a, ack);
8154 s = e;
8155 } else {
8156 /* does not all fit in one range,
8157 * make p[lo] maximal
8158 */
8159 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8160 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8161 s = a + BB_MAX_LEN;
8162 }
8163 sectors = e - s;
8164 }
8165 }
8166 if (sectors && hi < bb->count) {
8167 /* 'hi' points to the first range that starts after 's'.
8168 * Maybe we can merge with the start of that range */
8169 sector_t a = BB_OFFSET(p[hi]);
8170 sector_t e = a + BB_LEN(p[hi]);
8171 int ack = BB_ACK(p[hi]);
8172 if (a <= s + sectors) {
8173 /* merging is possible */
8174 if (e <= s + sectors) {
8175 /* full overlap */
8176 e = s + sectors;
8177 ack = acknowledged;
8178 } else
8179 ack = ack && acknowledged;
8180
8181 a = s;
8182 if (e - a <= BB_MAX_LEN) {
8183 p[hi] = BB_MAKE(a, e-a, ack);
8184 s = e;
8185 } else {
8186 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8187 s = a + BB_MAX_LEN;
8188 }
8189 sectors = e - s;
8190 lo = hi;
8191 hi++;
8192 }
8193 }
8194 if (sectors == 0 && hi < bb->count) {
8195 /* we might be able to combine lo and hi */
8196 /* Note: 's' is at the end of 'lo' */
8197 sector_t a = BB_OFFSET(p[hi]);
8198 int lolen = BB_LEN(p[lo]);
8199 int hilen = BB_LEN(p[hi]);
8200 int newlen = lolen + hilen - (s - a);
8201 if (s >= a && newlen < BB_MAX_LEN) {
8202 /* yes, we can combine them */
8203 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8204 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8205 memmove(p + hi, p + hi + 1,
8206 (bb->count - hi - 1) * 8);
8207 bb->count--;
8208 }
8209 }
8210 while (sectors) {
8211 /* didn't merge (it all).
8212 * Need to add a range just before 'hi' */
8213 if (bb->count >= MD_MAX_BADBLOCKS) {
8214 /* No room for more */
8215 rv = 0;
8216 break;
8217 } else {
8218 int this_sectors = sectors;
8219 memmove(p + hi + 1, p + hi,
8220 (bb->count - hi) * 8);
8221 bb->count++;
8222
8223 if (this_sectors > BB_MAX_LEN)
8224 this_sectors = BB_MAX_LEN;
8225 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8226 sectors -= this_sectors;
8227 s += this_sectors;
8228 }
8229 }
8230
8231 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008232 if (!acknowledged)
8233 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10008234 write_sequnlock_irq(&bb->lock);
8235
8236 return rv;
8237}
8238
NeilBrown3cb03002011-10-11 16:45:26 +11008239int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008240 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008241{
NeilBrownc6563a82012-05-21 09:27:00 +10008242 int rv;
8243 if (is_new)
8244 s += rdev->new_data_offset;
8245 else
8246 s += rdev->data_offset;
8247 rv = md_set_badblocks(&rdev->badblocks,
8248 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008249 if (rv) {
8250 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008251 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008252 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8253 md_wakeup_thread(rdev->mddev->thread);
8254 }
8255 return rv;
8256}
8257EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8258
8259/*
8260 * Remove a range of bad blocks from the table.
8261 * This may involve extending the table if we spilt a region,
8262 * but it must not fail. So if the table becomes full, we just
8263 * drop the remove request.
8264 */
8265static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8266{
8267 u64 *p;
8268 int lo, hi;
8269 sector_t target = s + sectors;
8270 int rv = 0;
8271
8272 if (bb->shift > 0) {
8273 /* When clearing we round the start up and the end down.
8274 * This should not matter as the shift should align with
8275 * the block size and no rounding should ever be needed.
8276 * However it is better the think a block is bad when it
8277 * isn't than to think a block is not bad when it is.
8278 */
8279 s += (1<<bb->shift) - 1;
8280 s >>= bb->shift;
8281 target >>= bb->shift;
8282 sectors = target - s;
8283 }
8284
8285 write_seqlock_irq(&bb->lock);
8286
8287 p = bb->page;
8288 lo = 0;
8289 hi = bb->count;
8290 /* Find the last range that starts before 'target' */
8291 while (hi - lo > 1) {
8292 int mid = (lo + hi) / 2;
8293 sector_t a = BB_OFFSET(p[mid]);
8294 if (a < target)
8295 lo = mid;
8296 else
8297 hi = mid;
8298 }
8299 if (hi > lo) {
8300 /* p[lo] is the last range that could overlap the
8301 * current range. Earlier ranges could also overlap,
8302 * but only this one can overlap the end of the range.
8303 */
8304 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8305 /* Partial overlap, leave the tail of this range */
8306 int ack = BB_ACK(p[lo]);
8307 sector_t a = BB_OFFSET(p[lo]);
8308 sector_t end = a + BB_LEN(p[lo]);
8309
8310 if (a < s) {
8311 /* we need to split this range */
8312 if (bb->count >= MD_MAX_BADBLOCKS) {
8313 rv = 0;
8314 goto out;
8315 }
8316 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8317 bb->count++;
8318 p[lo] = BB_MAKE(a, s-a, ack);
8319 lo++;
8320 }
8321 p[lo] = BB_MAKE(target, end - target, ack);
8322 /* there is no longer an overlap */
8323 hi = lo;
8324 lo--;
8325 }
8326 while (lo >= 0 &&
8327 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8328 /* This range does overlap */
8329 if (BB_OFFSET(p[lo]) < s) {
8330 /* Keep the early parts of this range. */
8331 int ack = BB_ACK(p[lo]);
8332 sector_t start = BB_OFFSET(p[lo]);
8333 p[lo] = BB_MAKE(start, s - start, ack);
8334 /* now low doesn't overlap, so.. */
8335 break;
8336 }
8337 lo--;
8338 }
8339 /* 'lo' is strictly before, 'hi' is strictly after,
8340 * anything between needs to be discarded
8341 */
8342 if (hi - lo > 1) {
8343 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8344 bb->count -= (hi - lo - 1);
8345 }
8346 }
8347
8348 bb->changed = 1;
8349out:
8350 write_sequnlock_irq(&bb->lock);
8351 return rv;
8352}
8353
NeilBrownc6563a82012-05-21 09:27:00 +10008354int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8355 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008356{
NeilBrownc6563a82012-05-21 09:27:00 +10008357 if (is_new)
8358 s += rdev->new_data_offset;
8359 else
8360 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008361 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008362 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008363}
8364EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8365
8366/*
8367 * Acknowledge all bad blocks in a list.
8368 * This only succeeds if ->changed is clear. It is used by
8369 * in-kernel metadata updates
8370 */
8371void md_ack_all_badblocks(struct badblocks *bb)
8372{
8373 if (bb->page == NULL || bb->changed)
8374 /* no point even trying */
8375 return;
8376 write_seqlock_irq(&bb->lock);
8377
majianpengecb178b2012-03-19 12:46:42 +11008378 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008379 u64 *p = bb->page;
8380 int i;
8381 for (i = 0; i < bb->count ; i++) {
8382 if (!BB_ACK(p[i])) {
8383 sector_t start = BB_OFFSET(p[i]);
8384 int len = BB_LEN(p[i]);
8385 p[i] = BB_MAKE(start, len, 1);
8386 }
8387 }
NeilBrownde393cd2011-07-28 11:31:48 +10008388 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008389 }
8390 write_sequnlock_irq(&bb->lock);
8391}
8392EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8393
NeilBrown16c791a2011-07-28 11:31:47 +10008394/* sysfs access to bad-blocks list.
8395 * We present two files.
8396 * 'bad-blocks' lists sector numbers and lengths of ranges that
8397 * are recorded as bad. The list is truncated to fit within
8398 * the one-page limit of sysfs.
8399 * Writing "sector length" to this file adds an acknowledged
8400 * bad block list.
8401 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8402 * been acknowledged. Writing to this file adds bad blocks
8403 * without acknowledging them. This is largely for testing.
8404 */
8405
8406static ssize_t
8407badblocks_show(struct badblocks *bb, char *page, int unack)
8408{
8409 size_t len;
8410 int i;
8411 u64 *p = bb->page;
8412 unsigned seq;
8413
8414 if (bb->shift < 0)
8415 return 0;
8416
8417retry:
8418 seq = read_seqbegin(&bb->lock);
8419
8420 len = 0;
8421 i = 0;
8422
8423 while (len < PAGE_SIZE && i < bb->count) {
8424 sector_t s = BB_OFFSET(p[i]);
8425 unsigned int length = BB_LEN(p[i]);
8426 int ack = BB_ACK(p[i]);
8427 i++;
8428
8429 if (unack && ack)
8430 continue;
8431
8432 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8433 (unsigned long long)s << bb->shift,
8434 length << bb->shift);
8435 }
NeilBrownde393cd2011-07-28 11:31:48 +10008436 if (unack && len == 0)
8437 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008438
8439 if (read_seqretry(&bb->lock, seq))
8440 goto retry;
8441
8442 return len;
8443}
8444
8445#define DO_DEBUG 1
8446
8447static ssize_t
8448badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8449{
8450 unsigned long long sector;
8451 int length;
8452 char newline;
8453#ifdef DO_DEBUG
8454 /* Allow clearing via sysfs *only* for testing/debugging.
8455 * Normally only a successful write may clear a badblock
8456 */
8457 int clear = 0;
8458 if (page[0] == '-') {
8459 clear = 1;
8460 page++;
8461 }
8462#endif /* DO_DEBUG */
8463
8464 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8465 case 3:
8466 if (newline != '\n')
8467 return -EINVAL;
8468 case 2:
8469 if (length <= 0)
8470 return -EINVAL;
8471 break;
8472 default:
8473 return -EINVAL;
8474 }
8475
8476#ifdef DO_DEBUG
8477 if (clear) {
8478 md_clear_badblocks(bb, sector, length);
8479 return len;
8480 }
8481#endif /* DO_DEBUG */
8482 if (md_set_badblocks(bb, sector, length, !unack))
8483 return len;
8484 else
8485 return -ENOSPC;
8486}
8487
Adrian Bunk75c96f82005-05-05 16:16:09 -07008488static int md_notify_reboot(struct notifier_block *this,
8489 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490{
8491 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008492 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008493 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494
NeilBrownc744a652012-03-19 12:46:37 +11008495 for_each_mddev(mddev, tmp) {
8496 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008497 if (mddev->pers)
8498 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008499 mddev->safemode = 2;
8500 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008501 }
NeilBrownc744a652012-03-19 12:46:37 +11008502 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503 }
NeilBrownc744a652012-03-19 12:46:37 +11008504 /*
8505 * certain more exotic SCSI devices are known to be
8506 * volatile wrt too early system reboots. While the
8507 * right place to handle this issue is the given
8508 * driver, we do want to have a safe RAID driver ...
8509 */
8510 if (need_delay)
8511 mdelay(1000*1);
8512
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 return NOTIFY_DONE;
8514}
8515
Adrian Bunk75c96f82005-05-05 16:16:09 -07008516static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517 .notifier_call = md_notify_reboot,
8518 .next = NULL,
8519 .priority = INT_MAX, /* before any real devices */
8520};
8521
8522static void md_geninit(void)
8523{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008524 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008525
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008526 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527}
8528
Adrian Bunk75c96f82005-05-05 16:16:09 -07008529static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530{
Tejun Heoe804ac72010-10-15 15:36:08 +02008531 int ret = -ENOMEM;
8532
Tejun Heoada609e2011-01-25 14:35:54 +01008533 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008534 if (!md_wq)
8535 goto err_wq;
8536
8537 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8538 if (!md_misc_wq)
8539 goto err_misc_wq;
8540
8541 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8542 goto err_md;
8543
8544 if ((ret = register_blkdev(0, "mdp")) < 0)
8545 goto err_mdp;
8546 mdp_major = ret;
8547
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008548 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008549 md_probe, NULL, NULL);
8550 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551 md_probe, NULL, NULL);
8552
Linus Torvalds1da177e2005-04-16 15:20:36 -07008553 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008554 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008555
8556 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558
Tejun Heoe804ac72010-10-15 15:36:08 +02008559err_mdp:
8560 unregister_blkdev(MD_MAJOR, "md");
8561err_md:
8562 destroy_workqueue(md_misc_wq);
8563err_misc_wq:
8564 destroy_workqueue(md_wq);
8565err_wq:
8566 return ret;
8567}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008568
8569#ifndef MODULE
8570
8571/*
8572 * Searches all registered partitions for autorun RAID arrays
8573 * at boot time.
8574 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008575
8576static LIST_HEAD(all_detected_devices);
8577struct detected_devices_node {
8578 struct list_head list;
8579 dev_t dev;
8580};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008581
8582void md_autodetect_dev(dev_t dev)
8583{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008584 struct detected_devices_node *node_detected_dev;
8585
8586 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8587 if (node_detected_dev) {
8588 node_detected_dev->dev = dev;
8589 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8590 } else {
8591 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8592 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008594}
8595
8596
8597static void autostart_arrays(int part)
8598{
NeilBrown3cb03002011-10-11 16:45:26 +11008599 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008600 struct detected_devices_node *node_detected_dev;
8601 dev_t dev;
8602 int i_scanned, i_passed;
8603
8604 i_scanned = 0;
8605 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008606
8607 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8608
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008609 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8610 i_scanned++;
8611 node_detected_dev = list_entry(all_detected_devices.next,
8612 struct detected_devices_node, list);
8613 list_del(&node_detected_dev->list);
8614 dev = node_detected_dev->dev;
8615 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008616 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617 if (IS_ERR(rdev))
8618 continue;
8619
NeilBrownb2d444d2005-11-08 21:39:31 -08008620 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621 MD_BUG();
8622 continue;
8623 }
NeilBrownd0fae182008-03-04 14:29:31 -08008624 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008626 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008627 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008628
8629 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8630 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008631
8632 autorun_devices(part);
8633}
8634
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008635#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636
8637static __exit void md_exit(void)
8638{
NeilBrownfd01b882011-10-11 16:47:53 +11008639 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008641
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008642 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008643 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008645 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 unregister_blkdev(mdp_major, "mdp");
8647 unregister_reboot_notifier(&md_notifier);
8648 unregister_sysctl_table(raid_table_header);
8649 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008650 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008651 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008652 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008654 destroy_workqueue(md_misc_wq);
8655 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008656}
8657
Dan Williams685784a2007-07-09 11:56:42 -07008658subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008659module_exit(md_exit)
8660
NeilBrownf91de922005-11-08 21:39:36 -08008661static int get_ro(char *buffer, struct kernel_param *kp)
8662{
8663 return sprintf(buffer, "%d", start_readonly);
8664}
8665static int set_ro(const char *val, struct kernel_param *kp)
8666{
8667 char *e;
8668 int num = simple_strtoul(val, &e, 10);
8669 if (*val && (*e == '\0' || *e == '\n')) {
8670 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008671 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008672 }
8673 return -EINVAL;
8674}
8675
NeilBrown80ca3a42006-07-10 04:44:18 -07008676module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8677module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008678
NeilBrownefeb53c2009-01-09 08:31:10 +11008679module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008680
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681EXPORT_SYMBOL(register_md_personality);
8682EXPORT_SYMBOL(unregister_md_personality);
8683EXPORT_SYMBOL(md_error);
8684EXPORT_SYMBOL(md_done_sync);
8685EXPORT_SYMBOL(md_write_start);
8686EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008687EXPORT_SYMBOL(md_register_thread);
8688EXPORT_SYMBOL(md_unregister_thread);
8689EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690EXPORT_SYMBOL(md_check_recovery);
8691MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008692MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008693MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008694MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);