blob: 9b82377a833bd6572b628c79426ca153781fd712 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb482011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
NeilBrown746d3202013-04-24 11:42:41 +100075static int remove_and_add_spares(struct mddev *mddev,
76 struct md_rdev *this);
77
Adrian Bunk5e563412006-06-26 00:27:42 -070078#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
Robert Becker1e509152009-12-14 12:49:58 +110081 * Default number of read corrections we'll attempt on an rdev
82 * before ejecting it from the array. We divide the read error
83 * count by 2 for every hour elapsed between read errors.
84 */
85#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
86/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
88 * is 1000 KB/sec, so the extra system load does not show up that much.
89 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070090 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * subsystem is idle. There is also an 'absolute maximum' reconstruction
92 * speed limit - in case reconstruction slows down your system despite
93 * idle IO detection.
94 *
95 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080096 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98
99static int sysctl_speed_limit_min = 1000;
100static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100101static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800102{
103 return mddev->sync_speed_min ?
104 mddev->sync_speed_min : sysctl_speed_limit_min;
105}
106
NeilBrownfd01b882011-10-11 16:47:53 +1100107static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800108{
109 return mddev->sync_speed_max ?
110 mddev->sync_speed_max : sysctl_speed_limit_max;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static struct ctl_table_header *raid_table_header;
114
115static ctl_table raid_table[] = {
116 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .procname = "speed_limit_min",
118 .data = &sysctl_speed_limit_min,
119 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700120 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800121 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 },
123 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .procname = "speed_limit_max",
125 .data = &sysctl_speed_limit_max,
126 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700127 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800128 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800130 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133static ctl_table raid_dir_table[] = {
134 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .procname = "raid",
136 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700137 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .child = raid_table,
139 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800140 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143static ctl_table raid_root_table[] = {
144 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .procname = "dev",
146 .maxlen = 0,
147 .mode = 0555,
148 .child = raid_dir_table,
149 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800150 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700153static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
NeilBrownf91de922005-11-08 21:39:36 -0800155static int start_readonly;
156
NeilBrowna167f662010-10-26 18:31:13 +1100157/* bio_clone_mddev
158 * like bio_clone, but with a local bio set
159 */
160
NeilBrowna167f662010-10-26 18:31:13 +1100161struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100162 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100163{
164 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100165
166 if (!mddev || !mddev->bio_set)
167 return bio_alloc(gfp_mask, nr_iovecs);
168
Kent Overstreet395c72a2012-09-06 15:34:55 -0700169 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100170 if (!b)
171 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100172 return b;
173}
174EXPORT_SYMBOL_GPL(bio_alloc_mddev);
175
176struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100177 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100178{
NeilBrowna167f662010-10-26 18:31:13 +1100179 if (!mddev || !mddev->bio_set)
180 return bio_clone(bio, gfp_mask);
181
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700182 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100183}
184EXPORT_SYMBOL_GPL(bio_clone_mddev);
185
NeilBrownd2eb35a2011-07-28 11:31:48 +1000186void md_trim_bio(struct bio *bio, int offset, int size)
187{
188 /* 'bio' is a cloned bio which we need to trim to match
189 * the given offset and size.
190 * This requires adjusting bi_sector, bi_size, and bi_io_vec
191 */
192 int i;
193 struct bio_vec *bvec;
194 int sofar = 0;
195
196 size <<= 9;
197 if (offset == 0 && size == bio->bi_size)
198 return;
199
NeilBrownd2eb35a2011-07-28 11:31:48 +1000200 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
201
Kent Overstreetfb9e3532012-09-26 21:41:20 -0700202 bio_advance(bio, offset << 9);
203
204 bio->bi_size = size;
205
NeilBrownd2eb35a2011-07-28 11:31:48 +1000206 /* avoid any complications with bi_idx being non-zero*/
207 if (bio->bi_idx) {
208 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
209 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
210 bio->bi_vcnt -= bio->bi_idx;
211 bio->bi_idx = 0;
212 }
213 /* Make sure vcnt and last bv are not too big */
214 bio_for_each_segment(bvec, bio, i) {
215 if (sofar + bvec->bv_len > size)
216 bvec->bv_len = size - sofar;
217 if (bvec->bv_len == 0) {
218 bio->bi_vcnt = i;
219 break;
220 }
221 sofar += bvec->bv_len;
222 }
223}
224EXPORT_SYMBOL_GPL(md_trim_bio);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/*
NeilBrownd7603b72006-01-06 00:20:30 -0800227 * We have a system wide 'event count' that is incremented
228 * on any 'interesting' event, and readers of /proc/mdstat
229 * can use 'poll' or 'select' to find out when the event
230 * count increases.
231 *
232 * Events are:
233 * start array, stop array, error, add device, remove device,
234 * start build, activate spare
235 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800236static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800237static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100238void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800239{
240 atomic_inc(&md_event_count);
241 wake_up(&md_event_waiters);
242}
NeilBrown29269552006-03-27 01:18:10 -0800243EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800244
NeilBrownc331eb02006-05-30 21:27:13 -0700245/* Alternate version that can be called from interrupts
246 * when calling sysfs_notify isn't needed.
247 */
NeilBrownfd01b882011-10-11 16:47:53 +1100248static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700249{
250 atomic_inc(&md_event_count);
251 wake_up(&md_event_waiters);
252}
253
NeilBrownd7603b72006-01-06 00:20:30 -0800254/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * Enables to iterate over all existing md arrays
256 * all_mddevs_lock protects this list.
257 */
258static LIST_HEAD(all_mddevs);
259static DEFINE_SPINLOCK(all_mddevs_lock);
260
261
262/*
263 * iterates through all used mddevs in the system.
264 * We take care to grab the all_mddevs_lock whenever navigating
265 * the list, and to always hold a refcount when unlocked.
266 * Any code which breaks out of this loop while own
267 * a reference to the current mddev and must mddev_put it.
268 */
NeilBrownfd01b882011-10-11 16:47:53 +1100269#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 \
271 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100272 _tmp = all_mddevs.next; \
273 _mddev = NULL;}); \
274 ({ if (_tmp != &all_mddevs) \
275 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100277 if (_mddev) mddev_put(_mddev); \
278 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
279 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100281 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 )
283
284
NeilBrown409c57f2009-03-31 14:39:39 +1100285/* Rather than calling directly into the personality make_request function,
286 * IO requests come here first so that we can check if the device is
287 * being suspended pending a reconfiguration.
288 * We hold a refcount over the call to ->make_request. By the time that
289 * call has finished, the bio has been linked into some internal structure
290 * and so is visible to ->quiesce(), so we don't need the refcount any more.
291 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200292static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
NeilBrown49077322010-03-25 16:20:56 +1100294 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100295 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100296 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500297 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100298
NeilBrown0ca69882011-01-14 09:14:33 +1100299 if (mddev == NULL || mddev->pers == NULL
300 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100301 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200302 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100303 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100304 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
305 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
306 return;
307 }
NeilBrown0ca69882011-01-14 09:14:33 +1100308 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100309 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200310 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100311 DEFINE_WAIT(__wait);
312 for (;;) {
313 prepare_to_wait(&mddev->sb_wait, &__wait,
314 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200315 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100316 break;
317 rcu_read_unlock();
318 schedule();
319 rcu_read_lock();
320 }
321 finish_wait(&mddev->sb_wait, &__wait);
322 }
323 atomic_inc(&mddev->active_io);
324 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100325
Chris Masone91ece52011-02-07 19:21:48 -0500326 /*
327 * save the sectors now since our bio can
328 * go away inside make_request
329 */
330 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200331 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100332
333 cpu = part_stat_lock();
334 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500335 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100336 part_stat_unlock();
337
NeilBrown409c57f2009-03-31 14:39:39 +1100338 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
339 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
NeilBrown9e35b992010-04-06 14:23:02 +1000342/* mddev_suspend makes sure no new requests are submitted
343 * to the device, and that any requests that have been submitted
344 * are completely handled.
345 * Once ->stop is called and completes, the module will be completely
346 * unused.
347 */
NeilBrownfd01b882011-10-11 16:47:53 +1100348void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100349{
350 BUG_ON(mddev->suspended);
351 mddev->suspended = 1;
352 synchronize_rcu();
353 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
354 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500355
356 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100357}
NeilBrown390ee602010-06-01 19:37:27 +1000358EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100359
NeilBrownfd01b882011-10-11 16:47:53 +1100360void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100361{
362 mddev->suspended = 0;
363 wake_up(&mddev->sb_wait);
364 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500365
Jonathan Brassow47525e52012-05-22 13:55:29 +1000366 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500367 md_wakeup_thread(mddev->thread);
368 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100369}
NeilBrown390ee602010-06-01 19:37:27 +1000370EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100371
NeilBrownfd01b882011-10-11 16:47:53 +1100372int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000373{
374 return mddev->suspended;
375}
376EXPORT_SYMBOL(mddev_congested);
377
NeilBrowna2826aa2009-12-14 12:49:49 +1100378/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200379 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100380 */
381
Tejun Heoe9c74692010-09-03 11:56:18 +0200382static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100383{
NeilBrown3cb03002011-10-11 16:45:26 +1100384 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100385 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100386
387 rdev_dec_pending(rdev, mddev);
388
389 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200390 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200391 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100392 }
393 bio_put(bio);
394}
395
NeilBrowna7a07e62010-12-09 16:04:25 +1100396static void md_submit_flush_data(struct work_struct *ws);
397
NeilBrowna035fc32010-12-09 16:17:51 +1100398static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100399{
NeilBrownfd01b882011-10-11 16:47:53 +1100400 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100401 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100402
NeilBrowna7a07e62010-12-09 16:04:25 +1100403 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
404 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100405 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100406 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100407 if (rdev->raid_disk >= 0 &&
408 !test_bit(Faulty, &rdev->flags)) {
409 /* Take two references, one is dropped
410 * when request finishes, one after
411 * we reclaim rcu_read_lock
412 */
413 struct bio *bi;
414 atomic_inc(&rdev->nr_pending);
415 atomic_inc(&rdev->nr_pending);
416 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000417 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200418 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100419 bi->bi_private = rdev;
420 bi->bi_bdev = rdev->bdev;
421 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200422 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100423 rcu_read_lock();
424 rdev_dec_pending(rdev, mddev);
425 }
426 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100427 if (atomic_dec_and_test(&mddev->flush_pending))
428 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100429}
430
Tejun Heoe9c74692010-09-03 11:56:18 +0200431static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100432{
NeilBrownfd01b882011-10-11 16:47:53 +1100433 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200434 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100435
Tejun Heoe9c74692010-09-03 11:56:18 +0200436 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100437 /* an empty barrier - all done */
438 bio_endio(bio, 0);
439 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200440 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200441 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100442 }
NeilBrown2b74e122010-12-09 15:59:01 +1100443
444 mddev->flush_bio = NULL;
445 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100446}
447
NeilBrownfd01b882011-10-11 16:47:53 +1100448void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100449{
450 spin_lock_irq(&mddev->write_lock);
451 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200452 !mddev->flush_bio,
Lukas Czernereed8c022012-11-30 11:42:40 +0100453 mddev->write_lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200454 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100455 spin_unlock_irq(&mddev->write_lock);
456
NeilBrowna035fc32010-12-09 16:17:51 +1100457 INIT_WORK(&mddev->flush_work, submit_flushes);
458 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100459}
Tejun Heoe9c74692010-09-03 11:56:18 +0200460EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100461
NeilBrown74018dc2012-07-31 09:08:15 +0200462void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000463{
NeilBrown9cbb1752012-07-31 09:08:14 +0200464 struct mddev *mddev = cb->data;
465 md_wakeup_thread(mddev->thread);
466 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000467}
NeilBrown9cbb1752012-07-31 09:08:14 +0200468EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000469
NeilBrownfd01b882011-10-11 16:47:53 +1100470static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 atomic_inc(&mddev->active);
473 return mddev;
474}
475
Dan Williams5fd3a172009-03-04 00:57:25 -0700476static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100477
NeilBrownfd01b882011-10-11 16:47:53 +1100478static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
NeilBrowna167f662010-10-26 18:31:13 +1100480 struct bio_set *bs = NULL;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
483 return;
NeilBrownd3374822009-01-09 08:31:10 +1100484 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100485 mddev->ctime == 0 && !mddev->hold_active) {
486 /* Array is not configured at all, and not held active,
487 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100488 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100489 bs = mddev->bio_set;
490 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100491 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200492 /* We did a probe so need to clean up. Call
493 * queue_work inside the spinlock so that
494 * flush_workqueue() after mddev_find will
495 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100496 */
497 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200498 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100499 } else
500 kfree(mddev);
501 }
502 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100503 if (bs)
504 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
NeilBrownfd01b882011-10-11 16:47:53 +1100507void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100508{
509 mutex_init(&mddev->open_mutex);
510 mutex_init(&mddev->reconfig_mutex);
511 mutex_init(&mddev->bitmap_info.mutex);
512 INIT_LIST_HEAD(&mddev->disks);
513 INIT_LIST_HEAD(&mddev->all_mddevs);
514 init_timer(&mddev->safemode_timer);
515 atomic_set(&mddev->active, 1);
516 atomic_set(&mddev->openers, 0);
517 atomic_set(&mddev->active_io, 0);
518 spin_lock_init(&mddev->write_lock);
519 atomic_set(&mddev->flush_pending, 0);
520 init_waitqueue_head(&mddev->sb_wait);
521 init_waitqueue_head(&mddev->recovery_wait);
522 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000523 mddev->reshape_backwards = 0;
NeilBrownfafd7fb2010-04-01 15:55:30 +1100524 mddev->resync_min = 0;
525 mddev->resync_max = MaxSector;
526 mddev->level = LEVEL_NONE;
527}
NeilBrown390ee602010-06-01 19:37:27 +1000528EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100529
NeilBrownfd01b882011-10-11 16:47:53 +1100530static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
NeilBrownfd01b882011-10-11 16:47:53 +1100532 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
NeilBrown8f5f02c2011-02-16 13:58:51 +1100534 if (unit && MAJOR(unit) != MD_MAJOR)
535 unit &= ~((1<<MdpMinorShift)-1);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 retry:
538 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
NeilBrownefeb53c2009-01-09 08:31:10 +1100540 if (unit) {
541 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
542 if (mddev->unit == unit) {
543 mddev_get(mddev);
544 spin_unlock(&all_mddevs_lock);
545 kfree(new);
546 return mddev;
547 }
548
549 if (new) {
550 list_add(&new->all_mddevs, &all_mddevs);
551 spin_unlock(&all_mddevs_lock);
552 new->hold_active = UNTIL_IOCTL;
553 return new;
554 }
555 } else if (new) {
556 /* find an unused unit number */
557 static int next_minor = 512;
558 int start = next_minor;
559 int is_free = 0;
560 int dev = 0;
561 while (!is_free) {
562 dev = MKDEV(MD_MAJOR, next_minor);
563 next_minor++;
564 if (next_minor > MINORMASK)
565 next_minor = 0;
566 if (next_minor == start) {
567 /* Oh dear, all in use. */
568 spin_unlock(&all_mddevs_lock);
569 kfree(new);
570 return NULL;
571 }
572
573 is_free = 1;
574 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
575 if (mddev->unit == dev) {
576 is_free = 0;
577 break;
578 }
579 }
580 new->unit = dev;
581 new->md_minor = MINOR(dev);
582 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 list_add(&new->all_mddevs, &all_mddevs);
584 spin_unlock(&all_mddevs_lock);
585 return new;
586 }
587 spin_unlock(&all_mddevs_lock);
588
NeilBrown9ffae0c2006-01-06 00:20:32 -0800589 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (!new)
591 return NULL;
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 new->unit = unit;
594 if (MAJOR(unit) == MD_MAJOR)
595 new->md_minor = MINOR(unit);
596 else
597 new->md_minor = MINOR(unit) >> MdpMinorShift;
598
NeilBrownfafd7fb2010-04-01 15:55:30 +1100599 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 goto retry;
602}
603
NeilBrownfd01b882011-10-11 16:47:53 +1100604static inline int mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800606 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
NeilBrownfd01b882011-10-11 16:47:53 +1100609static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100610{
611 return mutex_is_locked(&mddev->reconfig_mutex);
612}
613
NeilBrownfd01b882011-10-11 16:47:53 +1100614static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800616 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
NeilBrownb6eb1272010-04-15 10:13:47 +1000619static struct attribute_group md_redundancy_group;
620
NeilBrownfd01b882011-10-11 16:47:53 +1100621static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
NeilBrowna64c8762010-04-14 17:15:37 +1000623 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000624 /* These cannot be removed under reconfig_mutex as
625 * an access to the files will try to take reconfig_mutex
626 * while holding the file unremovable, which leads to
627 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000628 * So hold set sysfs_active while the remove in happeing,
629 * and anything else which might set ->to_remove or my
630 * otherwise change the sysfs namespace will fail with
631 * -EBUSY if sysfs_active is still set.
632 * We set sysfs_active under reconfig_mutex and elsewhere
633 * test it under the same mutex to ensure its correct value
634 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000635 */
NeilBrowna64c8762010-04-14 17:15:37 +1000636 struct attribute_group *to_remove = mddev->to_remove;
637 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000638 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000639 mutex_unlock(&mddev->reconfig_mutex);
640
NeilBrown00bcb4a2010-06-01 19:37:23 +1000641 if (mddev->kobj.sd) {
642 if (to_remove != &md_redundancy_group)
643 sysfs_remove_group(&mddev->kobj, to_remove);
644 if (mddev->pers == NULL ||
645 mddev->pers->sync_request == NULL) {
646 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
647 if (mddev->sysfs_action)
648 sysfs_put(mddev->sysfs_action);
649 mddev->sysfs_action = NULL;
650 }
NeilBrowna64c8762010-04-14 17:15:37 +1000651 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000652 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000653 } else
654 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Chris Dunlop751e67c2011-10-19 16:48:26 +1100656 /* As we've dropped the mutex we need a spinlock to
657 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000658 */
659 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700660 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000661 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
NeilBrownfd01b882011-10-11 16:47:53 +1100664static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
NeilBrown3cb03002011-10-11 16:45:26 +1100666 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
NeilBrowndafb20f2012-03-19 12:46:39 +1100668 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (rdev->desc_nr == nr)
670 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return NULL;
673}
674
NeilBrown1ca69c42012-10-11 13:37:33 +1100675static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
676{
677 struct md_rdev *rdev;
678
679 rdev_for_each_rcu(rdev, mddev)
680 if (rdev->desc_nr == nr)
681 return rdev;
682
683 return NULL;
684}
685
686static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
NeilBrown3cb03002011-10-11 16:45:26 +1100688 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
NeilBrowndafb20f2012-03-19 12:46:39 +1100690 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (rdev->bdev->bd_dev == dev)
692 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return NULL;
695}
696
NeilBrown1ca69c42012-10-11 13:37:33 +1100697static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
698{
699 struct md_rdev *rdev;
700
701 rdev_for_each_rcu(rdev, mddev)
702 if (rdev->bdev->bd_dev == dev)
703 return rdev;
704
705 return NULL;
706}
707
NeilBrown84fc4b52011-10-11 16:49:58 +1100708static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800709{
NeilBrown84fc4b52011-10-11 16:49:58 +1100710 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800711 list_for_each_entry(pers, &pers_list, list) {
712 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800713 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800714 if (strcmp(pers->name, clevel)==0)
715 return pers;
716 }
NeilBrown2604b702006-01-06 00:20:36 -0800717 return NULL;
718}
719
Andre Nollb73df2d2008-07-11 22:02:23 +1000720/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100721static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100723 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000724 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
NeilBrown3cb03002011-10-11 16:45:26 +1100727static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 if (rdev->sb_page)
730 MD_BUG();
731
732 rdev->sb_page = alloc_page(GFP_KERNEL);
733 if (!rdev->sb_page) {
734 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000735 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
738 return 0;
739}
740
NeilBrown545c8792012-05-22 13:54:30 +1000741void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800744 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 rdev->sb_loaded = 0;
746 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000747 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100748 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
NeilBrown2699b672011-07-28 11:31:47 +1000750 if (rdev->bb_page) {
751 put_page(rdev->bb_page);
752 rdev->bb_page = NULL;
753 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000754 kfree(rdev->badblocks.page);
755 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
NeilBrown545c8792012-05-22 13:54:30 +1000757EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
NeilBrown6712ecf2007-09-27 12:47:43 +0200759static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700760{
NeilBrown3cb03002011-10-11 16:45:26 +1100761 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100762 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700763
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700764 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
765 printk("md: super_written gets error=%d, uptodate=%d\n",
766 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
767 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800768 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700769 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700770
NeilBrowna9701a32005-11-08 21:39:34 -0800771 if (atomic_dec_and_test(&mddev->pending_writes))
772 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700773 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700774}
775
NeilBrownfd01b882011-10-11 16:47:53 +1100776void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700777 sector_t sector, int size, struct page *page)
778{
779 /* write first size bytes of page to sector of rdev
780 * Increment mddev->pending_writes before returning
781 * and decrement it on completion, waking up sb_wait
782 * if zero is reached.
783 * If an error occurred, call md_error
784 */
NeilBrowna167f662010-10-26 18:31:13 +1100785 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700786
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100787 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700788 bio->bi_sector = sector;
789 bio_add_page(bio, page, size, 0);
790 bio->bi_private = rdev;
791 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800792
NeilBrown7bfa19f2005-06-21 17:17:28 -0700793 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000794 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800795}
796
NeilBrownfd01b882011-10-11 16:47:53 +1100797void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800798{
Tejun Heoe9c74692010-09-03 11:56:18 +0200799 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800800 DEFINE_WAIT(wq);
801 for(;;) {
802 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
803 if (atomic_read(&mddev->pending_writes)==0)
804 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800805 schedule();
806 }
807 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700808}
809
NeilBrown6712ecf2007-09-27 12:47:43 +0200810static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
NeilBrown3cb03002011-10-11 16:45:26 +1100815int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100816 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
NeilBrowna167f662010-10-26 18:31:13 +1100818 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 struct completion event;
820 int ret;
821
Jens Axboe721a9602011-03-09 11:56:30 +0100822 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100824 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
825 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100826 if (metadata_op)
827 bio->bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000828 else if (rdev->mddev->reshape_position != MaxSector &&
829 (rdev->mddev->reshape_backwards ==
830 (sector >= rdev->mddev->reshape_position)))
831 bio->bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100832 else
833 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 bio_add_page(bio, page, size, 0);
835 init_completion(&event);
836 bio->bi_private = &event;
837 bio->bi_end_io = bi_complete;
838 submit_bio(rw, bio);
839 wait_for_completion(&event);
840
841 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
842 bio_put(bio);
843 return ret;
844}
NeilBrowna8745db2006-01-06 00:20:34 -0800845EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
NeilBrown3cb03002011-10-11 16:45:26 +1100847static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 char b[BDEVNAME_SIZE];
850 if (!rdev->sb_page) {
851 MD_BUG();
852 return -EINVAL;
853 }
854 if (rdev->sb_loaded)
855 return 0;
856
857
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100858 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 goto fail;
860 rdev->sb_loaded = 1;
861 return 0;
862
863fail:
864 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
865 bdevname(rdev->bdev,b));
866 return -EINVAL;
867}
868
869static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
870{
Andre Noll05710462008-07-11 22:02:20 +1000871 return sb1->set_uuid0 == sb2->set_uuid0 &&
872 sb1->set_uuid1 == sb2->set_uuid1 &&
873 sb1->set_uuid2 == sb2->set_uuid2 &&
874 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
878{
879 int ret;
880 mdp_super_t *tmp1, *tmp2;
881
882 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
883 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
884
885 if (!tmp1 || !tmp2) {
886 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100887 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto abort;
889 }
890
891 *tmp1 = *sb1;
892 *tmp2 = *sb2;
893
894 /*
895 * nr_disks is not constant
896 */
897 tmp1->nr_disks = 0;
898 tmp2->nr_disks = 0;
899
Andre Nollce0c8e02008-07-11 22:02:20 +1000900 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700902 kfree(tmp1);
903 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return ret;
905}
906
NeilBrown4d167f02007-05-09 02:35:37 -0700907
908static u32 md_csum_fold(u32 csum)
909{
910 csum = (csum & 0xffff) + (csum >> 16);
911 return (csum & 0xffff) + (csum >> 16);
912}
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914static unsigned int calc_sb_csum(mdp_super_t * sb)
915{
NeilBrown4d167f02007-05-09 02:35:37 -0700916 u64 newcsum = 0;
917 u32 *sb32 = (u32*)sb;
918 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 unsigned int disk_csum, csum;
920
921 disk_csum = sb->sb_csum;
922 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700923
924 for (i = 0; i < MD_SB_BYTES/4 ; i++)
925 newcsum += sb32[i];
926 csum = (newcsum & 0xffffffff) + (newcsum>>32);
927
928
929#ifdef CONFIG_ALPHA
930 /* This used to use csum_partial, which was wrong for several
931 * reasons including that different results are returned on
932 * different architectures. It isn't critical that we get exactly
933 * the same return value as before (we always csum_fold before
934 * testing, and that removes any differences). However as we
935 * know that csum_partial always returned a 16bit value on
936 * alphas, do a fold to maximise conformity to previous behaviour.
937 */
938 sb->sb_csum = md_csum_fold(disk_csum);
939#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700941#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return csum;
943}
944
945
946/*
947 * Handle superblock details.
948 * We want to be able to handle multiple superblock formats
949 * so we have a common interface to them all, and an array of
950 * different handlers.
951 * We rely on user-space to write the initial superblock, and support
952 * reading and updating of superblocks.
953 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100954 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * loads and validates a superblock on dev.
956 * if refdev != NULL, compare superblocks on both devices
957 * Return:
958 * 0 - dev has a superblock that is compatible with refdev
959 * 1 - dev has a superblock that is compatible and newer than refdev
960 * so dev should be used as the refdev in future
961 * -EINVAL superblock incompatible or invalid
962 * -othererror e.g. -EIO
963 *
NeilBrownfd01b882011-10-11 16:47:53 +1100964 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * Verify that dev is acceptable into mddev.
966 * The first time, mddev->raid_disks will be 0, and data from
967 * dev should be merged in. Subsequent calls check that dev
968 * is new enough. Return 0 or -EINVAL
969 *
NeilBrownfd01b882011-10-11 16:47:53 +1100970 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 * Update the superblock for rdev with data in mddev
972 * This does not write to disc.
973 *
974 */
975
976struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000977 char *name;
978 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000979 int (*load_super)(struct md_rdev *rdev,
980 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000981 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000982 int (*validate_super)(struct mddev *mddev,
983 struct md_rdev *rdev);
984 void (*sync_super)(struct mddev *mddev,
985 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100986 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000987 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000988 int (*allow_new_offset)(struct md_rdev *rdev,
989 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990};
991
992/*
Andre Noll0894cc32009-06-18 08:49:23 +1000993 * Check that the given mddev has no bitmap.
994 *
995 * This function is called from the run method of all personalities that do not
996 * support bitmaps. It prints an error message and returns non-zero if mddev
997 * has a bitmap. Otherwise, it returns 0.
998 *
999 */
NeilBrownfd01b882011-10-11 16:47:53 +11001000int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001001{
NeilBrownc3d97142009-12-14 12:49:52 +11001002 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001003 return 0;
1004 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1005 mdname(mddev), mddev->pers->name);
1006 return 1;
1007}
1008EXPORT_SYMBOL(md_check_no_bitmap);
1009
1010/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 * load_super for 0.90.0
1012 */
NeilBrown3cb03002011-10-11 16:45:26 +11001013static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1016 mdp_super_t *sb;
1017 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 /*
Andre Noll0f420352008-07-11 22:02:23 +10001020 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 * it's at the end of the disk.
1022 *
1023 * It also happens to be a multiple of 4Kb.
1024 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001025 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
NeilBrown0002b272005-09-09 16:23:53 -07001027 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (ret) return ret;
1029
1030 ret = -EINVAL;
1031
1032 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001033 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (sb->md_magic != MD_SB_MAGIC) {
1036 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1037 b);
1038 goto abort;
1039 }
1040
1041 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001042 sb->minor_version < 90 ||
1043 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1045 sb->major_version, sb->minor_version,
1046 b);
1047 goto abort;
1048 }
1049
1050 if (sb->raid_disks <= 0)
1051 goto abort;
1052
NeilBrown4d167f02007-05-09 02:35:37 -07001053 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1055 b);
1056 goto abort;
1057 }
1058
1059 rdev->preferred_minor = sb->md_minor;
1060 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001061 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001062 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001063 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 if (sb->level == LEVEL_MULTIPATH)
1066 rdev->desc_nr = -1;
1067 else
1068 rdev->desc_nr = sb->this_disk.number;
1069
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001070 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001072 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001074 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (!uuid_equal(refsb, sb)) {
1076 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1077 b, bdevname(refdev->bdev,b2));
1078 goto abort;
1079 }
1080 if (!sb_equal(refsb, sb)) {
1081 printk(KERN_WARNING "md: %s has same UUID"
1082 " but different superblock to %s\n",
1083 b, bdevname(refdev->bdev, b2));
1084 goto abort;
1085 }
1086 ev1 = md_event(sb);
1087 ev2 = md_event(refsb);
1088 if (ev1 > ev2)
1089 ret = 1;
1090 else
1091 ret = 0;
1092 }
NeilBrown8190e752009-06-18 08:48:58 +10001093 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001094 /* Limit to 4TB as metadata cannot record more than that.
1095 * (not needed for Linear and RAID0 as metadata doesn't
1096 * record this size)
1097 */
1098 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001099 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
NeilBrown27a7b262011-09-10 17:21:28 +10001101 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001102 /* "this cannot possibly happen" ... */
1103 ret = -EINVAL;
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 abort:
1106 return ret;
1107}
1108
1109/*
1110 * validate_super for 0.90.0
1111 */
NeilBrownfd01b882011-10-11 16:47:53 +11001112static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001115 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001116 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
NeilBrown41158c72005-06-21 17:17:25 -07001118 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001119 clear_bit(Faulty, &rdev->flags);
1120 clear_bit(In_sync, &rdev->flags);
1121 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (mddev->raid_disks == 0) {
1124 mddev->major_version = 0;
1125 mddev->minor_version = sb->minor_version;
1126 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001127 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001128 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 mddev->ctime = sb->ctime;
1130 mddev->utime = sb->utime;
1131 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001132 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 mddev->layout = sb->layout;
1134 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001135 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001136 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001137 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001138 mddev->bitmap_info.space = 0;
1139 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001140 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001141 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001142 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
NeilBrownf6705572006-03-27 01:18:11 -08001144 if (mddev->minor_version >= 91) {
1145 mddev->reshape_position = sb->reshape_position;
1146 mddev->delta_disks = sb->delta_disks;
1147 mddev->new_level = sb->new_level;
1148 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001149 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001150 if (mddev->delta_disks < 0)
1151 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001152 } else {
1153 mddev->reshape_position = MaxSector;
1154 mddev->delta_disks = 0;
1155 mddev->new_level = mddev->level;
1156 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001157 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001158 }
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (sb->state & (1<<MD_SB_CLEAN))
1161 mddev->recovery_cp = MaxSector;
1162 else {
1163 if (sb->events_hi == sb->cp_events_hi &&
1164 sb->events_lo == sb->cp_events_lo) {
1165 mddev->recovery_cp = sb->recovery_cp;
1166 } else
1167 mddev->recovery_cp = 0;
1168 }
1169
1170 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1171 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1172 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1173 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1174
1175 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001176
1177 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001178 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001179 mddev->bitmap_info.offset =
1180 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001181 mddev->bitmap_info.space =
1182 mddev->bitmap_info.space;
1183 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001184
NeilBrown41158c72005-06-21 17:17:25 -07001185 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001186 /* Insist on good event counter while assembling, except
1187 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001189 if (sb->disks[rdev->desc_nr].state & (
1190 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1191 if (ev1 < mddev->events)
1192 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001193 } else if (mddev->bitmap) {
1194 /* if adding to array with a bitmap, then we can accept an
1195 * older device ... but not too old.
1196 */
NeilBrown41158c72005-06-21 17:17:25 -07001197 if (ev1 < mddev->bitmap->events_cleared)
1198 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001199 } else {
1200 if (ev1 < mddev->events)
1201 /* just a hot-add of a new device, leave raid_disk at -1 */
1202 return 0;
1203 }
NeilBrown41158c72005-06-21 17:17:25 -07001204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 desc = sb->disks + rdev->desc_nr;
1207
1208 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001209 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001210 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1211 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001212 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001214 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1215 /* active but not in sync implies recovery up to
1216 * reshape position. We don't know exactly where
1217 * that is, so set to zero for now */
1218 if (mddev->minor_version >= 91) {
1219 rdev->recovery_offset = 0;
1220 rdev->raid_disk = desc->raid_disk;
1221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001223 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1224 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001225 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001226 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return 0;
1228}
1229
1230/*
1231 * sync_super for 0.90.0
1232 */
NeilBrownfd01b882011-10-11 16:47:53 +11001233static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001236 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /* make rdev->sb match mddev data..
1241 *
1242 * 1/ zero out disks
1243 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1244 * 3/ any empty disks < next_spare become removed
1245 *
1246 * disks[0] gets initialised to REMOVED because
1247 * we cannot be sure from other fields if it has
1248 * been initialised or not.
1249 */
1250 int i;
1251 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1252
NeilBrown61181562005-09-09 16:24:02 -07001253 rdev->sb_size = MD_SB_BYTES;
1254
Namhyung Kim65a06f062011-07-27 11:00:36 +10001255 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 memset(sb, 0, sizeof(*sb));
1258
1259 sb->md_magic = MD_SB_MAGIC;
1260 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 sb->patch_version = mddev->patch_version;
1262 sb->gvalid_words = 0; /* ignored */
1263 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1264 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1265 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1266 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1267
1268 sb->ctime = mddev->ctime;
1269 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001270 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 sb->raid_disks = mddev->raid_disks;
1272 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001273 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 sb->utime = mddev->utime;
1275 sb->state = 0;
1276 sb->events_hi = (mddev->events>>32);
1277 sb->events_lo = (u32)mddev->events;
1278
NeilBrownf6705572006-03-27 01:18:11 -08001279 if (mddev->reshape_position == MaxSector)
1280 sb->minor_version = 90;
1281 else {
1282 sb->minor_version = 91;
1283 sb->reshape_position = mddev->reshape_position;
1284 sb->new_level = mddev->new_level;
1285 sb->delta_disks = mddev->delta_disks;
1286 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001287 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001288 }
1289 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (mddev->in_sync)
1291 {
1292 sb->recovery_cp = mddev->recovery_cp;
1293 sb->cp_events_hi = (mddev->events>>32);
1294 sb->cp_events_lo = (u32)mddev->events;
1295 if (mddev->recovery_cp == MaxSector)
1296 sb->state = (1<< MD_SB_CLEAN);
1297 } else
1298 sb->recovery_cp = 0;
1299
1300 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001301 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
NeilBrownc3d97142009-12-14 12:49:52 +11001303 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001304 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001307 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001309 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001310 int is_active = test_bit(In_sync, &rdev2->flags);
1311
1312 if (rdev2->raid_disk >= 0 &&
1313 sb->minor_version >= 91)
1314 /* we have nowhere to store the recovery_offset,
1315 * but if it is not below the reshape_position,
1316 * we can piggy-back on that.
1317 */
1318 is_active = 1;
1319 if (rdev2->raid_disk < 0 ||
1320 test_bit(Faulty, &rdev2->flags))
1321 is_active = 0;
1322 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001323 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001325 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001326 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 d = &sb->disks[rdev2->desc_nr];
1328 nr_disks++;
1329 d->number = rdev2->desc_nr;
1330 d->major = MAJOR(rdev2->bdev->bd_dev);
1331 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001332 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 d->raid_disk = rdev2->raid_disk;
1334 else
1335 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001336 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001338 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001340 if (test_bit(In_sync, &rdev2->flags))
1341 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 active++;
1343 working++;
1344 } else {
1345 d->state = 0;
1346 spare++;
1347 working++;
1348 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001349 if (test_bit(WriteMostly, &rdev2->flags))
1350 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /* now set the "removed" and "faulty" bits on any missing devices */
1353 for (i=0 ; i < mddev->raid_disks ; i++) {
1354 mdp_disk_t *d = &sb->disks[i];
1355 if (d->state == 0 && d->number == 0) {
1356 d->number = i;
1357 d->raid_disk = i;
1358 d->state = (1<<MD_DISK_REMOVED);
1359 d->state |= (1<<MD_DISK_FAULTY);
1360 failed++;
1361 }
1362 }
1363 sb->nr_disks = nr_disks;
1364 sb->active_disks = active;
1365 sb->working_disks = working;
1366 sb->failed_disks = failed;
1367 sb->spare_disks = spare;
1368
1369 sb->this_disk = sb->disks[rdev->desc_nr];
1370 sb->sb_csum = calc_sb_csum(sb);
1371}
1372
1373/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001374 * rdev_size_change for 0.90.0
1375 */
1376static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001377super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001378{
Andre Noll58c0fed2009-03-31 14:33:13 +11001379 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001380 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001381 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001382 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001383 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001384 if (!num_sectors || num_sectors > rdev->sb_start)
1385 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001386 /* Limit to 4TB as metadata cannot record more than that.
1387 * 4TB == 2^32 KB, or 2*2^32 sectors.
1388 */
NeilBrown667a5312012-08-16 16:46:12 +10001389 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001390 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001391 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001392 rdev->sb_page);
1393 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001394 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001395}
1396
NeilBrownc6563a82012-05-21 09:27:00 +10001397static int
1398super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1399{
1400 /* non-zero offset changes not possible with v0.90 */
1401 return new_offset == 0;
1402}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001403
1404/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 * version 1 superblock
1406 */
1407
NeilBrown1c05b4b2006-10-21 10:24:08 -07001408static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001410 __le32 disk_csum;
1411 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 unsigned long long newcsum;
1413 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001414 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 disk_csum = sb->sb_csum;
1417 sb->sb_csum = 0;
1418 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001419 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 newcsum += le32_to_cpu(*isuper++);
1421
1422 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001423 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1426 sb->sb_csum = disk_csum;
1427 return cpu_to_le32(csum);
1428}
1429
NeilBrown2699b672011-07-28 11:31:47 +10001430static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1431 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001432static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct mdp_superblock_1 *sb;
1435 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001436 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001437 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001439 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 /*
Andre Noll0f420352008-07-11 22:02:23 +10001442 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 * It is always aligned to a 4K boundary and
1444 * depeding on minor_version, it can be:
1445 * 0: At least 8K, but less than 12K, from end of device
1446 * 1: At start of device
1447 * 2: 4K from start of device.
1448 */
1449 switch(minor_version) {
1450 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001451 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001452 sb_start -= 8*2;
1453 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 break;
1455 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001456 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 break;
1458 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001459 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 break;
1461 default:
1462 return -EINVAL;
1463 }
Andre Noll0f420352008-07-11 22:02:23 +10001464 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
NeilBrown0002b272005-09-09 16:23:53 -07001466 /* superblock is rarely larger than 1K, but it can be larger,
1467 * and it is safe to read 4k, so we do that
1468 */
1469 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (ret) return ret;
1471
1472
Namhyung Kim65a06f062011-07-27 11:00:36 +10001473 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1476 sb->major_version != cpu_to_le32(1) ||
1477 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001478 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001479 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return -EINVAL;
1481
1482 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1483 printk("md: invalid superblock checksum on %s\n",
1484 bdevname(rdev->bdev,b));
1485 return -EINVAL;
1486 }
1487 if (le64_to_cpu(sb->data_size) < 10) {
1488 printk("md: data_size too small on %s\n",
1489 bdevname(rdev->bdev,b));
1490 return -EINVAL;
1491 }
NeilBrownc6563a82012-05-21 09:27:00 +10001492 if (sb->pad0 ||
1493 sb->pad3[0] ||
1494 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1495 /* Some padding is non-zero, might be a new feature */
1496 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 rdev->preferred_minor = 0xffff;
1499 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001500 rdev->new_data_offset = rdev->data_offset;
1501 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1502 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1503 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001504 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
NeilBrown0002b272005-09-09 16:23:53 -07001506 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001507 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001508 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001509 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1510
1511 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001512 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001513 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001514 if (minor_version
1515 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1516 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001517
NeilBrown31b65a02006-07-10 04:44:14 -07001518 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1519 rdev->desc_nr = -1;
1520 else
1521 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1522
NeilBrown2699b672011-07-28 11:31:47 +10001523 if (!rdev->bb_page) {
1524 rdev->bb_page = alloc_page(GFP_KERNEL);
1525 if (!rdev->bb_page)
1526 return -ENOMEM;
1527 }
1528 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1529 rdev->badblocks.count == 0) {
1530 /* need to load the bad block list.
1531 * Currently we limit it to one page.
1532 */
1533 s32 offset;
1534 sector_t bb_sector;
1535 u64 *bbp;
1536 int i;
1537 int sectors = le16_to_cpu(sb->bblog_size);
1538 if (sectors > (PAGE_SIZE / 512))
1539 return -EINVAL;
1540 offset = le32_to_cpu(sb->bblog_offset);
1541 if (offset == 0)
1542 return -EINVAL;
1543 bb_sector = (long long)offset;
1544 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1545 rdev->bb_page, READ, true))
1546 return -EIO;
1547 bbp = (u64 *)page_address(rdev->bb_page);
1548 rdev->badblocks.shift = sb->bblog_shift;
1549 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1550 u64 bb = le64_to_cpu(*bbp);
1551 int count = bb & (0x3ff);
1552 u64 sector = bb >> 10;
1553 sector <<= sb->bblog_shift;
1554 count <<= sb->bblog_shift;
1555 if (bb + 1 == 0)
1556 break;
1557 if (md_set_badblocks(&rdev->badblocks,
1558 sector, count, 1) == 0)
1559 return -EINVAL;
1560 }
NeilBrown486adf72013-04-24 11:42:44 +10001561 } else if (sb->bblog_offset != 0)
1562 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001563
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001564 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001565 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001566 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001568 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1571 sb->level != refsb->level ||
1572 sb->layout != refsb->layout ||
1573 sb->chunksize != refsb->chunksize) {
1574 printk(KERN_WARNING "md: %s has strangely different"
1575 " superblock to %s\n",
1576 bdevname(rdev->bdev,b),
1577 bdevname(refdev->bdev,b2));
1578 return -EINVAL;
1579 }
1580 ev1 = le64_to_cpu(sb->events);
1581 ev2 = le64_to_cpu(refsb->events);
1582
1583 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001584 ret = 1;
1585 else
1586 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
NeilBrownc6563a82012-05-21 09:27:00 +10001588 if (minor_version) {
1589 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1590 sectors -= rdev->data_offset;
1591 } else
1592 sectors = rdev->sb_start;
1593 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001595 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
NeilBrownfd01b882011-10-11 16:47:53 +11001599static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001601 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001602 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
NeilBrown41158c72005-06-21 17:17:25 -07001604 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001605 clear_bit(Faulty, &rdev->flags);
1606 clear_bit(In_sync, &rdev->flags);
1607 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (mddev->raid_disks == 0) {
1610 mddev->major_version = 1;
1611 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001612 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001613 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1615 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1616 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001617 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 mddev->layout = le32_to_cpu(sb->layout);
1619 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001620 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001621 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001622 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001623 mddev->bitmap_info.space = 0;
1624 /* Default location for bitmap is 1K after superblock
1625 * using 3K - total of 4K
1626 */
NeilBrownc3d97142009-12-14 12:49:52 +11001627 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001628 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001629 mddev->reshape_backwards = 0;
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1632 memcpy(mddev->uuid, sb->set_uuid, 16);
1633
1634 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001635
NeilBrown71c08052005-09-09 16:23:51 -07001636 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001637 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001638 mddev->bitmap_info.offset =
1639 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001640 /* Metadata doesn't record how much space is available.
1641 * For 1.0, we assume we can use up to the superblock
1642 * if before, else to 4K beyond superblock.
1643 * For others, assume no change is possible.
1644 */
1645 if (mddev->minor_version > 0)
1646 mddev->bitmap_info.space = 0;
1647 else if (mddev->bitmap_info.offset > 0)
1648 mddev->bitmap_info.space =
1649 8 - mddev->bitmap_info.offset;
1650 else
1651 mddev->bitmap_info.space =
1652 -mddev->bitmap_info.offset;
1653 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001654
NeilBrownf6705572006-03-27 01:18:11 -08001655 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1656 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1657 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1658 mddev->new_level = le32_to_cpu(sb->new_level);
1659 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001660 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001661 if (mddev->delta_disks < 0 ||
1662 (mddev->delta_disks == 0 &&
1663 (le32_to_cpu(sb->feature_map)
1664 & MD_FEATURE_RESHAPE_BACKWARDS)))
1665 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001666 } else {
1667 mddev->reshape_position = MaxSector;
1668 mddev->delta_disks = 0;
1669 mddev->new_level = mddev->level;
1670 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001671 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001672 }
1673
NeilBrown41158c72005-06-21 17:17:25 -07001674 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001675 /* Insist of good event counter while assembling, except for
1676 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001678 if (rdev->desc_nr >= 0 &&
1679 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1680 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1681 if (ev1 < mddev->events)
1682 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001683 } else if (mddev->bitmap) {
1684 /* If adding to array with a bitmap, then we can accept an
1685 * older device, but not too old.
1686 */
NeilBrown41158c72005-06-21 17:17:25 -07001687 if (ev1 < mddev->bitmap->events_cleared)
1688 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001689 } else {
1690 if (ev1 < mddev->events)
1691 /* just a hot-add of a new device, leave raid_disk at -1 */
1692 return 0;
1693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (mddev->level != LEVEL_MULTIPATH) {
1695 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001696 if (rdev->desc_nr < 0 ||
1697 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1698 role = 0xffff;
1699 rdev->desc_nr = -1;
1700 } else
1701 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 switch(role) {
1703 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 break;
1705 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001706 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 break;
1708 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001709 if ((le32_to_cpu(sb->feature_map) &
1710 MD_FEATURE_RECOVERY_OFFSET))
1711 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1712 else
1713 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 rdev->raid_disk = role;
1715 break;
1716 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001717 if (sb->devflags & WriteMostly1)
1718 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001719 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1720 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001721 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001722 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return 0;
1725}
1726
NeilBrownfd01b882011-10-11 16:47:53 +11001727static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001730 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 int max_dev, i;
1732 /* make rdev->sb match mddev and rdev data. */
1733
Namhyung Kim65a06f062011-07-27 11:00:36 +10001734 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 sb->feature_map = 0;
1737 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001738 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 memset(sb->pad3, 0, sizeof(sb->pad3));
1740
1741 sb->utime = cpu_to_le64((__u64)mddev->utime);
1742 sb->events = cpu_to_le64(mddev->events);
1743 if (mddev->in_sync)
1744 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1745 else
1746 sb->resync_offset = cpu_to_le64(0);
1747
NeilBrown1c05b4b2006-10-21 10:24:08 -07001748 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001749
NeilBrownf0ca3402006-02-02 14:28:04 -08001750 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001751 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001752 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001753 sb->level = cpu_to_le32(mddev->level);
1754 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001755
NeilBrownaeb9b212011-08-25 14:43:08 +10001756 if (test_bit(WriteMostly, &rdev->flags))
1757 sb->devflags |= WriteMostly1;
1758 else
1759 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001760 sb->data_offset = cpu_to_le64(rdev->data_offset);
1761 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001762
NeilBrownc3d97142009-12-14 12:49:52 +11001763 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1764 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001765 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001766 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001767
1768 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001769 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001770 sb->feature_map |=
1771 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1772 sb->recovery_offset =
1773 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001774 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001775 if (test_bit(Replacement, &rdev->flags))
1776 sb->feature_map |=
1777 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001778
NeilBrownf6705572006-03-27 01:18:11 -08001779 if (mddev->reshape_position != MaxSector) {
1780 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1781 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1782 sb->new_layout = cpu_to_le32(mddev->new_layout);
1783 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1784 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001785 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001786 if (mddev->delta_disks == 0 &&
1787 mddev->reshape_backwards)
1788 sb->feature_map
1789 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001790 if (rdev->new_data_offset != rdev->data_offset) {
1791 sb->feature_map
1792 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1793 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1794 - rdev->data_offset));
1795 }
NeilBrownf6705572006-03-27 01:18:11 -08001796 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001797
NeilBrown2699b672011-07-28 11:31:47 +10001798 if (rdev->badblocks.count == 0)
1799 /* Nothing to do for bad blocks*/ ;
1800 else if (sb->bblog_offset == 0)
1801 /* Cannot record bad blocks on this device */
1802 md_error(mddev, rdev);
1803 else {
1804 struct badblocks *bb = &rdev->badblocks;
1805 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1806 u64 *p = bb->page;
1807 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1808 if (bb->changed) {
1809 unsigned seq;
1810
1811retry:
1812 seq = read_seqbegin(&bb->lock);
1813
1814 memset(bbp, 0xff, PAGE_SIZE);
1815
1816 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001817 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001818 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1819 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001820 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001821 }
NeilBrownd0962932012-03-19 12:46:41 +11001822 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001823 if (read_seqretry(&bb->lock, seq))
1824 goto retry;
1825
1826 bb->sector = (rdev->sb_start +
1827 (int)le32_to_cpu(sb->bblog_offset));
1828 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001829 }
1830 }
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001833 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (rdev2->desc_nr+1 > max_dev)
1835 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001836
NeilBrown70471da2009-08-03 10:59:57 +10001837 if (max_dev > le32_to_cpu(sb->max_dev)) {
1838 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001839 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001840 rdev->sb_size = max_dev * 2 + 256;
1841 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1842 if (rdev->sb_size & bmask)
1843 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001844 } else
1845 max_dev = le32_to_cpu(sb->max_dev);
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 for (i=0; i<max_dev;i++)
1848 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1849
NeilBrowndafb20f2012-03-19 12:46:39 +11001850 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001852 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001854 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001856 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001857 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 else
1859 sb->dev_roles[i] = cpu_to_le16(0xffff);
1860 }
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 sb->sb_csum = calc_sb_1_csum(sb);
1863}
1864
Chris Webb0cd17fe2008-06-28 08:31:46 +10001865static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001866super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001867{
1868 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001869 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001870 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001871 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001872 if (rdev->data_offset != rdev->new_data_offset)
1873 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001874 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001875 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001876 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001877 max_sectors -= rdev->data_offset;
1878 if (!num_sectors || num_sectors > max_sectors)
1879 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001880 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001881 /* minor version 0 with bitmap we can't move */
1882 return 0;
1883 } else {
1884 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001885 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001886 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001887 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001888 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001889 if (!num_sectors || num_sectors > max_sectors)
1890 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001891 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001892 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001893 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001894 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001895 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001896 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001897 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001898 rdev->sb_page);
1899 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001900 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001901
1902}
1903
1904static int
1905super_1_allow_new_offset(struct md_rdev *rdev,
1906 unsigned long long new_offset)
1907{
1908 /* All necessary checks on new >= old have been done */
1909 struct bitmap *bitmap;
1910 if (new_offset >= rdev->data_offset)
1911 return 1;
1912
1913 /* with 1.0 metadata, there is no metadata to tread on
1914 * so we can always move back */
1915 if (rdev->mddev->minor_version == 0)
1916 return 1;
1917
1918 /* otherwise we must be sure not to step on
1919 * any metadata, so stay:
1920 * 36K beyond start of superblock
1921 * beyond end of badblocks
1922 * beyond write-intent bitmap
1923 */
1924 if (rdev->sb_start + (32+4)*2 > new_offset)
1925 return 0;
1926 bitmap = rdev->mddev->bitmap;
1927 if (bitmap && !rdev->mddev->bitmap_info.file &&
1928 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001929 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001930 return 0;
1931 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1932 return 0;
1933
1934 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001935}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Adrian Bunk75c96f82005-05-05 16:16:09 -07001937static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 [0] = {
1939 .name = "0.90.0",
1940 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001941 .load_super = super_90_load,
1942 .validate_super = super_90_validate,
1943 .sync_super = super_90_sync,
1944 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001945 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 },
1947 [1] = {
1948 .name = "md-1",
1949 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001950 .load_super = super_1_load,
1951 .validate_super = super_1_validate,
1952 .sync_super = super_1_sync,
1953 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001954 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 },
1956};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
NeilBrownfd01b882011-10-11 16:47:53 +11001958static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001959{
1960 if (mddev->sync_super) {
1961 mddev->sync_super(mddev, rdev);
1962 return;
1963 }
1964
1965 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1966
1967 super_types[mddev->major_version].sync_super(mddev, rdev);
1968}
1969
NeilBrownfd01b882011-10-11 16:47:53 +11001970static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
NeilBrown3cb03002011-10-11 16:45:26 +11001972 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
NeilBrown4b809912008-07-21 17:05:25 +10001974 rcu_read_lock();
1975 rdev_for_each_rcu(rdev, mddev1)
1976 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001977 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001978 rdev2->bdev->bd_contains) {
1979 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001980 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001981 }
1982 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return 0;
1984}
1985
1986static LIST_HEAD(pending_raid_disks);
1987
Andre Nollac5e7112009-08-03 10:59:47 +10001988/*
1989 * Try to register data integrity profile for an mddev
1990 *
1991 * This is called when an array is started and after a disk has been kicked
1992 * from the array. It only succeeds if all working and active component devices
1993 * are integrity capable with matching profiles.
1994 */
NeilBrownfd01b882011-10-11 16:47:53 +11001995int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001996{
NeilBrown3cb03002011-10-11 16:45:26 +11001997 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001998
Andre Nollac5e7112009-08-03 10:59:47 +10001999 if (list_empty(&mddev->disks))
2000 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002001 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2002 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002003 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002004 /* skip spares and non-functional disks */
2005 if (test_bit(Faulty, &rdev->flags))
2006 continue;
2007 if (rdev->raid_disk < 0)
2008 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002009 if (!reference) {
2010 /* Use the first rdev as the reference */
2011 reference = rdev;
2012 continue;
2013 }
2014 /* does this rdev's profile match the reference profile? */
2015 if (blk_integrity_compare(reference->bdev->bd_disk,
2016 rdev->bdev->bd_disk) < 0)
2017 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002018 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002019 if (!reference || !bdev_get_integrity(reference->bdev))
2020 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002021 /*
2022 * All component devices are integrity capable and have matching
2023 * profiles, register the common profile for the md device.
2024 */
2025 if (blk_integrity_register(mddev->gendisk,
2026 bdev_get_integrity(reference->bdev)) != 0) {
2027 printk(KERN_ERR "md: failed to register integrity for %s\n",
2028 mdname(mddev));
2029 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002030 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002031 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2032 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2033 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2034 mdname(mddev));
2035 return -EINVAL;
2036 }
Andre Nollac5e7112009-08-03 10:59:47 +10002037 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002038}
Andre Nollac5e7112009-08-03 10:59:47 +10002039EXPORT_SYMBOL(md_integrity_register);
2040
2041/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002042void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002043{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002044 struct blk_integrity *bi_rdev;
2045 struct blk_integrity *bi_mddev;
2046
2047 if (!mddev->gendisk)
2048 return;
2049
2050 bi_rdev = bdev_get_integrity(rdev->bdev);
2051 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002052
2053 if (!bi_mddev) /* nothing to do */
2054 return;
2055 if (rdev->raid_disk < 0) /* skip spares */
2056 return;
2057 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2058 rdev->bdev->bd_disk) >= 0)
2059 return;
2060 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2061 blk_integrity_unregister(mddev->gendisk);
2062}
2063EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002064
NeilBrownfd01b882011-10-11 16:47:53 +11002065static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002067 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002068 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002069 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002070 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 if (rdev->mddev) {
2073 MD_BUG();
2074 return -EINVAL;
2075 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002076
2077 /* prevent duplicates */
2078 if (find_rdev(mddev, rdev->bdev->bd_dev))
2079 return -EEXIST;
2080
Andre Nolldd8ac332009-03-31 14:33:13 +11002081 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2082 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2083 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002084 if (mddev->pers) {
2085 /* Cannot change size, so fail
2086 * If mddev->level <= 0, then we don't care
2087 * about aligning sizes (e.g. linear)
2088 */
2089 if (mddev->level > 0)
2090 return -ENOSPC;
2091 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002092 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 /* Verify rdev->desc_nr is unique.
2096 * If it is -1, assign a free number, else
2097 * check number is not in use
2098 */
2099 if (rdev->desc_nr < 0) {
2100 int choice = 0;
2101 if (mddev->pers) choice = mddev->raid_disks;
2102 while (find_rdev_nr(mddev, choice))
2103 choice++;
2104 rdev->desc_nr = choice;
2105 } else {
2106 if (find_rdev_nr(mddev, rdev->desc_nr))
2107 return -EBUSY;
2108 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002109 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2110 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2111 mdname(mddev), mddev->max_disks);
2112 return -EBUSY;
2113 }
NeilBrown19133a42005-11-08 21:39:35 -08002114 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002115 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002116 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002119 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002120
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002121 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002122 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002123
Tejun Heo0762b8b2008-08-25 19:56:12 +09002124 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002125 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2126 /* failure here is OK */;
2127 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002128
NeilBrown4b809912008-07-21 17:05:25 +10002129 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002130 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002131
2132 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002133 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002136
2137 fail:
2138 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2139 b, mdname(mddev));
2140 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
NeilBrown177a99b2008-02-06 01:39:56 -08002143static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002144{
NeilBrown3cb03002011-10-11 16:45:26 +11002145 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002146 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002147 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002148}
2149
NeilBrown3cb03002011-10-11 16:45:26 +11002150static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 char b[BDEVNAME_SIZE];
2153 if (!rdev->mddev) {
2154 MD_BUG();
2155 return;
2156 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002157 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002158 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2160 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002161 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002162 sysfs_put(rdev->sysfs_state);
2163 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002164 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002165 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002166 * writing to 'remove' to "dev/state". We also need
2167 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002168 */
NeilBrown4b809912008-07-21 17:05:25 +10002169 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002170 INIT_WORK(&rdev->del_work, md_delayed_delete);
2171 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002172 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
2175/*
2176 * prevent the device from being mounted, repartitioned or
2177 * otherwise reused by a RAID array (or any other kernel
2178 * subsystem), by bd_claiming the device.
2179 */
NeilBrown3cb03002011-10-11 16:45:26 +11002180static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 int err = 0;
2183 struct block_device *bdev;
2184 char b[BDEVNAME_SIZE];
2185
Tejun Heod4d77622010-11-13 11:55:18 +01002186 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002187 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (IS_ERR(bdev)) {
2189 printk(KERN_ERR "md: could not open %s.\n",
2190 __bdevname(dev, b));
2191 return PTR_ERR(bdev);
2192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 rdev->bdev = bdev;
2194 return err;
2195}
2196
NeilBrown3cb03002011-10-11 16:45:26 +11002197static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
2199 struct block_device *bdev = rdev->bdev;
2200 rdev->bdev = NULL;
2201 if (!bdev)
2202 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002203 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
2206void md_autodetect_dev(dev_t dev);
2207
NeilBrown3cb03002011-10-11 16:45:26 +11002208static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 char b[BDEVNAME_SIZE];
2211 printk(KERN_INFO "md: export_rdev(%s)\n",
2212 bdevname(rdev->bdev,b));
2213 if (rdev->mddev)
2214 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002215 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002217 if (test_bit(AutoDetected, &rdev->flags))
2218 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219#endif
2220 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002221 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
NeilBrown3cb03002011-10-11 16:45:26 +11002224static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
2226 unbind_rdev_from_array(rdev);
2227 export_rdev(rdev);
2228}
2229
NeilBrownfd01b882011-10-11 16:47:53 +11002230static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
NeilBrown3cb03002011-10-11 16:45:26 +11002232 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
NeilBrowndafb20f2012-03-19 12:46:39 +11002234 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (!rdev->mddev) {
2236 MD_BUG();
2237 continue;
2238 }
2239 kick_rdev_from_array(rdev);
2240 }
2241 if (!list_empty(&mddev->disks))
2242 MD_BUG();
2243 mddev->raid_disks = 0;
2244 mddev->major_version = 0;
2245}
2246
2247static void print_desc(mdp_disk_t *desc)
2248{
2249 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2250 desc->major,desc->minor,desc->raid_disk,desc->state);
2251}
2252
Cheng Renquancd2ac932009-01-09 08:31:08 +11002253static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 int i;
2256
2257 printk(KERN_INFO
2258 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2259 sb->major_version, sb->minor_version, sb->patch_version,
2260 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2261 sb->ctime);
2262 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2263 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2264 sb->md_minor, sb->layout, sb->chunk_size);
2265 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2266 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2267 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2268 sb->failed_disks, sb->spare_disks,
2269 sb->sb_csum, (unsigned long)sb->events_lo);
2270
2271 printk(KERN_INFO);
2272 for (i = 0; i < MD_SB_DISKS; i++) {
2273 mdp_disk_t *desc;
2274
2275 desc = sb->disks + i;
2276 if (desc->number || desc->major || desc->minor ||
2277 desc->raid_disk || (desc->state && (desc->state != 4))) {
2278 printk(" D %2d: ", i);
2279 print_desc(desc);
2280 }
2281 }
2282 printk(KERN_INFO "md: THIS: ");
2283 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
Cheng Renquancd2ac932009-01-09 08:31:08 +11002286static void print_sb_1(struct mdp_superblock_1 *sb)
2287{
2288 __u8 *uuid;
2289
2290 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002291 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002292 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002293 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002294 le32_to_cpu(sb->major_version),
2295 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002296 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002297 sb->set_name,
2298 (unsigned long long)le64_to_cpu(sb->ctime)
2299 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2300
2301 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002302 printk(KERN_INFO
2303 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002304 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002305 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002306 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2307 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002308 le32_to_cpu(sb->level),
2309 (unsigned long long)le64_to_cpu(sb->size),
2310 le32_to_cpu(sb->raid_disks),
2311 le32_to_cpu(sb->layout),
2312 le32_to_cpu(sb->chunksize),
2313 (unsigned long long)le64_to_cpu(sb->data_offset),
2314 (unsigned long long)le64_to_cpu(sb->data_size),
2315 (unsigned long long)le64_to_cpu(sb->super_offset),
2316 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2317 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002318 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002319 sb->devflags,
2320 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2321 (unsigned long long)le64_to_cpu(sb->events),
2322 (unsigned long long)le64_to_cpu(sb->resync_offset),
2323 le32_to_cpu(sb->sb_csum),
2324 le32_to_cpu(sb->max_dev)
2325 );
2326}
2327
NeilBrown3cb03002011-10-11 16:45:26 +11002328static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
2330 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002331 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2332 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002333 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2334 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002336 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2337 switch (major_version) {
2338 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002339 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002340 break;
2341 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002342 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002343 break;
2344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 } else
2346 printk(KERN_INFO "md: no rdev superblock!\n");
2347}
2348
Adrian Bunk5e563412006-06-26 00:27:42 -07002349static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002351 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002352 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002353 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 char b[BDEVNAME_SIZE];
2355
2356 printk("\n");
2357 printk("md: **********************************\n");
2358 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2359 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002360 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
NeilBrown32a76272005-06-21 17:17:14 -07002362 if (mddev->bitmap)
2363 bitmap_print_sb(mddev->bitmap);
2364 else
2365 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002366 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 printk("<%s>", bdevname(rdev->bdev,b));
2368 printk("\n");
2369
NeilBrowndafb20f2012-03-19 12:46:39 +11002370 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002371 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
2373 printk("md: **********************************\n");
2374 printk("\n");
2375}
2376
2377
NeilBrownfd01b882011-10-11 16:47:53 +11002378static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
NeilBrown42543762006-06-26 00:27:57 -07002380 /* Update each superblock (in-memory image), but
2381 * if we are allowed to, skip spares which already
2382 * have the right event counter, or have one earlier
2383 * (which would mean they aren't being marked as dirty
2384 * with the rest of the array)
2385 */
NeilBrown3cb03002011-10-11 16:45:26 +11002386 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002387 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002388 if (rdev->sb_events == mddev->events ||
2389 (nospares &&
2390 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002391 rdev->sb_events+1 == mddev->events)) {
2392 /* Don't update this superblock */
2393 rdev->sb_loaded = 2;
2394 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002395 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002396 rdev->sb_loaded = 1;
2397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399}
2400
NeilBrownfd01b882011-10-11 16:47:53 +11002401static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
NeilBrown3cb03002011-10-11 16:45:26 +11002403 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002404 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002405 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002406 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
NeilBrownd87f0642013-04-24 11:42:40 +10002408 if (mddev->ro) {
2409 if (force_change)
2410 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2411 return;
2412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002414 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002415 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002416 if (rdev->raid_disk >= 0 &&
2417 mddev->delta_disks >= 0 &&
2418 !test_bit(In_sync, &rdev->flags) &&
2419 mddev->curr_resync_completed > rdev->recovery_offset)
2420 rdev->recovery_offset = mddev->curr_resync_completed;
2421
2422 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002423 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002424 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002425 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002426 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002427 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002428 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002429 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002430 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002431 md_ack_all_badblocks(&rdev->badblocks);
2432 md_error(mddev, rdev);
2433 }
2434 clear_bit(Blocked, &rdev->flags);
2435 clear_bit(BlockedBadBlocks, &rdev->flags);
2436 wake_up(&rdev->blocked_wait);
2437 }
2438 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002439 wake_up(&mddev->sb_wait);
2440 return;
2441 }
2442
NeilBrowna9701a32005-11-08 21:39:34 -08002443 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002444
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002445 mddev->utime = get_seconds();
2446
NeilBrown850b2b42006-10-03 01:15:46 -07002447 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2448 force_change = 1;
2449 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2450 /* just a clean<-> dirty transition, possibly leave spares alone,
2451 * though if events isn't the right even/odd, we will have to do
2452 * spares after all
2453 */
2454 nospares = 1;
2455 if (force_change)
2456 nospares = 0;
2457 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002458 /* If the array is degraded, then skipping spares is both
2459 * dangerous and fairly pointless.
2460 * Dangerous because a device that was removed from the array
2461 * might have a event_count that still looks up-to-date,
2462 * so it can be re-added without a resync.
2463 * Pointless because if there are any spares to skip,
2464 * then a recovery will happen and soon that array won't
2465 * be degraded any more and the spare can go back to sleep then.
2466 */
NeilBrown850b2b42006-10-03 01:15:46 -07002467 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002468
NeilBrown06d91a52005-06-21 17:17:12 -07002469 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002470
2471 /* If this is just a dirty<->clean transition, and the array is clean
2472 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002473 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002474 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002475 && mddev->can_decrease_events
2476 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002477 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002478 mddev->can_decrease_events = 0;
2479 } else {
NeilBrown42543762006-06-26 00:27:57 -07002480 /* otherwise we have to go forward and ... */
2481 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002482 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 if (!mddev->events) {
2486 /*
2487 * oops, this 64-bit counter should never wrap.
2488 * Either we are in around ~1 trillion A.C., assuming
2489 * 1 reboot per second, or we have a bug:
2490 */
2491 MD_BUG();
2492 mddev->events --;
2493 }
NeilBrown2699b672011-07-28 11:31:47 +10002494
NeilBrowndafb20f2012-03-19 12:46:39 +11002495 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002496 if (rdev->badblocks.changed)
2497 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002498 if (test_bit(Faulty, &rdev->flags))
2499 set_bit(FaultRecorded, &rdev->flags);
2500 }
NeilBrown2699b672011-07-28 11:31:47 +10002501
NeilBrowne6910632008-02-06 01:39:51 -08002502 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002503 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
NeilBrown36a4e1f2011-10-07 14:23:17 +11002505 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2506 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
NeilBrown4ad13662007-07-17 04:06:13 -07002508 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002509 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002511
NeilBrown42543762006-06-26 00:27:57 -07002512 if (rdev->sb_loaded != 1)
2513 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002515 if (!test_bit(Faulty, &rdev->flags) &&
2516 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002517 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002518 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002519 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002520 pr_debug("md: (write) %s's sb offset: %llu\n",
2521 bdevname(rdev->bdev, b),
2522 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002523 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002524 if (rdev->badblocks.size) {
2525 md_super_write(mddev, rdev,
2526 rdev->badblocks.sector,
2527 rdev->badblocks.size << 9,
2528 rdev->bb_page);
2529 rdev->badblocks.size = 0;
2530 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002531
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002532 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002533 pr_debug("md: %s (skipping faulty)\n",
2534 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002535 else
2536 pr_debug("(skipping incremental s/r ");
2537
NeilBrown7bfa19f2005-06-21 17:17:28 -07002538 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 /* only need to write one superblock... */
2540 break;
2541 }
NeilBrowna9701a32005-11-08 21:39:34 -08002542 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002543 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002544
NeilBrowna9701a32005-11-08 21:39:34 -08002545 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002546 if (mddev->in_sync != sync_req ||
2547 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002548 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002549 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002550 goto repeat;
2551 }
NeilBrown850b2b42006-10-03 01:15:46 -07002552 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002553 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002554 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002555 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2556 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002557
NeilBrowndafb20f2012-03-19 12:46:39 +11002558 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002559 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2560 clear_bit(Blocked, &rdev->flags);
2561
2562 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002563 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002564 clear_bit(BlockedBadBlocks, &rdev->flags);
2565 wake_up(&rdev->blocked_wait);
2566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
Andre Noll7f6ce762008-03-23 18:34:54 +01002569/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002570 * We want to accept with case. For this we use cmd_match.
2571 */
2572static int cmd_match(const char *cmd, const char *str)
2573{
2574 /* See if cmd, written into a sysfs file, matches
2575 * str. They must either be the same, or cmd can
2576 * have a trailing newline
2577 */
2578 while (*cmd && *str && *cmd == *str) {
2579 cmd++;
2580 str++;
2581 }
2582 if (*cmd == '\n')
2583 cmd++;
2584 if (*str || *cmd)
2585 return 0;
2586 return 1;
2587}
2588
NeilBrown86e6ffd2005-11-08 21:39:24 -08002589struct rdev_sysfs_entry {
2590 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002591 ssize_t (*show)(struct md_rdev *, char *);
2592 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002593};
2594
2595static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002596state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002597{
2598 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002599 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002600
NeilBrownde393cd2011-07-28 11:31:48 +10002601 if (test_bit(Faulty, &rdev->flags) ||
2602 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002603 len+= sprintf(page+len, "%sfaulty",sep);
2604 sep = ",";
2605 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002606 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002607 len += sprintf(page+len, "%sin_sync",sep);
2608 sep = ",";
2609 }
NeilBrownf6556752006-06-26 00:28:01 -07002610 if (test_bit(WriteMostly, &rdev->flags)) {
2611 len += sprintf(page+len, "%swrite_mostly",sep);
2612 sep = ",";
2613 }
NeilBrownde393cd2011-07-28 11:31:48 +10002614 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002615 (rdev->badblocks.unacked_exist
2616 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002617 len += sprintf(page+len, "%sblocked", sep);
2618 sep = ",";
2619 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002620 if (!test_bit(Faulty, &rdev->flags) &&
2621 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002622 len += sprintf(page+len, "%sspare", sep);
2623 sep = ",";
2624 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002625 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2626 len += sprintf(page+len, "%swrite_error", sep);
2627 sep = ",";
2628 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002629 if (test_bit(WantReplacement, &rdev->flags)) {
2630 len += sprintf(page+len, "%swant_replacement", sep);
2631 sep = ",";
2632 }
2633 if (test_bit(Replacement, &rdev->flags)) {
2634 len += sprintf(page+len, "%sreplacement", sep);
2635 sep = ",";
2636 }
2637
NeilBrown86e6ffd2005-11-08 21:39:24 -08002638 return len+sprintf(page+len, "\n");
2639}
2640
NeilBrown45dc2de2006-06-26 00:27:58 -07002641static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002642state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002643{
2644 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002645 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002646 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002647 * writemostly - sets write_mostly
2648 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002649 * blocked - sets the Blocked flags
2650 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002651 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002652 * write_error - sets WriteErrorSeen
2653 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002654 */
2655 int err = -EINVAL;
2656 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2657 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002658 if (test_bit(Faulty, &rdev->flags))
2659 err = 0;
2660 else
2661 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002662 } else if (cmd_match(buf, "remove")) {
2663 if (rdev->raid_disk >= 0)
2664 err = -EBUSY;
2665 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002666 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002667 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002668 if (mddev->pers)
2669 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002670 md_new_event(mddev);
2671 err = 0;
2672 }
NeilBrownf6556752006-06-26 00:28:01 -07002673 } else if (cmd_match(buf, "writemostly")) {
2674 set_bit(WriteMostly, &rdev->flags);
2675 err = 0;
2676 } else if (cmd_match(buf, "-writemostly")) {
2677 clear_bit(WriteMostly, &rdev->flags);
2678 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002679 } else if (cmd_match(buf, "blocked")) {
2680 set_bit(Blocked, &rdev->flags);
2681 err = 0;
2682 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002683 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002684 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002685 /* metadata handler doesn't understand badblocks,
2686 * so we need to fail the device
2687 */
2688 md_error(rdev->mddev, rdev);
2689 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002690 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002691 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002692 wake_up(&rdev->blocked_wait);
2693 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2694 md_wakeup_thread(rdev->mddev->thread);
2695
2696 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002697 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2698 set_bit(In_sync, &rdev->flags);
2699 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002700 } else if (cmd_match(buf, "write_error")) {
2701 set_bit(WriteErrorSeen, &rdev->flags);
2702 err = 0;
2703 } else if (cmd_match(buf, "-write_error")) {
2704 clear_bit(WriteErrorSeen, &rdev->flags);
2705 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002706 } else if (cmd_match(buf, "want_replacement")) {
2707 /* Any non-spare device that is not a replacement can
2708 * become want_replacement at any time, but we then need to
2709 * check if recovery is needed.
2710 */
2711 if (rdev->raid_disk >= 0 &&
2712 !test_bit(Replacement, &rdev->flags))
2713 set_bit(WantReplacement, &rdev->flags);
2714 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2715 md_wakeup_thread(rdev->mddev->thread);
2716 err = 0;
2717 } else if (cmd_match(buf, "-want_replacement")) {
2718 /* Clearing 'want_replacement' is always allowed.
2719 * Once replacements starts it is too late though.
2720 */
2721 err = 0;
2722 clear_bit(WantReplacement, &rdev->flags);
2723 } else if (cmd_match(buf, "replacement")) {
2724 /* Can only set a device as a replacement when array has not
2725 * yet been started. Once running, replacement is automatic
2726 * from spares, or by assigning 'slot'.
2727 */
2728 if (rdev->mddev->pers)
2729 err = -EBUSY;
2730 else {
2731 set_bit(Replacement, &rdev->flags);
2732 err = 0;
2733 }
2734 } else if (cmd_match(buf, "-replacement")) {
2735 /* Similarly, can only clear Replacement before start */
2736 if (rdev->mddev->pers)
2737 err = -EBUSY;
2738 else {
2739 clear_bit(Replacement, &rdev->flags);
2740 err = 0;
2741 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002742 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002743 if (!err)
2744 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002745 return err ? err : len;
2746}
NeilBrown80ca3a42006-07-10 04:44:18 -07002747static struct rdev_sysfs_entry rdev_state =
2748__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002749
2750static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002751errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002752{
2753 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2754}
2755
2756static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002757errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002758{
2759 char *e;
2760 unsigned long n = simple_strtoul(buf, &e, 10);
2761 if (*buf && (*e == 0 || *e == '\n')) {
2762 atomic_set(&rdev->corrected_errors, n);
2763 return len;
2764 }
2765 return -EINVAL;
2766}
2767static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002768__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002769
NeilBrown014236d2006-01-06 00:20:55 -08002770static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002771slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002772{
2773 if (rdev->raid_disk < 0)
2774 return sprintf(page, "none\n");
2775 else
2776 return sprintf(page, "%d\n", rdev->raid_disk);
2777}
2778
2779static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002780slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002781{
2782 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002783 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002784 int slot = simple_strtoul(buf, &e, 10);
2785 if (strncmp(buf, "none", 4)==0)
2786 slot = -1;
2787 else if (e==buf || (*e && *e!= '\n'))
2788 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002789 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002790 /* Setting 'slot' on an active array requires also
2791 * updating the 'rd%d' link, and communicating
2792 * with the personality with ->hot_*_disk.
2793 * For now we only support removing
2794 * failed/spare devices. This normally happens automatically,
2795 * but not when the metadata is externally managed.
2796 */
NeilBrownc303da62008-02-06 01:39:51 -08002797 if (rdev->raid_disk == -1)
2798 return -EEXIST;
2799 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002800 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002801 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002802 clear_bit(Blocked, &rdev->flags);
2803 remove_and_add_spares(rdev->mddev, rdev);
2804 if (rdev->raid_disk >= 0)
2805 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002806 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2807 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002808 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002809 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002810 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002811 */
2812
2813 if (rdev->raid_disk != -1)
2814 return -EBUSY;
2815
NeilBrownc6751b22011-02-02 11:57:13 +11002816 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2817 return -EBUSY;
2818
Neil Brown6c2fce22008-06-28 08:31:31 +10002819 if (rdev->mddev->pers->hot_add_disk == NULL)
2820 return -EINVAL;
2821
NeilBrownba1b41b2011-01-14 09:14:34 +11002822 if (slot >= rdev->mddev->raid_disks &&
2823 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2824 return -ENOSPC;
2825
Neil Brown6c2fce22008-06-28 08:31:31 +10002826 rdev->raid_disk = slot;
2827 if (test_bit(In_sync, &rdev->flags))
2828 rdev->saved_raid_disk = slot;
2829 else
2830 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002831 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002832 err = rdev->mddev->pers->
2833 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002834 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002835 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002836 return err;
Neil Brown52664732008-06-28 08:31:44 +10002837 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002838 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002839 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002840 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002841 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002842 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002843 if (slot >= rdev->mddev->raid_disks &&
2844 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002845 return -ENOSPC;
2846 rdev->raid_disk = slot;
2847 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002848 clear_bit(Faulty, &rdev->flags);
2849 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002850 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002851 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002852 }
NeilBrown014236d2006-01-06 00:20:55 -08002853 return len;
2854}
2855
2856
2857static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002858__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002859
NeilBrown93c8cad2006-01-06 00:20:56 -08002860static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002861offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002862{
Andrew Morton6961ece2006-01-06 00:20:59 -08002863 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002864}
2865
2866static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002867offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002868{
NeilBrownc6563a82012-05-21 09:27:00 +10002869 unsigned long long offset;
2870 if (strict_strtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002871 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002872 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002873 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002874 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002875 /* Must set offset before size, so overlap checks
2876 * can be sane */
2877 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002878 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002879 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002880 return len;
2881}
2882
2883static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002884__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002885
NeilBrownc6563a82012-05-21 09:27:00 +10002886static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2887{
2888 return sprintf(page, "%llu\n",
2889 (unsigned long long)rdev->new_data_offset);
2890}
2891
2892static ssize_t new_offset_store(struct md_rdev *rdev,
2893 const char *buf, size_t len)
2894{
2895 unsigned long long new_offset;
2896 struct mddev *mddev = rdev->mddev;
2897
2898 if (strict_strtoull(buf, 10, &new_offset) < 0)
2899 return -EINVAL;
2900
2901 if (mddev->sync_thread)
2902 return -EBUSY;
2903 if (new_offset == rdev->data_offset)
2904 /* reset is always permitted */
2905 ;
2906 else if (new_offset > rdev->data_offset) {
2907 /* must not push array size beyond rdev_sectors */
2908 if (new_offset - rdev->data_offset
2909 + mddev->dev_sectors > rdev->sectors)
2910 return -E2BIG;
2911 }
2912 /* Metadata worries about other space details. */
2913
2914 /* decreasing the offset is inconsistent with a backwards
2915 * reshape.
2916 */
2917 if (new_offset < rdev->data_offset &&
2918 mddev->reshape_backwards)
2919 return -EINVAL;
2920 /* Increasing offset is inconsistent with forwards
2921 * reshape. reshape_direction should be set to
2922 * 'backwards' first.
2923 */
2924 if (new_offset > rdev->data_offset &&
2925 !mddev->reshape_backwards)
2926 return -EINVAL;
2927
2928 if (mddev->pers && mddev->persistent &&
2929 !super_types[mddev->major_version]
2930 .allow_new_offset(rdev, new_offset))
2931 return -E2BIG;
2932 rdev->new_data_offset = new_offset;
2933 if (new_offset > rdev->data_offset)
2934 mddev->reshape_backwards = 1;
2935 else if (new_offset < rdev->data_offset)
2936 mddev->reshape_backwards = 0;
2937
2938 return len;
2939}
2940static struct rdev_sysfs_entry rdev_new_offset =
2941__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2942
NeilBrown83303b62006-01-06 00:21:06 -08002943static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002944rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002945{
Andre Nolldd8ac332009-03-31 14:33:13 +11002946 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002947}
2948
NeilBrownc5d79ad2008-02-06 01:39:54 -08002949static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2950{
2951 /* check if two start/length pairs overlap */
2952 if (s1+l1 <= s2)
2953 return 0;
2954 if (s2+l2 <= s1)
2955 return 0;
2956 return 1;
2957}
2958
Dan Williamsb522adc2009-03-31 15:00:31 +11002959static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2960{
2961 unsigned long long blocks;
2962 sector_t new;
2963
2964 if (strict_strtoull(buf, 10, &blocks) < 0)
2965 return -EINVAL;
2966
2967 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2968 return -EINVAL; /* sector conversion overflow */
2969
2970 new = blocks * 2;
2971 if (new != blocks * 2)
2972 return -EINVAL; /* unsigned long long to sector_t overflow */
2973
2974 *sectors = new;
2975 return 0;
2976}
2977
NeilBrown83303b62006-01-06 00:21:06 -08002978static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002979rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002980{
NeilBrownfd01b882011-10-11 16:47:53 +11002981 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002982 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002983 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002984
Dan Williamsb522adc2009-03-31 15:00:31 +11002985 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002986 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002987 if (rdev->data_offset != rdev->new_data_offset)
2988 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002989 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002990 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002991 sectors = super_types[my_mddev->major_version].
2992 rdev_size_change(rdev, sectors);
2993 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002994 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002995 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002996 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002997 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002998 if (!my_mddev->pers->resize)
2999 /* Cannot change size for RAID0 or Linear etc */
3000 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003001 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003002 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003003 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003004
Andre Nolldd8ac332009-03-31 14:33:13 +11003005 rdev->sectors = sectors;
3006 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003007 /* need to check that all other rdevs with the same ->bdev
3008 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11003009 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08003010 * we have to change it back, we will have the lock again.
3011 */
NeilBrownfd01b882011-10-11 16:47:53 +11003012 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003013 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003014 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003015
NeilBrown27c529b2008-03-04 14:29:33 -08003016 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08003017 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003018 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003019
3020 mddev_lock(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11003021 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003022 if (rdev->bdev == rdev2->bdev &&
3023 rdev != rdev2 &&
3024 overlaps(rdev->data_offset, rdev->sectors,
3025 rdev2->data_offset,
3026 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003027 overlap = 1;
3028 break;
3029 }
3030 mddev_unlock(mddev);
3031 if (overlap) {
3032 mddev_put(mddev);
3033 break;
3034 }
3035 }
NeilBrown27c529b2008-03-04 14:29:33 -08003036 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08003037 if (overlap) {
3038 /* Someone else could have slipped in a size
3039 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003040 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003041 * safe, and trust userspace not to race with
3042 * itself
3043 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003044 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003045 return -EBUSY;
3046 }
3047 }
NeilBrown83303b62006-01-06 00:21:06 -08003048 return len;
3049}
3050
3051static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003052__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003053
Dan Williams06e3c812009-12-12 21:17:12 -07003054
NeilBrown3cb03002011-10-11 16:45:26 +11003055static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003056{
3057 unsigned long long recovery_start = rdev->recovery_offset;
3058
3059 if (test_bit(In_sync, &rdev->flags) ||
3060 recovery_start == MaxSector)
3061 return sprintf(page, "none\n");
3062
3063 return sprintf(page, "%llu\n", recovery_start);
3064}
3065
NeilBrown3cb03002011-10-11 16:45:26 +11003066static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003067{
3068 unsigned long long recovery_start;
3069
3070 if (cmd_match(buf, "none"))
3071 recovery_start = MaxSector;
3072 else if (strict_strtoull(buf, 10, &recovery_start))
3073 return -EINVAL;
3074
3075 if (rdev->mddev->pers &&
3076 rdev->raid_disk >= 0)
3077 return -EBUSY;
3078
3079 rdev->recovery_offset = recovery_start;
3080 if (recovery_start == MaxSector)
3081 set_bit(In_sync, &rdev->flags);
3082 else
3083 clear_bit(In_sync, &rdev->flags);
3084 return len;
3085}
3086
3087static struct rdev_sysfs_entry rdev_recovery_start =
3088__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3089
NeilBrown16c791a2011-07-28 11:31:47 +10003090
3091static ssize_t
3092badblocks_show(struct badblocks *bb, char *page, int unack);
3093static ssize_t
3094badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3095
NeilBrown3cb03002011-10-11 16:45:26 +11003096static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003097{
3098 return badblocks_show(&rdev->badblocks, page, 0);
3099}
NeilBrown3cb03002011-10-11 16:45:26 +11003100static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003101{
NeilBrownde393cd2011-07-28 11:31:48 +10003102 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3103 /* Maybe that ack was all we needed */
3104 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3105 wake_up(&rdev->blocked_wait);
3106 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003107}
3108static struct rdev_sysfs_entry rdev_bad_blocks =
3109__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3110
3111
NeilBrown3cb03002011-10-11 16:45:26 +11003112static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003113{
3114 return badblocks_show(&rdev->badblocks, page, 1);
3115}
NeilBrown3cb03002011-10-11 16:45:26 +11003116static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003117{
3118 return badblocks_store(&rdev->badblocks, page, len, 1);
3119}
3120static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3121__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3122
NeilBrown86e6ffd2005-11-08 21:39:24 -08003123static struct attribute *rdev_default_attrs[] = {
3124 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003125 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003126 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003127 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003128 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003129 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003130 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003131 &rdev_bad_blocks.attr,
3132 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003133 NULL,
3134};
3135static ssize_t
3136rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3137{
3138 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003139 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003140 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003141 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003142
3143 if (!entry->show)
3144 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003145
3146 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3147 if (!rv) {
3148 if (rdev->mddev == NULL)
3149 rv = -EBUSY;
3150 else
3151 rv = entry->show(rdev, page);
3152 mddev_unlock(mddev);
3153 }
3154 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003155}
3156
3157static ssize_t
3158rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3159 const char *page, size_t length)
3160{
3161 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003162 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003163 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003164 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003165
3166 if (!entry->store)
3167 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003168 if (!capable(CAP_SYS_ADMIN))
3169 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003170 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003171 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003172 if (rdev->mddev == NULL)
3173 rv = -EBUSY;
3174 else
3175 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003176 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003177 }
3178 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003179}
3180
3181static void rdev_free(struct kobject *ko)
3182{
NeilBrown3cb03002011-10-11 16:45:26 +11003183 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003184 kfree(rdev);
3185}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003186static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003187 .show = rdev_attr_show,
3188 .store = rdev_attr_store,
3189};
3190static struct kobj_type rdev_ktype = {
3191 .release = rdev_free,
3192 .sysfs_ops = &rdev_sysfs_ops,
3193 .default_attrs = rdev_default_attrs,
3194};
3195
NeilBrown3cb03002011-10-11 16:45:26 +11003196int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003197{
3198 rdev->desc_nr = -1;
3199 rdev->saved_raid_disk = -1;
3200 rdev->raid_disk = -1;
3201 rdev->flags = 0;
3202 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003203 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003204 rdev->sb_events = 0;
3205 rdev->last_read_error.tv_sec = 0;
3206 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003207 rdev->sb_loaded = 0;
3208 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003209 atomic_set(&rdev->nr_pending, 0);
3210 atomic_set(&rdev->read_errors, 0);
3211 atomic_set(&rdev->corrected_errors, 0);
3212
3213 INIT_LIST_HEAD(&rdev->same_set);
3214 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003215
3216 /* Add space to store bad block list.
3217 * This reserves the space even on arrays where it cannot
3218 * be used - I wonder if that matters
3219 */
3220 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003221 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003222 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3223 seqlock_init(&rdev->badblocks.lock);
3224 if (rdev->badblocks.page == NULL)
3225 return -ENOMEM;
3226
3227 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003228}
3229EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230/*
3231 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3232 *
3233 * mark the device faulty if:
3234 *
3235 * - the device is nonexistent (zero size)
3236 * - the device has no valid superblock
3237 *
3238 * a faulty rdev _never_ has rdev->sb set.
3239 */
NeilBrown3cb03002011-10-11 16:45:26 +11003240static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
3242 char b[BDEVNAME_SIZE];
3243 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003244 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 sector_t size;
3246
NeilBrown9ffae0c2006-01-06 00:20:32 -08003247 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (!rdev) {
3249 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3250 return ERR_PTR(-ENOMEM);
3251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
NeilBrown2230dfe2011-07-28 11:31:46 +10003253 err = md_rdev_init(rdev);
3254 if (err)
3255 goto abort_free;
3256 err = alloc_disk_sb(rdev);
3257 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 goto abort_free;
3259
NeilBrownc5d79ad2008-02-06 01:39:54 -08003260 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 if (err)
3262 goto abort_free;
3263
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003264 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003265
Mike Snitzer77304d22010-11-08 14:39:12 +01003266 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 if (!size) {
3268 printk(KERN_WARNING
3269 "md: %s has zero or unknown size, marking faulty!\n",
3270 bdevname(rdev->bdev,b));
3271 err = -EINVAL;
3272 goto abort_free;
3273 }
3274
3275 if (super_format >= 0) {
3276 err = super_types[super_format].
3277 load_super(rdev, NULL, super_minor);
3278 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003279 printk(KERN_WARNING
3280 "md: %s does not have a valid v%d.%d "
3281 "superblock, not importing!\n",
3282 bdevname(rdev->bdev,b),
3283 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 goto abort_free;
3285 }
3286 if (err < 0) {
3287 printk(KERN_WARNING
3288 "md: could not read %s's sb, not importing!\n",
3289 bdevname(rdev->bdev,b));
3290 goto abort_free;
3291 }
3292 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 return rdev;
3295
3296abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003297 if (rdev->bdev)
3298 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003299 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 kfree(rdev);
3301 return ERR_PTR(err);
3302}
3303
3304/*
3305 * Check a full RAID array for plausibility
3306 */
3307
3308
NeilBrownfd01b882011-10-11 16:47:53 +11003309static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
3311 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003312 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 char b[BDEVNAME_SIZE];
3314
3315 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003316 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 switch (super_types[mddev->major_version].
3318 load_super(rdev, freshest, mddev->minor_version)) {
3319 case 1:
3320 freshest = rdev;
3321 break;
3322 case 0:
3323 break;
3324 default:
3325 printk( KERN_ERR \
3326 "md: fatal superblock inconsistency in %s"
3327 " -- removing from array\n",
3328 bdevname(rdev->bdev,b));
3329 kick_rdev_from_array(rdev);
3330 }
3331
3332
3333 super_types[mddev->major_version].
3334 validate_super(mddev, freshest);
3335
3336 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003337 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003338 if (mddev->max_disks &&
3339 (rdev->desc_nr >= mddev->max_disks ||
3340 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003341 printk(KERN_WARNING
3342 "md: %s: %s: only %d devices permitted\n",
3343 mdname(mddev), bdevname(rdev->bdev, b),
3344 mddev->max_disks);
3345 kick_rdev_from_array(rdev);
3346 continue;
3347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if (rdev != freshest)
3349 if (super_types[mddev->major_version].
3350 validate_super(mddev, rdev)) {
3351 printk(KERN_WARNING "md: kicking non-fresh %s"
3352 " from array!\n",
3353 bdevname(rdev->bdev,b));
3354 kick_rdev_from_array(rdev);
3355 continue;
3356 }
3357 if (mddev->level == LEVEL_MULTIPATH) {
3358 rdev->desc_nr = i++;
3359 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003360 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003361 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003362 rdev->raid_disk = -1;
3363 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 }
3365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
NeilBrown72e02072009-12-14 12:49:55 +11003368/* Read a fixed-point number.
3369 * Numbers in sysfs attributes should be in "standard" units where
3370 * possible, so time should be in seconds.
3371 * However we internally use a a much smaller unit such as
3372 * milliseconds or jiffies.
3373 * This function takes a decimal number with a possible fractional
3374 * component, and produces an integer which is the result of
3375 * multiplying that number by 10^'scale'.
3376 * all without any floating-point arithmetic.
3377 */
3378int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3379{
3380 unsigned long result = 0;
3381 long decimals = -1;
3382 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3383 if (*cp == '.')
3384 decimals = 0;
3385 else if (decimals < scale) {
3386 unsigned int value;
3387 value = *cp - '0';
3388 result = result * 10 + value;
3389 if (decimals >= 0)
3390 decimals++;
3391 }
3392 cp++;
3393 }
3394 if (*cp == '\n')
3395 cp++;
3396 if (*cp)
3397 return -EINVAL;
3398 if (decimals < 0)
3399 decimals = 0;
3400 while (decimals < scale) {
3401 result *= 10;
3402 decimals ++;
3403 }
3404 *res = result;
3405 return 0;
3406}
3407
3408
NeilBrown19052c02008-08-05 15:54:13 +10003409static void md_safemode_timeout(unsigned long data);
3410
NeilBrowneae17012005-11-08 21:39:23 -08003411static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003412safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003413{
3414 int msec = (mddev->safemode_delay*1000)/HZ;
3415 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3416}
3417static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003418safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003419{
NeilBrown16f17b32006-06-26 00:27:37 -07003420 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003421
NeilBrown72e02072009-12-14 12:49:55 +11003422 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003423 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003424 if (msec == 0)
3425 mddev->safemode_delay = 0;
3426 else {
NeilBrown19052c02008-08-05 15:54:13 +10003427 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003428 mddev->safemode_delay = (msec*HZ)/1000;
3429 if (mddev->safemode_delay == 0)
3430 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003431 if (mddev->safemode_delay < old_delay)
3432 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003433 }
3434 return len;
3435}
3436static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003437__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003438
3439static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003440level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003441{
NeilBrown84fc4b52011-10-11 16:49:58 +11003442 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003443 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003444 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003445 else if (mddev->clevel[0])
3446 return sprintf(page, "%s\n", mddev->clevel);
3447 else if (mddev->level != LEVEL_NONE)
3448 return sprintf(page, "%d\n", mddev->level);
3449 else
3450 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003451}
3452
NeilBrownd9d166c2006-01-06 00:20:51 -08003453static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003454level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003455{
Dan Williamsf2859af2010-05-02 10:04:16 -07003456 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003457 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003458 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003459 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003460 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003461 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003462
3463 if (mddev->pers == NULL) {
3464 if (len == 0)
3465 return 0;
3466 if (len >= sizeof(mddev->clevel))
3467 return -ENOSPC;
3468 strncpy(mddev->clevel, buf, len);
3469 if (mddev->clevel[len-1] == '\n')
3470 len--;
3471 mddev->clevel[len] = 0;
3472 mddev->level = LEVEL_NONE;
3473 return rv;
3474 }
3475
3476 /* request to change the personality. Need to ensure:
3477 * - array is not engaged in resync/recovery/reshape
3478 * - old personality can be suspended
3479 * - new personality will access other array.
3480 */
3481
NeilBrownbb4f1e92010-08-08 21:18:03 +10003482 if (mddev->sync_thread ||
3483 mddev->reshape_position != MaxSector ||
3484 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003485 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003486
3487 if (!mddev->pers->quiesce) {
3488 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3489 mdname(mddev), mddev->pers->name);
3490 return -EINVAL;
3491 }
3492
3493 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003494 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003495 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003496 strncpy(clevel, buf, len);
3497 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003498 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003499 clevel[len] = 0;
3500 if (strict_strtol(clevel, 10, &level))
3501 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003502
Dan Williamsf2859af2010-05-02 10:04:16 -07003503 if (request_module("md-%s", clevel) != 0)
3504 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003505 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003506 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003507 if (!pers || !try_module_get(pers->owner)) {
3508 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003509 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003510 return -EINVAL;
3511 }
3512 spin_unlock(&pers_lock);
3513
3514 if (pers == mddev->pers) {
3515 /* Nothing to do! */
3516 module_put(pers->owner);
3517 return rv;
3518 }
3519 if (!pers->takeover) {
3520 module_put(pers->owner);
3521 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003522 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003523 return -EINVAL;
3524 }
3525
NeilBrowndafb20f2012-03-19 12:46:39 +11003526 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003527 rdev->new_raid_disk = rdev->raid_disk;
3528
NeilBrown245f46c2009-03-31 14:39:39 +11003529 /* ->takeover must set new_* and/or delta_disks
3530 * if it succeeds, and may set them when it fails.
3531 */
3532 priv = pers->takeover(mddev);
3533 if (IS_ERR(priv)) {
3534 mddev->new_level = mddev->level;
3535 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003536 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003537 mddev->raid_disks -= mddev->delta_disks;
3538 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003539 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003540 module_put(pers->owner);
3541 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003542 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003543 return PTR_ERR(priv);
3544 }
3545
3546 /* Looks like we have a winner */
3547 mddev_suspend(mddev);
3548 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003549
3550 if (mddev->pers->sync_request == NULL &&
3551 pers->sync_request != NULL) {
3552 /* need to add the md_redundancy_group */
3553 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3554 printk(KERN_WARNING
3555 "md: cannot register extra attributes for %s\n",
3556 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003557 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003558 }
3559 if (mddev->pers->sync_request != NULL &&
3560 pers->sync_request == NULL) {
3561 /* need to remove the md_redundancy_group */
3562 if (mddev->to_remove == NULL)
3563 mddev->to_remove = &md_redundancy_group;
3564 }
3565
Trela Maciej54071b32010-03-08 16:02:42 +11003566 if (mddev->pers->sync_request == NULL &&
3567 mddev->external) {
3568 /* We are converting from a no-redundancy array
3569 * to a redundancy array and metadata is managed
3570 * externally so we need to be sure that writes
3571 * won't block due to a need to transition
3572 * clean->dirty
3573 * until external management is started.
3574 */
3575 mddev->in_sync = 0;
3576 mddev->safemode_delay = 0;
3577 mddev->safemode = 0;
3578 }
3579
NeilBrowndafb20f2012-03-19 12:46:39 +11003580 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003581 if (rdev->raid_disk < 0)
3582 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003583 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003584 rdev->new_raid_disk = -1;
3585 if (rdev->new_raid_disk == rdev->raid_disk)
3586 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003587 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003588 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003589 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003590 if (rdev->raid_disk < 0)
3591 continue;
3592 if (rdev->new_raid_disk == rdev->raid_disk)
3593 continue;
3594 rdev->raid_disk = rdev->new_raid_disk;
3595 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003596 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003597 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003598 if (sysfs_link_rdev(mddev, rdev))
3599 printk(KERN_WARNING "md: cannot register rd%d"
3600 " for %s after level change\n",
3601 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003602 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003603 }
3604
3605 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003606 mddev->pers = pers;
3607 mddev->private = priv;
3608 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3609 mddev->level = mddev->new_level;
3610 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003611 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003612 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003613 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003614 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003615 if (mddev->pers->sync_request == NULL) {
3616 /* this is now an array without redundancy, so
3617 * it must always be in_sync
3618 */
3619 mddev->in_sync = 1;
3620 del_timer_sync(&mddev->safemode_timer);
3621 }
NeilBrown245f46c2009-03-31 14:39:39 +11003622 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003623 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003624 mddev_resume(mddev);
Maciej Trela5cac78612010-04-14 17:17:39 +10003625 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003626 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003627 return rv;
3628}
3629
3630static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003631__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003632
NeilBrownd4dbd022006-06-26 00:27:59 -07003633
3634static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003635layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003636{
3637 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003638 if (mddev->reshape_position != MaxSector &&
3639 mddev->layout != mddev->new_layout)
3640 return sprintf(page, "%d (%d)\n",
3641 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003642 return sprintf(page, "%d\n", mddev->layout);
3643}
3644
3645static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003646layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003647{
3648 char *e;
3649 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003650
3651 if (!*buf || (*e && *e != '\n'))
3652 return -EINVAL;
3653
NeilBrownb3546032009-03-31 14:56:41 +11003654 if (mddev->pers) {
3655 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003656 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003657 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003658 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003659 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003660 if (err) {
3661 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003662 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003663 }
NeilBrownb3546032009-03-31 14:56:41 +11003664 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003665 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003666 if (mddev->reshape_position == MaxSector)
3667 mddev->layout = n;
3668 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003669 return len;
3670}
3671static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003672__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003673
3674
NeilBrowneae17012005-11-08 21:39:23 -08003675static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003676raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003677{
NeilBrownbb636542005-11-08 21:39:45 -08003678 if (mddev->raid_disks == 0)
3679 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003680 if (mddev->reshape_position != MaxSector &&
3681 mddev->delta_disks != 0)
3682 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3683 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003684 return sprintf(page, "%d\n", mddev->raid_disks);
3685}
3686
NeilBrownfd01b882011-10-11 16:47:53 +11003687static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003688
3689static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003690raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003691{
NeilBrownda943b992006-01-06 00:20:54 -08003692 char *e;
3693 int rv = 0;
3694 unsigned long n = simple_strtoul(buf, &e, 10);
3695
3696 if (!*buf || (*e && *e != '\n'))
3697 return -EINVAL;
3698
3699 if (mddev->pers)
3700 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003701 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003702 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003703 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003704
3705 rdev_for_each(rdev, mddev) {
3706 if (olddisks < n &&
3707 rdev->data_offset < rdev->new_data_offset)
3708 return -EINVAL;
3709 if (olddisks > n &&
3710 rdev->data_offset > rdev->new_data_offset)
3711 return -EINVAL;
3712 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003713 mddev->delta_disks = n - olddisks;
3714 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003715 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003716 } else
NeilBrownda943b992006-01-06 00:20:54 -08003717 mddev->raid_disks = n;
3718 return rv ? rv : len;
3719}
3720static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003721__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003722
NeilBrown24dd4692005-11-08 21:39:26 -08003723static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003724chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003725{
NeilBrown08a02ec2007-05-09 02:35:38 -07003726 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003727 mddev->chunk_sectors != mddev->new_chunk_sectors)
3728 return sprintf(page, "%d (%d)\n",
3729 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003730 mddev->chunk_sectors << 9);
3731 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003732}
3733
3734static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003735chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003736{
NeilBrown3b343802006-01-06 00:20:47 -08003737 char *e;
3738 unsigned long n = simple_strtoul(buf, &e, 10);
3739
NeilBrown3b343802006-01-06 00:20:47 -08003740 if (!*buf || (*e && *e != '\n'))
3741 return -EINVAL;
3742
NeilBrownb3546032009-03-31 14:56:41 +11003743 if (mddev->pers) {
3744 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003745 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003746 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003747 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003748 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003749 if (err) {
3750 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003751 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003752 }
NeilBrownb3546032009-03-31 14:56:41 +11003753 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003754 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003755 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003756 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003757 }
NeilBrown3b343802006-01-06 00:20:47 -08003758 return len;
3759}
3760static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003761__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003762
NeilBrowna94213b2006-06-26 00:28:00 -07003763static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003764resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003765{
NeilBrownd1a7c502009-03-31 15:24:32 +11003766 if (mddev->recovery_cp == MaxSector)
3767 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003768 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3769}
3770
3771static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003772resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003773{
NeilBrowna94213b2006-06-26 00:28:00 -07003774 char *e;
3775 unsigned long long n = simple_strtoull(buf, &e, 10);
3776
NeilBrownb0986362011-05-11 15:52:21 +10003777 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003778 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003779 if (cmd_match(buf, "none"))
3780 n = MaxSector;
3781 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003782 return -EINVAL;
3783
3784 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003785 if (mddev->pers)
3786 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003787 return len;
3788}
3789static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003790__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003791
NeilBrown9e653b62006-06-26 00:27:58 -07003792/*
3793 * The array state can be:
3794 *
3795 * clear
3796 * No devices, no size, no level
3797 * Equivalent to STOP_ARRAY ioctl
3798 * inactive
3799 * May have some settings, but array is not active
3800 * all IO results in error
3801 * When written, doesn't tear down array, but just stops it
3802 * suspended (not supported yet)
3803 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003804 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003805 * readonly
3806 * no resync can happen. no superblocks get written.
3807 * write requests fail
3808 * read-auto
3809 * like readonly, but behaves like 'clean' on a write request.
3810 *
3811 * clean - no pending writes, but otherwise active.
3812 * When written to inactive array, starts without resync
3813 * If a write request arrives then
3814 * if metadata is known, mark 'dirty' and switch to 'active'.
3815 * if not known, block and switch to write-pending
3816 * If written to an active array that has pending writes, then fails.
3817 * active
3818 * fully active: IO and resync can be happening.
3819 * When written to inactive array, starts with resync
3820 *
3821 * write-pending
3822 * clean, but writes are blocked waiting for 'active' to be written.
3823 *
3824 * active-idle
3825 * like active, but no writes have been seen for a while (100msec).
3826 *
3827 */
3828enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3829 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003830static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003831 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3832 "write-pending", "active-idle", NULL };
3833
3834static int match_word(const char *word, char **list)
3835{
3836 int n;
3837 for (n=0; list[n]; n++)
3838 if (cmd_match(word, list[n]))
3839 break;
3840 return n;
3841}
3842
3843static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003844array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003845{
3846 enum array_state st = inactive;
3847
3848 if (mddev->pers)
3849 switch(mddev->ro) {
3850 case 1:
3851 st = readonly;
3852 break;
3853 case 2:
3854 st = read_auto;
3855 break;
3856 case 0:
3857 if (mddev->in_sync)
3858 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003859 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003860 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003861 else if (mddev->safemode)
3862 st = active_idle;
3863 else
3864 st = active;
3865 }
3866 else {
3867 if (list_empty(&mddev->disks) &&
3868 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003869 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003870 st = clear;
3871 else
3872 st = inactive;
3873 }
3874 return sprintf(page, "%s\n", array_states[st]);
3875}
3876
NeilBrowna05b7ea02012-07-19 15:59:18 +10003877static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3878static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003879static int do_md_run(struct mddev * mddev);
3880static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003881
3882static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003883array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003884{
3885 int err = -EINVAL;
3886 enum array_state st = match_word(buf, array_states);
3887 switch(st) {
3888 case bad_word:
3889 break;
3890 case clear:
3891 /* stopping an active array */
NeilBrowna05b7ea02012-07-19 15:59:18 +10003892 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003893 break;
3894 case inactive:
3895 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003896 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003897 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003898 else
NeilBrowne6910632008-02-06 01:39:51 -08003899 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003900 break;
3901 case suspended:
3902 break; /* not supported yet */
3903 case readonly:
3904 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003905 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003906 else {
3907 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003908 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003909 err = do_md_run(mddev);
3910 }
3911 break;
3912 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003913 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003914 if (mddev->ro == 0)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003915 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003916 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003917 err = restart_array(mddev);
3918 if (err == 0) {
3919 mddev->ro = 2;
3920 set_disk_ro(mddev->gendisk, 0);
3921 }
NeilBrown9e653b62006-06-26 00:27:58 -07003922 } else {
3923 mddev->ro = 2;
3924 err = do_md_run(mddev);
3925 }
3926 break;
3927 case clean:
3928 if (mddev->pers) {
3929 restart_array(mddev);
3930 spin_lock_irq(&mddev->write_lock);
3931 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003932 if (mddev->in_sync == 0) {
3933 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003934 if (mddev->safemode == 1)
3935 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003936 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003937 }
3938 err = 0;
3939 } else
3940 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003941 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003942 } else
3943 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003944 break;
3945 case active:
3946 if (mddev->pers) {
3947 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003948 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003949 wake_up(&mddev->sb_wait);
3950 err = 0;
3951 } else {
3952 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003953 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003954 err = do_md_run(mddev);
3955 }
3956 break;
3957 case write_pending:
3958 case active_idle:
3959 /* these cannot be set */
3960 break;
3961 }
3962 if (err)
3963 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003964 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003965 if (mddev->hold_active == UNTIL_IOCTL)
3966 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003967 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003968 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003969 }
NeilBrown9e653b62006-06-26 00:27:58 -07003970}
NeilBrown80ca3a42006-07-10 04:44:18 -07003971static struct md_sysfs_entry md_array_state =
3972__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003973
NeilBrown6d7ff732006-01-06 00:21:16 -08003974static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003975max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003976 return sprintf(page, "%d\n",
3977 atomic_read(&mddev->max_corr_read_errors));
3978}
3979
3980static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003981max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003982{
3983 char *e;
3984 unsigned long n = simple_strtoul(buf, &e, 10);
3985
3986 if (*buf && (*e == 0 || *e == '\n')) {
3987 atomic_set(&mddev->max_corr_read_errors, n);
3988 return len;
3989 }
3990 return -EINVAL;
3991}
3992
3993static struct md_sysfs_entry max_corr_read_errors =
3994__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3995 max_corrected_read_errors_store);
3996
3997static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003998null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003999{
4000 return -EINVAL;
4001}
4002
4003static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004004new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08004005{
4006 /* buf must be %d:%d\n? giving major and minor numbers */
4007 /* The new device is added to the array.
4008 * If the array has a persistent superblock, we read the
4009 * superblock to initialise info and check validity.
4010 * Otherwise, only checking done is that in bind_rdev_to_array,
4011 * which mainly checks size.
4012 */
4013 char *e;
4014 int major = simple_strtoul(buf, &e, 10);
4015 int minor;
4016 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004017 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08004018 int err;
4019
4020 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4021 return -EINVAL;
4022 minor = simple_strtoul(e+1, &e, 10);
4023 if (*e && *e != '\n')
4024 return -EINVAL;
4025 dev = MKDEV(major, minor);
4026 if (major != MAJOR(dev) ||
4027 minor != MINOR(dev))
4028 return -EOVERFLOW;
4029
4030
4031 if (mddev->persistent) {
4032 rdev = md_import_device(dev, mddev->major_version,
4033 mddev->minor_version);
4034 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004035 struct md_rdev *rdev0
4036 = list_entry(mddev->disks.next,
4037 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08004038 err = super_types[mddev->major_version]
4039 .load_super(rdev, rdev0, mddev->minor_version);
4040 if (err < 0)
4041 goto out;
4042 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004043 } else if (mddev->external)
4044 rdev = md_import_device(dev, -2, -1);
4045 else
NeilBrown6d7ff732006-01-06 00:21:16 -08004046 rdev = md_import_device(dev, -1, -1);
4047
4048 if (IS_ERR(rdev))
4049 return PTR_ERR(rdev);
4050 err = bind_rdev_to_array(rdev, mddev);
4051 out:
4052 if (err)
4053 export_rdev(rdev);
4054 return err ? err : len;
4055}
4056
4057static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004058__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004059
4060static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004061bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004062{
4063 char *end;
4064 unsigned long chunk, end_chunk;
4065
4066 if (!mddev->bitmap)
4067 goto out;
4068 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4069 while (*buf) {
4070 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4071 if (buf == end) break;
4072 if (*end == '-') { /* range */
4073 buf = end + 1;
4074 end_chunk = simple_strtoul(buf, &end, 0);
4075 if (buf == end) break;
4076 }
4077 if (*end && !isspace(*end)) break;
4078 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004079 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004080 }
4081 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4082out:
4083 return len;
4084}
4085
4086static struct md_sysfs_entry md_bitmap =
4087__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4088
4089static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004090size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004091{
Andre Noll58c0fed2009-03-31 14:33:13 +11004092 return sprintf(page, "%llu\n",
4093 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004094}
4095
NeilBrownfd01b882011-10-11 16:47:53 +11004096static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004097
4098static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004099size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004100{
4101 /* If array is inactive, we can reduce the component size, but
4102 * not increase it (except from 0).
4103 * If array is active, we can try an on-line resize
4104 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004105 sector_t sectors;
4106 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004107
Andre Noll58c0fed2009-03-31 14:33:13 +11004108 if (err < 0)
4109 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004110 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004111 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004112 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004113 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004114 if (mddev->dev_sectors == 0 ||
4115 mddev->dev_sectors > sectors)
4116 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004117 else
4118 err = -ENOSPC;
4119 }
4120 return err ? err : len;
4121}
4122
4123static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004124__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004125
NeilBrown8bb93aa2006-01-06 00:20:50 -08004126
Masanari Iida83f0d772012-10-30 00:18:08 +09004127/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004128 * This is one of
4129 * 'none' for arrays with no metadata (good luck...)
4130 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004131 * or N.M for internally known formats
4132 */
4133static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004134metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004135{
4136 if (mddev->persistent)
4137 return sprintf(page, "%d.%d\n",
4138 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004139 else if (mddev->external)
4140 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004141 else
4142 return sprintf(page, "none\n");
4143}
4144
4145static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004146metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004147{
4148 int major, minor;
4149 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004150 /* Changing the details of 'external' metadata is
4151 * always permitted. Otherwise there must be
4152 * no devices attached to the array.
4153 */
4154 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4155 ;
4156 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004157 return -EBUSY;
4158
4159 if (cmd_match(buf, "none")) {
4160 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004161 mddev->external = 0;
4162 mddev->major_version = 0;
4163 mddev->minor_version = 90;
4164 return len;
4165 }
4166 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004167 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004168 if (namelen >= sizeof(mddev->metadata_type))
4169 namelen = sizeof(mddev->metadata_type)-1;
4170 strncpy(mddev->metadata_type, buf+9, namelen);
4171 mddev->metadata_type[namelen] = 0;
4172 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4173 mddev->metadata_type[--namelen] = 0;
4174 mddev->persistent = 0;
4175 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004176 mddev->major_version = 0;
4177 mddev->minor_version = 90;
4178 return len;
4179 }
4180 major = simple_strtoul(buf, &e, 10);
4181 if (e==buf || *e != '.')
4182 return -EINVAL;
4183 buf = e+1;
4184 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004185 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004186 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004187 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004188 return -ENOENT;
4189 mddev->major_version = major;
4190 mddev->minor_version = minor;
4191 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004192 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004193 return len;
4194}
4195
4196static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004197__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004198
NeilBrowna35b0d62006-01-06 00:20:49 -08004199static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004200action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004201{
NeilBrown7eec3142005-11-08 21:39:44 -08004202 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004203 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4204 type = "frozen";
4205 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004206 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004207 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4208 type = "reshape";
4209 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004210 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4211 type = "resync";
4212 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4213 type = "check";
4214 else
4215 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004216 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004217 type = "recover";
4218 }
4219 return sprintf(page, "%s\n", type);
4220}
4221
4222static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004223action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004224{
NeilBrown7eec3142005-11-08 21:39:44 -08004225 if (!mddev->pers || !mddev->pers->sync_request)
4226 return -EINVAL;
4227
NeilBrownb6a9ce62009-05-26 09:41:17 +10004228 if (cmd_match(page, "frozen"))
4229 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4230 else
4231 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4232
4233 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004234 if (mddev->sync_thread) {
4235 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004236 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004237 }
NeilBrown03c902e2006-01-06 00:20:46 -08004238 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4239 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004240 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004241 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004242 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004243 else if (cmd_match(page, "recover")) {
4244 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4245 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4246 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004247 int err;
4248 if (mddev->pers->start_reshape == NULL)
4249 return -EINVAL;
4250 err = mddev->pers->start_reshape(mddev);
4251 if (err)
4252 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004253 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004254 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004255 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004256 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004257 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004258 return -EINVAL;
4259 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4260 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004261 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004262 if (mddev->ro == 2) {
4263 /* A write to sync_action is enough to justify
4264 * canceling read-auto mode
4265 */
4266 mddev->ro = 0;
4267 md_wakeup_thread(mddev->sync_thread);
4268 }
NeilBrown03c902e2006-01-06 00:20:46 -08004269 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004270 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004271 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004272 return len;
4273}
4274
NeilBrown9d888832005-11-08 21:39:26 -08004275static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004276mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004277{
4278 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004279 (unsigned long long)
4280 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004281}
4282
NeilBrown80ca3a42006-07-10 04:44:18 -07004283static struct md_sysfs_entry md_scan_mode =
4284__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004285
NeilBrown96de1e62005-11-08 21:39:39 -08004286
NeilBrown80ca3a42006-07-10 04:44:18 -07004287static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004288
NeilBrown88202a02006-01-06 00:21:36 -08004289static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004290sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004291{
4292 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4293 mddev->sync_speed_min ? "local": "system");
4294}
4295
4296static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004297sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004298{
4299 int min;
4300 char *e;
4301 if (strncmp(buf, "system", 6)==0) {
4302 mddev->sync_speed_min = 0;
4303 return len;
4304 }
4305 min = simple_strtoul(buf, &e, 10);
4306 if (buf == e || (*e && *e != '\n') || min <= 0)
4307 return -EINVAL;
4308 mddev->sync_speed_min = min;
4309 return len;
4310}
4311
4312static struct md_sysfs_entry md_sync_min =
4313__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4314
4315static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004316sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004317{
4318 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4319 mddev->sync_speed_max ? "local": "system");
4320}
4321
4322static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004323sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004324{
4325 int max;
4326 char *e;
4327 if (strncmp(buf, "system", 6)==0) {
4328 mddev->sync_speed_max = 0;
4329 return len;
4330 }
4331 max = simple_strtoul(buf, &e, 10);
4332 if (buf == e || (*e && *e != '\n') || max <= 0)
4333 return -EINVAL;
4334 mddev->sync_speed_max = max;
4335 return len;
4336}
4337
4338static struct md_sysfs_entry md_sync_max =
4339__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4340
Iustin Popd7f3d292007-10-16 23:30:54 -07004341static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004342degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004343{
4344 return sprintf(page, "%d\n", mddev->degraded);
4345}
4346static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004347
4348static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004349sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004350{
4351 return sprintf(page, "%d\n", mddev->parallel_resync);
4352}
4353
4354static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004355sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004356{
4357 long n;
4358
4359 if (strict_strtol(buf, 10, &n))
4360 return -EINVAL;
4361
4362 if (n != 0 && n != 1)
4363 return -EINVAL;
4364
4365 mddev->parallel_resync = n;
4366
4367 if (mddev->sync_thread)
4368 wake_up(&resync_wait);
4369
4370 return len;
4371}
4372
4373/* force parallel resync, even with shared block devices */
4374static struct md_sysfs_entry md_sync_force_parallel =
4375__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4376 sync_force_parallel_show, sync_force_parallel_store);
4377
4378static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004379sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004380{
4381 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004382 if (mddev->curr_resync == 0)
4383 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004384 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4385 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004386 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004387 db = resync - mddev->resync_mark_cnt;
4388 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004389}
4390
NeilBrown80ca3a42006-07-10 04:44:18 -07004391static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004392
4393static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004394sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004395{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004396 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004397
NeilBrownacb180b2009-04-14 16:28:34 +10004398 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4399 return sprintf(page, "none\n");
4400
NeilBrown72f36d52012-10-11 14:25:57 +11004401 if (mddev->curr_resync == 1 ||
4402 mddev->curr_resync == 2)
4403 return sprintf(page, "delayed\n");
4404
NeilBrownc804cde2012-05-21 09:28:33 +10004405 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4406 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004407 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004408 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004409 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004410
NeilBrownacb180b2009-04-14 16:28:34 +10004411 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004412 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004413}
4414
NeilBrown80ca3a42006-07-10 04:44:18 -07004415static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004416
NeilBrowne464eaf2006-03-27 01:18:14 -08004417static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004418min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004419{
4420 return sprintf(page, "%llu\n",
4421 (unsigned long long)mddev->resync_min);
4422}
4423static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004424min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004425{
4426 unsigned long long min;
4427 if (strict_strtoull(buf, 10, &min))
4428 return -EINVAL;
4429 if (min > mddev->resync_max)
4430 return -EINVAL;
4431 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4432 return -EBUSY;
4433
4434 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004435 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004436 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004437 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004438 return -EINVAL;
4439 }
4440 mddev->resync_min = min;
4441
4442 return len;
4443}
4444
4445static struct md_sysfs_entry md_min_sync =
4446__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4447
4448static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004449max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004450{
4451 if (mddev->resync_max == MaxSector)
4452 return sprintf(page, "max\n");
4453 else
4454 return sprintf(page, "%llu\n",
4455 (unsigned long long)mddev->resync_max);
4456}
4457static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004458max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004459{
4460 if (strncmp(buf, "max", 3) == 0)
4461 mddev->resync_max = MaxSector;
4462 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004463 unsigned long long max;
4464 if (strict_strtoull(buf, 10, &max))
4465 return -EINVAL;
4466 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004467 return -EINVAL;
4468 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004469 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004470 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4471 return -EBUSY;
4472
4473 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004474 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004475 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004476 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004477 return -EINVAL;
4478 }
4479 mddev->resync_max = max;
4480 }
4481 wake_up(&mddev->recovery_wait);
4482 return len;
4483}
4484
4485static struct md_sysfs_entry md_max_sync =
4486__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4487
4488static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004489suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004490{
4491 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4492}
4493
4494static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004495suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004496{
4497 char *e;
4498 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004499 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004500
NeilBrownb8d966e2009-07-01 11:14:04 +10004501 if (mddev->pers == NULL ||
4502 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004503 return -EINVAL;
4504 if (buf == e || (*e && *e != '\n'))
4505 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004506
4507 mddev->suspend_lo = new;
4508 if (new >= old)
4509 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004510 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004511 else {
4512 /* Expanding suspended region - need to wait */
4513 mddev->pers->quiesce(mddev, 1);
4514 mddev->pers->quiesce(mddev, 0);
4515 }
4516 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004517}
4518static struct md_sysfs_entry md_suspend_lo =
4519__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4520
4521
4522static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004523suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004524{
4525 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4526}
4527
4528static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004529suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004530{
4531 char *e;
4532 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004533 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004534
NeilBrownb8d966e2009-07-01 11:14:04 +10004535 if (mddev->pers == NULL ||
4536 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004537 return -EINVAL;
4538 if (buf == e || (*e && *e != '\n'))
4539 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004540
4541 mddev->suspend_hi = new;
4542 if (new <= old)
4543 /* Shrinking suspended region */
4544 mddev->pers->quiesce(mddev, 2);
4545 else {
4546 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004547 mddev->pers->quiesce(mddev, 1);
4548 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004549 }
4550 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004551}
4552static struct md_sysfs_entry md_suspend_hi =
4553__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4554
NeilBrown08a02ec2007-05-09 02:35:38 -07004555static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004556reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004557{
4558 if (mddev->reshape_position != MaxSector)
4559 return sprintf(page, "%llu\n",
4560 (unsigned long long)mddev->reshape_position);
4561 strcpy(page, "none\n");
4562 return 5;
4563}
4564
4565static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004566reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004567{
NeilBrownc6563a82012-05-21 09:27:00 +10004568 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004569 char *e;
4570 unsigned long long new = simple_strtoull(buf, &e, 10);
4571 if (mddev->pers)
4572 return -EBUSY;
4573 if (buf == e || (*e && *e != '\n'))
4574 return -EINVAL;
4575 mddev->reshape_position = new;
4576 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004577 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004578 mddev->new_level = mddev->level;
4579 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004580 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004581 rdev_for_each(rdev, mddev)
4582 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004583 return len;
4584}
4585
4586static struct md_sysfs_entry md_reshape_position =
4587__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4588 reshape_position_store);
4589
Dan Williamsb522adc2009-03-31 15:00:31 +11004590static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004591reshape_direction_show(struct mddev *mddev, char *page)
4592{
4593 return sprintf(page, "%s\n",
4594 mddev->reshape_backwards ? "backwards" : "forwards");
4595}
4596
4597static ssize_t
4598reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4599{
4600 int backwards = 0;
4601 if (cmd_match(buf, "forwards"))
4602 backwards = 0;
4603 else if (cmd_match(buf, "backwards"))
4604 backwards = 1;
4605 else
4606 return -EINVAL;
4607 if (mddev->reshape_backwards == backwards)
4608 return len;
4609
4610 /* check if we are allowed to change */
4611 if (mddev->delta_disks)
4612 return -EBUSY;
4613
4614 if (mddev->persistent &&
4615 mddev->major_version == 0)
4616 return -EINVAL;
4617
4618 mddev->reshape_backwards = backwards;
4619 return len;
4620}
4621
4622static struct md_sysfs_entry md_reshape_direction =
4623__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4624 reshape_direction_store);
4625
4626static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004627array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004628{
4629 if (mddev->external_size)
4630 return sprintf(page, "%llu\n",
4631 (unsigned long long)mddev->array_sectors/2);
4632 else
4633 return sprintf(page, "default\n");
4634}
4635
4636static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004637array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004638{
4639 sector_t sectors;
4640
4641 if (strncmp(buf, "default", 7) == 0) {
4642 if (mddev->pers)
4643 sectors = mddev->pers->size(mddev, 0, 0);
4644 else
4645 sectors = mddev->array_sectors;
4646
4647 mddev->external_size = 0;
4648 } else {
4649 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4650 return -EINVAL;
4651 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004652 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004653
4654 mddev->external_size = 1;
4655 }
4656
4657 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004658 if (mddev->pers) {
4659 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004660 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004661 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004662 return len;
4663}
4664
4665static struct md_sysfs_entry md_array_size =
4666__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4667 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004668
NeilBrowneae17012005-11-08 21:39:23 -08004669static struct attribute *md_default_attrs[] = {
4670 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004671 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004672 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004673 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004674 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004675 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004676 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004677 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004678 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004679 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004680 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004681 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004682 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004683 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004684 NULL,
4685};
4686
4687static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004688 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004689 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004690 &md_sync_min.attr,
4691 &md_sync_max.attr,
4692 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004693 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004694 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004695 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004696 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004697 &md_suspend_lo.attr,
4698 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004699 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004700 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004701 NULL,
4702};
NeilBrown411036f2005-11-08 21:39:40 -08004703static struct attribute_group md_redundancy_group = {
4704 .name = NULL,
4705 .attrs = md_redundancy_attrs,
4706};
4707
NeilBrowneae17012005-11-08 21:39:23 -08004708
4709static ssize_t
4710md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4711{
4712 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004713 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004714 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004715
4716 if (!entry->show)
4717 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004718 spin_lock(&all_mddevs_lock);
4719 if (list_empty(&mddev->all_mddevs)) {
4720 spin_unlock(&all_mddevs_lock);
4721 return -EBUSY;
4722 }
4723 mddev_get(mddev);
4724 spin_unlock(&all_mddevs_lock);
4725
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004726 rv = mddev_lock(mddev);
4727 if (!rv) {
4728 rv = entry->show(mddev, page);
4729 mddev_unlock(mddev);
4730 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004731 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004732 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004733}
4734
4735static ssize_t
4736md_attr_store(struct kobject *kobj, struct attribute *attr,
4737 const char *page, size_t length)
4738{
4739 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004740 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004741 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004742
4743 if (!entry->store)
4744 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004745 if (!capable(CAP_SYS_ADMIN))
4746 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004747 spin_lock(&all_mddevs_lock);
4748 if (list_empty(&mddev->all_mddevs)) {
4749 spin_unlock(&all_mddevs_lock);
4750 return -EBUSY;
4751 }
4752 mddev_get(mddev);
4753 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004754 if (entry->store == new_dev_store)
4755 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004756 rv = mddev_lock(mddev);
4757 if (!rv) {
4758 rv = entry->store(mddev, page, length);
4759 mddev_unlock(mddev);
4760 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004761 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004762 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004763}
4764
4765static void md_free(struct kobject *ko)
4766{
NeilBrownfd01b882011-10-11 16:47:53 +11004767 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004768
4769 if (mddev->sysfs_state)
4770 sysfs_put(mddev->sysfs_state);
4771
4772 if (mddev->gendisk) {
4773 del_gendisk(mddev->gendisk);
4774 put_disk(mddev->gendisk);
4775 }
4776 if (mddev->queue)
4777 blk_cleanup_queue(mddev->queue);
4778
NeilBrowneae17012005-11-08 21:39:23 -08004779 kfree(mddev);
4780}
4781
Emese Revfy52cf25d2010-01-19 02:58:23 +01004782static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004783 .show = md_attr_show,
4784 .store = md_attr_store,
4785};
4786static struct kobj_type md_ktype = {
4787 .release = md_free,
4788 .sysfs_ops = &md_sysfs_ops,
4789 .default_attrs = md_default_attrs,
4790};
4791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792int mdp_major = 0;
4793
Dan Williams5fd3a172009-03-04 00:57:25 -07004794static void mddev_delayed_delete(struct work_struct *ws)
4795{
NeilBrownfd01b882011-10-11 16:47:53 +11004796 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004797
NeilBrown43a70502009-12-14 12:49:55 +11004798 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004799 kobject_del(&mddev->kobj);
4800 kobject_put(&mddev->kobj);
4801}
4802
NeilBrownefeb53c2009-01-09 08:31:10 +11004803static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004805 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004806 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004808 int partitioned;
4809 int shift;
4810 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004811 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
4813 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004814 return -ENODEV;
4815
4816 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4817 shift = partitioned ? MdpMinorShift : 0;
4818 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
Tejun Heoe804ac72010-10-15 15:36:08 +02004820 /* wait for any previous instance of this device to be
4821 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004822 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004823 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004824
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004825 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004826 error = -EEXIST;
4827 if (mddev->gendisk)
4828 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004829
4830 if (name) {
4831 /* Need to ensure that 'name' is not a duplicate.
4832 */
NeilBrownfd01b882011-10-11 16:47:53 +11004833 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004834 spin_lock(&all_mddevs_lock);
4835
4836 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4837 if (mddev2->gendisk &&
4838 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4839 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004840 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004841 }
4842 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 }
NeilBrown8b765392009-01-09 08:31:08 +11004844
NeilBrown0909dc42009-07-01 12:27:21 +10004845 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004846 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004847 if (!mddev->queue)
4848 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004849 mddev->queue->queuedata = mddev;
4850
NeilBrown409c57f2009-03-31 14:39:39 +11004851 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004852 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 disk = alloc_disk(1 << shift);
4855 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004856 blk_cleanup_queue(mddev->queue);
4857 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004858 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004860 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004862 if (name)
4863 strcpy(disk->disk_name, name);
4864 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004866 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 disk->fops = &md_fops;
4869 disk->private_data = mddev;
4870 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004871 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004872 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004873 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004874 * remove it now.
4875 */
4876 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004878 /* As soon as we call add_disk(), another thread could get
4879 * through to md_open, so make sure it doesn't get too far
4880 */
4881 mutex_lock(&mddev->open_mutex);
4882 add_disk(disk);
4883
Tejun Heoed9e1982008-08-25 19:56:05 +09004884 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4885 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004886 if (error) {
4887 /* This isn't possible, but as kobject_init_and_add is marked
4888 * __must_check, we must do something with the result
4889 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004890 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4891 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004892 error = 0;
4893 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004894 if (mddev->kobj.sd &&
4895 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004896 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004897 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004898 abort:
4899 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004900 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004901 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004902 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004903 }
NeilBrownd3374822009-01-09 08:31:10 +11004904 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004905 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004906}
4907
4908static struct kobject *md_probe(dev_t dev, int *part, void *data)
4909{
4910 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 return NULL;
4912}
4913
NeilBrownefeb53c2009-01-09 08:31:10 +11004914static int add_named_array(const char *val, struct kernel_param *kp)
4915{
4916 /* val must be "md_*" where * is not all digits.
4917 * We allocate an array with a large free minor number, and
4918 * set the name to val. val must not already be an active name.
4919 */
4920 int len = strlen(val);
4921 char buf[DISK_NAME_LEN];
4922
4923 while (len && val[len-1] == '\n')
4924 len--;
4925 if (len >= DISK_NAME_LEN)
4926 return -E2BIG;
4927 strlcpy(buf, val, len+1);
4928 if (strncmp(buf, "md_", 3) != 0)
4929 return -EINVAL;
4930 return md_alloc(0, buf);
4931}
4932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933static void md_safemode_timeout(unsigned long data)
4934{
NeilBrownfd01b882011-10-11 16:47:53 +11004935 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
Neil Brown0fd62b82008-06-28 08:31:36 +10004937 if (!atomic_read(&mddev->writes_pending)) {
4938 mddev->safemode = 1;
4939 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004940 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 md_wakeup_thread(mddev->thread);
4943}
4944
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004945static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
NeilBrownfd01b882011-10-11 16:47:53 +11004947int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948{
NeilBrown2604b702006-01-06 00:20:36 -08004949 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004950 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004951 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
NeilBrowna757e642005-04-16 15:26:42 -07004953 if (list_empty(&mddev->disks))
4954 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
4957 if (mddev->pers)
4958 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004959 /* Cannot run until previous stop completes properly */
4960 if (mddev->sysfs_active)
4961 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004962
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 /*
4964 * Analyze all RAID superblock(s)
4965 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004966 if (!mddev->raid_disks) {
4967 if (!mddev->persistent)
4968 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004969 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
NeilBrownd9d166c2006-01-06 00:20:51 -08004972 if (mddev->level != LEVEL_NONE)
4973 request_module("md-level-%d", mddev->level);
4974 else if (mddev->clevel[0])
4975 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
4977 /*
4978 * Drop all container device buffers, from now on
4979 * the only valid external interface is through the md
4980 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004982 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004983 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 continue;
4985 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004986 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004987
4988 /* perform some consistency tests on the device.
4989 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004990 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004991 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004992 if (rdev->meta_bdev) {
4993 /* Nothing to check */;
4994 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004995 if (mddev->dev_sectors &&
4996 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004997 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004998 printk("md: %s: data overlaps metadata\n",
4999 mdname(mddev));
5000 return -EINVAL;
5001 }
5002 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005003 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005004 > rdev->data_offset) {
5005 printk("md: %s: metadata overlaps data\n",
5006 mdname(mddev));
5007 return -EINVAL;
5008 }
5009 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005010 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 }
5012
NeilBrowna167f662010-10-26 18:31:13 +11005013 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005014 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005017 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005018 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005020 if (mddev->level != LEVEL_NONE)
5021 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5022 mddev->level);
5023 else
5024 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5025 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 return -EINVAL;
5027 }
NeilBrown2604b702006-01-06 00:20:36 -08005028 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005030 if (mddev->level != pers->level) {
5031 mddev->level = pers->level;
5032 mddev->new_level = pers->level;
5033 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005034 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
NeilBrownf6705572006-03-27 01:18:11 -08005036 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005037 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005038 /* This personality cannot handle reshaping... */
5039 mddev->pers = NULL;
5040 module_put(pers->owner);
5041 return -EINVAL;
5042 }
5043
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005044 if (pers->sync_request) {
5045 /* Warn if this is a potentially silly
5046 * configuration.
5047 */
5048 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005049 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005050 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005051
NeilBrowndafb20f2012-03-19 12:46:39 +11005052 rdev_for_each(rdev, mddev)
5053 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005054 if (rdev < rdev2 &&
5055 rdev->bdev->bd_contains ==
5056 rdev2->bdev->bd_contains) {
5057 printk(KERN_WARNING
5058 "%s: WARNING: %s appears to be"
5059 " on the same physical disk as"
5060 " %s.\n",
5061 mdname(mddev),
5062 bdevname(rdev->bdev,b),
5063 bdevname(rdev2->bdev,b2));
5064 warned = 1;
5065 }
5066 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005067
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005068 if (warned)
5069 printk(KERN_WARNING
5070 "True protection against single-disk"
5071 " failure might be compromised.\n");
5072 }
5073
NeilBrown657390d2005-08-26 18:34:16 -07005074 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005075 /* may be over-ridden by personality */
5076 mddev->resync_max_sectors = mddev->dev_sectors;
5077
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005078 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
NeilBrown0f9552b2009-12-30 12:08:50 +11005080 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005081 mddev->ro = 2; /* read-only, but switch on first write */
5082
NeilBrownb15c2e52006-01-06 00:20:16 -08005083 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005084 if (err)
5085 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005086 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5087 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5088 " but 'external_size' not in effect?\n", __func__);
5089 printk(KERN_ERR
5090 "md: invalid array_size %llu > default size %llu\n",
5091 (unsigned long long)mddev->array_sectors / 2,
5092 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5093 err = -EINVAL;
5094 mddev->pers->stop(mddev);
5095 }
NeilBrownef99bf42012-05-22 13:55:08 +10005096 if (err == 0 && mddev->pers->sync_request &&
5097 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005098 err = bitmap_create(mddev);
5099 if (err) {
5100 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5101 mdname(mddev), err);
5102 mddev->pers->stop(mddev);
5103 }
5104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 module_put(mddev->pers->owner);
5107 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005108 bitmap_destroy(mddev);
5109 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005111 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005112 if (mddev->kobj.sd &&
5113 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005114 printk(KERN_WARNING
5115 "md: cannot register extra attributes for %s\n",
5116 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005117 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005118 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005119 mddev->ro = 0;
5120
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005122 atomic_set(&mddev->max_corr_read_errors,
5123 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 mddev->safemode = 0;
5125 mddev->safemode_timer.function = md_safemode_timeout;
5126 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005127 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005129 smp_wmb();
5130 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005131 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005132 if (rdev->raid_disk >= 0)
5133 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005134 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
5136 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5137
NeilBrown850b2b42006-10-03 01:15:46 -07005138 if (mddev->flags)
5139 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
NeilBrownd7603b72006-01-06 00:20:30 -08005141 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005142 sysfs_notify_dirent_safe(mddev->sysfs_state);
5143 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005144 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 return 0;
5146}
NeilBrown390ee602010-06-01 19:37:27 +10005147EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
NeilBrownfd01b882011-10-11 16:47:53 +11005149static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005150{
5151 int err;
5152
5153 err = md_run(mddev);
5154 if (err)
5155 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005156 err = bitmap_load(mddev);
5157 if (err) {
5158 bitmap_destroy(mddev);
5159 goto out;
5160 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005161
5162 md_wakeup_thread(mddev->thread);
5163 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5164
NeilBrownfe60b012010-03-29 11:10:42 +11005165 set_capacity(mddev->gendisk, mddev->array_sectors);
5166 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005167 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005168 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5169out:
5170 return err;
5171}
5172
NeilBrownfd01b882011-10-11 16:47:53 +11005173static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174{
5175 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
Andre Noll80fab1d2008-07-11 22:02:21 +10005177 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005179 return -ENXIO;
5180 if (!mddev->pers)
5181 return -EINVAL;
5182 if (!mddev->ro)
5183 return -EBUSY;
5184 mddev->safemode = 0;
5185 mddev->ro = 0;
5186 set_disk_ro(disk, 0);
5187 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5188 mdname(mddev));
5189 /* Kick recovery or resync if necessary */
5190 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5191 md_wakeup_thread(mddev->thread);
5192 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005193 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195}
5196
NeilBrownacc55e22006-06-26 00:27:47 -07005197/* similar to deny_write_access, but accounts for our holding a reference
5198 * to the file ourselves */
5199static int deny_bitmap_write_access(struct file * file)
5200{
5201 struct inode *inode = file->f_mapping->host;
5202
5203 spin_lock(&inode->i_lock);
5204 if (atomic_read(&inode->i_writecount) > 1) {
5205 spin_unlock(&inode->i_lock);
5206 return -ETXTBSY;
5207 }
5208 atomic_set(&inode->i_writecount, -1);
5209 spin_unlock(&inode->i_lock);
5210
5211 return 0;
5212}
5213
NeilBrown43a70502009-12-14 12:49:55 +11005214void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005215{
5216 struct inode *inode = file->f_mapping->host;
5217
5218 spin_lock(&inode->i_lock);
5219 atomic_set(&inode->i_writecount, 1);
5220 spin_unlock(&inode->i_lock);
5221}
5222
NeilBrownfd01b882011-10-11 16:47:53 +11005223static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005224{
5225 mddev->array_sectors = 0;
5226 mddev->external_size = 0;
5227 mddev->dev_sectors = 0;
5228 mddev->raid_disks = 0;
5229 mddev->recovery_cp = 0;
5230 mddev->resync_min = 0;
5231 mddev->resync_max = MaxSector;
5232 mddev->reshape_position = MaxSector;
5233 mddev->external = 0;
5234 mddev->persistent = 0;
5235 mddev->level = LEVEL_NONE;
5236 mddev->clevel[0] = 0;
5237 mddev->flags = 0;
5238 mddev->ro = 0;
5239 mddev->metadata_type[0] = 0;
5240 mddev->chunk_sectors = 0;
5241 mddev->ctime = mddev->utime = 0;
5242 mddev->layout = 0;
5243 mddev->max_disks = 0;
5244 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005245 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005246 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005247 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005248 mddev->new_level = LEVEL_NONE;
5249 mddev->new_layout = 0;
5250 mddev->new_chunk_sectors = 0;
5251 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005252 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005253 mddev->suspend_lo = mddev->suspend_hi = 0;
5254 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5255 mddev->recovery = 0;
5256 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005257 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005258 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005259 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005260 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005261 mddev->bitmap_info.offset = 0;
5262 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005263 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005264 mddev->bitmap_info.chunksize = 0;
5265 mddev->bitmap_info.daemon_sleep = 0;
5266 mddev->bitmap_info.max_write_behind = 0;
5267}
5268
NeilBrownfd01b882011-10-11 16:47:53 +11005269static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005270{
NeilBrown6b6204e2013-05-09 09:48:30 +10005271 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrowna047e122010-03-29 12:07:53 +11005272 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005273 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005274 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005275 }
5276
5277 del_timer_sync(&mddev->safemode_timer);
5278
5279 bitmap_flush(mddev);
5280 md_super_wait(mddev);
5281
NeilBrownb6d428c2013-04-24 11:42:42 +10005282 if (mddev->ro == 0 &&
5283 (!mddev->in_sync || mddev->flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005284 /* mark array as shutdown cleanly */
5285 mddev->in_sync = 1;
5286 md_update_sb(mddev, 1);
5287 }
5288}
NeilBrowndefad612011-01-14 09:14:33 +11005289
NeilBrownfd01b882011-10-11 16:47:53 +11005290void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005291{
5292 mddev_lock(mddev);
5293 __md_stop_writes(mddev);
5294 mddev_unlock(mddev);
5295}
NeilBrown390ee602010-06-01 19:37:27 +10005296EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005297
NeilBrown5eff3c42012-11-19 10:47:48 +11005298static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005299{
NeilBrown0ca69882011-01-14 09:14:33 +11005300 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005301 mddev->pers->stop(mddev);
5302 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5303 mddev->to_remove = &md_redundancy_group;
5304 module_put(mddev->pers->owner);
5305 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005306 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005307}
NeilBrown5eff3c42012-11-19 10:47:48 +11005308
5309void md_stop(struct mddev *mddev)
5310{
5311 /* stop the array and free an attached data structures.
5312 * This is called from dm-raid
5313 */
5314 __md_stop(mddev);
5315 bitmap_destroy(mddev);
5316 if (mddev->bio_set)
5317 bioset_free(mddev->bio_set);
5318}
5319
NeilBrown390ee602010-06-01 19:37:27 +10005320EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005321
NeilBrowna05b7ea02012-07-19 15:59:18 +10005322static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005323{
5324 int err = 0;
5325 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005326 if (atomic_read(&mddev->openers) > !!bdev) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005327 printk("md: %s still in use.\n",mdname(mddev));
5328 err = -EBUSY;
5329 goto out;
5330 }
NeilBrowna05b7ea02012-07-19 15:59:18 +10005331 if (bdev)
5332 sync_blockdev(bdev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005333 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005334 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005335
5336 err = -ENXIO;
5337 if (mddev->ro==1)
5338 goto out;
5339 mddev->ro = 1;
5340 set_disk_ro(mddev->gendisk, 1);
5341 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005342 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005343 err = 0;
5344 }
5345out:
5346 mutex_unlock(&mddev->open_mutex);
5347 return err;
5348}
5349
NeilBrown9e653b62006-06-26 00:27:58 -07005350/* mode:
5351 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005352 * 2 - stop but do not disassemble array
5353 */
NeilBrowna05b7ea02012-07-19 15:59:18 +10005354static int do_md_stop(struct mddev * mddev, int mode,
5355 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005358 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
NeilBrownc8c00a62009-08-10 12:50:52 +10005360 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005361 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10005362 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005363 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005364 mutex_unlock(&mddev->open_mutex);
5365 return -EBUSY;
5366 }
NeilBrowna05b7ea02012-07-19 15:59:18 +10005367 if (bdev)
5368 /* It is possible IO was issued on some other
5369 * open file which was closed before we took ->open_mutex.
5370 * As that was not the last close __blkdev_put will not
5371 * have called sync_blockdev, so we must.
5372 */
5373 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
NeilBrown6e17b022010-08-07 21:41:19 +10005375 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005376 if (mddev->ro)
5377 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005378
NeilBrowndefad612011-01-14 09:14:33 +11005379 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005380 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005381 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005382 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005383
NeilBrowna4bd82d2010-03-29 13:23:10 +11005384 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005385 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005386
NeilBrowndafb20f2012-03-19 12:46:39 +11005387 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005388 if (rdev->raid_disk >= 0)
5389 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005390
NeilBrowna4bd82d2010-03-29 13:23:10 +11005391 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005392 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005393 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005394 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005395
NeilBrowna4bd82d2010-03-29 13:23:10 +11005396 if (mddev->ro)
5397 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005398 } else
5399 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 /*
5401 * Free resources if final stop
5402 */
NeilBrown9e653b62006-06-26 00:27:58 -07005403 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5405
NeilBrown978f9462006-02-02 14:28:05 -08005406 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005407 if (mddev->bitmap_info.file) {
5408 restore_bitmap_write_access(mddev->bitmap_info.file);
5409 fput(mddev->bitmap_info.file);
5410 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005411 }
NeilBrownc3d97142009-12-14 12:49:52 +11005412 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005413
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 export_array(mddev);
5415
NeilBrown6177b472010-03-29 11:37:13 +11005416 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005417 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005418 if (mddev->hold_active == UNTIL_STOP)
5419 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005420 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005421 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005422 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005423 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425}
5426
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005427#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005428static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429{
NeilBrown3cb03002011-10-11 16:45:26 +11005430 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 int err;
5432
NeilBrowna757e642005-04-16 15:26:42 -07005433 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435
5436 printk(KERN_INFO "md: running: ");
5437
NeilBrowndafb20f2012-03-19 12:46:39 +11005438 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 char b[BDEVNAME_SIZE];
5440 printk("<%s>", bdevname(rdev->bdev,b));
5441 }
5442 printk("\n");
5443
NeilBrownd710e132008-10-13 11:55:12 +11005444 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 if (err) {
5446 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005447 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 }
5449}
5450
5451/*
5452 * lets try to run arrays based on all disks that have arrived
5453 * until now. (those are in pending_raid_disks)
5454 *
5455 * the method: pick the first pending disk, collect all disks with
5456 * the same UUID, remove all from the pending list and put them into
5457 * the 'same_array' list. Then order this list based on superblock
5458 * update time (freshest comes first), kick out 'old' disks and
5459 * compare superblocks. If everything's fine then run it.
5460 *
5461 * If "unit" is allocated, then bump its reference count
5462 */
5463static void autorun_devices(int part)
5464{
NeilBrown3cb03002011-10-11 16:45:26 +11005465 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005466 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 char b[BDEVNAME_SIZE];
5468
5469 printk(KERN_INFO "md: autorun ...\n");
5470 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005471 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005473 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005475 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
5477 printk(KERN_INFO "md: considering %s ...\n",
5478 bdevname(rdev0->bdev,b));
5479 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005480 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 if (super_90_load(rdev, rdev0, 0) >= 0) {
5482 printk(KERN_INFO "md: adding %s ...\n",
5483 bdevname(rdev->bdev,b));
5484 list_move(&rdev->same_set, &candidates);
5485 }
5486 /*
5487 * now we have a set of devices, with all of them having
5488 * mostly sane superblocks. It's time to allocate the
5489 * mddev.
5490 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005491 if (part) {
5492 dev = MKDEV(mdp_major,
5493 rdev0->preferred_minor << MdpMinorShift);
5494 unit = MINOR(dev) >> MdpMinorShift;
5495 } else {
5496 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5497 unit = MINOR(dev);
5498 }
5499 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5501 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5502 break;
5503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
5505 md_probe(dev, NULL, NULL);
5506 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005507 if (!mddev || !mddev->gendisk) {
5508 if (mddev)
5509 mddev_put(mddev);
5510 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 "md: cannot allocate memory for md drive.\n");
5512 break;
5513 }
5514 if (mddev_lock(mddev))
5515 printk(KERN_WARNING "md: %s locked, cannot run\n",
5516 mdname(mddev));
5517 else if (mddev->raid_disks || mddev->major_version
5518 || !list_empty(&mddev->disks)) {
5519 printk(KERN_WARNING
5520 "md: %s already running, cannot run %s\n",
5521 mdname(mddev), bdevname(rdev0->bdev,b));
5522 mddev_unlock(mddev);
5523 } else {
5524 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005525 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005526 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 list_del_init(&rdev->same_set);
5528 if (bind_rdev_to_array(rdev, mddev))
5529 export_rdev(rdev);
5530 }
5531 autorun_array(mddev);
5532 mddev_unlock(mddev);
5533 }
5534 /* on success, candidates will be empty, on error
5535 * it won't...
5536 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005537 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005538 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 mddev_put(mddev);
5542 }
5543 printk(KERN_INFO "md: ... autorun DONE.\n");
5544}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005545#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547static int get_version(void __user * arg)
5548{
5549 mdu_version_t ver;
5550
5551 ver.major = MD_MAJOR_VERSION;
5552 ver.minor = MD_MINOR_VERSION;
5553 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5554
5555 if (copy_to_user(arg, &ver, sizeof(ver)))
5556 return -EFAULT;
5557
5558 return 0;
5559}
5560
NeilBrownfd01b882011-10-11 16:47:53 +11005561static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562{
5563 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005564 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005565 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
NeilBrown1ca69c42012-10-11 13:37:33 +11005567 nr = working = insync = failed = spare = 0;
5568 rcu_read_lock();
5569 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005571 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 failed++;
5573 else {
5574 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005575 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005576 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 else
5578 spare++;
5579 }
5580 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005581 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
5583 info.major_version = mddev->major_version;
5584 info.minor_version = mddev->minor_version;
5585 info.patch_version = MD_PATCHLEVEL_VERSION;
5586 info.ctime = mddev->ctime;
5587 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005588 info.size = mddev->dev_sectors / 2;
5589 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005590 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 info.nr_disks = nr;
5592 info.raid_disks = mddev->raid_disks;
5593 info.md_minor = mddev->md_minor;
5594 info.not_persistent= !mddev->persistent;
5595
5596 info.utime = mddev->utime;
5597 info.state = 0;
5598 if (mddev->in_sync)
5599 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005600 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005601 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005602 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 info.working_disks = working;
5604 info.failed_disks = failed;
5605 info.spare_disks = spare;
5606
5607 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005608 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
5610 if (copy_to_user(arg, &info, sizeof(info)))
5611 return -EFAULT;
5612
5613 return 0;
5614}
5615
NeilBrownfd01b882011-10-11 16:47:53 +11005616static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005617{
5618 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5619 char *ptr, *buf = NULL;
5620 int err = -ENOMEM;
5621
Dan Williamsb5470dc2008-06-27 21:44:04 -07005622 if (md_allow_write(mddev))
5623 file = kmalloc(sizeof(*file), GFP_NOIO);
5624 else
5625 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005626
NeilBrown32a76272005-06-21 17:17:14 -07005627 if (!file)
5628 goto out;
5629
5630 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005631 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005632 file->pathname[0] = '\0';
5633 goto copy_out;
5634 }
5635
5636 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5637 if (!buf)
5638 goto out;
5639
NeilBrown1ec885c2012-05-22 13:55:10 +10005640 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5641 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005642 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005643 goto out;
5644
5645 strcpy(file->pathname, ptr);
5646
5647copy_out:
5648 err = 0;
5649 if (copy_to_user(arg, file, sizeof(*file)))
5650 err = -EFAULT;
5651out:
5652 kfree(buf);
5653 kfree(file);
5654 return err;
5655}
5656
NeilBrownfd01b882011-10-11 16:47:53 +11005657static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658{
5659 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005660 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661
5662 if (copy_from_user(&info, arg, sizeof(info)))
5663 return -EFAULT;
5664
NeilBrown1ca69c42012-10-11 13:37:33 +11005665 rcu_read_lock();
5666 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 if (rdev) {
5668 info.major = MAJOR(rdev->bdev->bd_dev);
5669 info.minor = MINOR(rdev->bdev->bd_dev);
5670 info.raid_disk = rdev->raid_disk;
5671 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005672 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005674 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 info.state |= (1<<MD_DISK_ACTIVE);
5676 info.state |= (1<<MD_DISK_SYNC);
5677 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005678 if (test_bit(WriteMostly, &rdev->flags))
5679 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 } else {
5681 info.major = info.minor = 0;
5682 info.raid_disk = -1;
5683 info.state = (1<<MD_DISK_REMOVED);
5684 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005685 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
5687 if (copy_to_user(arg, &info, sizeof(info)))
5688 return -EFAULT;
5689
5690 return 0;
5691}
5692
NeilBrownfd01b882011-10-11 16:47:53 +11005693static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694{
5695 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005696 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 dev_t dev = MKDEV(info->major,info->minor);
5698
5699 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5700 return -EOVERFLOW;
5701
5702 if (!mddev->raid_disks) {
5703 int err;
5704 /* expecting a device which has a superblock */
5705 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5706 if (IS_ERR(rdev)) {
5707 printk(KERN_WARNING
5708 "md: md_import_device returned %ld\n",
5709 PTR_ERR(rdev));
5710 return PTR_ERR(rdev);
5711 }
5712 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005713 struct md_rdev *rdev0
5714 = list_entry(mddev->disks.next,
5715 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005716 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 .load_super(rdev, rdev0, mddev->minor_version);
5718 if (err < 0) {
5719 printk(KERN_WARNING
5720 "md: %s has different UUID to %s\n",
5721 bdevname(rdev->bdev,b),
5722 bdevname(rdev0->bdev,b2));
5723 export_rdev(rdev);
5724 return -EINVAL;
5725 }
5726 }
5727 err = bind_rdev_to_array(rdev, mddev);
5728 if (err)
5729 export_rdev(rdev);
5730 return err;
5731 }
5732
5733 /*
5734 * add_new_disk can be used once the array is assembled
5735 * to add "hot spares". They must already have a superblock
5736 * written
5737 */
5738 if (mddev->pers) {
5739 int err;
5740 if (!mddev->pers->hot_add_disk) {
5741 printk(KERN_WARNING
5742 "%s: personality does not support diskops!\n",
5743 mdname(mddev));
5744 return -EINVAL;
5745 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005746 if (mddev->persistent)
5747 rdev = md_import_device(dev, mddev->major_version,
5748 mddev->minor_version);
5749 else
5750 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 if (IS_ERR(rdev)) {
5752 printk(KERN_WARNING
5753 "md: md_import_device returned %ld\n",
5754 PTR_ERR(rdev));
5755 return PTR_ERR(rdev);
5756 }
NeilBrown1a855a02010-12-09 16:36:28 +11005757 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005758 if (!mddev->persistent) {
5759 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005760 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005761 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005762 set_bit(In_sync, &rdev->flags);
5763 } else
NeilBrown41158c72005-06-21 17:17:25 -07005764 rdev->raid_disk = -1;
5765 } else
5766 super_types[mddev->major_version].
5767 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005768 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005769 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005770 /* This was a hot-add request, but events doesn't
5771 * match, so reject it.
5772 */
5773 export_rdev(rdev);
5774 return -EINVAL;
5775 }
5776
NeilBrown1a855a02010-12-09 16:36:28 +11005777 if (test_bit(In_sync, &rdev->flags))
5778 rdev->saved_raid_disk = rdev->raid_disk;
5779 else
5780 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005781
NeilBrownb2d444d2005-11-08 21:39:31 -08005782 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005783 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5784 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005785 else
5786 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005787
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 rdev->raid_disk = -1;
5789 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005790 if (!err && !mddev->pers->hot_remove_disk) {
5791 /* If there is hot_add_disk but no hot_remove_disk
5792 * then added disks for geometry changes,
5793 * and should be added immediately.
5794 */
5795 super_types[mddev->major_version].
5796 validate_super(mddev, rdev);
5797 err = mddev->pers->hot_add_disk(mddev, rdev);
5798 if (err)
5799 unbind_rdev_from_array(rdev);
5800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 if (err)
5802 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005803 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005804 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005805
NeilBrown7ceb17e2013-04-24 11:42:42 +10005806 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005807 if (mddev->degraded)
5808 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005809 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005810 if (!err)
5811 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005812 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 return err;
5814 }
5815
5816 /* otherwise, add_new_disk is only allowed
5817 * for major_version==0 superblocks
5818 */
5819 if (mddev->major_version != 0) {
5820 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5821 mdname(mddev));
5822 return -EINVAL;
5823 }
5824
5825 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5826 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005827 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 if (IS_ERR(rdev)) {
5829 printk(KERN_WARNING
5830 "md: error, md_import_device() returned %ld\n",
5831 PTR_ERR(rdev));
5832 return PTR_ERR(rdev);
5833 }
5834 rdev->desc_nr = info->number;
5835 if (info->raid_disk < mddev->raid_disks)
5836 rdev->raid_disk = info->raid_disk;
5837 else
5838 rdev->raid_disk = -1;
5839
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005841 if (info->state & (1<<MD_DISK_SYNC))
5842 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005844 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5845 set_bit(WriteMostly, &rdev->flags);
5846
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 if (!mddev->persistent) {
5848 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005849 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5850 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005851 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005852 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
NeilBrown2bf071b2006-01-06 00:20:55 -08005854 err = bind_rdev_to_array(rdev, mddev);
5855 if (err) {
5856 export_rdev(rdev);
5857 return err;
5858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 }
5860
5861 return 0;
5862}
5863
NeilBrownfd01b882011-10-11 16:47:53 +11005864static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865{
5866 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005867 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 rdev = find_rdev(mddev, dev);
5870 if (!rdev)
5871 return -ENXIO;
5872
NeilBrown3ea8929d2013-04-24 11:42:41 +10005873 clear_bit(Blocked, &rdev->flags);
5874 remove_and_add_spares(mddev, rdev);
5875
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 if (rdev->raid_disk >= 0)
5877 goto busy;
5878
5879 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005880 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005881 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
5883 return 0;
5884busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005885 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 bdevname(rdev->bdev,b), mdname(mddev));
5887 return -EBUSY;
5888}
5889
NeilBrownfd01b882011-10-11 16:47:53 +11005890static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891{
5892 char b[BDEVNAME_SIZE];
5893 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005894 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
5896 if (!mddev->pers)
5897 return -ENODEV;
5898
5899 if (mddev->major_version != 0) {
5900 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5901 " version-0 superblocks.\n",
5902 mdname(mddev));
5903 return -EINVAL;
5904 }
5905 if (!mddev->pers->hot_add_disk) {
5906 printk(KERN_WARNING
5907 "%s: personality does not support diskops!\n",
5908 mdname(mddev));
5909 return -EINVAL;
5910 }
5911
NeilBrownd710e132008-10-13 11:55:12 +11005912 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 if (IS_ERR(rdev)) {
5914 printk(KERN_WARNING
5915 "md: error, md_import_device() returned %ld\n",
5916 PTR_ERR(rdev));
5917 return -EINVAL;
5918 }
5919
5920 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005921 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005923 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
NeilBrown8190e752009-06-18 08:48:58 +10005925 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
NeilBrownb2d444d2005-11-08 21:39:31 -08005927 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 printk(KERN_WARNING
5929 "md: can not hot-add faulty %s disk to %s!\n",
5930 bdevname(rdev->bdev,b), mdname(mddev));
5931 err = -EINVAL;
5932 goto abort_export;
5933 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005934 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005936 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005937 err = bind_rdev_to_array(rdev, mddev);
5938 if (err)
5939 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
5941 /*
5942 * The rest should better be atomic, we can have disk failures
5943 * noticed in interrupt contexts ...
5944 */
5945
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 rdev->raid_disk = -1;
5947
NeilBrown850b2b42006-10-03 01:15:46 -07005948 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949
5950 /*
5951 * Kick recovery, maybe this spare has to be added to the
5952 * array immediately.
5953 */
5954 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5955 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005956 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 return 0;
5958
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959abort_export:
5960 export_rdev(rdev);
5961 return err;
5962}
5963
NeilBrownfd01b882011-10-11 16:47:53 +11005964static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005965{
5966 int err;
5967
NeilBrown36fa3062005-09-09 16:23:45 -07005968 if (mddev->pers) {
5969 if (!mddev->pers->quiesce)
5970 return -EBUSY;
5971 if (mddev->recovery || mddev->sync_thread)
5972 return -EBUSY;
5973 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005974 }
5975
NeilBrown36fa3062005-09-09 16:23:45 -07005976
5977 if (fd >= 0) {
5978 if (mddev->bitmap)
5979 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005980 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005981
NeilBrownc3d97142009-12-14 12:49:52 +11005982 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005983 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5984 mdname(mddev));
5985 return -EBADF;
5986 }
5987
NeilBrownc3d97142009-12-14 12:49:52 +11005988 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005989 if (err) {
5990 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5991 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005992 fput(mddev->bitmap_info.file);
5993 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005994 return err;
5995 }
NeilBrownc3d97142009-12-14 12:49:52 +11005996 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005997 } else if (mddev->bitmap == NULL)
5998 return -ENOENT; /* cannot remove what isn't there */
5999 err = 0;
6000 if (mddev->pers) {
6001 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006002 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07006003 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006004 if (!err)
6005 err = bitmap_load(mddev);
6006 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006007 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006008 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006009 fd = -1; /* make sure to put the file */
6010 }
NeilBrown36fa3062005-09-09 16:23:45 -07006011 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006012 }
6013 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11006014 if (mddev->bitmap_info.file) {
6015 restore_bitmap_write_access(mddev->bitmap_info.file);
6016 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07006017 }
NeilBrownc3d97142009-12-14 12:49:52 +11006018 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006019 }
6020
NeilBrown32a76272005-06-21 17:17:14 -07006021 return err;
6022}
6023
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024/*
6025 * set_array_info is used two different ways
6026 * The original usage is when creating a new array.
6027 * In this usage, raid_disks is > 0 and it together with
6028 * level, size, not_persistent,layout,chunksize determine the
6029 * shape of the array.
6030 * This will always create an array with a type-0.90.0 superblock.
6031 * The newer usage is when assembling an array.
6032 * In this case raid_disks will be 0, and the major_version field is
6033 * use to determine which style super-blocks are to be found on the devices.
6034 * The minor and patch _version numbers are also kept incase the
6035 * super_block handler wishes to interpret them.
6036 */
NeilBrownfd01b882011-10-11 16:47:53 +11006037static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038{
6039
6040 if (info->raid_disks == 0) {
6041 /* just setting version number for superblock loading */
6042 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006043 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 super_types[info->major_version].name == NULL) {
6045 /* maybe try to auto-load a module? */
6046 printk(KERN_INFO
6047 "md: superblock version %d not known\n",
6048 info->major_version);
6049 return -EINVAL;
6050 }
6051 mddev->major_version = info->major_version;
6052 mddev->minor_version = info->minor_version;
6053 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006054 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006055 /* ensure mddev_put doesn't delete this now that there
6056 * is some minimal configuration.
6057 */
6058 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 return 0;
6060 }
6061 mddev->major_version = MD_MAJOR_VERSION;
6062 mddev->minor_version = MD_MINOR_VERSION;
6063 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6064 mddev->ctime = get_seconds();
6065
6066 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006067 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006068 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 mddev->raid_disks = info->raid_disks;
6070 /* don't set md_minor, it is determined by which /dev/md* was
6071 * openned
6072 */
6073 if (info->state & (1<<MD_SB_CLEAN))
6074 mddev->recovery_cp = MaxSector;
6075 else
6076 mddev->recovery_cp = 0;
6077 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006078 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
6080 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006081 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
6083 mddev->max_disks = MD_SB_DISKS;
6084
NeilBrowne6910632008-02-06 01:39:51 -08006085 if (mddev->persistent)
6086 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006087 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
NeilBrownc3d97142009-12-14 12:49:52 +11006089 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006090 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006091 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006092
NeilBrownf6705572006-03-27 01:18:11 -08006093 mddev->reshape_position = MaxSector;
6094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 /*
6096 * Generate a 128 bit UUID
6097 */
6098 get_random_bytes(mddev->uuid, 16);
6099
NeilBrownf6705572006-03-27 01:18:11 -08006100 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006101 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006102 mddev->new_layout = mddev->layout;
6103 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006104 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006105
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 return 0;
6107}
6108
NeilBrownfd01b882011-10-11 16:47:53 +11006109void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006110{
Dan Williamsb522adc2009-03-31 15:00:31 +11006111 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6112
6113 if (mddev->external_size)
6114 return;
6115
Dan Williams1f403622009-03-31 14:59:03 +11006116 mddev->array_sectors = array_sectors;
6117}
6118EXPORT_SYMBOL(md_set_array_sectors);
6119
NeilBrownfd01b882011-10-11 16:47:53 +11006120static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006121{
NeilBrown3cb03002011-10-11 16:45:26 +11006122 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006123 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006124 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006125
6126 if (mddev->pers->resize == NULL)
6127 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006128 /* The "num_sectors" is the number of sectors of each device that
6129 * is used. This can only make sense for arrays with redundancy.
6130 * linear and raid0 always use whatever space is available. We can only
6131 * consider changing this number if no resync or reconstruction is
6132 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006133 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006134 * of each device. If num_sectors is zero, we find the largest size
6135 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006136 */
6137 if (mddev->sync_thread)
6138 return -EBUSY;
NeilBrowna4a61252012-05-22 13:55:27 +10006139
NeilBrowndafb20f2012-03-19 12:46:39 +11006140 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006141 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006142
Andre Nolld71f9f82008-07-11 22:02:22 +10006143 if (fit && (num_sectors == 0 || num_sectors > avail))
6144 num_sectors = avail;
6145 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006146 return -ENOSPC;
6147 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006148 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006149 if (!rv)
6150 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006151 return rv;
6152}
6153
NeilBrownfd01b882011-10-11 16:47:53 +11006154static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006155{
6156 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006157 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006158 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006159 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006160 return -EINVAL;
6161 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006162 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006163 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006164 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006165 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006166
6167 rdev_for_each(rdev, mddev) {
6168 if (mddev->raid_disks < raid_disks &&
6169 rdev->data_offset < rdev->new_data_offset)
6170 return -EINVAL;
6171 if (mddev->raid_disks > raid_disks &&
6172 rdev->data_offset > rdev->new_data_offset)
6173 return -EINVAL;
6174 }
6175
NeilBrown63c70c42006-03-27 01:18:13 -08006176 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006177 if (mddev->delta_disks < 0)
6178 mddev->reshape_backwards = 1;
6179 else if (mddev->delta_disks > 0)
6180 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006181
6182 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006183 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006184 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006185 mddev->reshape_backwards = 0;
6186 }
NeilBrownda943b992006-01-06 00:20:54 -08006187 return rv;
6188}
6189
6190
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191/*
6192 * update_array_info is used to change the configuration of an
6193 * on-line array.
6194 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6195 * fields in the info are checked against the array.
6196 * Any differences that cannot be handled will cause an error.
6197 * Normally, only one change can be managed at a time.
6198 */
NeilBrownfd01b882011-10-11 16:47:53 +11006199static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200{
6201 int rv = 0;
6202 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006203 int state = 0;
6204
6205 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006206 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006207 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
6209 if (mddev->major_version != info->major_version ||
6210 mddev->minor_version != info->minor_version ||
6211/* mddev->patch_version != info->patch_version || */
6212 mddev->ctime != info->ctime ||
6213 mddev->level != info->level ||
6214/* mddev->layout != info->layout || */
6215 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006216 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006217 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6218 ((state^info->state) & 0xfffffe00)
6219 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 return -EINVAL;
6221 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006222 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6223 cnt++;
6224 if (mddev->raid_disks != info->raid_disks)
6225 cnt++;
6226 if (mddev->layout != info->layout)
6227 cnt++;
6228 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6229 cnt++;
6230 if (cnt == 0)
6231 return 0;
6232 if (cnt > 1)
6233 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234
6235 if (mddev->layout != info->layout) {
6236 /* Change layout
6237 * we don't need to do anything at the md level, the
6238 * personality will take care of it all.
6239 */
NeilBrown50ac1682009-06-18 08:47:55 +10006240 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006242 else {
6243 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006244 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006245 if (rv)
6246 mddev->new_layout = mddev->layout;
6247 return rv;
6248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006250 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006251 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252
NeilBrownda943b992006-01-06 00:20:54 -08006253 if (mddev->raid_disks != info->raid_disks)
6254 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
NeilBrown36fa3062005-09-09 16:23:45 -07006256 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6257 if (mddev->pers->quiesce == NULL)
6258 return -EINVAL;
6259 if (mddev->recovery || mddev->sync_thread)
6260 return -EBUSY;
6261 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6262 /* add the bitmap */
6263 if (mddev->bitmap)
6264 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006265 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006266 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006267 mddev->bitmap_info.offset =
6268 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006269 mddev->bitmap_info.space =
6270 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006271 mddev->pers->quiesce(mddev, 1);
6272 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006273 if (!rv)
6274 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006275 if (rv)
6276 bitmap_destroy(mddev);
6277 mddev->pers->quiesce(mddev, 0);
6278 } else {
6279 /* remove the bitmap */
6280 if (!mddev->bitmap)
6281 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006282 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006283 return -EINVAL;
6284 mddev->pers->quiesce(mddev, 1);
6285 bitmap_destroy(mddev);
6286 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006287 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006288 }
6289 }
NeilBrown850b2b42006-10-03 01:15:46 -07006290 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 return rv;
6292}
6293
NeilBrownfd01b882011-10-11 16:47:53 +11006294static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295{
NeilBrown3cb03002011-10-11 16:45:26 +11006296 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006297 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
6299 if (mddev->pers == NULL)
6300 return -ENODEV;
6301
NeilBrown1ca69c42012-10-11 13:37:33 +11006302 rcu_read_lock();
6303 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006305 err = -ENODEV;
6306 else {
6307 md_error(mddev, rdev);
6308 if (!test_bit(Faulty, &rdev->flags))
6309 err = -EBUSY;
6310 }
6311 rcu_read_unlock();
6312 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313}
6314
Andre Noll2f9618c2008-04-25 18:57:58 +02006315/*
6316 * We have a problem here : there is no easy way to give a CHS
6317 * virtual geometry. We currently pretend that we have a 2 heads
6318 * 4 sectors (with a BIG number of cylinders...). This drives
6319 * dosfs just mad... ;-)
6320 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006321static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6322{
NeilBrownfd01b882011-10-11 16:47:53 +11006323 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006324
6325 geo->heads = 2;
6326 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006327 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006328 return 0;
6329}
6330
Al Viroa39907f2008-03-02 10:31:15 -05006331static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 unsigned int cmd, unsigned long arg)
6333{
6334 int err = 0;
6335 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006336 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006337 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
NeilBrown506c9e42011-12-23 10:17:26 +11006339 switch (cmd) {
6340 case RAID_VERSION:
6341 case GET_ARRAY_INFO:
6342 case GET_DISK_INFO:
6343 break;
6344 default:
6345 if (!capable(CAP_SYS_ADMIN))
6346 return -EACCES;
6347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348
6349 /*
6350 * Commands dealing with the RAID driver but not any
6351 * particular array:
6352 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006353 switch (cmd) {
6354 case RAID_VERSION:
6355 err = get_version(argp);
6356 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
NeilBrownc02c0ae2012-12-11 13:39:21 +11006358 case PRINT_RAID_DEBUG:
6359 err = 0;
6360 md_print_devices();
6361 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
6363#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006364 case RAID_AUTORUN:
6365 err = 0;
6366 autostart_arrays(arg);
6367 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006369 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 }
6371
6372 /*
6373 * Commands creating/starting a new array:
6374 */
6375
Al Viroa39907f2008-03-02 10:31:15 -05006376 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
6378 if (!mddev) {
6379 BUG();
6380 goto abort;
6381 }
6382
NeilBrown1ca69c42012-10-11 13:37:33 +11006383 /* Some actions do not requires the mutex */
6384 switch (cmd) {
6385 case GET_ARRAY_INFO:
6386 if (!mddev->raid_disks && !mddev->external)
6387 err = -ENODEV;
6388 else
6389 err = get_array_info(mddev, argp);
6390 goto abort;
6391
6392 case GET_DISK_INFO:
6393 if (!mddev->raid_disks && !mddev->external)
6394 err = -ENODEV;
6395 else
6396 err = get_disk_info(mddev, argp);
6397 goto abort;
6398
6399 case SET_DISK_FAULTY:
6400 err = set_disk_faulty(mddev, new_decode_dev(arg));
6401 goto abort;
6402 }
6403
NeilBrowna7a3f082012-12-11 13:35:54 +11006404 if (cmd == ADD_NEW_DISK)
6405 /* need to ensure md_delayed_delete() has completed */
6406 flush_workqueue(md_misc_wq);
6407
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 err = mddev_lock(mddev);
6409 if (err) {
6410 printk(KERN_INFO
6411 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6412 err, cmd);
6413 goto abort;
6414 }
6415
NeilBrownc02c0ae2012-12-11 13:39:21 +11006416 if (cmd == SET_ARRAY_INFO) {
6417 mdu_array_info_t info;
6418 if (!arg)
6419 memset(&info, 0, sizeof(info));
6420 else if (copy_from_user(&info, argp, sizeof(info))) {
6421 err = -EFAULT;
6422 goto abort_unlock;
6423 }
6424 if (mddev->pers) {
6425 err = update_array_info(mddev, &info);
6426 if (err) {
6427 printk(KERN_WARNING "md: couldn't update"
6428 " array info. %d\n", err);
6429 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 }
6431 goto done_unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006432 }
6433 if (!list_empty(&mddev->disks)) {
6434 printk(KERN_WARNING
6435 "md: array %s already has disks!\n",
6436 mdname(mddev));
6437 err = -EBUSY;
6438 goto abort_unlock;
6439 }
6440 if (mddev->raid_disks) {
6441 printk(KERN_WARNING
6442 "md: array %s already initialised!\n",
6443 mdname(mddev));
6444 err = -EBUSY;
6445 goto abort_unlock;
6446 }
6447 err = set_array_info(mddev, &info);
6448 if (err) {
6449 printk(KERN_WARNING "md: couldn't set"
6450 " array info. %d\n", err);
6451 goto abort_unlock;
6452 }
6453 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 }
6455
6456 /*
6457 * Commands querying/configuring an existing array:
6458 */
NeilBrown32a76272005-06-21 17:17:14 -07006459 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006460 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006461 if ((!mddev->raid_disks && !mddev->external)
6462 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6463 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6464 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 err = -ENODEV;
6466 goto abort_unlock;
6467 }
6468
6469 /*
6470 * Commands even a read-only array can execute:
6471 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006472 switch (cmd) {
6473 case GET_BITMAP_FILE:
6474 err = get_bitmap_file(mddev, argp);
6475 goto done_unlock;
6476
6477 case RESTART_ARRAY_RW:
6478 err = restart_array(mddev);
6479 goto done_unlock;
6480
6481 case STOP_ARRAY:
6482 err = do_md_stop(mddev, 0, bdev);
6483 goto done_unlock;
6484
6485 case STOP_ARRAY_RO:
6486 err = md_set_readonly(mddev, bdev);
6487 goto done_unlock;
6488
NeilBrown3ea8929d2013-04-24 11:42:41 +10006489 case HOT_REMOVE_DISK:
6490 err = hot_remove_disk(mddev, new_decode_dev(arg));
6491 goto done_unlock;
6492
NeilBrown7ceb17e2013-04-24 11:42:42 +10006493 case ADD_NEW_DISK:
6494 /* We can support ADD_NEW_DISK on read-only arrays
6495 * on if we are re-adding a preexisting device.
6496 * So require mddev->pers and MD_DISK_SYNC.
6497 */
6498 if (mddev->pers) {
6499 mdu_disk_info_t info;
6500 if (copy_from_user(&info, argp, sizeof(info)))
6501 err = -EFAULT;
6502 else if (!(info.state & (1<<MD_DISK_SYNC)))
6503 /* Need to clear read-only for this */
6504 break;
6505 else
6506 err = add_new_disk(mddev, &info);
6507 goto done_unlock;
6508 }
6509 break;
6510
NeilBrownc02c0ae2012-12-11 13:39:21 +11006511 case BLKROSET:
6512 if (get_user(ro, (int __user *)(arg))) {
6513 err = -EFAULT;
6514 goto done_unlock;
6515 }
6516 err = -EINVAL;
6517
6518 /* if the bdev is going readonly the value of mddev->ro
6519 * does not matter, no writes are coming
6520 */
6521 if (ro)
NeilBrown32a76272005-06-21 17:17:14 -07006522 goto done_unlock;
6523
NeilBrownc02c0ae2012-12-11 13:39:21 +11006524 /* are we are already prepared for writes? */
6525 if (mddev->ro != 1)
6526 goto done_unlock;
6527
6528 /* transitioning to readauto need only happen for
6529 * arrays that call md_write_start
6530 */
6531 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006533 if (err == 0) {
6534 mddev->ro = 2;
6535 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006536 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006537 }
6538 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 }
6540
6541 /*
6542 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006543 * superblock, so we do not allow them on read-only arrays.
6544 * However non-MD ioctls (e.g. get-size) will still come through
6545 * here and hit the 'default' below, so only disallow
6546 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006548 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006549 if (mddev->ro == 2) {
6550 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006551 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006552 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006553 /* mddev_unlock will wake thread */
6554 /* If a device failed while we were read-only, we
6555 * need to make sure the metadata is updated now.
6556 */
6557 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6558 mddev_unlock(mddev);
6559 wait_event(mddev->sb_wait,
6560 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6561 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6562 mddev_lock(mddev);
6563 }
NeilBrownf91de922005-11-08 21:39:36 -08006564 } else {
6565 err = -EROFS;
6566 goto abort_unlock;
6567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 }
6569
NeilBrownc02c0ae2012-12-11 13:39:21 +11006570 switch (cmd) {
6571 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006573 mdu_disk_info_t info;
6574 if (copy_from_user(&info, argp, sizeof(info)))
6575 err = -EFAULT;
6576 else
6577 err = add_new_disk(mddev, &info);
6578 goto done_unlock;
6579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
NeilBrownc02c0ae2012-12-11 13:39:21 +11006581 case HOT_ADD_DISK:
6582 err = hot_add_disk(mddev, new_decode_dev(arg));
6583 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584
NeilBrownc02c0ae2012-12-11 13:39:21 +11006585 case RUN_ARRAY:
6586 err = do_md_run(mddev);
6587 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588
NeilBrownc02c0ae2012-12-11 13:39:21 +11006589 case SET_BITMAP_FILE:
6590 err = set_bitmap_file(mddev, (int)arg);
6591 goto done_unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006592
NeilBrownc02c0ae2012-12-11 13:39:21 +11006593 default:
6594 err = -EINVAL;
6595 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 }
6597
6598done_unlock:
6599abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006600 if (mddev->hold_active == UNTIL_IOCTL &&
6601 err != -EINVAL)
6602 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 mddev_unlock(mddev);
6604
6605 return err;
6606done:
6607 if (err)
6608 MD_BUG();
6609abort:
6610 return err;
6611}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006612#ifdef CONFIG_COMPAT
6613static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6614 unsigned int cmd, unsigned long arg)
6615{
6616 switch (cmd) {
6617 case HOT_REMOVE_DISK:
6618 case HOT_ADD_DISK:
6619 case SET_DISK_FAULTY:
6620 case SET_BITMAP_FILE:
6621 /* These take in integer arg, do not convert */
6622 break;
6623 default:
6624 arg = (unsigned long)compat_ptr(arg);
6625 break;
6626 }
6627
6628 return md_ioctl(bdev, mode, cmd, arg);
6629}
6630#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631
Al Viroa39907f2008-03-02 10:31:15 -05006632static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633{
6634 /*
6635 * Succeed if we can lock the mddev, which confirms that
6636 * it isn't being stopped right now.
6637 */
NeilBrownfd01b882011-10-11 16:47:53 +11006638 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 int err;
6640
Yuanhan Liu0c098222012-05-22 13:55:32 +10006641 if (!mddev)
6642 return -ENODEV;
6643
NeilBrownd3374822009-01-09 08:31:10 +11006644 if (mddev->gendisk != bdev->bd_disk) {
6645 /* we are racing with mddev_put which is discarding this
6646 * bd_disk.
6647 */
6648 mddev_put(mddev);
6649 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006650 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006651 /* Then retry the open from the top */
6652 return -ERESTARTSYS;
6653 }
6654 BUG_ON(mddev != bdev->bd_disk->private_data);
6655
NeilBrownc8c00a62009-08-10 12:50:52 +10006656 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 goto out;
6658
6659 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006660 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006661 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006663 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 out:
6665 return err;
6666}
6667
Al Virodb2a1442013-05-05 21:52:57 -04006668static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669{
NeilBrownfd01b882011-10-11 16:47:53 +11006670 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006672 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006673 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006676
6677static int md_media_changed(struct gendisk *disk)
6678{
NeilBrownfd01b882011-10-11 16:47:53 +11006679 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006680
6681 return mddev->changed;
6682}
6683
6684static int md_revalidate(struct gendisk *disk)
6685{
NeilBrownfd01b882011-10-11 16:47:53 +11006686 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006687
6688 mddev->changed = 0;
6689 return 0;
6690}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006691static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692{
6693 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006694 .open = md_open,
6695 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006696 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006697#ifdef CONFIG_COMPAT
6698 .compat_ioctl = md_compat_ioctl,
6699#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006700 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006701 .media_changed = md_media_changed,
6702 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703};
6704
Adrian Bunk75c96f82005-05-05 16:16:09 -07006705static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706{
NeilBrown2b8bf342011-10-11 16:48:23 +11006707 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 /*
6710 * md_thread is a 'system-thread', it's priority should be very
6711 * high. We avoid resource deadlocks individually in each
6712 * raid personality. (RAID5 does preallocation) We also use RR and
6713 * the very same RT priority as kswapd, thus we will never get
6714 * into a priority inversion deadlock.
6715 *
6716 * we definitely have to have equal or higher priority than
6717 * bdflush, otherwise bdflush will deadlock if there are too
6718 * many dirty RAID5 blocks.
6719 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720
NeilBrown6985c432005-10-19 21:23:47 -07006721 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006722 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723
NeilBrown93588e22005-11-15 00:09:12 -08006724 /* We need to wait INTERRUPTIBLE so that
6725 * we don't add to the load-average.
6726 * That means we need to be sure no signals are
6727 * pending
6728 */
6729 if (signal_pending(current))
6730 flush_signals(current);
6731
6732 wait_event_interruptible_timeout
6733 (thread->wqueue,
6734 test_bit(THREAD_WAKEUP, &thread->flags)
6735 || kthread_should_stop(),
6736 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737
NeilBrown6c987912011-01-14 09:13:53 +11006738 clear_bit(THREAD_WAKEUP, &thread->flags);
6739 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006740 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006742
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 return 0;
6744}
6745
NeilBrown2b8bf342011-10-11 16:48:23 +11006746void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747{
6748 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006749 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 set_bit(THREAD_WAKEUP, &thread->flags);
6751 wake_up(&thread->wqueue);
6752 }
6753}
6754
Shaohua Li4ed87312012-10-11 13:34:00 +11006755struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6756 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757{
NeilBrown2b8bf342011-10-11 16:48:23 +11006758 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759
NeilBrown2b8bf342011-10-11 16:48:23 +11006760 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 if (!thread)
6762 return NULL;
6763
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 init_waitqueue_head(&thread->wqueue);
6765
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 thread->run = run;
6767 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006768 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006769 thread->tsk = kthread_run(md_thread, thread,
6770 "%s_%s",
6771 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006772 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006773 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 kfree(thread);
6775 return NULL;
6776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 return thread;
6778}
6779
NeilBrown2b8bf342011-10-11 16:48:23 +11006780void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781{
NeilBrown2b8bf342011-10-11 16:48:23 +11006782 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006783 if (!thread)
6784 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006785 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006786 /* Locking ensures that mddev_unlock does not wake_up a
6787 * non-existent thread
6788 */
6789 spin_lock(&pers_lock);
6790 *threadp = NULL;
6791 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006792
6793 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 kfree(thread);
6795}
6796
NeilBrownfd01b882011-10-11 16:47:53 +11006797void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798{
6799 if (!mddev) {
6800 MD_BUG();
6801 return;
6802 }
6803
NeilBrownb2d444d2005-11-08 21:39:31 -08006804 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006806
NeilBrownde393cd2011-07-28 11:31:48 +10006807 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 return;
6809 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006810 if (mddev->degraded)
6811 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006812 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6814 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6815 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006816 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006817 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006818 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819}
6820
6821/* seq_file implementation /proc/mdstat */
6822
6823static void status_unused(struct seq_file *seq)
6824{
6825 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006826 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827
6828 seq_printf(seq, "unused devices: ");
6829
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006830 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 char b[BDEVNAME_SIZE];
6832 i++;
6833 seq_printf(seq, "%s ",
6834 bdevname(rdev->bdev,b));
6835 }
6836 if (!i)
6837 seq_printf(seq, "<none>");
6838
6839 seq_printf(seq, "\n");
6840}
6841
6842
NeilBrownfd01b882011-10-11 16:47:53 +11006843static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844{
NeilBrowndd71cf62009-05-07 12:49:35 +10006845 sector_t max_sectors, resync, res;
6846 unsigned long dt, db;
6847 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006848 int scale;
6849 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850
NeilBrown72f36d52012-10-11 14:25:57 +11006851 if (mddev->curr_resync <= 3)
6852 resync = 0;
6853 else
6854 resync = mddev->curr_resync
6855 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856
NeilBrownc804cde2012-05-21 09:28:33 +10006857 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6858 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006859 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006861 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862
6863 /*
6864 * Should not happen.
6865 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006866 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 MD_BUG();
6868 return;
6869 }
NeilBrown4588b422006-03-27 01:18:04 -08006870 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006871 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006872 * u32, as those are the requirements for sector_div.
6873 * Thus 'scale' must be at least 10
6874 */
6875 scale = 10;
6876 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006877 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006878 scale++;
6879 }
6880 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006881 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006882
6883 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 {
NeilBrown4588b422006-03-27 01:18:04 -08006885 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 seq_printf(seq, "[");
6887 for (i = 0; i < x; i++)
6888 seq_printf(seq, "=");
6889 seq_printf(seq, ">");
6890 for (i = 0; i < y; i++)
6891 seq_printf(seq, ".");
6892 seq_printf(seq, "] ");
6893 }
NeilBrown4588b422006-03-27 01:18:04 -08006894 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006895 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6896 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006897 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6898 "check" :
6899 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6900 "resync" : "recovery"))),
6901 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006902 (unsigned long long) resync/2,
6903 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904
6905 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 * dt: time from mark until now
6907 * db: blocks written from mark until now
6908 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006909 *
6910 * rt is a sector_t, so could be 32bit or 64bit.
6911 * So we divide before multiply in case it is 32bit and close
6912 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006913 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006914 * near the end of resync when the number of remaining sectors
6915 * is close to 'db'.
6916 * We then divide rt by 32 after multiplying by db to compensate.
6917 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 */
6919 dt = ((jiffies - mddev->resync_mark) / HZ);
6920 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006921 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6922 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
NeilBrowndd71cf62009-05-07 12:49:35 +10006924 rt = max_sectors - resync; /* number of remaining sectors */
6925 sector_div(rt, db/32+1);
6926 rt *= dt;
6927 rt >>= 5;
6928
6929 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6930 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931
NeilBrownff4e8d92006-07-10 04:44:16 -07006932 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933}
6934
6935static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6936{
6937 struct list_head *tmp;
6938 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006939 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940
6941 if (l >= 0x10000)
6942 return NULL;
6943 if (!l--)
6944 /* header */
6945 return (void*)1;
6946
6947 spin_lock(&all_mddevs_lock);
6948 list_for_each(tmp,&all_mddevs)
6949 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006950 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 mddev_get(mddev);
6952 spin_unlock(&all_mddevs_lock);
6953 return mddev;
6954 }
6955 spin_unlock(&all_mddevs_lock);
6956 if (!l--)
6957 return (void*)2;/* tail */
6958 return NULL;
6959}
6960
6961static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6962{
6963 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006964 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965
6966 ++*pos;
6967 if (v == (void*)2)
6968 return NULL;
6969
6970 spin_lock(&all_mddevs_lock);
6971 if (v == (void*)1)
6972 tmp = all_mddevs.next;
6973 else
6974 tmp = mddev->all_mddevs.next;
6975 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006976 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 else {
6978 next_mddev = (void*)2;
6979 *pos = 0x10000;
6980 }
6981 spin_unlock(&all_mddevs_lock);
6982
6983 if (v != (void*)1)
6984 mddev_put(mddev);
6985 return next_mddev;
6986
6987}
6988
6989static void md_seq_stop(struct seq_file *seq, void *v)
6990{
NeilBrownfd01b882011-10-11 16:47:53 +11006991 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
6993 if (mddev && v != (void*)1 && v != (void*)2)
6994 mddev_put(mddev);
6995}
6996
6997static int md_seq_show(struct seq_file *seq, void *v)
6998{
NeilBrownfd01b882011-10-11 16:47:53 +11006999 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007000 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007001 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002
7003 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007004 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 seq_printf(seq, "Personalities : ");
7006 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007007 list_for_each_entry(pers, &pers_list, list)
7008 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009
7010 spin_unlock(&pers_lock);
7011 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007012 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013 return 0;
7014 }
7015 if (v == (void*)2) {
7016 status_unused(seq);
7017 return 0;
7018 }
7019
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007020 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007022
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7024 seq_printf(seq, "%s : %sactive", mdname(mddev),
7025 mddev->pers ? "" : "in");
7026 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007027 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007029 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007030 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031 seq_printf(seq, " %s", mddev->pers->name);
7032 }
7033
Andre Nolldd8ac332009-03-31 14:33:13 +11007034 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007035 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 char b[BDEVNAME_SIZE];
7037 seq_printf(seq, " %s[%d]",
7038 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007039 if (test_bit(WriteMostly, &rdev->flags))
7040 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007041 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 seq_printf(seq, "(F)");
7043 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007044 }
7045 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007046 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007047 if (test_bit(Replacement, &rdev->flags))
7048 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007049 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050 }
7051
7052 if (!list_empty(&mddev->disks)) {
7053 if (mddev->pers)
7054 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007055 (unsigned long long)
7056 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 else
7058 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007059 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007061 if (mddev->persistent) {
7062 if (mddev->major_version != 0 ||
7063 mddev->minor_version != 90) {
7064 seq_printf(seq," super %d.%d",
7065 mddev->major_version,
7066 mddev->minor_version);
7067 }
NeilBrowne6910632008-02-06 01:39:51 -08007068 } else if (mddev->external)
7069 seq_printf(seq, " super external:%s",
7070 mddev->metadata_type);
7071 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007072 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073
7074 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007075 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007077 if (mddev->pers->sync_request) {
7078 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007079 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007080 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007081 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007082 seq_printf(seq, "\tresync=DELAYED\n ");
7083 else if (mddev->recovery_cp < MaxSector)
7084 seq_printf(seq, "\tresync=PENDING\n ");
7085 }
NeilBrown32a76272005-06-21 17:17:14 -07007086 } else
7087 seq_printf(seq, "\n ");
7088
NeilBrown57148962012-03-19 12:46:40 +11007089 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090
7091 seq_printf(seq, "\n");
7092 }
7093 mddev_unlock(mddev);
7094
7095 return 0;
7096}
7097
Jan Engelhardt110518b2009-05-07 12:49:37 +10007098static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099 .start = md_seq_start,
7100 .next = md_seq_next,
7101 .stop = md_seq_stop,
7102 .show = md_seq_show,
7103};
7104
7105static int md_seq_open(struct inode *inode, struct file *file)
7106{
Kay Sieversf1514632011-07-12 20:48:39 +02007107 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 int error;
7109
7110 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007111 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007112 return error;
7113
7114 seq = file->private_data;
7115 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 return error;
7117}
7118
NeilBrownd7603b72006-01-06 00:20:30 -08007119static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7120{
Kay Sieversf1514632011-07-12 20:48:39 +02007121 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007122 int mask;
7123
7124 poll_wait(filp, &md_event_waiters, wait);
7125
7126 /* always allow read */
7127 mask = POLLIN | POLLRDNORM;
7128
Kay Sieversf1514632011-07-12 20:48:39 +02007129 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007130 mask |= POLLERR | POLLPRI;
7131 return mask;
7132}
7133
Arjan van de Venfa027c22007-02-12 00:55:33 -08007134static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007135 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 .open = md_seq_open,
7137 .read = seq_read,
7138 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007139 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007140 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141};
7142
NeilBrown84fc4b52011-10-11 16:49:58 +11007143int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007146 list_add_tail(&p->list, &pers_list);
7147 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 spin_unlock(&pers_lock);
7149 return 0;
7150}
7151
NeilBrown84fc4b52011-10-11 16:49:58 +11007152int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153{
NeilBrown2604b702006-01-06 00:20:36 -08007154 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007156 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 spin_unlock(&pers_lock);
7158 return 0;
7159}
7160
NeilBrownfd01b882011-10-11 16:47:53 +11007161static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162{
NeilBrown3cb03002011-10-11 16:45:26 +11007163 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007165 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
7167 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007168 rcu_read_lock();
7169 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007171 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7172 (int)part_stat_read(&disk->part0, sectors[1]) -
7173 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007174 /* sync IO will cause sync_io to increase before the disk_stats
7175 * as sync_io is counted when a request starts, and
7176 * disk_stats is counted when it completes.
7177 * So resync activity will cause curr_events to be smaller than
7178 * when there was no such activity.
7179 * non-sync IO will cause disk_stat to increase without
7180 * increasing sync_io so curr_events will (eventually)
7181 * be larger than it was before. Once it becomes
7182 * substantially larger, the test below will cause
7183 * the array to appear non-idle, and resync will slow
7184 * down.
7185 * If there is a lot of outstanding resync activity when
7186 * we set last_event to curr_events, then all that activity
7187 * completing might cause the array to appear non-idle
7188 * and resync will be slowed down even though there might
7189 * not have been non-resync activity. This will only
7190 * happen once though. 'last_events' will soon reflect
7191 * the state where there is little or no outstanding
7192 * resync requests, and further resync activity will
7193 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007194 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007196 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 rdev->last_events = curr_events;
7198 idle = 0;
7199 }
7200 }
NeilBrown4b809912008-07-21 17:05:25 +10007201 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 return idle;
7203}
7204
NeilBrownfd01b882011-10-11 16:47:53 +11007205void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206{
7207 /* another "blocks" (512byte) blocks have been synced */
7208 atomic_sub(blocks, &mddev->recovery_active);
7209 wake_up(&mddev->recovery_wait);
7210 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007211 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007212 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 md_wakeup_thread(mddev->thread);
7214 // stop recovery, signal do_sync ....
7215 }
7216}
7217
7218
NeilBrown06d91a52005-06-21 17:17:12 -07007219/* md_write_start(mddev, bi)
7220 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007221 * in superblock) before writing, schedule a superblock update
7222 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007223 */
NeilBrownfd01b882011-10-11 16:47:53 +11007224void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225{
Neil Brown0fd62b82008-06-28 08:31:36 +10007226 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007227 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007228 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007229
NeilBrownf91de922005-11-08 21:39:36 -08007230 BUG_ON(mddev->ro == 1);
7231 if (mddev->ro == 2) {
7232 /* need to switch to read/write */
7233 mddev->ro = 0;
7234 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7235 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007236 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007237 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007238 }
NeilBrown06d91a52005-06-21 17:17:12 -07007239 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007240 if (mddev->safemode == 1)
7241 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007242 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007243 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007244 if (mddev->in_sync) {
7245 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007246 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007247 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007248 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007249 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007250 }
NeilBrowna9701a32005-11-08 21:39:34 -08007251 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007252 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007253 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007254 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007255 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007256 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257}
7258
NeilBrownfd01b882011-10-11 16:47:53 +11007259void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260{
7261 if (atomic_dec_and_test(&mddev->writes_pending)) {
7262 if (mddev->safemode == 2)
7263 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007264 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7266 }
7267}
7268
NeilBrown2a2275d2007-01-26 00:57:11 -08007269/* md_allow_write(mddev)
7270 * Calling this ensures that the array is marked 'active' so that writes
7271 * may proceed without blocking. It is important to call this before
7272 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7273 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007274 *
7275 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7276 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007277 */
NeilBrownfd01b882011-10-11 16:47:53 +11007278int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007279{
7280 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007281 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007282 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007283 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007284 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007285 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007286
7287 spin_lock_irq(&mddev->write_lock);
7288 if (mddev->in_sync) {
7289 mddev->in_sync = 0;
7290 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007291 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007292 if (mddev->safemode_delay &&
7293 mddev->safemode == 0)
7294 mddev->safemode = 1;
7295 spin_unlock_irq(&mddev->write_lock);
7296 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007297 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007298 } else
7299 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007300
NeilBrown070dc6d2010-08-30 17:33:34 +10007301 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007302 return -EAGAIN;
7303 else
7304 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007305}
7306EXPORT_SYMBOL_GPL(md_allow_write);
7307
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308#define SYNC_MARKS 10
7309#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007310#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007311void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312{
Shaohua Li4ed87312012-10-11 13:34:00 +11007313 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007314 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315 unsigned int currspeed = 0,
7316 window;
NeilBrown57afd892005-06-21 17:17:13 -07007317 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007319 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 sector_t mark_cnt[SYNC_MARKS];
7321 int last_mark,m;
7322 struct list_head *tmp;
7323 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007324 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007325 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007326 char *desc;
majianpeng7c2c57c2012-07-03 12:12:26 +10007327 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328
7329 /* just incase thread restarts... */
7330 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7331 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007332 if (mddev->ro) /* never try to sync a read-only array */
7333 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334
NeilBrown61df9d92006-10-03 01:15:57 -07007335 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7336 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7337 desc = "data-check";
7338 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7339 desc = "requested-resync";
7340 else
7341 desc = "resync";
7342 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7343 desc = "reshape";
7344 else
7345 desc = "recovery";
7346
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 /* we overload curr_resync somewhat here.
7348 * 0 == not engaged in resync at all
7349 * 2 == checking that there is no conflict with another sync
7350 * 1 == like 2, but have yielded to allow conflicting resync to
7351 * commense
7352 * other == active in resync - this many blocks
7353 *
7354 * Before starting a resync we must have set curr_resync to
7355 * 2, and then checked that every "conflicting" array has curr_resync
7356 * less than ours. When we find one that is the same or higher
7357 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7358 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7359 * This will mean we have to start checking from the beginning again.
7360 *
7361 */
7362
7363 do {
7364 mddev->curr_resync = 2;
7365
7366 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007367 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007368 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007369
7370 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007372 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 if (mddev2 == mddev)
7374 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007375 if (!mddev->parallel_resync
7376 && mddev2->curr_resync
7377 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 DEFINE_WAIT(wq);
7379 if (mddev < mddev2 && mddev->curr_resync == 2) {
7380 /* arbitrarily yield */
7381 mddev->curr_resync = 1;
7382 wake_up(&resync_wait);
7383 }
7384 if (mddev > mddev2 && mddev->curr_resync == 1)
7385 /* no need to wait here, we can wait the next
7386 * time 'round when curr_resync == 2
7387 */
7388 continue;
NeilBrown97441972008-09-19 11:49:54 +10007389 /* We need to wait 'interruptible' so as not to
7390 * contribute to the load average, and not to
7391 * be caught by 'softlockup'
7392 */
7393 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007394 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007395 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007396 printk(KERN_INFO "md: delaying %s of %s"
7397 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007399 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007401 if (signal_pending(current))
7402 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 schedule();
7404 finish_wait(&resync_wait, &wq);
7405 goto try_again;
7406 }
7407 finish_wait(&resync_wait, &wq);
7408 }
7409 }
7410 } while (mddev->curr_resync < 2);
7411
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007412 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007413 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007415 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416 */
7417 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007418 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007419 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007420 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7421 j = mddev->resync_min;
7422 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007423 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007424
NeilBrownccfcc3c2006-03-27 01:18:09 -08007425 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007426 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007427 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007429 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007430 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007431 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007432 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007433 if (rdev->raid_disk >= 0 &&
7434 !test_bit(Faulty, &rdev->flags) &&
7435 !test_bit(In_sync, &rdev->flags) &&
7436 rdev->recovery_offset < j)
7437 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007438 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440
NeilBrown61df9d92006-10-03 01:15:57 -07007441 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7442 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7443 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007444 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007445 "(but not more than %d KB/sec) for %s.\n",
7446 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447
NeilBrowneea1bf32009-03-31 14:27:02 +11007448 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007449
NeilBrown57afd892005-06-21 17:17:13 -07007450 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 for (m = 0; m < SYNC_MARKS; m++) {
7452 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007453 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 }
7455 last_mark = 0;
7456 mddev->resync_mark = mark[last_mark];
7457 mddev->resync_mark_cnt = mark_cnt[last_mark];
7458
7459 /*
7460 * Tune reconstruction:
7461 */
7462 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007463 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7464 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465
7466 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 last_check = 0;
7468
7469 if (j>2) {
7470 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007471 "md: resuming %s of %s from checkpoint.\n",
7472 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007474 } else
7475 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007476 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007477 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7478 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007479 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480
majianpeng7c2c57c2012-07-03 12:12:26 +10007481 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007483 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484
NeilBrown57afd892005-06-21 17:17:13 -07007485 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007486
NeilBrown7a91ee12009-05-26 12:57:21 +10007487 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7488 ((mddev->curr_resync > mddev->curr_resync_completed &&
7489 (mddev->curr_resync - mddev->curr_resync_completed)
7490 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007491 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007492 (j - mddev->curr_resync_completed)*2
7493 >= mddev->resync_max - mddev->curr_resync_completed
7494 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007495 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007496 wait_event(mddev->recovery_wait,
7497 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007498 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007499 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7500 j > mddev->recovery_cp)
7501 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007502 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007503 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007504 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007505 }
NeilBrownacb180b2009-04-14 16:28:34 +10007506
NeilBrowne62e58a2009-07-01 13:15:35 +10007507 while (j >= mddev->resync_max && !kthread_should_stop()) {
7508 /* As this condition is controlled by user-space,
7509 * we can block indefinitely, so use '_interruptible'
7510 * to avoid triggering warnings.
7511 */
7512 flush_signals(current); /* just in case */
7513 wait_event_interruptible(mddev->recovery_wait,
7514 mddev->resync_max > j
7515 || kthread_should_stop());
7516 }
NeilBrownacb180b2009-04-14 16:28:34 +10007517
7518 if (kthread_should_stop())
7519 goto interrupted;
7520
NeilBrown57afd892005-06-21 17:17:13 -07007521 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007522 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007523 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007524 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 goto out;
7526 }
NeilBrown57afd892005-06-21 17:17:13 -07007527
7528 if (!skipped) { /* actual IO requested */
7529 io_sectors += sectors;
7530 atomic_add(sectors, &mddev->recovery_active);
7531 }
7532
NeilBrowne875ece2011-07-28 11:39:24 +10007533 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7534 break;
7535
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007537 if (j > 2)
7538 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007539 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007540 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007541 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007542 * visible in /proc/mdstat
7543 */
7544 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007545
7546 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 continue;
7548
NeilBrown57afd892005-06-21 17:17:13 -07007549 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 repeat:
7551 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7552 /* step marks */
7553 int next = (last_mark+1) % SYNC_MARKS;
7554
7555 mddev->resync_mark = mark[next];
7556 mddev->resync_mark_cnt = mark_cnt[next];
7557 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007558 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 last_mark = next;
7560 }
7561
7562
NeilBrownc6207272008-02-06 01:39:52 -08007563 if (kthread_should_stop())
7564 goto interrupted;
7565
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566
7567 /*
7568 * this loop exits only if either when we are slower than
7569 * the 'hard' speed limit, or the system was IO-idle for
7570 * a jiffy.
7571 * the system might be non-idle CPU-wise, but we only care
7572 * about not overloading the IO subsystem. (things like an
7573 * e2fsck being done on the RAID array should execute fast)
7574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 cond_resched();
7576
NeilBrown57afd892005-06-21 17:17:13 -07007577 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7578 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579
NeilBrown88202a02006-01-06 00:21:36 -08007580 if (currspeed > speed_min(mddev)) {
7581 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007582 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007583 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584 goto repeat;
7585 }
7586 }
7587 }
NeilBrown61df9d92006-10-03 01:15:57 -07007588 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 /*
7590 * this also signals 'finished resyncing' to md_stop
7591 */
7592 out:
majianpeng7c2c57c2012-07-03 12:12:26 +10007593 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7595
7596 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007597 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598
NeilBrowndfc70642008-05-23 13:04:39 -07007599 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007600 mddev->curr_resync > 2) {
7601 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7602 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7603 if (mddev->curr_resync >= mddev->recovery_cp) {
7604 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007605 "md: checkpointing %s of %s.\n",
7606 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007607 if (test_bit(MD_RECOVERY_ERROR,
7608 &mddev->recovery))
7609 mddev->recovery_cp =
7610 mddev->curr_resync_completed;
7611 else
7612 mddev->recovery_cp =
7613 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007614 }
7615 } else
7616 mddev->recovery_cp = MaxSector;
7617 } else {
7618 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7619 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007620 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007621 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007622 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007623 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007624 !test_bit(Faulty, &rdev->flags) &&
7625 !test_bit(In_sync, &rdev->flags) &&
7626 rdev->recovery_offset < mddev->curr_resync)
7627 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007628 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007631 skip:
NeilBrown17571282006-12-10 02:20:52 -08007632 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633
NeilBrownc07b70a2009-12-14 12:49:48 +11007634 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7635 /* We completed so min/max setting can be forgotten if used. */
7636 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7637 mddev->resync_min = 0;
7638 mddev->resync_max = MaxSector;
7639 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7640 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641 mddev->curr_resync = 0;
7642 wake_up(&resync_wait);
7643 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7644 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007645 return;
7646
7647 interrupted:
7648 /*
7649 * got a signal, exit.
7650 */
7651 printk(KERN_INFO
7652 "md: md_do_sync() got signal ... exiting\n");
7653 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7654 goto out;
7655
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656}
NeilBrown29269552006-03-27 01:18:10 -08007657EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658
NeilBrown746d3202013-04-24 11:42:41 +10007659static int remove_and_add_spares(struct mddev *mddev,
7660 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007661{
NeilBrown3cb03002011-10-11 16:45:26 +11007662 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007663 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007664 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007665
NeilBrowndafb20f2012-03-19 12:46:39 +11007666 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007667 if ((this == NULL || rdev == this) &&
7668 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007669 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007670 (test_bit(Faulty, &rdev->flags) ||
7671 ! test_bit(In_sync, &rdev->flags)) &&
7672 atomic_read(&rdev->nr_pending)==0) {
7673 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007674 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007675 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007676 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007677 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007678 }
7679 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007680 if (removed && mddev->kobj.sd)
7681 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007682
NeilBrown746d3202013-04-24 11:42:41 +10007683 if (this)
7684 goto no_add;
7685
NeilBrowndafb20f2012-03-19 12:46:39 +11007686 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007687 if (rdev->raid_disk >= 0 &&
7688 !test_bit(In_sync, &rdev->flags) &&
7689 !test_bit(Faulty, &rdev->flags))
7690 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007691 if (rdev->raid_disk >= 0)
7692 continue;
7693 if (test_bit(Faulty, &rdev->flags))
7694 continue;
7695 if (mddev->ro &&
7696 rdev->saved_raid_disk < 0)
7697 continue;
7698
7699 rdev->recovery_offset = 0;
7700 if (rdev->saved_raid_disk >= 0 && mddev->in_sync) {
7701 spin_lock_irq(&mddev->write_lock);
7702 if (mddev->in_sync)
7703 /* OK, this device, which is in_sync,
7704 * will definitely be noticed before
7705 * the next write, so recovery isn't
7706 * needed.
7707 */
7708 rdev->recovery_offset = mddev->recovery_cp;
7709 spin_unlock_irq(&mddev->write_lock);
7710 }
7711 if (mddev->ro && rdev->recovery_offset != MaxSector)
7712 /* not safe to add this disk now */
7713 continue;
7714 if (mddev->pers->
7715 hot_add_disk(mddev, rdev) == 0) {
7716 if (sysfs_link_rdev(mddev, rdev))
7717 /* failure here is OK */;
7718 spares++;
7719 md_new_event(mddev);
7720 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007721 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007722 }
NeilBrown746d3202013-04-24 11:42:41 +10007723no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007724 if (removed)
7725 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007726 return spares;
7727}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007728
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729/*
7730 * This routine is regularly called by all per-raid-array threads to
7731 * deal with generic issues like resync and super-block update.
7732 * Raid personalities that don't have a thread (linear/raid0) do not
7733 * need this as they never do any recovery or update the superblock.
7734 *
7735 * It does not do any resync itself, but rather "forks" off other threads
7736 * to do that as needed.
7737 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7738 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007739 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740 * and wakeups up this thread which will reap the thread and finish up.
7741 * This thread also removes any faulty devices (with nr_pending == 0).
7742 *
7743 * The overall approach is:
7744 * 1/ if the superblock needs updating, update it.
7745 * 2/ If a recovery thread is running, don't do anything else.
7746 * 3/ If recovery has finished, clean up, possibly marking spares active.
7747 * 4/ If there are any faulty devices, remove them.
7748 * 5/ If array is degraded, try to add spares devices
7749 * 6/ If array has spares or is not in-sync, start a resync thread.
7750 */
NeilBrownfd01b882011-10-11 16:47:53 +11007751void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007752{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007753 if (mddev->suspended)
7754 return;
7755
NeilBrown5f404022005-06-21 17:17:16 -07007756 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007757 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007758
NeilBrownfca4d842005-06-21 17:17:11 -07007759 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007760 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007761 printk(KERN_INFO "md: %s in immediate safe mode\n",
7762 mdname(mddev));
7763 mddev->safemode = 2;
7764 }
7765 flush_signals(current);
7766 }
7767
NeilBrownc89a8ee2008-08-05 15:54:13 +10007768 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7769 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007771 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007773 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007774 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007775 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7776 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777 ))
7778 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007779
NeilBrowndf5b89b2006-03-27 01:18:20 -08007780 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007781 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007782
NeilBrownc89a8ee2008-08-05 15:54:13 +10007783 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007784 /* On a read-only array we can:
7785 * - remove failed devices
7786 * - add already-in_sync devices if the array itself
7787 * is in-sync.
7788 * As we only add devices that are already in-sync,
7789 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007790 */
NeilBrownc89a8ee2008-08-05 15:54:13 +10007791 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown7ceb17e2013-04-24 11:42:42 +10007792 remove_and_add_spares(mddev, NULL);
7793 mddev->pers->spare_active(mddev);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007794 goto unlock;
7795 }
7796
NeilBrown31a59e32008-04-30 00:52:30 -07007797 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007798 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007799 spin_lock_irq(&mddev->write_lock);
7800 if (mddev->safemode &&
7801 !atomic_read(&mddev->writes_pending) &&
7802 !mddev->in_sync &&
7803 mddev->recovery_cp == MaxSector) {
7804 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007805 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007806 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007807 }
7808 if (mddev->safemode == 1)
7809 mddev->safemode = 0;
7810 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007811 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007812 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007813 }
NeilBrownfca4d842005-06-21 17:17:11 -07007814
NeilBrown850b2b42006-10-03 01:15:46 -07007815 if (mddev->flags)
7816 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007817
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7819 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7820 /* resync/recovery still happening */
7821 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7822 goto unlock;
7823 }
7824 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007825 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826 goto unlock;
7827 }
Neil Brown72a23c22008-06-28 08:31:41 +10007828 /* Set RUNNING before clearing NEEDED to avoid
7829 * any transients in the value of "sync_action".
7830 */
NeilBrown72f36d52012-10-11 14:25:57 +11007831 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007832 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007833 /* Clear some bits that don't mean anything, but
7834 * might be left set
7835 */
NeilBrown24dd4692005-11-08 21:39:26 -08007836 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7837 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838
NeilBrowned209582012-04-24 10:23:14 +10007839 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7840 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007841 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842 /* no recovery is running.
7843 * remove any failed drives, then
7844 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007845 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846 * the personality to fail the re-add.
7847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007848
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007849 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007850 if (mddev->pers->check_reshape == NULL ||
7851 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007852 /* Cannot proceed */
7853 goto unlock;
7854 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007855 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007856 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007857 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7858 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007859 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007860 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007861 } else if (mddev->recovery_cp < MaxSector) {
7862 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007863 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007864 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7865 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007867
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007869 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007870 /* We are adding a device or devices to an array
7871 * which has the bitmap stored on all devices.
7872 * So make sure all bitmap pages get written
7873 */
7874 bitmap_write_all(mddev->bitmap);
7875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 mddev->sync_thread = md_register_thread(md_do_sync,
7877 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007878 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 if (!mddev->sync_thread) {
7880 printk(KERN_ERR "%s: could not start resync"
7881 " thread...\n",
7882 mdname(mddev));
7883 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007884 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7885 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7886 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7887 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7888 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007889 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007891 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007892 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893 }
7894 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007895 if (!mddev->sync_thread) {
7896 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7897 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7898 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007899 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007900 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 mddev_unlock(mddev);
7903 }
7904}
7905
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007906void md_reap_sync_thread(struct mddev *mddev)
7907{
7908 struct md_rdev *rdev;
7909
7910 /* resync has finished, collect result */
7911 md_unregister_thread(&mddev->sync_thread);
7912 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7913 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7914 /* success...*/
7915 /* activate any spares */
7916 if (mddev->pers->spare_active(mddev)) {
7917 sysfs_notify(&mddev->kobj, NULL,
7918 "degraded");
7919 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7920 }
7921 }
7922 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7923 mddev->pers->finish_reshape)
7924 mddev->pers->finish_reshape(mddev);
7925
7926 /* If array is no-longer degraded, then any saved_raid_disk
7927 * information must be scrapped. Also if any device is now
7928 * In_sync we must scrape the saved_raid_disk for that device
7929 * do the superblock for an incrementally recovered device
7930 * written out.
7931 */
7932 rdev_for_each(rdev, mddev)
7933 if (!mddev->degraded ||
7934 test_bit(In_sync, &rdev->flags))
7935 rdev->saved_raid_disk = -1;
7936
7937 md_update_sb(mddev, 1);
7938 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7939 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7940 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7941 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7942 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7943 /* flag recovery needed just to double check */
7944 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7945 sysfs_notify_dirent_safe(mddev->sysfs_action);
7946 md_new_event(mddev);
7947 if (mddev->event_work.func)
7948 queue_work(md_misc_wq, &mddev->event_work);
7949}
7950
NeilBrownfd01b882011-10-11 16:47:53 +11007951void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007952{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007953 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007954 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007955 !test_bit(Blocked, &rdev->flags) &&
7956 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007957 msecs_to_jiffies(5000));
7958 rdev_dec_pending(rdev, mddev);
7959}
7960EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7961
NeilBrownc6563a82012-05-21 09:27:00 +10007962void md_finish_reshape(struct mddev *mddev)
7963{
7964 /* called be personality module when reshape completes. */
7965 struct md_rdev *rdev;
7966
7967 rdev_for_each(rdev, mddev) {
7968 if (rdev->data_offset > rdev->new_data_offset)
7969 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7970 else
7971 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7972 rdev->data_offset = rdev->new_data_offset;
7973 }
7974}
7975EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007976
7977/* Bad block management.
7978 * We can record which blocks on each device are 'bad' and so just
7979 * fail those blocks, or that stripe, rather than the whole device.
7980 * Entries in the bad-block table are 64bits wide. This comprises:
7981 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7982 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7983 * A 'shift' can be set so that larger blocks are tracked and
7984 * consequently larger devices can be covered.
7985 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7986 *
7987 * Locking of the bad-block table uses a seqlock so md_is_badblock
7988 * might need to retry if it is very unlucky.
7989 * We will sometimes want to check for bad blocks in a bi_end_io function,
7990 * so we use the write_seqlock_irq variant.
7991 *
7992 * When looking for a bad block we specify a range and want to
7993 * know if any block in the range is bad. So we binary-search
7994 * to the last range that starts at-or-before the given endpoint,
7995 * (or "before the sector after the target range")
7996 * then see if it ends after the given start.
7997 * We return
7998 * 0 if there are no known bad blocks in the range
7999 * 1 if there are known bad block which are all acknowledged
8000 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8001 * plus the start/length of the first bad section we overlap.
8002 */
8003int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8004 sector_t *first_bad, int *bad_sectors)
8005{
8006 int hi;
majianpengab056132012-11-06 17:13:44 +08008007 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008008 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008009 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008010 sector_t target = s + sectors;
8011 unsigned seq;
8012
8013 if (bb->shift > 0) {
8014 /* round the start down, and the end up */
8015 s >>= bb->shift;
8016 target += (1<<bb->shift) - 1;
8017 target >>= bb->shift;
8018 sectors = target - s;
8019 }
8020 /* 'target' is now the first block after the bad range */
8021
8022retry:
8023 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008024 lo = 0;
8025 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008026 hi = bb->count;
8027
8028 /* Binary search between lo and hi for 'target'
8029 * i.e. for the last range that starts before 'target'
8030 */
8031 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8032 * are known not to be the last range before target.
8033 * VARIANT: hi-lo is the number of possible
8034 * ranges, and decreases until it reaches 1
8035 */
8036 while (hi - lo > 1) {
8037 int mid = (lo + hi) / 2;
8038 sector_t a = BB_OFFSET(p[mid]);
8039 if (a < target)
8040 /* This could still be the one, earlier ranges
8041 * could not. */
8042 lo = mid;
8043 else
8044 /* This and later ranges are definitely out. */
8045 hi = mid;
8046 }
8047 /* 'lo' might be the last that started before target, but 'hi' isn't */
8048 if (hi > lo) {
8049 /* need to check all range that end after 's' to see if
8050 * any are unacknowledged.
8051 */
8052 while (lo >= 0 &&
8053 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8054 if (BB_OFFSET(p[lo]) < target) {
8055 /* starts before the end, and finishes after
8056 * the start, so they must overlap
8057 */
8058 if (rv != -1 && BB_ACK(p[lo]))
8059 rv = 1;
8060 else
8061 rv = -1;
8062 *first_bad = BB_OFFSET(p[lo]);
8063 *bad_sectors = BB_LEN(p[lo]);
8064 }
8065 lo--;
8066 }
8067 }
8068
8069 if (read_seqretry(&bb->lock, seq))
8070 goto retry;
8071
8072 return rv;
8073}
8074EXPORT_SYMBOL_GPL(md_is_badblock);
8075
8076/*
8077 * Add a range of bad blocks to the table.
8078 * This might extend the table, or might contract it
8079 * if two adjacent ranges can be merged.
8080 * We binary-search to find the 'insertion' point, then
8081 * decide how best to handle it.
8082 */
8083static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8084 int acknowledged)
8085{
8086 u64 *p;
8087 int lo, hi;
8088 int rv = 1;
8089
8090 if (bb->shift < 0)
8091 /* badblocks are disabled */
8092 return 0;
8093
8094 if (bb->shift) {
8095 /* round the start down, and the end up */
8096 sector_t next = s + sectors;
8097 s >>= bb->shift;
8098 next += (1<<bb->shift) - 1;
8099 next >>= bb->shift;
8100 sectors = next - s;
8101 }
8102
8103 write_seqlock_irq(&bb->lock);
8104
8105 p = bb->page;
8106 lo = 0;
8107 hi = bb->count;
8108 /* Find the last range that starts at-or-before 's' */
8109 while (hi - lo > 1) {
8110 int mid = (lo + hi) / 2;
8111 sector_t a = BB_OFFSET(p[mid]);
8112 if (a <= s)
8113 lo = mid;
8114 else
8115 hi = mid;
8116 }
8117 if (hi > lo && BB_OFFSET(p[lo]) > s)
8118 hi = lo;
8119
8120 if (hi > lo) {
8121 /* we found a range that might merge with the start
8122 * of our new range
8123 */
8124 sector_t a = BB_OFFSET(p[lo]);
8125 sector_t e = a + BB_LEN(p[lo]);
8126 int ack = BB_ACK(p[lo]);
8127 if (e >= s) {
8128 /* Yes, we can merge with a previous range */
8129 if (s == a && s + sectors >= e)
8130 /* new range covers old */
8131 ack = acknowledged;
8132 else
8133 ack = ack && acknowledged;
8134
8135 if (e < s + sectors)
8136 e = s + sectors;
8137 if (e - a <= BB_MAX_LEN) {
8138 p[lo] = BB_MAKE(a, e-a, ack);
8139 s = e;
8140 } else {
8141 /* does not all fit in one range,
8142 * make p[lo] maximal
8143 */
8144 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8145 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8146 s = a + BB_MAX_LEN;
8147 }
8148 sectors = e - s;
8149 }
8150 }
8151 if (sectors && hi < bb->count) {
8152 /* 'hi' points to the first range that starts after 's'.
8153 * Maybe we can merge with the start of that range */
8154 sector_t a = BB_OFFSET(p[hi]);
8155 sector_t e = a + BB_LEN(p[hi]);
8156 int ack = BB_ACK(p[hi]);
8157 if (a <= s + sectors) {
8158 /* merging is possible */
8159 if (e <= s + sectors) {
8160 /* full overlap */
8161 e = s + sectors;
8162 ack = acknowledged;
8163 } else
8164 ack = ack && acknowledged;
8165
8166 a = s;
8167 if (e - a <= BB_MAX_LEN) {
8168 p[hi] = BB_MAKE(a, e-a, ack);
8169 s = e;
8170 } else {
8171 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8172 s = a + BB_MAX_LEN;
8173 }
8174 sectors = e - s;
8175 lo = hi;
8176 hi++;
8177 }
8178 }
8179 if (sectors == 0 && hi < bb->count) {
8180 /* we might be able to combine lo and hi */
8181 /* Note: 's' is at the end of 'lo' */
8182 sector_t a = BB_OFFSET(p[hi]);
8183 int lolen = BB_LEN(p[lo]);
8184 int hilen = BB_LEN(p[hi]);
8185 int newlen = lolen + hilen - (s - a);
8186 if (s >= a && newlen < BB_MAX_LEN) {
8187 /* yes, we can combine them */
8188 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8189 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8190 memmove(p + hi, p + hi + 1,
8191 (bb->count - hi - 1) * 8);
8192 bb->count--;
8193 }
8194 }
8195 while (sectors) {
8196 /* didn't merge (it all).
8197 * Need to add a range just before 'hi' */
8198 if (bb->count >= MD_MAX_BADBLOCKS) {
8199 /* No room for more */
8200 rv = 0;
8201 break;
8202 } else {
8203 int this_sectors = sectors;
8204 memmove(p + hi + 1, p + hi,
8205 (bb->count - hi) * 8);
8206 bb->count++;
8207
8208 if (this_sectors > BB_MAX_LEN)
8209 this_sectors = BB_MAX_LEN;
8210 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8211 sectors -= this_sectors;
8212 s += this_sectors;
8213 }
8214 }
8215
8216 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008217 if (!acknowledged)
8218 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10008219 write_sequnlock_irq(&bb->lock);
8220
8221 return rv;
8222}
8223
NeilBrown3cb03002011-10-11 16:45:26 +11008224int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008225 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008226{
NeilBrownc6563a82012-05-21 09:27:00 +10008227 int rv;
8228 if (is_new)
8229 s += rdev->new_data_offset;
8230 else
8231 s += rdev->data_offset;
8232 rv = md_set_badblocks(&rdev->badblocks,
8233 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008234 if (rv) {
8235 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008236 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008237 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8238 md_wakeup_thread(rdev->mddev->thread);
8239 }
8240 return rv;
8241}
8242EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8243
8244/*
8245 * Remove a range of bad blocks from the table.
8246 * This may involve extending the table if we spilt a region,
8247 * but it must not fail. So if the table becomes full, we just
8248 * drop the remove request.
8249 */
8250static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8251{
8252 u64 *p;
8253 int lo, hi;
8254 sector_t target = s + sectors;
8255 int rv = 0;
8256
8257 if (bb->shift > 0) {
8258 /* When clearing we round the start up and the end down.
8259 * This should not matter as the shift should align with
8260 * the block size and no rounding should ever be needed.
8261 * However it is better the think a block is bad when it
8262 * isn't than to think a block is not bad when it is.
8263 */
8264 s += (1<<bb->shift) - 1;
8265 s >>= bb->shift;
8266 target >>= bb->shift;
8267 sectors = target - s;
8268 }
8269
8270 write_seqlock_irq(&bb->lock);
8271
8272 p = bb->page;
8273 lo = 0;
8274 hi = bb->count;
8275 /* Find the last range that starts before 'target' */
8276 while (hi - lo > 1) {
8277 int mid = (lo + hi) / 2;
8278 sector_t a = BB_OFFSET(p[mid]);
8279 if (a < target)
8280 lo = mid;
8281 else
8282 hi = mid;
8283 }
8284 if (hi > lo) {
8285 /* p[lo] is the last range that could overlap the
8286 * current range. Earlier ranges could also overlap,
8287 * but only this one can overlap the end of the range.
8288 */
8289 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8290 /* Partial overlap, leave the tail of this range */
8291 int ack = BB_ACK(p[lo]);
8292 sector_t a = BB_OFFSET(p[lo]);
8293 sector_t end = a + BB_LEN(p[lo]);
8294
8295 if (a < s) {
8296 /* we need to split this range */
8297 if (bb->count >= MD_MAX_BADBLOCKS) {
8298 rv = 0;
8299 goto out;
8300 }
8301 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8302 bb->count++;
8303 p[lo] = BB_MAKE(a, s-a, ack);
8304 lo++;
8305 }
8306 p[lo] = BB_MAKE(target, end - target, ack);
8307 /* there is no longer an overlap */
8308 hi = lo;
8309 lo--;
8310 }
8311 while (lo >= 0 &&
8312 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8313 /* This range does overlap */
8314 if (BB_OFFSET(p[lo]) < s) {
8315 /* Keep the early parts of this range. */
8316 int ack = BB_ACK(p[lo]);
8317 sector_t start = BB_OFFSET(p[lo]);
8318 p[lo] = BB_MAKE(start, s - start, ack);
8319 /* now low doesn't overlap, so.. */
8320 break;
8321 }
8322 lo--;
8323 }
8324 /* 'lo' is strictly before, 'hi' is strictly after,
8325 * anything between needs to be discarded
8326 */
8327 if (hi - lo > 1) {
8328 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8329 bb->count -= (hi - lo - 1);
8330 }
8331 }
8332
8333 bb->changed = 1;
8334out:
8335 write_sequnlock_irq(&bb->lock);
8336 return rv;
8337}
8338
NeilBrownc6563a82012-05-21 09:27:00 +10008339int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8340 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008341{
NeilBrownc6563a82012-05-21 09:27:00 +10008342 if (is_new)
8343 s += rdev->new_data_offset;
8344 else
8345 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008346 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008347 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008348}
8349EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8350
8351/*
8352 * Acknowledge all bad blocks in a list.
8353 * This only succeeds if ->changed is clear. It is used by
8354 * in-kernel metadata updates
8355 */
8356void md_ack_all_badblocks(struct badblocks *bb)
8357{
8358 if (bb->page == NULL || bb->changed)
8359 /* no point even trying */
8360 return;
8361 write_seqlock_irq(&bb->lock);
8362
majianpengecb178b2012-03-19 12:46:42 +11008363 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008364 u64 *p = bb->page;
8365 int i;
8366 for (i = 0; i < bb->count ; i++) {
8367 if (!BB_ACK(p[i])) {
8368 sector_t start = BB_OFFSET(p[i]);
8369 int len = BB_LEN(p[i]);
8370 p[i] = BB_MAKE(start, len, 1);
8371 }
8372 }
NeilBrownde393cd2011-07-28 11:31:48 +10008373 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008374 }
8375 write_sequnlock_irq(&bb->lock);
8376}
8377EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8378
NeilBrown16c791a2011-07-28 11:31:47 +10008379/* sysfs access to bad-blocks list.
8380 * We present two files.
8381 * 'bad-blocks' lists sector numbers and lengths of ranges that
8382 * are recorded as bad. The list is truncated to fit within
8383 * the one-page limit of sysfs.
8384 * Writing "sector length" to this file adds an acknowledged
8385 * bad block list.
8386 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8387 * been acknowledged. Writing to this file adds bad blocks
8388 * without acknowledging them. This is largely for testing.
8389 */
8390
8391static ssize_t
8392badblocks_show(struct badblocks *bb, char *page, int unack)
8393{
8394 size_t len;
8395 int i;
8396 u64 *p = bb->page;
8397 unsigned seq;
8398
8399 if (bb->shift < 0)
8400 return 0;
8401
8402retry:
8403 seq = read_seqbegin(&bb->lock);
8404
8405 len = 0;
8406 i = 0;
8407
8408 while (len < PAGE_SIZE && i < bb->count) {
8409 sector_t s = BB_OFFSET(p[i]);
8410 unsigned int length = BB_LEN(p[i]);
8411 int ack = BB_ACK(p[i]);
8412 i++;
8413
8414 if (unack && ack)
8415 continue;
8416
8417 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8418 (unsigned long long)s << bb->shift,
8419 length << bb->shift);
8420 }
NeilBrownde393cd2011-07-28 11:31:48 +10008421 if (unack && len == 0)
8422 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008423
8424 if (read_seqretry(&bb->lock, seq))
8425 goto retry;
8426
8427 return len;
8428}
8429
8430#define DO_DEBUG 1
8431
8432static ssize_t
8433badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8434{
8435 unsigned long long sector;
8436 int length;
8437 char newline;
8438#ifdef DO_DEBUG
8439 /* Allow clearing via sysfs *only* for testing/debugging.
8440 * Normally only a successful write may clear a badblock
8441 */
8442 int clear = 0;
8443 if (page[0] == '-') {
8444 clear = 1;
8445 page++;
8446 }
8447#endif /* DO_DEBUG */
8448
8449 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8450 case 3:
8451 if (newline != '\n')
8452 return -EINVAL;
8453 case 2:
8454 if (length <= 0)
8455 return -EINVAL;
8456 break;
8457 default:
8458 return -EINVAL;
8459 }
8460
8461#ifdef DO_DEBUG
8462 if (clear) {
8463 md_clear_badblocks(bb, sector, length);
8464 return len;
8465 }
8466#endif /* DO_DEBUG */
8467 if (md_set_badblocks(bb, sector, length, !unack))
8468 return len;
8469 else
8470 return -ENOSPC;
8471}
8472
Adrian Bunk75c96f82005-05-05 16:16:09 -07008473static int md_notify_reboot(struct notifier_block *this,
8474 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475{
8476 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008477 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008478 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008479
NeilBrownc744a652012-03-19 12:46:37 +11008480 for_each_mddev(mddev, tmp) {
8481 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008482 if (mddev->pers)
8483 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008484 mddev->safemode = 2;
8485 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008486 }
NeilBrownc744a652012-03-19 12:46:37 +11008487 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008488 }
NeilBrownc744a652012-03-19 12:46:37 +11008489 /*
8490 * certain more exotic SCSI devices are known to be
8491 * volatile wrt too early system reboots. While the
8492 * right place to handle this issue is the given
8493 * driver, we do want to have a safe RAID driver ...
8494 */
8495 if (need_delay)
8496 mdelay(1000*1);
8497
Linus Torvalds1da177e2005-04-16 15:20:36 -07008498 return NOTIFY_DONE;
8499}
8500
Adrian Bunk75c96f82005-05-05 16:16:09 -07008501static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008502 .notifier_call = md_notify_reboot,
8503 .next = NULL,
8504 .priority = INT_MAX, /* before any real devices */
8505};
8506
8507static void md_geninit(void)
8508{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008509 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008511 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512}
8513
Adrian Bunk75c96f82005-05-05 16:16:09 -07008514static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515{
Tejun Heoe804ac72010-10-15 15:36:08 +02008516 int ret = -ENOMEM;
8517
Tejun Heoada609e2011-01-25 14:35:54 +01008518 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008519 if (!md_wq)
8520 goto err_wq;
8521
8522 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8523 if (!md_misc_wq)
8524 goto err_misc_wq;
8525
8526 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8527 goto err_md;
8528
8529 if ((ret = register_blkdev(0, "mdp")) < 0)
8530 goto err_mdp;
8531 mdp_major = ret;
8532
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008533 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008534 md_probe, NULL, NULL);
8535 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008536 md_probe, NULL, NULL);
8537
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008539 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540
8541 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543
Tejun Heoe804ac72010-10-15 15:36:08 +02008544err_mdp:
8545 unregister_blkdev(MD_MAJOR, "md");
8546err_md:
8547 destroy_workqueue(md_misc_wq);
8548err_misc_wq:
8549 destroy_workqueue(md_wq);
8550err_wq:
8551 return ret;
8552}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008553
8554#ifndef MODULE
8555
8556/*
8557 * Searches all registered partitions for autorun RAID arrays
8558 * at boot time.
8559 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008560
8561static LIST_HEAD(all_detected_devices);
8562struct detected_devices_node {
8563 struct list_head list;
8564 dev_t dev;
8565};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566
8567void md_autodetect_dev(dev_t dev)
8568{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008569 struct detected_devices_node *node_detected_dev;
8570
8571 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8572 if (node_detected_dev) {
8573 node_detected_dev->dev = dev;
8574 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8575 } else {
8576 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8577 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008579}
8580
8581
8582static void autostart_arrays(int part)
8583{
NeilBrown3cb03002011-10-11 16:45:26 +11008584 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008585 struct detected_devices_node *node_detected_dev;
8586 dev_t dev;
8587 int i_scanned, i_passed;
8588
8589 i_scanned = 0;
8590 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008591
8592 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8593
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008594 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8595 i_scanned++;
8596 node_detected_dev = list_entry(all_detected_devices.next,
8597 struct detected_devices_node, list);
8598 list_del(&node_detected_dev->list);
8599 dev = node_detected_dev->dev;
8600 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008601 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602 if (IS_ERR(rdev))
8603 continue;
8604
NeilBrownb2d444d2005-11-08 21:39:31 -08008605 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008606 MD_BUG();
8607 continue;
8608 }
NeilBrownd0fae182008-03-04 14:29:31 -08008609 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008610 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008611 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008613
8614 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8615 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008616
8617 autorun_devices(part);
8618}
8619
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008620#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621
8622static __exit void md_exit(void)
8623{
NeilBrownfd01b882011-10-11 16:47:53 +11008624 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008626
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008627 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008628 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008629
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008630 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008631 unregister_blkdev(mdp_major, "mdp");
8632 unregister_reboot_notifier(&md_notifier);
8633 unregister_sysctl_table(raid_table_header);
8634 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008635 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008637 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008638 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008639 destroy_workqueue(md_misc_wq);
8640 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641}
8642
Dan Williams685784a2007-07-09 11:56:42 -07008643subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644module_exit(md_exit)
8645
NeilBrownf91de922005-11-08 21:39:36 -08008646static int get_ro(char *buffer, struct kernel_param *kp)
8647{
8648 return sprintf(buffer, "%d", start_readonly);
8649}
8650static int set_ro(const char *val, struct kernel_param *kp)
8651{
8652 char *e;
8653 int num = simple_strtoul(val, &e, 10);
8654 if (*val && (*e == '\0' || *e == '\n')) {
8655 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008656 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008657 }
8658 return -EINVAL;
8659}
8660
NeilBrown80ca3a42006-07-10 04:44:18 -07008661module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8662module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008663
NeilBrownefeb53c2009-01-09 08:31:10 +11008664module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008665
Linus Torvalds1da177e2005-04-16 15:20:36 -07008666EXPORT_SYMBOL(register_md_personality);
8667EXPORT_SYMBOL(unregister_md_personality);
8668EXPORT_SYMBOL(md_error);
8669EXPORT_SYMBOL(md_done_sync);
8670EXPORT_SYMBOL(md_write_start);
8671EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008672EXPORT_SYMBOL(md_register_thread);
8673EXPORT_SYMBOL(md_unregister_thread);
8674EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675EXPORT_SYMBOL(md_check_recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008676EXPORT_SYMBOL(md_reap_sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008678MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008679MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008680MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);