blob: 5c95ccb595007cab1aba9859cc4da62cad80536f [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>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020039#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080041#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070042#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080043#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110044#include <linux/hdreg.h>
45#include <linux/proc_fs.h>
46#include <linux/random.h>
47#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
57#define DEBUG 0
58#define dprintk(x...) ((void)(DEBUG && printk(x)))
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110061static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
NeilBrown01f96c02011-09-21 15:30:20 +100064/* pers_list is a list of registered personalities protected
65 * by pers_lock.
66 * pers_lock does extra service to protect accesses to
67 * mddev->thread when the mutex cannot be held.
68 */
NeilBrown2604b702006-01-06 00:20:36 -080069static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static DEFINE_SPINLOCK(pers_lock);
71
Adrian Bunk5e563412006-06-26 00:27:42 -070072static void md_print_devices(void);
73
Bernd Schubert90b08712008-05-23 13:04:38 -070074static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020075static struct workqueue_struct *md_wq;
76static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070077
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;
NeilBrown88202a02006-01-06 00:21:36 -0800101static inline int speed_min(mddev_t *mddev)
102{
103 return mddev->sync_speed_min ?
104 mddev->sync_speed_min : sysctl_speed_limit_min;
105}
106
107static inline int speed_max(mddev_t *mddev)
108{
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
161static void mddev_bio_destructor(struct bio *bio)
162{
163 mddev_t *mddev, **mddevp;
164
165 mddevp = (void*)bio;
166 mddev = mddevp[-1];
167
168 bio_free(bio, mddev->bio_set);
169}
170
171struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
172 mddev_t *mddev)
173{
174 struct bio *b;
175 mddev_t **mddevp;
176
177 if (!mddev || !mddev->bio_set)
178 return bio_alloc(gfp_mask, nr_iovecs);
179
180 b = bio_alloc_bioset(gfp_mask, nr_iovecs,
181 mddev->bio_set);
182 if (!b)
183 return NULL;
184 mddevp = (void*)b;
185 mddevp[-1] = mddev;
186 b->bi_destructor = mddev_bio_destructor;
187 return b;
188}
189EXPORT_SYMBOL_GPL(bio_alloc_mddev);
190
191struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
192 mddev_t *mddev)
193{
194 struct bio *b;
195 mddev_t **mddevp;
196
197 if (!mddev || !mddev->bio_set)
198 return bio_clone(bio, gfp_mask);
199
200 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
201 mddev->bio_set);
202 if (!b)
203 return NULL;
204 mddevp = (void*)b;
205 mddevp[-1] = mddev;
206 b->bi_destructor = mddev_bio_destructor;
207 __bio_clone(b, bio);
208 if (bio_integrity(bio)) {
209 int ret;
210
211 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
212
213 if (ret < 0) {
214 bio_put(b);
215 return NULL;
216 }
217 }
218
219 return b;
220}
221EXPORT_SYMBOL_GPL(bio_clone_mddev);
222
NeilBrownd2eb35a2011-07-28 11:31:48 +1000223void md_trim_bio(struct bio *bio, int offset, int size)
224{
225 /* 'bio' is a cloned bio which we need to trim to match
226 * the given offset and size.
227 * This requires adjusting bi_sector, bi_size, and bi_io_vec
228 */
229 int i;
230 struct bio_vec *bvec;
231 int sofar = 0;
232
233 size <<= 9;
234 if (offset == 0 && size == bio->bi_size)
235 return;
236
237 bio->bi_sector += offset;
238 bio->bi_size = size;
239 offset <<= 9;
240 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
241
242 while (bio->bi_idx < bio->bi_vcnt &&
243 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
244 /* remove this whole bio_vec */
245 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
246 bio->bi_idx++;
247 }
248 if (bio->bi_idx < bio->bi_vcnt) {
249 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
250 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
251 }
252 /* avoid any complications with bi_idx being non-zero*/
253 if (bio->bi_idx) {
254 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
255 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
256 bio->bi_vcnt -= bio->bi_idx;
257 bio->bi_idx = 0;
258 }
259 /* Make sure vcnt and last bv are not too big */
260 bio_for_each_segment(bvec, bio, i) {
261 if (sofar + bvec->bv_len > size)
262 bvec->bv_len = size - sofar;
263 if (bvec->bv_len == 0) {
264 bio->bi_vcnt = i;
265 break;
266 }
267 sofar += bvec->bv_len;
268 }
269}
270EXPORT_SYMBOL_GPL(md_trim_bio);
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/*
NeilBrownd7603b72006-01-06 00:20:30 -0800273 * We have a system wide 'event count' that is incremented
274 * on any 'interesting' event, and readers of /proc/mdstat
275 * can use 'poll' or 'select' to find out when the event
276 * count increases.
277 *
278 * Events are:
279 * start array, stop array, error, add device, remove device,
280 * start build, activate spare
281 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800282static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800283static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800284void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800285{
286 atomic_inc(&md_event_count);
287 wake_up(&md_event_waiters);
288}
NeilBrown29269552006-03-27 01:18:10 -0800289EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800290
NeilBrownc331eb02006-05-30 21:27:13 -0700291/* Alternate version that can be called from interrupts
292 * when calling sysfs_notify isn't needed.
293 */
Adrian Bunk05381952006-06-26 00:28:01 -0700294static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700295{
296 atomic_inc(&md_event_count);
297 wake_up(&md_event_waiters);
298}
299
NeilBrownd7603b72006-01-06 00:20:30 -0800300/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * Enables to iterate over all existing md arrays
302 * all_mddevs_lock protects this list.
303 */
304static LIST_HEAD(all_mddevs);
305static DEFINE_SPINLOCK(all_mddevs_lock);
306
307
308/*
309 * iterates through all used mddevs in the system.
310 * We take care to grab the all_mddevs_lock whenever navigating
311 * the list, and to always hold a refcount when unlocked.
312 * Any code which breaks out of this loop while own
313 * a reference to the current mddev and must mddev_put it.
314 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800315#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 \
317 for (({ spin_lock(&all_mddevs_lock); \
318 tmp = all_mddevs.next; \
319 mddev = NULL;}); \
320 ({ if (tmp != &all_mddevs) \
321 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
322 spin_unlock(&all_mddevs_lock); \
323 if (mddev) mddev_put(mddev); \
324 mddev = list_entry(tmp, mddev_t, all_mddevs); \
325 tmp != &all_mddevs;}); \
326 ({ spin_lock(&all_mddevs_lock); \
327 tmp = tmp->next;}) \
328 )
329
330
NeilBrown409c57f2009-03-31 14:39:39 +1100331/* Rather than calling directly into the personality make_request function,
332 * IO requests come here first so that we can check if the device is
333 * being suspended pending a reconfiguration.
334 * We hold a refcount over the call to ->make_request. By the time that
335 * call has finished, the bio has been linked into some internal structure
336 * and so is visible to ->quiesce(), so we don't need the refcount any more.
337 */
338static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
NeilBrown49077322010-03-25 16:20:56 +1100340 const int rw = bio_data_dir(bio);
NeilBrown409c57f2009-03-31 14:39:39 +1100341 mddev_t *mddev = q->queuedata;
342 int rv;
NeilBrown49077322010-03-25 16:20:56 +1100343 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500344 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100345
NeilBrown0ca69882011-01-14 09:14:33 +1100346 if (mddev == NULL || mddev->pers == NULL
347 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100348 bio_io_error(bio);
349 return 0;
350 }
NeilBrown0ca69882011-01-14 09:14:33 +1100351 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100352 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200353 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100354 DEFINE_WAIT(__wait);
355 for (;;) {
356 prepare_to_wait(&mddev->sb_wait, &__wait,
357 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200358 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100359 break;
360 rcu_read_unlock();
361 schedule();
362 rcu_read_lock();
363 }
364 finish_wait(&mddev->sb_wait, &__wait);
365 }
366 atomic_inc(&mddev->active_io);
367 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100368
Chris Masone91ece52011-02-07 19:21:48 -0500369 /*
370 * save the sectors now since our bio can
371 * go away inside make_request
372 */
373 sectors = bio_sectors(bio);
NeilBrown21a52c62010-04-01 15:02:13 +1100374 rv = mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100375
376 cpu = part_stat_lock();
377 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500378 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100379 part_stat_unlock();
380
NeilBrown409c57f2009-03-31 14:39:39 +1100381 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
382 wake_up(&mddev->sb_wait);
383
384 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
NeilBrown9e35b992010-04-06 14:23:02 +1000387/* mddev_suspend makes sure no new requests are submitted
388 * to the device, and that any requests that have been submitted
389 * are completely handled.
390 * Once ->stop is called and completes, the module will be completely
391 * unused.
392 */
NeilBrown390ee602010-06-01 19:37:27 +1000393void mddev_suspend(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100394{
395 BUG_ON(mddev->suspended);
396 mddev->suspended = 1;
397 synchronize_rcu();
398 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
399 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100400}
NeilBrown390ee602010-06-01 19:37:27 +1000401EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100402
NeilBrown390ee602010-06-01 19:37:27 +1000403void mddev_resume(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100404{
405 mddev->suspended = 0;
406 wake_up(&mddev->sb_wait);
407 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500408
409 md_wakeup_thread(mddev->thread);
410 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100411}
NeilBrown390ee602010-06-01 19:37:27 +1000412EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100413
NeilBrown3fa841d2009-09-23 18:10:29 +1000414int mddev_congested(mddev_t *mddev, int bits)
415{
416 return mddev->suspended;
417}
418EXPORT_SYMBOL(mddev_congested);
419
NeilBrowna2826aa2009-12-14 12:49:49 +1100420/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200421 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100422 */
423
Tejun Heoe9c74692010-09-03 11:56:18 +0200424static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100425{
426 mdk_rdev_t *rdev = bio->bi_private;
427 mddev_t *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100428
429 rdev_dec_pending(rdev, mddev);
430
431 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200432 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200433 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100434 }
435 bio_put(bio);
436}
437
NeilBrowna7a07e62010-12-09 16:04:25 +1100438static void md_submit_flush_data(struct work_struct *ws);
439
NeilBrowna035fc32010-12-09 16:17:51 +1100440static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100441{
NeilBrowna035fc32010-12-09 16:17:51 +1100442 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100443 mdk_rdev_t *rdev;
444
NeilBrowna7a07e62010-12-09 16:04:25 +1100445 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
446 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100447 rcu_read_lock();
448 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
449 if (rdev->raid_disk >= 0 &&
450 !test_bit(Faulty, &rdev->flags)) {
451 /* Take two references, one is dropped
452 * when request finishes, one after
453 * we reclaim rcu_read_lock
454 */
455 struct bio *bi;
456 atomic_inc(&rdev->nr_pending);
457 atomic_inc(&rdev->nr_pending);
458 rcu_read_unlock();
NeilBrowna167f662010-10-26 18:31:13 +1100459 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200460 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100461 bi->bi_private = rdev;
462 bi->bi_bdev = rdev->bdev;
463 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200464 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100465 rcu_read_lock();
466 rdev_dec_pending(rdev, mddev);
467 }
468 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100469 if (atomic_dec_and_test(&mddev->flush_pending))
470 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100471}
472
Tejun Heoe9c74692010-09-03 11:56:18 +0200473static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100474{
Tejun Heoe9c74692010-09-03 11:56:18 +0200475 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
476 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100477
Tejun Heoe9c74692010-09-03 11:56:18 +0200478 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100479 /* an empty barrier - all done */
480 bio_endio(bio, 0);
481 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200482 bio->bi_rw &= ~REQ_FLUSH;
NeilBrown21a52c62010-04-01 15:02:13 +1100483 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100484 generic_make_request(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100485 }
NeilBrown2b74e122010-12-09 15:59:01 +1100486
487 mddev->flush_bio = NULL;
488 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100489}
490
Tejun Heoe9c74692010-09-03 11:56:18 +0200491void md_flush_request(mddev_t *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100492{
493 spin_lock_irq(&mddev->write_lock);
494 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200495 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100496 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200497 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100498 spin_unlock_irq(&mddev->write_lock);
499
NeilBrowna035fc32010-12-09 16:17:51 +1100500 INIT_WORK(&mddev->flush_work, submit_flushes);
501 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100502}
Tejun Heoe9c74692010-09-03 11:56:18 +0200503EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100504
NeilBrown97658cd2011-04-18 18:25:42 +1000505/* Support for plugging.
506 * This mirrors the plugging support in request_queue, but does not
507 * require having a whole queue or request structures.
508 * We allocate an md_plug_cb for each md device and each thread it gets
509 * plugged on. This links tot the private plug_handle structure in the
510 * personality data where we keep a count of the number of outstanding
511 * plugs so other code can see if a plug is active.
512 */
513struct md_plug_cb {
514 struct blk_plug_cb cb;
515 mddev_t *mddev;
516};
517
518static void plugger_unplug(struct blk_plug_cb *cb)
519{
520 struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb);
521 if (atomic_dec_and_test(&mdcb->mddev->plug_cnt))
522 md_wakeup_thread(mdcb->mddev->thread);
523 kfree(mdcb);
524}
525
526/* Check that an unplug wakeup will come shortly.
527 * If not, wakeup the md thread immediately
528 */
529int mddev_check_plugged(mddev_t *mddev)
530{
531 struct blk_plug *plug = current->plug;
532 struct md_plug_cb *mdcb;
533
534 if (!plug)
535 return 0;
536
537 list_for_each_entry(mdcb, &plug->cb_list, cb.list) {
538 if (mdcb->cb.callback == plugger_unplug &&
539 mdcb->mddev == mddev) {
540 /* Already on the list, move to top */
541 if (mdcb != list_first_entry(&plug->cb_list,
542 struct md_plug_cb,
543 cb.list))
544 list_move(&mdcb->cb.list, &plug->cb_list);
545 return 1;
546 }
547 }
548 /* Not currently on the callback list */
549 mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC);
550 if (!mdcb)
551 return 0;
552
553 mdcb->mddev = mddev;
554 mdcb->cb.callback = plugger_unplug;
555 atomic_inc(&mddev->plug_cnt);
556 list_add(&mdcb->cb.list, &plug->cb_list);
557 return 1;
558}
559EXPORT_SYMBOL_GPL(mddev_check_plugged);
NeilBrown2ac87402010-06-01 19:37:29 +1000560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561static inline mddev_t *mddev_get(mddev_t *mddev)
562{
563 atomic_inc(&mddev->active);
564 return mddev;
565}
566
Dan Williams5fd3a172009-03-04 00:57:25 -0700567static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569static void mddev_put(mddev_t *mddev)
570{
NeilBrowna167f662010-10-26 18:31:13 +1100571 struct bio_set *bs = NULL;
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
574 return;
NeilBrownd3374822009-01-09 08:31:10 +1100575 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100576 mddev->ctime == 0 && !mddev->hold_active) {
577 /* Array is not configured at all, and not held active,
578 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 list_del(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100580 bs = mddev->bio_set;
581 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100582 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200583 /* We did a probe so need to clean up. Call
584 * queue_work inside the spinlock so that
585 * flush_workqueue() after mddev_find will
586 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100587 */
588 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200589 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100590 } else
591 kfree(mddev);
592 }
593 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100594 if (bs)
595 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
NeilBrown390ee602010-06-01 19:37:27 +1000598void mddev_init(mddev_t *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100599{
600 mutex_init(&mddev->open_mutex);
601 mutex_init(&mddev->reconfig_mutex);
602 mutex_init(&mddev->bitmap_info.mutex);
603 INIT_LIST_HEAD(&mddev->disks);
604 INIT_LIST_HEAD(&mddev->all_mddevs);
605 init_timer(&mddev->safemode_timer);
606 atomic_set(&mddev->active, 1);
607 atomic_set(&mddev->openers, 0);
608 atomic_set(&mddev->active_io, 0);
NeilBrown97658cd2011-04-18 18:25:42 +1000609 atomic_set(&mddev->plug_cnt, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100610 spin_lock_init(&mddev->write_lock);
611 atomic_set(&mddev->flush_pending, 0);
612 init_waitqueue_head(&mddev->sb_wait);
613 init_waitqueue_head(&mddev->recovery_wait);
614 mddev->reshape_position = MaxSector;
615 mddev->resync_min = 0;
616 mddev->resync_max = MaxSector;
617 mddev->level = LEVEL_NONE;
618}
NeilBrown390ee602010-06-01 19:37:27 +1000619EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621static mddev_t * mddev_find(dev_t unit)
622{
623 mddev_t *mddev, *new = NULL;
624
NeilBrown8f5f02c2011-02-16 13:58:51 +1100625 if (unit && MAJOR(unit) != MD_MAJOR)
626 unit &= ~((1<<MdpMinorShift)-1);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 retry:
629 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
NeilBrownefeb53c2009-01-09 08:31:10 +1100631 if (unit) {
632 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
633 if (mddev->unit == unit) {
634 mddev_get(mddev);
635 spin_unlock(&all_mddevs_lock);
636 kfree(new);
637 return mddev;
638 }
639
640 if (new) {
641 list_add(&new->all_mddevs, &all_mddevs);
642 spin_unlock(&all_mddevs_lock);
643 new->hold_active = UNTIL_IOCTL;
644 return new;
645 }
646 } else if (new) {
647 /* find an unused unit number */
648 static int next_minor = 512;
649 int start = next_minor;
650 int is_free = 0;
651 int dev = 0;
652 while (!is_free) {
653 dev = MKDEV(MD_MAJOR, next_minor);
654 next_minor++;
655 if (next_minor > MINORMASK)
656 next_minor = 0;
657 if (next_minor == start) {
658 /* Oh dear, all in use. */
659 spin_unlock(&all_mddevs_lock);
660 kfree(new);
661 return NULL;
662 }
663
664 is_free = 1;
665 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
666 if (mddev->unit == dev) {
667 is_free = 0;
668 break;
669 }
670 }
671 new->unit = dev;
672 new->md_minor = MINOR(dev);
673 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 list_add(&new->all_mddevs, &all_mddevs);
675 spin_unlock(&all_mddevs_lock);
676 return new;
677 }
678 spin_unlock(&all_mddevs_lock);
679
NeilBrown9ffae0c2006-01-06 00:20:32 -0800680 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (!new)
682 return NULL;
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 new->unit = unit;
685 if (MAJOR(unit) == MD_MAJOR)
686 new->md_minor = MINOR(unit);
687 else
688 new->md_minor = MINOR(unit) >> MdpMinorShift;
689
NeilBrownfafd7fb2010-04-01 15:55:30 +1100690 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 goto retry;
693}
694
695static inline int mddev_lock(mddev_t * mddev)
696{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800697 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Dan Williamsb522adc2009-03-31 15:00:31 +1100700static inline int mddev_is_locked(mddev_t *mddev)
701{
702 return mutex_is_locked(&mddev->reconfig_mutex);
703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705static inline int mddev_trylock(mddev_t * mddev)
706{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800707 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
NeilBrownb6eb1272010-04-15 10:13:47 +1000710static struct attribute_group md_redundancy_group;
711
NeilBrowna64c8762010-04-14 17:15:37 +1000712static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
NeilBrowna64c8762010-04-14 17:15:37 +1000714 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000715 /* These cannot be removed under reconfig_mutex as
716 * an access to the files will try to take reconfig_mutex
717 * while holding the file unremovable, which leads to
718 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000719 * So hold set sysfs_active while the remove in happeing,
720 * and anything else which might set ->to_remove or my
721 * otherwise change the sysfs namespace will fail with
722 * -EBUSY if sysfs_active is still set.
723 * We set sysfs_active under reconfig_mutex and elsewhere
724 * test it under the same mutex to ensure its correct value
725 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000726 */
NeilBrowna64c8762010-04-14 17:15:37 +1000727 struct attribute_group *to_remove = mddev->to_remove;
728 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000729 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000730 mutex_unlock(&mddev->reconfig_mutex);
731
NeilBrown00bcb4a2010-06-01 19:37:23 +1000732 if (mddev->kobj.sd) {
733 if (to_remove != &md_redundancy_group)
734 sysfs_remove_group(&mddev->kobj, to_remove);
735 if (mddev->pers == NULL ||
736 mddev->pers->sync_request == NULL) {
737 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
738 if (mddev->sysfs_action)
739 sysfs_put(mddev->sysfs_action);
740 mddev->sysfs_action = NULL;
741 }
NeilBrowna64c8762010-04-14 17:15:37 +1000742 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000743 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000744 } else
745 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
NeilBrown01f96c02011-09-21 15:30:20 +1000747 /* was we've dropped the mutex we need a spinlock to
748 * make sur the thread doesn't disappear
749 */
750 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700751 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000752 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
NeilBrown2989ddb2006-01-06 00:20:43 -0800755static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100757 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100759 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (rdev->desc_nr == nr)
761 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return NULL;
764}
765
766static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
767{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 mdk_rdev_t *rdev;
769
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100770 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (rdev->bdev->bd_dev == dev)
772 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return NULL;
775}
776
NeilBrownd9d166c2006-01-06 00:20:51 -0800777static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800778{
779 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800780 list_for_each_entry(pers, &pers_list, list) {
781 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800782 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800783 if (strcmp(pers->name, clevel)==0)
784 return pers;
785 }
NeilBrown2604b702006-01-06 00:20:36 -0800786 return NULL;
787}
788
Andre Nollb73df2d2008-07-11 22:02:23 +1000789/* return the offset of the super block in 512byte sectors */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100790static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100792 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000793 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796static int alloc_disk_sb(mdk_rdev_t * rdev)
797{
798 if (rdev->sb_page)
799 MD_BUG();
800
801 rdev->sb_page = alloc_page(GFP_KERNEL);
802 if (!rdev->sb_page) {
803 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000804 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
807 return 0;
808}
809
810static void free_disk_sb(mdk_rdev_t * rdev)
811{
812 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800813 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 rdev->sb_loaded = 0;
815 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000816 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100817 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
NeilBrown2699b672011-07-28 11:31:47 +1000819 if (rdev->bb_page) {
820 put_page(rdev->bb_page);
821 rdev->bb_page = NULL;
822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825
NeilBrown6712ecf2007-09-27 12:47:43 +0200826static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700827{
828 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800829 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700830
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700831 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
832 printk("md: super_written gets error=%d, uptodate=%d\n",
833 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
834 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800835 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700836 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700837
NeilBrowna9701a32005-11-08 21:39:34 -0800838 if (atomic_dec_and_test(&mddev->pending_writes))
839 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700840 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700841}
842
843void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
844 sector_t sector, int size, struct page *page)
845{
846 /* write first size bytes of page to sector of rdev
847 * Increment mddev->pending_writes before returning
848 * and decrement it on completion, waking up sb_wait
849 * if zero is reached.
850 * If an error occurred, call md_error
851 */
NeilBrowna167f662010-10-26 18:31:13 +1100852 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700853
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100854 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700855 bio->bi_sector = sector;
856 bio_add_page(bio, page, size, 0);
857 bio->bi_private = rdev;
858 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800859
NeilBrown7bfa19f2005-06-21 17:17:28 -0700860 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000861 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800862}
863
864void md_super_wait(mddev_t *mddev)
865{
Tejun Heoe9c74692010-09-03 11:56:18 +0200866 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800867 DEFINE_WAIT(wq);
868 for(;;) {
869 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
870 if (atomic_read(&mddev->pending_writes)==0)
871 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800872 schedule();
873 }
874 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700875}
876
NeilBrown6712ecf2007-09-27 12:47:43 +0200877static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
NeilBrown2b193362010-10-27 15:16:40 +1100882int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100883 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
NeilBrowna167f662010-10-26 18:31:13 +1100885 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 struct completion event;
887 int ret;
888
Jens Axboe721a9602011-03-09 11:56:30 +0100889 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100891 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
892 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100893 if (metadata_op)
894 bio->bi_sector = sector + rdev->sb_start;
895 else
896 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 bio_add_page(bio, page, size, 0);
898 init_completion(&event);
899 bio->bi_private = &event;
900 bio->bi_end_io = bi_complete;
901 submit_bio(rw, bio);
902 wait_for_completion(&event);
903
904 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
905 bio_put(bio);
906 return ret;
907}
NeilBrowna8745db2006-01-06 00:20:34 -0800908EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
NeilBrown0002b272005-09-09 16:23:53 -0700910static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 char b[BDEVNAME_SIZE];
913 if (!rdev->sb_page) {
914 MD_BUG();
915 return -EINVAL;
916 }
917 if (rdev->sb_loaded)
918 return 0;
919
920
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100921 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 goto fail;
923 rdev->sb_loaded = 1;
924 return 0;
925
926fail:
927 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
928 bdevname(rdev->bdev,b));
929 return -EINVAL;
930}
931
932static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
933{
Andre Noll05710462008-07-11 22:02:20 +1000934 return sb1->set_uuid0 == sb2->set_uuid0 &&
935 sb1->set_uuid1 == sb2->set_uuid1 &&
936 sb1->set_uuid2 == sb2->set_uuid2 &&
937 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
941{
942 int ret;
943 mdp_super_t *tmp1, *tmp2;
944
945 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
946 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
947
948 if (!tmp1 || !tmp2) {
949 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100950 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 goto abort;
952 }
953
954 *tmp1 = *sb1;
955 *tmp2 = *sb2;
956
957 /*
958 * nr_disks is not constant
959 */
960 tmp1->nr_disks = 0;
961 tmp2->nr_disks = 0;
962
Andre Nollce0c8e02008-07-11 22:02:20 +1000963 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700965 kfree(tmp1);
966 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return ret;
968}
969
NeilBrown4d167f02007-05-09 02:35:37 -0700970
971static u32 md_csum_fold(u32 csum)
972{
973 csum = (csum & 0xffff) + (csum >> 16);
974 return (csum & 0xffff) + (csum >> 16);
975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977static unsigned int calc_sb_csum(mdp_super_t * sb)
978{
NeilBrown4d167f02007-05-09 02:35:37 -0700979 u64 newcsum = 0;
980 u32 *sb32 = (u32*)sb;
981 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 unsigned int disk_csum, csum;
983
984 disk_csum = sb->sb_csum;
985 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700986
987 for (i = 0; i < MD_SB_BYTES/4 ; i++)
988 newcsum += sb32[i];
989 csum = (newcsum & 0xffffffff) + (newcsum>>32);
990
991
992#ifdef CONFIG_ALPHA
993 /* This used to use csum_partial, which was wrong for several
994 * reasons including that different results are returned on
995 * different architectures. It isn't critical that we get exactly
996 * the same return value as before (we always csum_fold before
997 * testing, and that removes any differences). However as we
998 * know that csum_partial always returned a 16bit value on
999 * alphas, do a fold to maximise conformity to previous behaviour.
1000 */
1001 sb->sb_csum = md_csum_fold(disk_csum);
1002#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -07001004#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return csum;
1006}
1007
1008
1009/*
1010 * Handle superblock details.
1011 * We want to be able to handle multiple superblock formats
1012 * so we have a common interface to them all, and an array of
1013 * different handlers.
1014 * We rely on user-space to write the initial superblock, and support
1015 * reading and updating of superblocks.
1016 * Interface methods are:
1017 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
1018 * loads and validates a superblock on dev.
1019 * if refdev != NULL, compare superblocks on both devices
1020 * Return:
1021 * 0 - dev has a superblock that is compatible with refdev
1022 * 1 - dev has a superblock that is compatible and newer than refdev
1023 * so dev should be used as the refdev in future
1024 * -EINVAL superblock incompatible or invalid
1025 * -othererror e.g. -EIO
1026 *
1027 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
1028 * Verify that dev is acceptable into mddev.
1029 * The first time, mddev->raid_disks will be 0, and data from
1030 * dev should be merged in. Subsequent calls check that dev
1031 * is new enough. Return 0 or -EINVAL
1032 *
1033 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
1034 * Update the superblock for rdev with data in mddev
1035 * This does not write to disc.
1036 *
1037 */
1038
1039struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001040 char *name;
1041 struct module *owner;
1042 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
1043 int minor_version);
1044 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
1045 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
1046 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001047 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048};
1049
1050/*
Andre Noll0894cc32009-06-18 08:49:23 +10001051 * Check that the given mddev has no bitmap.
1052 *
1053 * This function is called from the run method of all personalities that do not
1054 * support bitmaps. It prints an error message and returns non-zero if mddev
1055 * has a bitmap. Otherwise, it returns 0.
1056 *
1057 */
1058int md_check_no_bitmap(mddev_t *mddev)
1059{
NeilBrownc3d97142009-12-14 12:49:52 +11001060 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001061 return 0;
1062 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1063 mdname(mddev), mddev->pers->name);
1064 return 1;
1065}
1066EXPORT_SYMBOL(md_check_no_bitmap);
1067
1068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 * load_super for 0.90.0
1070 */
1071static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1072{
1073 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1074 mdp_super_t *sb;
1075 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /*
Andre Noll0f420352008-07-11 22:02:23 +10001078 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 * it's at the end of the disk.
1080 *
1081 * It also happens to be a multiple of 4Kb.
1082 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001083 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
NeilBrown0002b272005-09-09 16:23:53 -07001085 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (ret) return ret;
1087
1088 ret = -EINVAL;
1089
1090 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001091 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (sb->md_magic != MD_SB_MAGIC) {
1094 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1095 b);
1096 goto abort;
1097 }
1098
1099 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001100 sb->minor_version < 90 ||
1101 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1103 sb->major_version, sb->minor_version,
1104 b);
1105 goto abort;
1106 }
1107
1108 if (sb->raid_disks <= 0)
1109 goto abort;
1110
NeilBrown4d167f02007-05-09 02:35:37 -07001111 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1113 b);
1114 goto abort;
1115 }
1116
1117 rdev->preferred_minor = sb->md_minor;
1118 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001119 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001120 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 if (sb->level == LEVEL_MULTIPATH)
1123 rdev->desc_nr = -1;
1124 else
1125 rdev->desc_nr = sb->this_disk.number;
1126
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001127 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001129 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001131 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (!uuid_equal(refsb, sb)) {
1133 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1134 b, bdevname(refdev->bdev,b2));
1135 goto abort;
1136 }
1137 if (!sb_equal(refsb, sb)) {
1138 printk(KERN_WARNING "md: %s has same UUID"
1139 " but different superblock to %s\n",
1140 b, bdevname(refdev->bdev, b2));
1141 goto abort;
1142 }
1143 ev1 = md_event(sb);
1144 ev2 = md_event(refsb);
1145 if (ev1 > ev2)
1146 ret = 1;
1147 else
1148 ret = 0;
1149 }
NeilBrown8190e752009-06-18 08:48:58 +10001150 rdev->sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001151 /* Limit to 4TB as metadata cannot record more than that */
1152 if (rdev->sectors >= (2ULL << 32))
1153 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
NeilBrown27a7b262011-09-10 17:21:28 +10001155 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001156 /* "this cannot possibly happen" ... */
1157 ret = -EINVAL;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 abort:
1160 return ret;
1161}
1162
1163/*
1164 * validate_super for 0.90.0
1165 */
1166static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1167{
1168 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001169 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001170 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
NeilBrown41158c72005-06-21 17:17:25 -07001172 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001173 clear_bit(Faulty, &rdev->flags);
1174 clear_bit(In_sync, &rdev->flags);
1175 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (mddev->raid_disks == 0) {
1178 mddev->major_version = 0;
1179 mddev->minor_version = sb->minor_version;
1180 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001181 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001182 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 mddev->ctime = sb->ctime;
1184 mddev->utime = sb->utime;
1185 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001186 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 mddev->layout = sb->layout;
1188 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001189 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001190 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001191 mddev->bitmap_info.offset = 0;
1192 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
NeilBrownf6705572006-03-27 01:18:11 -08001194 if (mddev->minor_version >= 91) {
1195 mddev->reshape_position = sb->reshape_position;
1196 mddev->delta_disks = sb->delta_disks;
1197 mddev->new_level = sb->new_level;
1198 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001199 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001200 } else {
1201 mddev->reshape_position = MaxSector;
1202 mddev->delta_disks = 0;
1203 mddev->new_level = mddev->level;
1204 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001205 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001206 }
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (sb->state & (1<<MD_SB_CLEAN))
1209 mddev->recovery_cp = MaxSector;
1210 else {
1211 if (sb->events_hi == sb->cp_events_hi &&
1212 sb->events_lo == sb->cp_events_lo) {
1213 mddev->recovery_cp = sb->recovery_cp;
1214 } else
1215 mddev->recovery_cp = 0;
1216 }
1217
1218 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1219 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1220 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1221 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1222
1223 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001224
1225 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001226 mddev->bitmap_info.file == NULL)
1227 mddev->bitmap_info.offset =
1228 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001229
NeilBrown41158c72005-06-21 17:17:25 -07001230 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001231 /* Insist on good event counter while assembling, except
1232 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001234 if (sb->disks[rdev->desc_nr].state & (
1235 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1236 if (ev1 < mddev->events)
1237 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001238 } else if (mddev->bitmap) {
1239 /* if adding to array with a bitmap, then we can accept an
1240 * older device ... but not too old.
1241 */
NeilBrown41158c72005-06-21 17:17:25 -07001242 if (ev1 < mddev->bitmap->events_cleared)
1243 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001244 } else {
1245 if (ev1 < mddev->events)
1246 /* just a hot-add of a new device, leave raid_disk at -1 */
1247 return 0;
1248 }
NeilBrown41158c72005-06-21 17:17:25 -07001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 desc = sb->disks + rdev->desc_nr;
1252
1253 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001254 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001255 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1256 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001257 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001259 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1260 /* active but not in sync implies recovery up to
1261 * reshape position. We don't know exactly where
1262 * that is, so set to zero for now */
1263 if (mddev->minor_version >= 91) {
1264 rdev->recovery_offset = 0;
1265 rdev->raid_disk = desc->raid_disk;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001268 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1269 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001270 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001271 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return 0;
1273}
1274
1275/*
1276 * sync_super for 0.90.0
1277 */
1278static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1279{
1280 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 mdk_rdev_t *rdev2;
1282 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /* make rdev->sb match mddev data..
1286 *
1287 * 1/ zero out disks
1288 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1289 * 3/ any empty disks < next_spare become removed
1290 *
1291 * disks[0] gets initialised to REMOVED because
1292 * we cannot be sure from other fields if it has
1293 * been initialised or not.
1294 */
1295 int i;
1296 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1297
NeilBrown61181562005-09-09 16:24:02 -07001298 rdev->sb_size = MD_SB_BYTES;
1299
Namhyung Kim65a06f062011-07-27 11:00:36 +10001300 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 memset(sb, 0, sizeof(*sb));
1303
1304 sb->md_magic = MD_SB_MAGIC;
1305 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 sb->patch_version = mddev->patch_version;
1307 sb->gvalid_words = 0; /* ignored */
1308 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1309 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1310 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1311 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1312
1313 sb->ctime = mddev->ctime;
1314 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001315 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 sb->raid_disks = mddev->raid_disks;
1317 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001318 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 sb->utime = mddev->utime;
1320 sb->state = 0;
1321 sb->events_hi = (mddev->events>>32);
1322 sb->events_lo = (u32)mddev->events;
1323
NeilBrownf6705572006-03-27 01:18:11 -08001324 if (mddev->reshape_position == MaxSector)
1325 sb->minor_version = 90;
1326 else {
1327 sb->minor_version = 91;
1328 sb->reshape_position = mddev->reshape_position;
1329 sb->new_level = mddev->new_level;
1330 sb->delta_disks = mddev->delta_disks;
1331 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001332 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001333 }
1334 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (mddev->in_sync)
1336 {
1337 sb->recovery_cp = mddev->recovery_cp;
1338 sb->cp_events_hi = (mddev->events>>32);
1339 sb->cp_events_lo = (u32)mddev->events;
1340 if (mddev->recovery_cp == MaxSector)
1341 sb->state = (1<< MD_SB_CLEAN);
1342 } else
1343 sb->recovery_cp = 0;
1344
1345 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001346 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
NeilBrownc3d97142009-12-14 12:49:52 +11001348 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001349 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001352 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001354 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001355 int is_active = test_bit(In_sync, &rdev2->flags);
1356
1357 if (rdev2->raid_disk >= 0 &&
1358 sb->minor_version >= 91)
1359 /* we have nowhere to store the recovery_offset,
1360 * but if it is not below the reshape_position,
1361 * we can piggy-back on that.
1362 */
1363 is_active = 1;
1364 if (rdev2->raid_disk < 0 ||
1365 test_bit(Faulty, &rdev2->flags))
1366 is_active = 0;
1367 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001368 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001370 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001371 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 d = &sb->disks[rdev2->desc_nr];
1373 nr_disks++;
1374 d->number = rdev2->desc_nr;
1375 d->major = MAJOR(rdev2->bdev->bd_dev);
1376 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001377 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 d->raid_disk = rdev2->raid_disk;
1379 else
1380 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001381 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001383 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001385 if (test_bit(In_sync, &rdev2->flags))
1386 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 active++;
1388 working++;
1389 } else {
1390 d->state = 0;
1391 spare++;
1392 working++;
1393 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001394 if (test_bit(WriteMostly, &rdev2->flags))
1395 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /* now set the "removed" and "faulty" bits on any missing devices */
1398 for (i=0 ; i < mddev->raid_disks ; i++) {
1399 mdp_disk_t *d = &sb->disks[i];
1400 if (d->state == 0 && d->number == 0) {
1401 d->number = i;
1402 d->raid_disk = i;
1403 d->state = (1<<MD_DISK_REMOVED);
1404 d->state |= (1<<MD_DISK_FAULTY);
1405 failed++;
1406 }
1407 }
1408 sb->nr_disks = nr_disks;
1409 sb->active_disks = active;
1410 sb->working_disks = working;
1411 sb->failed_disks = failed;
1412 sb->spare_disks = spare;
1413
1414 sb->this_disk = sb->disks[rdev->desc_nr];
1415 sb->sb_csum = calc_sb_csum(sb);
1416}
1417
1418/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001419 * rdev_size_change for 0.90.0
1420 */
1421static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001422super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001423{
Andre Noll58c0fed2009-03-31 14:33:13 +11001424 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001425 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001426 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001427 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001428 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001429 if (!num_sectors || num_sectors > rdev->sb_start)
1430 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001431 /* Limit to 4TB as metadata cannot record more than that.
1432 * 4TB == 2^32 KB, or 2*2^32 sectors.
1433 */
1434 if (num_sectors >= (2ULL << 32))
1435 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001436 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001437 rdev->sb_page);
1438 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001439 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001440}
1441
1442
1443/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 * version 1 superblock
1445 */
1446
NeilBrown1c05b4b2006-10-21 10:24:08 -07001447static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001449 __le32 disk_csum;
1450 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 unsigned long long newcsum;
1452 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001453 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 int i;
1455
1456 disk_csum = sb->sb_csum;
1457 sb->sb_csum = 0;
1458 newcsum = 0;
1459 for (i=0; size>=4; size -= 4 )
1460 newcsum += le32_to_cpu(*isuper++);
1461
1462 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001463 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1466 sb->sb_csum = disk_csum;
1467 return cpu_to_le32(csum);
1468}
1469
NeilBrown2699b672011-07-28 11:31:47 +10001470static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1471 int acknowledged);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1473{
1474 struct mdp_superblock_1 *sb;
1475 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001476 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001478 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 /*
Andre Noll0f420352008-07-11 22:02:23 +10001481 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 * It is always aligned to a 4K boundary and
1483 * depeding on minor_version, it can be:
1484 * 0: At least 8K, but less than 12K, from end of device
1485 * 1: At start of device
1486 * 2: 4K from start of device.
1487 */
1488 switch(minor_version) {
1489 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001490 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001491 sb_start -= 8*2;
1492 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
1494 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001495 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 break;
1497 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001498 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 break;
1500 default:
1501 return -EINVAL;
1502 }
Andre Noll0f420352008-07-11 22:02:23 +10001503 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
NeilBrown0002b272005-09-09 16:23:53 -07001505 /* superblock is rarely larger than 1K, but it can be larger,
1506 * and it is safe to read 4k, so we do that
1507 */
1508 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (ret) return ret;
1510
1511
Namhyung Kim65a06f062011-07-27 11:00:36 +10001512 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1515 sb->major_version != cpu_to_le32(1) ||
1516 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001517 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001518 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return -EINVAL;
1520
1521 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1522 printk("md: invalid superblock checksum on %s\n",
1523 bdevname(rdev->bdev,b));
1524 return -EINVAL;
1525 }
1526 if (le64_to_cpu(sb->data_size) < 10) {
1527 printk("md: data_size too small on %s\n",
1528 bdevname(rdev->bdev,b));
1529 return -EINVAL;
1530 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 rdev->preferred_minor = 0xffff;
1533 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001534 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
NeilBrown0002b272005-09-09 16:23:53 -07001536 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001537 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001538 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001539 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1540
1541 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001542 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001543 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001544
NeilBrown31b65a02006-07-10 04:44:14 -07001545 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1546 rdev->desc_nr = -1;
1547 else
1548 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1549
NeilBrown2699b672011-07-28 11:31:47 +10001550 if (!rdev->bb_page) {
1551 rdev->bb_page = alloc_page(GFP_KERNEL);
1552 if (!rdev->bb_page)
1553 return -ENOMEM;
1554 }
1555 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1556 rdev->badblocks.count == 0) {
1557 /* need to load the bad block list.
1558 * Currently we limit it to one page.
1559 */
1560 s32 offset;
1561 sector_t bb_sector;
1562 u64 *bbp;
1563 int i;
1564 int sectors = le16_to_cpu(sb->bblog_size);
1565 if (sectors > (PAGE_SIZE / 512))
1566 return -EINVAL;
1567 offset = le32_to_cpu(sb->bblog_offset);
1568 if (offset == 0)
1569 return -EINVAL;
1570 bb_sector = (long long)offset;
1571 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1572 rdev->bb_page, READ, true))
1573 return -EIO;
1574 bbp = (u64 *)page_address(rdev->bb_page);
1575 rdev->badblocks.shift = sb->bblog_shift;
1576 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1577 u64 bb = le64_to_cpu(*bbp);
1578 int count = bb & (0x3ff);
1579 u64 sector = bb >> 10;
1580 sector <<= sb->bblog_shift;
1581 count <<= sb->bblog_shift;
1582 if (bb + 1 == 0)
1583 break;
1584 if (md_set_badblocks(&rdev->badblocks,
1585 sector, count, 1) == 0)
1586 return -EINVAL;
1587 }
1588 } else if (sb->bblog_offset == 0)
1589 rdev->badblocks.shift = -1;
1590
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001591 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001592 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001593 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001595 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1598 sb->level != refsb->level ||
1599 sb->layout != refsb->layout ||
1600 sb->chunksize != refsb->chunksize) {
1601 printk(KERN_WARNING "md: %s has strangely different"
1602 " superblock to %s\n",
1603 bdevname(rdev->bdev,b),
1604 bdevname(refdev->bdev,b2));
1605 return -EINVAL;
1606 }
1607 ev1 = le64_to_cpu(sb->events);
1608 ev2 = le64_to_cpu(refsb->events);
1609
1610 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001611 ret = 1;
1612 else
1613 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
NeilBrowna1801f82008-03-04 14:29:31 -08001615 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001616 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001617 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001619 rdev->sectors = rdev->sb_start;
1620 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001622 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001623 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001624 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001625 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1629{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001630 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001631 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
NeilBrown41158c72005-06-21 17:17:25 -07001633 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001634 clear_bit(Faulty, &rdev->flags);
1635 clear_bit(In_sync, &rdev->flags);
1636 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (mddev->raid_disks == 0) {
1639 mddev->major_version = 1;
1640 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001641 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001642 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1644 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1645 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001646 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 mddev->layout = le32_to_cpu(sb->layout);
1648 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001649 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001650 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001651 mddev->bitmap_info.offset = 0;
1652 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1655 memcpy(mddev->uuid, sb->set_uuid, 16);
1656
1657 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001658
NeilBrown71c08052005-09-09 16:23:51 -07001659 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001660 mddev->bitmap_info.file == NULL )
1661 mddev->bitmap_info.offset =
1662 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001663
NeilBrownf6705572006-03-27 01:18:11 -08001664 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1665 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1666 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1667 mddev->new_level = le32_to_cpu(sb->new_level);
1668 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001669 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001670 } else {
1671 mddev->reshape_position = MaxSector;
1672 mddev->delta_disks = 0;
1673 mddev->new_level = mddev->level;
1674 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001675 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001676 }
1677
NeilBrown41158c72005-06-21 17:17:25 -07001678 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001679 /* Insist of good event counter while assembling, except for
1680 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001682 if (rdev->desc_nr >= 0 &&
1683 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1684 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1685 if (ev1 < mddev->events)
1686 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001687 } else if (mddev->bitmap) {
1688 /* If adding to array with a bitmap, then we can accept an
1689 * older device, but not too old.
1690 */
NeilBrown41158c72005-06-21 17:17:25 -07001691 if (ev1 < mddev->bitmap->events_cleared)
1692 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001693 } else {
1694 if (ev1 < mddev->events)
1695 /* just a hot-add of a new device, leave raid_disk at -1 */
1696 return 0;
1697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (mddev->level != LEVEL_MULTIPATH) {
1699 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001700 if (rdev->desc_nr < 0 ||
1701 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1702 role = 0xffff;
1703 rdev->desc_nr = -1;
1704 } else
1705 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 switch(role) {
1707 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 break;
1709 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001710 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 break;
1712 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001713 if ((le32_to_cpu(sb->feature_map) &
1714 MD_FEATURE_RECOVERY_OFFSET))
1715 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1716 else
1717 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 rdev->raid_disk = role;
1719 break;
1720 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001721 if (sb->devflags & WriteMostly1)
1722 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001723 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001724 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return 0;
1727}
1728
1729static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1730{
1731 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 mdk_rdev_t *rdev2;
1733 int max_dev, i;
1734 /* make rdev->sb match mddev and rdev data. */
1735
Namhyung Kim65a06f062011-07-27 11:00:36 +10001736 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 sb->feature_map = 0;
1739 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001740 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 memset(sb->pad1, 0, sizeof(sb->pad1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 memset(sb->pad3, 0, sizeof(sb->pad3));
1743
1744 sb->utime = cpu_to_le64((__u64)mddev->utime);
1745 sb->events = cpu_to_le64(mddev->events);
1746 if (mddev->in_sync)
1747 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1748 else
1749 sb->resync_offset = cpu_to_le64(0);
1750
NeilBrown1c05b4b2006-10-21 10:24:08 -07001751 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001752
NeilBrownf0ca3402006-02-02 14:28:04 -08001753 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001754 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001755 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001756 sb->level = cpu_to_le32(mddev->level);
1757 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001758
NeilBrownaeb9b212011-08-25 14:43:08 +10001759 if (test_bit(WriteMostly, &rdev->flags))
1760 sb->devflags |= WriteMostly1;
1761 else
1762 sb->devflags &= ~WriteMostly1;
1763
NeilBrownc3d97142009-12-14 12:49:52 +11001764 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1765 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001766 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001767 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001768
1769 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001770 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001771 sb->feature_map |=
1772 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1773 sb->recovery_offset =
1774 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001775 }
1776
NeilBrownf6705572006-03-27 01:18:11 -08001777 if (mddev->reshape_position != MaxSector) {
1778 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1779 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1780 sb->new_layout = cpu_to_le32(mddev->new_layout);
1781 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1782 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001783 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001784 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001785
NeilBrown2699b672011-07-28 11:31:47 +10001786 if (rdev->badblocks.count == 0)
1787 /* Nothing to do for bad blocks*/ ;
1788 else if (sb->bblog_offset == 0)
1789 /* Cannot record bad blocks on this device */
1790 md_error(mddev, rdev);
1791 else {
1792 struct badblocks *bb = &rdev->badblocks;
1793 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1794 u64 *p = bb->page;
1795 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1796 if (bb->changed) {
1797 unsigned seq;
1798
1799retry:
1800 seq = read_seqbegin(&bb->lock);
1801
1802 memset(bbp, 0xff, PAGE_SIZE);
1803
1804 for (i = 0 ; i < bb->count ; i++) {
1805 u64 internal_bb = *p++;
1806 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1807 | BB_LEN(internal_bb));
1808 *bbp++ = cpu_to_le64(store_bb);
1809 }
1810 if (read_seqretry(&bb->lock, seq))
1811 goto retry;
1812
1813 bb->sector = (rdev->sb_start +
1814 (int)le32_to_cpu(sb->bblog_offset));
1815 bb->size = le16_to_cpu(sb->bblog_size);
1816 bb->changed = 0;
1817 }
1818 }
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001821 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (rdev2->desc_nr+1 > max_dev)
1823 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001824
NeilBrown70471da2009-08-03 10:59:57 +10001825 if (max_dev > le32_to_cpu(sb->max_dev)) {
1826 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001827 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001828 rdev->sb_size = max_dev * 2 + 256;
1829 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1830 if (rdev->sb_size & bmask)
1831 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001832 } else
1833 max_dev = le32_to_cpu(sb->max_dev);
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 for (i=0; i<max_dev;i++)
1836 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1837
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001838 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001840 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001842 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001844 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001845 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 else
1847 sb->dev_roles[i] = cpu_to_le16(0xffff);
1848 }
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 sb->sb_csum = calc_sb_1_csum(sb);
1851}
1852
Chris Webb0cd17fe2008-06-28 08:31:46 +10001853static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001854super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001855{
1856 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001857 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001858 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001859 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001860 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001861 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001862 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001863 max_sectors -= rdev->data_offset;
1864 if (!num_sectors || num_sectors > max_sectors)
1865 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001866 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001867 /* minor version 0 with bitmap we can't move */
1868 return 0;
1869 } else {
1870 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001871 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001872 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001873 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001874 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001875 if (!num_sectors || num_sectors > max_sectors)
1876 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001877 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001878 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001879 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001880 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001881 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001882 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001883 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001884 rdev->sb_page);
1885 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001886 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001887}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Adrian Bunk75c96f82005-05-05 16:16:09 -07001889static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 [0] = {
1891 .name = "0.90.0",
1892 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001893 .load_super = super_90_load,
1894 .validate_super = super_90_validate,
1895 .sync_super = super_90_sync,
1896 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 },
1898 [1] = {
1899 .name = "md-1",
1900 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001901 .load_super = super_1_load,
1902 .validate_super = super_1_validate,
1903 .sync_super = super_1_sync,
1904 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 },
1906};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Jonathan Brassow076f9682011-06-07 17:51:30 -05001908static void sync_super(mddev_t *mddev, mdk_rdev_t *rdev)
1909{
1910 if (mddev->sync_super) {
1911 mddev->sync_super(mddev, rdev);
1912 return;
1913 }
1914
1915 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1916
1917 super_types[mddev->major_version].sync_super(mddev, rdev);
1918}
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1921{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001922 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
NeilBrown4b809912008-07-21 17:05:25 +10001924 rcu_read_lock();
1925 rdev_for_each_rcu(rdev, mddev1)
1926 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001927 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001928 rdev2->bdev->bd_contains) {
1929 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001930 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001931 }
1932 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return 0;
1934}
1935
1936static LIST_HEAD(pending_raid_disks);
1937
Andre Nollac5e7112009-08-03 10:59:47 +10001938/*
1939 * Try to register data integrity profile for an mddev
1940 *
1941 * This is called when an array is started and after a disk has been kicked
1942 * from the array. It only succeeds if all working and active component devices
1943 * are integrity capable with matching profiles.
1944 */
1945int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001946{
Andre Nollac5e7112009-08-03 10:59:47 +10001947 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001948
Andre Nollac5e7112009-08-03 10:59:47 +10001949 if (list_empty(&mddev->disks))
1950 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001951 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1952 return 0; /* shouldn't register, or already is */
Andre Nollac5e7112009-08-03 10:59:47 +10001953 list_for_each_entry(rdev, &mddev->disks, same_set) {
1954 /* skip spares and non-functional disks */
1955 if (test_bit(Faulty, &rdev->flags))
1956 continue;
1957 if (rdev->raid_disk < 0)
1958 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001959 if (!reference) {
1960 /* Use the first rdev as the reference */
1961 reference = rdev;
1962 continue;
1963 }
1964 /* does this rdev's profile match the reference profile? */
1965 if (blk_integrity_compare(reference->bdev->bd_disk,
1966 rdev->bdev->bd_disk) < 0)
1967 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001968 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001969 if (!reference || !bdev_get_integrity(reference->bdev))
1970 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001971 /*
1972 * All component devices are integrity capable and have matching
1973 * profiles, register the common profile for the md device.
1974 */
1975 if (blk_integrity_register(mddev->gendisk,
1976 bdev_get_integrity(reference->bdev)) != 0) {
1977 printk(KERN_ERR "md: failed to register integrity for %s\n",
1978 mdname(mddev));
1979 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001980 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001981 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1982 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1983 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1984 mdname(mddev));
1985 return -EINVAL;
1986 }
Andre Nollac5e7112009-08-03 10:59:47 +10001987 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001988}
Andre Nollac5e7112009-08-03 10:59:47 +10001989EXPORT_SYMBOL(md_integrity_register);
1990
1991/* Disable data integrity if non-capable/non-matching disk is being added */
1992void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1993{
1994 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1995 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1996
1997 if (!bi_mddev) /* nothing to do */
1998 return;
1999 if (rdev->raid_disk < 0) /* skip spares */
2000 return;
2001 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2002 rdev->bdev->bd_disk) >= 0)
2003 return;
2004 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2005 blk_integrity_unregister(mddev->gendisk);
2006}
2007EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
2010{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002011 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002012 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002013 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002014 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 if (rdev->mddev) {
2017 MD_BUG();
2018 return -EINVAL;
2019 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002020
2021 /* prevent duplicates */
2022 if (find_rdev(mddev, rdev->bdev->bd_dev))
2023 return -EEXIST;
2024
Andre Nolldd8ac332009-03-31 14:33:13 +11002025 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2026 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2027 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002028 if (mddev->pers) {
2029 /* Cannot change size, so fail
2030 * If mddev->level <= 0, then we don't care
2031 * about aligning sizes (e.g. linear)
2032 */
2033 if (mddev->level > 0)
2034 return -ENOSPC;
2035 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002036 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 /* Verify rdev->desc_nr is unique.
2040 * If it is -1, assign a free number, else
2041 * check number is not in use
2042 */
2043 if (rdev->desc_nr < 0) {
2044 int choice = 0;
2045 if (mddev->pers) choice = mddev->raid_disks;
2046 while (find_rdev_nr(mddev, choice))
2047 choice++;
2048 rdev->desc_nr = choice;
2049 } else {
2050 if (find_rdev_nr(mddev, rdev->desc_nr))
2051 return -EBUSY;
2052 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002053 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2054 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2055 mdname(mddev), mddev->max_disks);
2056 return -EBUSY;
2057 }
NeilBrown19133a42005-11-08 21:39:35 -08002058 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002059 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002060 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002063 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002064
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002065 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002066 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002067
Tejun Heo0762b8b2008-08-25 19:56:12 +09002068 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002069 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2070 /* failure here is OK */;
2071 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002072
NeilBrown4b809912008-07-21 17:05:25 +10002073 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002074 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002075
2076 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002077 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002080
2081 fail:
2082 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2083 b, mdname(mddev));
2084 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
NeilBrown177a99b2008-02-06 01:39:56 -08002087static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002088{
2089 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
2090 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002091 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002092}
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094static void unbind_rdev_from_array(mdk_rdev_t * rdev)
2095{
2096 char b[BDEVNAME_SIZE];
2097 if (!rdev->mddev) {
2098 MD_BUG();
2099 return;
2100 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002101 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002102 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2104 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002105 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002106 sysfs_put(rdev->sysfs_state);
2107 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002108 kfree(rdev->badblocks.page);
2109 rdev->badblocks.count = 0;
2110 rdev->badblocks.page = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07002111 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002112 * writing to 'remove' to "dev/state". We also need
2113 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002114 */
NeilBrown4b809912008-07-21 17:05:25 +10002115 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002116 INIT_WORK(&rdev->del_work, md_delayed_delete);
2117 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002118 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
2121/*
2122 * prevent the device from being mounted, repartitioned or
2123 * otherwise reused by a RAID array (or any other kernel
2124 * subsystem), by bd_claiming the device.
2125 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002126static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 int err = 0;
2129 struct block_device *bdev;
2130 char b[BDEVNAME_SIZE];
2131
Tejun Heod4d77622010-11-13 11:55:18 +01002132 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2133 shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (IS_ERR(bdev)) {
2135 printk(KERN_ERR "md: could not open %s.\n",
2136 __bdevname(dev, b));
2137 return PTR_ERR(bdev);
2138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 rdev->bdev = bdev;
2140 return err;
2141}
2142
2143static void unlock_rdev(mdk_rdev_t *rdev)
2144{
2145 struct block_device *bdev = rdev->bdev;
2146 rdev->bdev = NULL;
2147 if (!bdev)
2148 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002149 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
2152void md_autodetect_dev(dev_t dev);
2153
2154static void export_rdev(mdk_rdev_t * rdev)
2155{
2156 char b[BDEVNAME_SIZE];
2157 printk(KERN_INFO "md: export_rdev(%s)\n",
2158 bdevname(rdev->bdev,b));
2159 if (rdev->mddev)
2160 MD_BUG();
2161 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002163 if (test_bit(AutoDetected, &rdev->flags))
2164 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165#endif
2166 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002167 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
2170static void kick_rdev_from_array(mdk_rdev_t * rdev)
2171{
2172 unbind_rdev_from_array(rdev);
2173 export_rdev(rdev);
2174}
2175
2176static void export_array(mddev_t *mddev)
2177{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002178 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
NeilBrownd089c6a2008-02-06 01:39:59 -08002180 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (!rdev->mddev) {
2182 MD_BUG();
2183 continue;
2184 }
2185 kick_rdev_from_array(rdev);
2186 }
2187 if (!list_empty(&mddev->disks))
2188 MD_BUG();
2189 mddev->raid_disks = 0;
2190 mddev->major_version = 0;
2191}
2192
2193static void print_desc(mdp_disk_t *desc)
2194{
2195 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2196 desc->major,desc->minor,desc->raid_disk,desc->state);
2197}
2198
Cheng Renquancd2ac932009-01-09 08:31:08 +11002199static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
2201 int i;
2202
2203 printk(KERN_INFO
2204 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2205 sb->major_version, sb->minor_version, sb->patch_version,
2206 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2207 sb->ctime);
2208 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2209 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2210 sb->md_minor, sb->layout, sb->chunk_size);
2211 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2212 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2213 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2214 sb->failed_disks, sb->spare_disks,
2215 sb->sb_csum, (unsigned long)sb->events_lo);
2216
2217 printk(KERN_INFO);
2218 for (i = 0; i < MD_SB_DISKS; i++) {
2219 mdp_disk_t *desc;
2220
2221 desc = sb->disks + i;
2222 if (desc->number || desc->major || desc->minor ||
2223 desc->raid_disk || (desc->state && (desc->state != 4))) {
2224 printk(" D %2d: ", i);
2225 print_desc(desc);
2226 }
2227 }
2228 printk(KERN_INFO "md: THIS: ");
2229 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
Cheng Renquancd2ac932009-01-09 08:31:08 +11002232static void print_sb_1(struct mdp_superblock_1 *sb)
2233{
2234 __u8 *uuid;
2235
2236 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002237 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002238 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002239 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002240 le32_to_cpu(sb->major_version),
2241 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002242 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002243 sb->set_name,
2244 (unsigned long long)le64_to_cpu(sb->ctime)
2245 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2246
2247 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002248 printk(KERN_INFO
2249 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002250 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002251 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002252 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2253 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002254 le32_to_cpu(sb->level),
2255 (unsigned long long)le64_to_cpu(sb->size),
2256 le32_to_cpu(sb->raid_disks),
2257 le32_to_cpu(sb->layout),
2258 le32_to_cpu(sb->chunksize),
2259 (unsigned long long)le64_to_cpu(sb->data_offset),
2260 (unsigned long long)le64_to_cpu(sb->data_size),
2261 (unsigned long long)le64_to_cpu(sb->super_offset),
2262 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2263 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002264 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002265 sb->devflags,
2266 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2267 (unsigned long long)le64_to_cpu(sb->events),
2268 (unsigned long long)le64_to_cpu(sb->resync_offset),
2269 le32_to_cpu(sb->sb_csum),
2270 le32_to_cpu(sb->max_dev)
2271 );
2272}
2273
2274static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
2276 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002277 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2278 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002279 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2280 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002282 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2283 switch (major_version) {
2284 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002285 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002286 break;
2287 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002288 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002289 break;
2290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 } else
2292 printk(KERN_INFO "md: no rdev superblock!\n");
2293}
2294
Adrian Bunk5e563412006-06-26 00:27:42 -07002295static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002297 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 mdk_rdev_t *rdev;
2299 mddev_t *mddev;
2300 char b[BDEVNAME_SIZE];
2301
2302 printk("\n");
2303 printk("md: **********************************\n");
2304 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2305 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002306 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
NeilBrown32a76272005-06-21 17:17:14 -07002308 if (mddev->bitmap)
2309 bitmap_print_sb(mddev->bitmap);
2310 else
2311 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002312 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 printk("<%s>", bdevname(rdev->bdev,b));
2314 printk("\n");
2315
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002316 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002317 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 }
2319 printk("md: **********************************\n");
2320 printk("\n");
2321}
2322
2323
NeilBrown42543762006-06-26 00:27:57 -07002324static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
NeilBrown42543762006-06-26 00:27:57 -07002326 /* Update each superblock (in-memory image), but
2327 * if we are allowed to, skip spares which already
2328 * have the right event counter, or have one earlier
2329 * (which would mean they aren't being marked as dirty
2330 * with the rest of the array)
2331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002333 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002334 if (rdev->sb_events == mddev->events ||
2335 (nospares &&
2336 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002337 rdev->sb_events+1 == mddev->events)) {
2338 /* Don't update this superblock */
2339 rdev->sb_loaded = 2;
2340 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002341 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002342 rdev->sb_loaded = 1;
2343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
2345}
2346
NeilBrown850b2b42006-10-03 01:15:46 -07002347static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002350 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002351 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002352 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002355 /* First make sure individual recovery_offsets are correct */
2356 list_for_each_entry(rdev, &mddev->disks, same_set) {
2357 if (rdev->raid_disk >= 0 &&
2358 mddev->delta_disks >= 0 &&
2359 !test_bit(In_sync, &rdev->flags) &&
2360 mddev->curr_resync_completed > rdev->recovery_offset)
2361 rdev->recovery_offset = mddev->curr_resync_completed;
2362
2363 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002364 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002365 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002366 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002367 if (!mddev->external) {
NeilBrownd97a41dc2010-10-28 17:30:20 +11002368 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002369 list_for_each_entry(rdev, &mddev->disks, same_set) {
2370 if (rdev->badblocks.changed) {
2371 md_ack_all_badblocks(&rdev->badblocks);
2372 md_error(mddev, rdev);
2373 }
2374 clear_bit(Blocked, &rdev->flags);
2375 clear_bit(BlockedBadBlocks, &rdev->flags);
2376 wake_up(&rdev->blocked_wait);
2377 }
2378 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002379 wake_up(&mddev->sb_wait);
2380 return;
2381 }
2382
NeilBrowna9701a32005-11-08 21:39:34 -08002383 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002384
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002385 mddev->utime = get_seconds();
2386
NeilBrown850b2b42006-10-03 01:15:46 -07002387 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2388 force_change = 1;
2389 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2390 /* just a clean<-> dirty transition, possibly leave spares alone,
2391 * though if events isn't the right even/odd, we will have to do
2392 * spares after all
2393 */
2394 nospares = 1;
2395 if (force_change)
2396 nospares = 0;
2397 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002398 /* If the array is degraded, then skipping spares is both
2399 * dangerous and fairly pointless.
2400 * Dangerous because a device that was removed from the array
2401 * might have a event_count that still looks up-to-date,
2402 * so it can be re-added without a resync.
2403 * Pointless because if there are any spares to skip,
2404 * then a recovery will happen and soon that array won't
2405 * be degraded any more and the spare can go back to sleep then.
2406 */
NeilBrown850b2b42006-10-03 01:15:46 -07002407 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002408
NeilBrown06d91a52005-06-21 17:17:12 -07002409 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002410
2411 /* If this is just a dirty<->clean transition, and the array is clean
2412 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002413 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002414 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002415 && mddev->can_decrease_events
2416 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002417 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002418 mddev->can_decrease_events = 0;
2419 } else {
NeilBrown42543762006-06-26 00:27:57 -07002420 /* otherwise we have to go forward and ... */
2421 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002422 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 if (!mddev->events) {
2426 /*
2427 * oops, this 64-bit counter should never wrap.
2428 * Either we are in around ~1 trillion A.C., assuming
2429 * 1 reboot per second, or we have a bug:
2430 */
2431 MD_BUG();
2432 mddev->events --;
2433 }
NeilBrown2699b672011-07-28 11:31:47 +10002434
NeilBrownde393cd2011-07-28 11:31:48 +10002435 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown2699b672011-07-28 11:31:47 +10002436 if (rdev->badblocks.changed)
2437 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002438 if (test_bit(Faulty, &rdev->flags))
2439 set_bit(FaultRecorded, &rdev->flags);
2440 }
NeilBrown2699b672011-07-28 11:31:47 +10002441
NeilBrowne6910632008-02-06 01:39:51 -08002442 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002443 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 dprintk(KERN_INFO
2446 "md: updating %s RAID superblock on device (in sync %d)\n",
2447 mdname(mddev),mddev->in_sync);
2448
NeilBrown4ad13662007-07-17 04:06:13 -07002449 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002450 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 char b[BDEVNAME_SIZE];
2452 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002453 if (rdev->sb_loaded != 1)
2454 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002455 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 dprintk("(skipping faulty ");
2457
2458 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002459 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002460 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002461 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002462 rdev->sb_page);
2463 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2464 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002465 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002466 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002467 if (rdev->badblocks.size) {
2468 md_super_write(mddev, rdev,
2469 rdev->badblocks.sector,
2470 rdev->badblocks.size << 9,
2471 rdev->bb_page);
2472 rdev->badblocks.size = 0;
2473 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 } else
2476 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002477 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 /* only need to write one superblock... */
2479 break;
2480 }
NeilBrowna9701a32005-11-08 21:39:34 -08002481 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002482 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002483
NeilBrowna9701a32005-11-08 21:39:34 -08002484 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002485 if (mddev->in_sync != sync_req ||
2486 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002487 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002488 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002489 goto repeat;
2490 }
NeilBrown850b2b42006-10-03 01:15:46 -07002491 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002492 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002493 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002494 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2495 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002496
NeilBrownde393cd2011-07-28 11:31:48 +10002497 list_for_each_entry(rdev, &mddev->disks, same_set) {
2498 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2499 clear_bit(Blocked, &rdev->flags);
2500
2501 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002502 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002503 clear_bit(BlockedBadBlocks, &rdev->flags);
2504 wake_up(&rdev->blocked_wait);
2505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
Andre Noll7f6ce762008-03-23 18:34:54 +01002508/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002509 * We want to accept with case. For this we use cmd_match.
2510 */
2511static int cmd_match(const char *cmd, const char *str)
2512{
2513 /* See if cmd, written into a sysfs file, matches
2514 * str. They must either be the same, or cmd can
2515 * have a trailing newline
2516 */
2517 while (*cmd && *str && *cmd == *str) {
2518 cmd++;
2519 str++;
2520 }
2521 if (*cmd == '\n')
2522 cmd++;
2523 if (*str || *cmd)
2524 return 0;
2525 return 1;
2526}
2527
NeilBrown86e6ffd2005-11-08 21:39:24 -08002528struct rdev_sysfs_entry {
2529 struct attribute attr;
2530 ssize_t (*show)(mdk_rdev_t *, char *);
2531 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2532};
2533
2534static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002535state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002536{
2537 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002538 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002539
NeilBrownde393cd2011-07-28 11:31:48 +10002540 if (test_bit(Faulty, &rdev->flags) ||
2541 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002542 len+= sprintf(page+len, "%sfaulty",sep);
2543 sep = ",";
2544 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002545 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002546 len += sprintf(page+len, "%sin_sync",sep);
2547 sep = ",";
2548 }
NeilBrownf6556752006-06-26 00:28:01 -07002549 if (test_bit(WriteMostly, &rdev->flags)) {
2550 len += sprintf(page+len, "%swrite_mostly",sep);
2551 sep = ",";
2552 }
NeilBrownde393cd2011-07-28 11:31:48 +10002553 if (test_bit(Blocked, &rdev->flags) ||
2554 rdev->badblocks.unacked_exist) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002555 len += sprintf(page+len, "%sblocked", sep);
2556 sep = ",";
2557 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002558 if (!test_bit(Faulty, &rdev->flags) &&
2559 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002560 len += sprintf(page+len, "%sspare", sep);
2561 sep = ",";
2562 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002563 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2564 len += sprintf(page+len, "%swrite_error", sep);
2565 sep = ",";
2566 }
NeilBrown86e6ffd2005-11-08 21:39:24 -08002567 return len+sprintf(page+len, "\n");
2568}
2569
NeilBrown45dc2de2006-06-26 00:27:58 -07002570static ssize_t
2571state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2572{
2573 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002574 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002575 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002576 * writemostly - sets write_mostly
2577 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002578 * blocked - sets the Blocked flags
2579 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002580 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002581 * write_error - sets WriteErrorSeen
2582 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002583 */
2584 int err = -EINVAL;
2585 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2586 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002587 if (test_bit(Faulty, &rdev->flags))
2588 err = 0;
2589 else
2590 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002591 } else if (cmd_match(buf, "remove")) {
2592 if (rdev->raid_disk >= 0)
2593 err = -EBUSY;
2594 else {
2595 mddev_t *mddev = rdev->mddev;
2596 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002597 if (mddev->pers)
2598 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002599 md_new_event(mddev);
2600 err = 0;
2601 }
NeilBrownf6556752006-06-26 00:28:01 -07002602 } else if (cmd_match(buf, "writemostly")) {
2603 set_bit(WriteMostly, &rdev->flags);
2604 err = 0;
2605 } else if (cmd_match(buf, "-writemostly")) {
2606 clear_bit(WriteMostly, &rdev->flags);
2607 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002608 } else if (cmd_match(buf, "blocked")) {
2609 set_bit(Blocked, &rdev->flags);
2610 err = 0;
2611 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002612 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002613 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002614 /* metadata handler doesn't understand badblocks,
2615 * so we need to fail the device
2616 */
2617 md_error(rdev->mddev, rdev);
2618 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002619 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002620 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002621 wake_up(&rdev->blocked_wait);
2622 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2623 md_wakeup_thread(rdev->mddev->thread);
2624
2625 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002626 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2627 set_bit(In_sync, &rdev->flags);
2628 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002629 } else if (cmd_match(buf, "write_error")) {
2630 set_bit(WriteErrorSeen, &rdev->flags);
2631 err = 0;
2632 } else if (cmd_match(buf, "-write_error")) {
2633 clear_bit(WriteErrorSeen, &rdev->flags);
2634 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002635 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002636 if (!err)
2637 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002638 return err ? err : len;
2639}
NeilBrown80ca3a42006-07-10 04:44:18 -07002640static struct rdev_sysfs_entry rdev_state =
2641__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002642
2643static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002644errors_show(mdk_rdev_t *rdev, char *page)
2645{
2646 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2647}
2648
2649static ssize_t
2650errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2651{
2652 char *e;
2653 unsigned long n = simple_strtoul(buf, &e, 10);
2654 if (*buf && (*e == 0 || *e == '\n')) {
2655 atomic_set(&rdev->corrected_errors, n);
2656 return len;
2657 }
2658 return -EINVAL;
2659}
2660static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002661__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002662
NeilBrown014236d2006-01-06 00:20:55 -08002663static ssize_t
2664slot_show(mdk_rdev_t *rdev, char *page)
2665{
2666 if (rdev->raid_disk < 0)
2667 return sprintf(page, "none\n");
2668 else
2669 return sprintf(page, "%d\n", rdev->raid_disk);
2670}
2671
2672static ssize_t
2673slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2674{
2675 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002676 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002677 int slot = simple_strtoul(buf, &e, 10);
2678 if (strncmp(buf, "none", 4)==0)
2679 slot = -1;
2680 else if (e==buf || (*e && *e!= '\n'))
2681 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002682 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002683 /* Setting 'slot' on an active array requires also
2684 * updating the 'rd%d' link, and communicating
2685 * with the personality with ->hot_*_disk.
2686 * For now we only support removing
2687 * failed/spare devices. This normally happens automatically,
2688 * but not when the metadata is externally managed.
2689 */
NeilBrownc303da62008-02-06 01:39:51 -08002690 if (rdev->raid_disk == -1)
2691 return -EEXIST;
2692 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002693 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002694 return -EINVAL;
2695 err = rdev->mddev->pers->
2696 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2697 if (err)
2698 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002699 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002700 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002701 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2702 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002703 } else if (rdev->mddev->pers) {
2704 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002705 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002706 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002707 */
2708
2709 if (rdev->raid_disk != -1)
2710 return -EBUSY;
2711
NeilBrownc6751b22011-02-02 11:57:13 +11002712 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2713 return -EBUSY;
2714
Neil Brown6c2fce22008-06-28 08:31:31 +10002715 if (rdev->mddev->pers->hot_add_disk == NULL)
2716 return -EINVAL;
2717
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002718 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002719 if (rdev2->raid_disk == slot)
2720 return -EEXIST;
2721
NeilBrownba1b41b2011-01-14 09:14:34 +11002722 if (slot >= rdev->mddev->raid_disks &&
2723 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2724 return -ENOSPC;
2725
Neil Brown6c2fce22008-06-28 08:31:31 +10002726 rdev->raid_disk = slot;
2727 if (test_bit(In_sync, &rdev->flags))
2728 rdev->saved_raid_disk = slot;
2729 else
2730 rdev->saved_raid_disk = -1;
2731 err = rdev->mddev->pers->
2732 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002733 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002734 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002735 return err;
Neil Brown52664732008-06-28 08:31:44 +10002736 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002737 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002738 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002739 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002740 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002741 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002742 if (slot >= rdev->mddev->raid_disks &&
2743 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002744 return -ENOSPC;
2745 rdev->raid_disk = slot;
2746 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002747 clear_bit(Faulty, &rdev->flags);
2748 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002749 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002750 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002751 }
NeilBrown014236d2006-01-06 00:20:55 -08002752 return len;
2753}
2754
2755
2756static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002757__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002758
NeilBrown93c8cad2006-01-06 00:20:56 -08002759static ssize_t
2760offset_show(mdk_rdev_t *rdev, char *page)
2761{
Andrew Morton6961ece2006-01-06 00:20:59 -08002762 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002763}
2764
2765static ssize_t
2766offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2767{
2768 char *e;
2769 unsigned long long offset = simple_strtoull(buf, &e, 10);
2770 if (e==buf || (*e && *e != '\n'))
2771 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002772 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002773 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002774 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002775 /* Must set offset before size, so overlap checks
2776 * can be sane */
2777 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002778 rdev->data_offset = offset;
2779 return len;
2780}
2781
2782static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002783__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002784
NeilBrown83303b62006-01-06 00:21:06 -08002785static ssize_t
2786rdev_size_show(mdk_rdev_t *rdev, char *page)
2787{
Andre Nolldd8ac332009-03-31 14:33:13 +11002788 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002789}
2790
NeilBrownc5d79ad2008-02-06 01:39:54 -08002791static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2792{
2793 /* check if two start/length pairs overlap */
2794 if (s1+l1 <= s2)
2795 return 0;
2796 if (s2+l2 <= s1)
2797 return 0;
2798 return 1;
2799}
2800
Dan Williamsb522adc2009-03-31 15:00:31 +11002801static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2802{
2803 unsigned long long blocks;
2804 sector_t new;
2805
2806 if (strict_strtoull(buf, 10, &blocks) < 0)
2807 return -EINVAL;
2808
2809 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2810 return -EINVAL; /* sector conversion overflow */
2811
2812 new = blocks * 2;
2813 if (new != blocks * 2)
2814 return -EINVAL; /* unsigned long long to sector_t overflow */
2815
2816 *sectors = new;
2817 return 0;
2818}
2819
NeilBrown83303b62006-01-06 00:21:06 -08002820static ssize_t
2821rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2822{
NeilBrown27c529b2008-03-04 14:29:33 -08002823 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002824 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002825 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002826
Dan Williamsb522adc2009-03-31 15:00:31 +11002827 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002828 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002829 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002830 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002831 sectors = super_types[my_mddev->major_version].
2832 rdev_size_change(rdev, sectors);
2833 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002834 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002835 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002836 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002837 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002838 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002839 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002840 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002841
Andre Nolldd8ac332009-03-31 14:33:13 +11002842 rdev->sectors = sectors;
2843 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002844 /* need to check that all other rdevs with the same ->bdev
2845 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002846 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002847 * we have to change it back, we will have the lock again.
2848 */
2849 mddev_t *mddev;
2850 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002851 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002852
NeilBrown27c529b2008-03-04 14:29:33 -08002853 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002854 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002855 mdk_rdev_t *rdev2;
2856
2857 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002858 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002859 if (rdev->bdev == rdev2->bdev &&
2860 rdev != rdev2 &&
2861 overlaps(rdev->data_offset, rdev->sectors,
2862 rdev2->data_offset,
2863 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002864 overlap = 1;
2865 break;
2866 }
2867 mddev_unlock(mddev);
2868 if (overlap) {
2869 mddev_put(mddev);
2870 break;
2871 }
2872 }
NeilBrown27c529b2008-03-04 14:29:33 -08002873 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002874 if (overlap) {
2875 /* Someone else could have slipped in a size
2876 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002877 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002878 * safe, and trust userspace not to race with
2879 * itself
2880 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002881 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002882 return -EBUSY;
2883 }
2884 }
NeilBrown83303b62006-01-06 00:21:06 -08002885 return len;
2886}
2887
2888static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002889__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002890
Dan Williams06e3c812009-12-12 21:17:12 -07002891
2892static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2893{
2894 unsigned long long recovery_start = rdev->recovery_offset;
2895
2896 if (test_bit(In_sync, &rdev->flags) ||
2897 recovery_start == MaxSector)
2898 return sprintf(page, "none\n");
2899
2900 return sprintf(page, "%llu\n", recovery_start);
2901}
2902
2903static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2904{
2905 unsigned long long recovery_start;
2906
2907 if (cmd_match(buf, "none"))
2908 recovery_start = MaxSector;
2909 else if (strict_strtoull(buf, 10, &recovery_start))
2910 return -EINVAL;
2911
2912 if (rdev->mddev->pers &&
2913 rdev->raid_disk >= 0)
2914 return -EBUSY;
2915
2916 rdev->recovery_offset = recovery_start;
2917 if (recovery_start == MaxSector)
2918 set_bit(In_sync, &rdev->flags);
2919 else
2920 clear_bit(In_sync, &rdev->flags);
2921 return len;
2922}
2923
2924static struct rdev_sysfs_entry rdev_recovery_start =
2925__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2926
NeilBrown16c791a2011-07-28 11:31:47 +10002927
2928static ssize_t
2929badblocks_show(struct badblocks *bb, char *page, int unack);
2930static ssize_t
2931badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2932
2933static ssize_t bb_show(mdk_rdev_t *rdev, char *page)
2934{
2935 return badblocks_show(&rdev->badblocks, page, 0);
2936}
2937static ssize_t bb_store(mdk_rdev_t *rdev, const char *page, size_t len)
2938{
NeilBrownde393cd2011-07-28 11:31:48 +10002939 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2940 /* Maybe that ack was all we needed */
2941 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2942 wake_up(&rdev->blocked_wait);
2943 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002944}
2945static struct rdev_sysfs_entry rdev_bad_blocks =
2946__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2947
2948
2949static ssize_t ubb_show(mdk_rdev_t *rdev, char *page)
2950{
2951 return badblocks_show(&rdev->badblocks, page, 1);
2952}
2953static ssize_t ubb_store(mdk_rdev_t *rdev, const char *page, size_t len)
2954{
2955 return badblocks_store(&rdev->badblocks, page, len, 1);
2956}
2957static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2958__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2959
NeilBrown86e6ffd2005-11-08 21:39:24 -08002960static struct attribute *rdev_default_attrs[] = {
2961 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002962 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002963 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002964 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002965 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002966 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002967 &rdev_bad_blocks.attr,
2968 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002969 NULL,
2970};
2971static ssize_t
2972rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2973{
2974 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2975 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002976 mddev_t *mddev = rdev->mddev;
2977 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002978
2979 if (!entry->show)
2980 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002981
2982 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2983 if (!rv) {
2984 if (rdev->mddev == NULL)
2985 rv = -EBUSY;
2986 else
2987 rv = entry->show(rdev, page);
2988 mddev_unlock(mddev);
2989 }
2990 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002991}
2992
2993static ssize_t
2994rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2995 const char *page, size_t length)
2996{
2997 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2998 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002999 ssize_t rv;
3000 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003001
3002 if (!entry->store)
3003 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003004 if (!capable(CAP_SYS_ADMIN))
3005 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003006 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003007 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003008 if (rdev->mddev == NULL)
3009 rv = -EBUSY;
3010 else
3011 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003012 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003013 }
3014 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003015}
3016
3017static void rdev_free(struct kobject *ko)
3018{
3019 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
3020 kfree(rdev);
3021}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003022static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003023 .show = rdev_attr_show,
3024 .store = rdev_attr_store,
3025};
3026static struct kobj_type rdev_ktype = {
3027 .release = rdev_free,
3028 .sysfs_ops = &rdev_sysfs_ops,
3029 .default_attrs = rdev_default_attrs,
3030};
3031
NeilBrown2230dfe2011-07-28 11:31:46 +10003032int md_rdev_init(mdk_rdev_t *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003033{
3034 rdev->desc_nr = -1;
3035 rdev->saved_raid_disk = -1;
3036 rdev->raid_disk = -1;
3037 rdev->flags = 0;
3038 rdev->data_offset = 0;
3039 rdev->sb_events = 0;
3040 rdev->last_read_error.tv_sec = 0;
3041 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003042 rdev->sb_loaded = 0;
3043 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003044 atomic_set(&rdev->nr_pending, 0);
3045 atomic_set(&rdev->read_errors, 0);
3046 atomic_set(&rdev->corrected_errors, 0);
3047
3048 INIT_LIST_HEAD(&rdev->same_set);
3049 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003050
3051 /* Add space to store bad block list.
3052 * This reserves the space even on arrays where it cannot
3053 * be used - I wonder if that matters
3054 */
3055 rdev->badblocks.count = 0;
3056 rdev->badblocks.shift = 0;
3057 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3058 seqlock_init(&rdev->badblocks.lock);
3059 if (rdev->badblocks.page == NULL)
3060 return -ENOMEM;
3061
3062 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003063}
3064EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065/*
3066 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3067 *
3068 * mark the device faulty if:
3069 *
3070 * - the device is nonexistent (zero size)
3071 * - the device has no valid superblock
3072 *
3073 * a faulty rdev _never_ has rdev->sb set.
3074 */
3075static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
3076{
3077 char b[BDEVNAME_SIZE];
3078 int err;
3079 mdk_rdev_t *rdev;
3080 sector_t size;
3081
NeilBrown9ffae0c2006-01-06 00:20:32 -08003082 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 if (!rdev) {
3084 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3085 return ERR_PTR(-ENOMEM);
3086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
NeilBrown2230dfe2011-07-28 11:31:46 +10003088 err = md_rdev_init(rdev);
3089 if (err)
3090 goto abort_free;
3091 err = alloc_disk_sb(rdev);
3092 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 goto abort_free;
3094
NeilBrownc5d79ad2008-02-06 01:39:54 -08003095 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 if (err)
3097 goto abort_free;
3098
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003099 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003100
Mike Snitzer77304d22010-11-08 14:39:12 +01003101 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 if (!size) {
3103 printk(KERN_WARNING
3104 "md: %s has zero or unknown size, marking faulty!\n",
3105 bdevname(rdev->bdev,b));
3106 err = -EINVAL;
3107 goto abort_free;
3108 }
3109
3110 if (super_format >= 0) {
3111 err = super_types[super_format].
3112 load_super(rdev, NULL, super_minor);
3113 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003114 printk(KERN_WARNING
3115 "md: %s does not have a valid v%d.%d "
3116 "superblock, not importing!\n",
3117 bdevname(rdev->bdev,b),
3118 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 goto abort_free;
3120 }
3121 if (err < 0) {
3122 printk(KERN_WARNING
3123 "md: could not read %s's sb, not importing!\n",
3124 bdevname(rdev->bdev,b));
3125 goto abort_free;
3126 }
3127 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003128 if (super_format == -1)
3129 /* hot-add for 0.90, or non-persistent: so no badblocks */
3130 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 return rdev;
3133
3134abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003135 if (rdev->bdev)
3136 unlock_rdev(rdev);
3137 free_disk_sb(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003138 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 kfree(rdev);
3140 return ERR_PTR(err);
3141}
3142
3143/*
3144 * Check a full RAID array for plausibility
3145 */
3146
3147
NeilBrowna757e642005-04-16 15:26:42 -07003148static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
3150 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003151 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 char b[BDEVNAME_SIZE];
3153
3154 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08003155 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 switch (super_types[mddev->major_version].
3157 load_super(rdev, freshest, mddev->minor_version)) {
3158 case 1:
3159 freshest = rdev;
3160 break;
3161 case 0:
3162 break;
3163 default:
3164 printk( KERN_ERR \
3165 "md: fatal superblock inconsistency in %s"
3166 " -- removing from array\n",
3167 bdevname(rdev->bdev,b));
3168 kick_rdev_from_array(rdev);
3169 }
3170
3171
3172 super_types[mddev->major_version].
3173 validate_super(mddev, freshest);
3174
3175 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003176 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003177 if (mddev->max_disks &&
3178 (rdev->desc_nr >= mddev->max_disks ||
3179 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003180 printk(KERN_WARNING
3181 "md: %s: %s: only %d devices permitted\n",
3182 mdname(mddev), bdevname(rdev->bdev, b),
3183 mddev->max_disks);
3184 kick_rdev_from_array(rdev);
3185 continue;
3186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 if (rdev != freshest)
3188 if (super_types[mddev->major_version].
3189 validate_super(mddev, rdev)) {
3190 printk(KERN_WARNING "md: kicking non-fresh %s"
3191 " from array!\n",
3192 bdevname(rdev->bdev,b));
3193 kick_rdev_from_array(rdev);
3194 continue;
3195 }
3196 if (mddev->level == LEVEL_MULTIPATH) {
3197 rdev->desc_nr = i++;
3198 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003199 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003200 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003201 rdev->raid_disk = -1;
3202 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 }
3204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
NeilBrown72e02072009-12-14 12:49:55 +11003207/* Read a fixed-point number.
3208 * Numbers in sysfs attributes should be in "standard" units where
3209 * possible, so time should be in seconds.
3210 * However we internally use a a much smaller unit such as
3211 * milliseconds or jiffies.
3212 * This function takes a decimal number with a possible fractional
3213 * component, and produces an integer which is the result of
3214 * multiplying that number by 10^'scale'.
3215 * all without any floating-point arithmetic.
3216 */
3217int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3218{
3219 unsigned long result = 0;
3220 long decimals = -1;
3221 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3222 if (*cp == '.')
3223 decimals = 0;
3224 else if (decimals < scale) {
3225 unsigned int value;
3226 value = *cp - '0';
3227 result = result * 10 + value;
3228 if (decimals >= 0)
3229 decimals++;
3230 }
3231 cp++;
3232 }
3233 if (*cp == '\n')
3234 cp++;
3235 if (*cp)
3236 return -EINVAL;
3237 if (decimals < 0)
3238 decimals = 0;
3239 while (decimals < scale) {
3240 result *= 10;
3241 decimals ++;
3242 }
3243 *res = result;
3244 return 0;
3245}
3246
3247
NeilBrown19052c02008-08-05 15:54:13 +10003248static void md_safemode_timeout(unsigned long data);
3249
NeilBrowneae17012005-11-08 21:39:23 -08003250static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07003251safe_delay_show(mddev_t *mddev, char *page)
3252{
3253 int msec = (mddev->safemode_delay*1000)/HZ;
3254 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3255}
3256static ssize_t
3257safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
3258{
NeilBrown16f17b32006-06-26 00:27:37 -07003259 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003260
NeilBrown72e02072009-12-14 12:49:55 +11003261 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003262 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003263 if (msec == 0)
3264 mddev->safemode_delay = 0;
3265 else {
NeilBrown19052c02008-08-05 15:54:13 +10003266 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003267 mddev->safemode_delay = (msec*HZ)/1000;
3268 if (mddev->safemode_delay == 0)
3269 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003270 if (mddev->safemode_delay < old_delay)
3271 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003272 }
3273 return len;
3274}
3275static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003276__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003277
3278static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003279level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003280{
NeilBrown2604b702006-01-06 00:20:36 -08003281 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003282 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003283 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003284 else if (mddev->clevel[0])
3285 return sprintf(page, "%s\n", mddev->clevel);
3286 else if (mddev->level != LEVEL_NONE)
3287 return sprintf(page, "%d\n", mddev->level);
3288 else
3289 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003290}
3291
NeilBrownd9d166c2006-01-06 00:20:51 -08003292static ssize_t
3293level_store(mddev_t *mddev, const char *buf, size_t len)
3294{
Dan Williamsf2859af2010-05-02 10:04:16 -07003295 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003296 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11003297 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003298 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003299 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10003300 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003301
3302 if (mddev->pers == NULL) {
3303 if (len == 0)
3304 return 0;
3305 if (len >= sizeof(mddev->clevel))
3306 return -ENOSPC;
3307 strncpy(mddev->clevel, buf, len);
3308 if (mddev->clevel[len-1] == '\n')
3309 len--;
3310 mddev->clevel[len] = 0;
3311 mddev->level = LEVEL_NONE;
3312 return rv;
3313 }
3314
3315 /* request to change the personality. Need to ensure:
3316 * - array is not engaged in resync/recovery/reshape
3317 * - old personality can be suspended
3318 * - new personality will access other array.
3319 */
3320
NeilBrownbb4f1e92010-08-08 21:18:03 +10003321 if (mddev->sync_thread ||
3322 mddev->reshape_position != MaxSector ||
3323 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003324 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003325
3326 if (!mddev->pers->quiesce) {
3327 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3328 mdname(mddev), mddev->pers->name);
3329 return -EINVAL;
3330 }
3331
3332 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003333 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003334 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003335 strncpy(clevel, buf, len);
3336 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003337 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003338 clevel[len] = 0;
3339 if (strict_strtol(clevel, 10, &level))
3340 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003341
Dan Williamsf2859af2010-05-02 10:04:16 -07003342 if (request_module("md-%s", clevel) != 0)
3343 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003344 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003345 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003346 if (!pers || !try_module_get(pers->owner)) {
3347 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003348 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003349 return -EINVAL;
3350 }
3351 spin_unlock(&pers_lock);
3352
3353 if (pers == mddev->pers) {
3354 /* Nothing to do! */
3355 module_put(pers->owner);
3356 return rv;
3357 }
3358 if (!pers->takeover) {
3359 module_put(pers->owner);
3360 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003361 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003362 return -EINVAL;
3363 }
3364
NeilBrowne93f68a2010-06-15 09:36:03 +01003365 list_for_each_entry(rdev, &mddev->disks, same_set)
3366 rdev->new_raid_disk = rdev->raid_disk;
3367
NeilBrown245f46c2009-03-31 14:39:39 +11003368 /* ->takeover must set new_* and/or delta_disks
3369 * if it succeeds, and may set them when it fails.
3370 */
3371 priv = pers->takeover(mddev);
3372 if (IS_ERR(priv)) {
3373 mddev->new_level = mddev->level;
3374 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003375 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003376 mddev->raid_disks -= mddev->delta_disks;
3377 mddev->delta_disks = 0;
3378 module_put(pers->owner);
3379 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003380 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003381 return PTR_ERR(priv);
3382 }
3383
3384 /* Looks like we have a winner */
3385 mddev_suspend(mddev);
3386 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003387
3388 if (mddev->pers->sync_request == NULL &&
3389 pers->sync_request != NULL) {
3390 /* need to add the md_redundancy_group */
3391 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3392 printk(KERN_WARNING
3393 "md: cannot register extra attributes for %s\n",
3394 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003395 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003396 }
3397 if (mddev->pers->sync_request != NULL &&
3398 pers->sync_request == NULL) {
3399 /* need to remove the md_redundancy_group */
3400 if (mddev->to_remove == NULL)
3401 mddev->to_remove = &md_redundancy_group;
3402 }
3403
Trela Maciej54071b32010-03-08 16:02:42 +11003404 if (mddev->pers->sync_request == NULL &&
3405 mddev->external) {
3406 /* We are converting from a no-redundancy array
3407 * to a redundancy array and metadata is managed
3408 * externally so we need to be sure that writes
3409 * won't block due to a need to transition
3410 * clean->dirty
3411 * until external management is started.
3412 */
3413 mddev->in_sync = 0;
3414 mddev->safemode_delay = 0;
3415 mddev->safemode = 0;
3416 }
3417
NeilBrowne93f68a2010-06-15 09:36:03 +01003418 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003419 if (rdev->raid_disk < 0)
3420 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003421 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003422 rdev->new_raid_disk = -1;
3423 if (rdev->new_raid_disk == rdev->raid_disk)
3424 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003425 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003426 }
3427 list_for_each_entry(rdev, &mddev->disks, same_set) {
3428 if (rdev->raid_disk < 0)
3429 continue;
3430 if (rdev->new_raid_disk == rdev->raid_disk)
3431 continue;
3432 rdev->raid_disk = rdev->new_raid_disk;
3433 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003434 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003435 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003436 if (sysfs_link_rdev(mddev, rdev))
3437 printk(KERN_WARNING "md: cannot register rd%d"
3438 " for %s after level change\n",
3439 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003440 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003441 }
3442
3443 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003444 mddev->pers = pers;
3445 mddev->private = priv;
3446 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3447 mddev->level = mddev->new_level;
3448 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003449 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003450 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003451 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003452 if (mddev->pers->sync_request == NULL) {
3453 /* this is now an array without redundancy, so
3454 * it must always be in_sync
3455 */
3456 mddev->in_sync = 1;
3457 del_timer_sync(&mddev->safemode_timer);
3458 }
NeilBrown245f46c2009-03-31 14:39:39 +11003459 pers->run(mddev);
3460 mddev_resume(mddev);
3461 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3462 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3463 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003464 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003465 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003466 return rv;
3467}
3468
3469static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003470__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003471
NeilBrownd4dbd022006-06-26 00:27:59 -07003472
3473static ssize_t
3474layout_show(mddev_t *mddev, char *page)
3475{
3476 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003477 if (mddev->reshape_position != MaxSector &&
3478 mddev->layout != mddev->new_layout)
3479 return sprintf(page, "%d (%d)\n",
3480 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003481 return sprintf(page, "%d\n", mddev->layout);
3482}
3483
3484static ssize_t
3485layout_store(mddev_t *mddev, const char *buf, size_t len)
3486{
3487 char *e;
3488 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003489
3490 if (!*buf || (*e && *e != '\n'))
3491 return -EINVAL;
3492
NeilBrownb3546032009-03-31 14:56:41 +11003493 if (mddev->pers) {
3494 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003495 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003496 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003497 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003498 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003499 if (err) {
3500 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003501 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003502 }
NeilBrownb3546032009-03-31 14:56:41 +11003503 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003504 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003505 if (mddev->reshape_position == MaxSector)
3506 mddev->layout = n;
3507 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003508 return len;
3509}
3510static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003511__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003512
3513
NeilBrowneae17012005-11-08 21:39:23 -08003514static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003515raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003516{
NeilBrownbb636542005-11-08 21:39:45 -08003517 if (mddev->raid_disks == 0)
3518 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003519 if (mddev->reshape_position != MaxSector &&
3520 mddev->delta_disks != 0)
3521 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3522 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003523 return sprintf(page, "%d\n", mddev->raid_disks);
3524}
3525
NeilBrownda943b992006-01-06 00:20:54 -08003526static int update_raid_disks(mddev_t *mddev, int raid_disks);
3527
3528static ssize_t
3529raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3530{
NeilBrownda943b992006-01-06 00:20:54 -08003531 char *e;
3532 int rv = 0;
3533 unsigned long n = simple_strtoul(buf, &e, 10);
3534
3535 if (!*buf || (*e && *e != '\n'))
3536 return -EINVAL;
3537
3538 if (mddev->pers)
3539 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003540 else if (mddev->reshape_position != MaxSector) {
3541 int olddisks = mddev->raid_disks - mddev->delta_disks;
3542 mddev->delta_disks = n - olddisks;
3543 mddev->raid_disks = n;
3544 } else
NeilBrownda943b992006-01-06 00:20:54 -08003545 mddev->raid_disks = n;
3546 return rv ? rv : len;
3547}
3548static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003549__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003550
NeilBrown24dd4692005-11-08 21:39:26 -08003551static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003552chunk_size_show(mddev_t *mddev, char *page)
3553{
NeilBrown08a02ec2007-05-09 02:35:38 -07003554 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003555 mddev->chunk_sectors != mddev->new_chunk_sectors)
3556 return sprintf(page, "%d (%d)\n",
3557 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003558 mddev->chunk_sectors << 9);
3559 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003560}
3561
3562static ssize_t
3563chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3564{
NeilBrown3b343802006-01-06 00:20:47 -08003565 char *e;
3566 unsigned long n = simple_strtoul(buf, &e, 10);
3567
NeilBrown3b343802006-01-06 00:20:47 -08003568 if (!*buf || (*e && *e != '\n'))
3569 return -EINVAL;
3570
NeilBrownb3546032009-03-31 14:56:41 +11003571 if (mddev->pers) {
3572 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003573 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003574 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003575 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003576 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003577 if (err) {
3578 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003579 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003580 }
NeilBrownb3546032009-03-31 14:56:41 +11003581 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003582 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003583 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003584 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003585 }
NeilBrown3b343802006-01-06 00:20:47 -08003586 return len;
3587}
3588static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003589__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003590
NeilBrowna94213b2006-06-26 00:28:00 -07003591static ssize_t
3592resync_start_show(mddev_t *mddev, char *page)
3593{
NeilBrownd1a7c502009-03-31 15:24:32 +11003594 if (mddev->recovery_cp == MaxSector)
3595 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003596 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3597}
3598
3599static ssize_t
3600resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3601{
NeilBrowna94213b2006-06-26 00:28:00 -07003602 char *e;
3603 unsigned long long n = simple_strtoull(buf, &e, 10);
3604
NeilBrownb0986362011-05-11 15:52:21 +10003605 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003606 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003607 if (cmd_match(buf, "none"))
3608 n = MaxSector;
3609 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003610 return -EINVAL;
3611
3612 mddev->recovery_cp = n;
3613 return len;
3614}
3615static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003616__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003617
NeilBrown9e653b62006-06-26 00:27:58 -07003618/*
3619 * The array state can be:
3620 *
3621 * clear
3622 * No devices, no size, no level
3623 * Equivalent to STOP_ARRAY ioctl
3624 * inactive
3625 * May have some settings, but array is not active
3626 * all IO results in error
3627 * When written, doesn't tear down array, but just stops it
3628 * suspended (not supported yet)
3629 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003630 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003631 * readonly
3632 * no resync can happen. no superblocks get written.
3633 * write requests fail
3634 * read-auto
3635 * like readonly, but behaves like 'clean' on a write request.
3636 *
3637 * clean - no pending writes, but otherwise active.
3638 * When written to inactive array, starts without resync
3639 * If a write request arrives then
3640 * if metadata is known, mark 'dirty' and switch to 'active'.
3641 * if not known, block and switch to write-pending
3642 * If written to an active array that has pending writes, then fails.
3643 * active
3644 * fully active: IO and resync can be happening.
3645 * When written to inactive array, starts with resync
3646 *
3647 * write-pending
3648 * clean, but writes are blocked waiting for 'active' to be written.
3649 *
3650 * active-idle
3651 * like active, but no writes have been seen for a while (100msec).
3652 *
3653 */
3654enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3655 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003656static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003657 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3658 "write-pending", "active-idle", NULL };
3659
3660static int match_word(const char *word, char **list)
3661{
3662 int n;
3663 for (n=0; list[n]; n++)
3664 if (cmd_match(word, list[n]))
3665 break;
3666 return n;
3667}
3668
3669static ssize_t
3670array_state_show(mddev_t *mddev, char *page)
3671{
3672 enum array_state st = inactive;
3673
3674 if (mddev->pers)
3675 switch(mddev->ro) {
3676 case 1:
3677 st = readonly;
3678 break;
3679 case 2:
3680 st = read_auto;
3681 break;
3682 case 0:
3683 if (mddev->in_sync)
3684 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003685 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003686 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003687 else if (mddev->safemode)
3688 st = active_idle;
3689 else
3690 st = active;
3691 }
3692 else {
3693 if (list_empty(&mddev->disks) &&
3694 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003695 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003696 st = clear;
3697 else
3698 st = inactive;
3699 }
3700 return sprintf(page, "%s\n", array_states[st]);
3701}
3702
Neil Browndf5b20c2008-07-11 22:02:22 +10003703static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003704static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003705static int do_md_run(mddev_t * mddev);
3706static int restart_array(mddev_t *mddev);
3707
3708static ssize_t
3709array_state_store(mddev_t *mddev, const char *buf, size_t len)
3710{
3711 int err = -EINVAL;
3712 enum array_state st = match_word(buf, array_states);
3713 switch(st) {
3714 case bad_word:
3715 break;
3716 case clear:
3717 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003718 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003719 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003720 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003721 break;
3722 case inactive:
3723 /* stopping an active array */
3724 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003725 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003726 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003727 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003728 } else
3729 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003730 break;
3731 case suspended:
3732 break; /* not supported yet */
3733 case readonly:
3734 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003735 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003736 else {
3737 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003738 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003739 err = do_md_run(mddev);
3740 }
3741 break;
3742 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003743 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003744 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003745 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003746 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003747 err = restart_array(mddev);
3748 if (err == 0) {
3749 mddev->ro = 2;
3750 set_disk_ro(mddev->gendisk, 0);
3751 }
NeilBrown9e653b62006-06-26 00:27:58 -07003752 } else {
3753 mddev->ro = 2;
3754 err = do_md_run(mddev);
3755 }
3756 break;
3757 case clean:
3758 if (mddev->pers) {
3759 restart_array(mddev);
3760 spin_lock_irq(&mddev->write_lock);
3761 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003762 if (mddev->in_sync == 0) {
3763 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003764 if (mddev->safemode == 1)
3765 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003766 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003767 }
3768 err = 0;
3769 } else
3770 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003771 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003772 } else
3773 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003774 break;
3775 case active:
3776 if (mddev->pers) {
3777 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003778 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003779 wake_up(&mddev->sb_wait);
3780 err = 0;
3781 } else {
3782 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003783 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003784 err = do_md_run(mddev);
3785 }
3786 break;
3787 case write_pending:
3788 case active_idle:
3789 /* these cannot be set */
3790 break;
3791 }
3792 if (err)
3793 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003794 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003795 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003796 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003797 }
NeilBrown9e653b62006-06-26 00:27:58 -07003798}
NeilBrown80ca3a42006-07-10 04:44:18 -07003799static struct md_sysfs_entry md_array_state =
3800__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003801
NeilBrown6d7ff732006-01-06 00:21:16 -08003802static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003803max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3804 return sprintf(page, "%d\n",
3805 atomic_read(&mddev->max_corr_read_errors));
3806}
3807
3808static ssize_t
3809max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3810{
3811 char *e;
3812 unsigned long n = simple_strtoul(buf, &e, 10);
3813
3814 if (*buf && (*e == 0 || *e == '\n')) {
3815 atomic_set(&mddev->max_corr_read_errors, n);
3816 return len;
3817 }
3818 return -EINVAL;
3819}
3820
3821static struct md_sysfs_entry max_corr_read_errors =
3822__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3823 max_corrected_read_errors_store);
3824
3825static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003826null_show(mddev_t *mddev, char *page)
3827{
3828 return -EINVAL;
3829}
3830
3831static ssize_t
3832new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3833{
3834 /* buf must be %d:%d\n? giving major and minor numbers */
3835 /* The new device is added to the array.
3836 * If the array has a persistent superblock, we read the
3837 * superblock to initialise info and check validity.
3838 * Otherwise, only checking done is that in bind_rdev_to_array,
3839 * which mainly checks size.
3840 */
3841 char *e;
3842 int major = simple_strtoul(buf, &e, 10);
3843 int minor;
3844 dev_t dev;
3845 mdk_rdev_t *rdev;
3846 int err;
3847
3848 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3849 return -EINVAL;
3850 minor = simple_strtoul(e+1, &e, 10);
3851 if (*e && *e != '\n')
3852 return -EINVAL;
3853 dev = MKDEV(major, minor);
3854 if (major != MAJOR(dev) ||
3855 minor != MINOR(dev))
3856 return -EOVERFLOW;
3857
3858
3859 if (mddev->persistent) {
3860 rdev = md_import_device(dev, mddev->major_version,
3861 mddev->minor_version);
3862 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3863 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3864 mdk_rdev_t, same_set);
3865 err = super_types[mddev->major_version]
3866 .load_super(rdev, rdev0, mddev->minor_version);
3867 if (err < 0)
3868 goto out;
3869 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003870 } else if (mddev->external)
3871 rdev = md_import_device(dev, -2, -1);
3872 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003873 rdev = md_import_device(dev, -1, -1);
3874
3875 if (IS_ERR(rdev))
3876 return PTR_ERR(rdev);
3877 err = bind_rdev_to_array(rdev, mddev);
3878 out:
3879 if (err)
3880 export_rdev(rdev);
3881 return err ? err : len;
3882}
3883
3884static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003885__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003886
3887static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003888bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3889{
3890 char *end;
3891 unsigned long chunk, end_chunk;
3892
3893 if (!mddev->bitmap)
3894 goto out;
3895 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3896 while (*buf) {
3897 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3898 if (buf == end) break;
3899 if (*end == '-') { /* range */
3900 buf = end + 1;
3901 end_chunk = simple_strtoul(buf, &end, 0);
3902 if (buf == end) break;
3903 }
3904 if (*end && !isspace(*end)) break;
3905 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003906 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003907 }
3908 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3909out:
3910 return len;
3911}
3912
3913static struct md_sysfs_entry md_bitmap =
3914__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3915
3916static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003917size_show(mddev_t *mddev, char *page)
3918{
Andre Noll58c0fed2009-03-31 14:33:13 +11003919 return sprintf(page, "%llu\n",
3920 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003921}
3922
Andre Nolld71f9f82008-07-11 22:02:22 +10003923static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003924
3925static ssize_t
3926size_store(mddev_t *mddev, const char *buf, size_t len)
3927{
3928 /* If array is inactive, we can reduce the component size, but
3929 * not increase it (except from 0).
3930 * If array is active, we can try an on-line resize
3931 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003932 sector_t sectors;
3933 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003934
Andre Noll58c0fed2009-03-31 14:33:13 +11003935 if (err < 0)
3936 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003937 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003938 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003939 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003940 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003941 if (mddev->dev_sectors == 0 ||
3942 mddev->dev_sectors > sectors)
3943 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003944 else
3945 err = -ENOSPC;
3946 }
3947 return err ? err : len;
3948}
3949
3950static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003951__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003952
NeilBrown8bb93aa2006-01-06 00:20:50 -08003953
3954/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003955 * This is one of
3956 * 'none' for arrays with no metadata (good luck...)
3957 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003958 * or N.M for internally known formats
3959 */
3960static ssize_t
3961metadata_show(mddev_t *mddev, char *page)
3962{
3963 if (mddev->persistent)
3964 return sprintf(page, "%d.%d\n",
3965 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003966 else if (mddev->external)
3967 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003968 else
3969 return sprintf(page, "none\n");
3970}
3971
3972static ssize_t
3973metadata_store(mddev_t *mddev, const char *buf, size_t len)
3974{
3975 int major, minor;
3976 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003977 /* Changing the details of 'external' metadata is
3978 * always permitted. Otherwise there must be
3979 * no devices attached to the array.
3980 */
3981 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3982 ;
3983 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003984 return -EBUSY;
3985
3986 if (cmd_match(buf, "none")) {
3987 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003988 mddev->external = 0;
3989 mddev->major_version = 0;
3990 mddev->minor_version = 90;
3991 return len;
3992 }
3993 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003994 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003995 if (namelen >= sizeof(mddev->metadata_type))
3996 namelen = sizeof(mddev->metadata_type)-1;
3997 strncpy(mddev->metadata_type, buf+9, namelen);
3998 mddev->metadata_type[namelen] = 0;
3999 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4000 mddev->metadata_type[--namelen] = 0;
4001 mddev->persistent = 0;
4002 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004003 mddev->major_version = 0;
4004 mddev->minor_version = 90;
4005 return len;
4006 }
4007 major = simple_strtoul(buf, &e, 10);
4008 if (e==buf || *e != '.')
4009 return -EINVAL;
4010 buf = e+1;
4011 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004012 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004013 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004014 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004015 return -ENOENT;
4016 mddev->major_version = major;
4017 mddev->minor_version = minor;
4018 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004019 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004020 return len;
4021}
4022
4023static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004024__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004025
NeilBrowna35b0d62006-01-06 00:20:49 -08004026static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08004027action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004028{
NeilBrown7eec3142005-11-08 21:39:44 -08004029 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004030 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4031 type = "frozen";
4032 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004033 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004034 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4035 type = "reshape";
4036 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004037 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4038 type = "resync";
4039 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4040 type = "check";
4041 else
4042 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004043 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004044 type = "recover";
4045 }
4046 return sprintf(page, "%s\n", type);
4047}
4048
NeilBrown7ebc0be2011-01-14 09:14:33 +11004049static void reap_sync_thread(mddev_t *mddev);
4050
NeilBrown24dd4692005-11-08 21:39:26 -08004051static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08004052action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004053{
NeilBrown7eec3142005-11-08 21:39:44 -08004054 if (!mddev->pers || !mddev->pers->sync_request)
4055 return -EINVAL;
4056
NeilBrownb6a9ce62009-05-26 09:41:17 +10004057 if (cmd_match(page, "frozen"))
4058 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4059 else
4060 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4061
4062 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004063 if (mddev->sync_thread) {
4064 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004065 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004066 }
NeilBrown03c902e2006-01-06 00:20:46 -08004067 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4068 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004069 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004070 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004071 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004072 else if (cmd_match(page, "recover")) {
4073 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4074 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4075 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004076 int err;
4077 if (mddev->pers->start_reshape == NULL)
4078 return -EINVAL;
4079 err = mddev->pers->start_reshape(mddev);
4080 if (err)
4081 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004082 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004083 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004084 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004085 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004086 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004087 return -EINVAL;
4088 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4089 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004090 }
NeilBrown03c902e2006-01-06 00:20:46 -08004091 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004092 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004093 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004094 return len;
4095}
4096
NeilBrown9d888832005-11-08 21:39:26 -08004097static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004098mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004099{
4100 return sprintf(page, "%llu\n",
4101 (unsigned long long) mddev->resync_mismatches);
4102}
4103
NeilBrown80ca3a42006-07-10 04:44:18 -07004104static struct md_sysfs_entry md_scan_mode =
4105__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004106
NeilBrown96de1e62005-11-08 21:39:39 -08004107
NeilBrown80ca3a42006-07-10 04:44:18 -07004108static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004109
NeilBrown88202a02006-01-06 00:21:36 -08004110static ssize_t
4111sync_min_show(mddev_t *mddev, char *page)
4112{
4113 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4114 mddev->sync_speed_min ? "local": "system");
4115}
4116
4117static ssize_t
4118sync_min_store(mddev_t *mddev, const char *buf, size_t len)
4119{
4120 int min;
4121 char *e;
4122 if (strncmp(buf, "system", 6)==0) {
4123 mddev->sync_speed_min = 0;
4124 return len;
4125 }
4126 min = simple_strtoul(buf, &e, 10);
4127 if (buf == e || (*e && *e != '\n') || min <= 0)
4128 return -EINVAL;
4129 mddev->sync_speed_min = min;
4130 return len;
4131}
4132
4133static struct md_sysfs_entry md_sync_min =
4134__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4135
4136static ssize_t
4137sync_max_show(mddev_t *mddev, char *page)
4138{
4139 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4140 mddev->sync_speed_max ? "local": "system");
4141}
4142
4143static ssize_t
4144sync_max_store(mddev_t *mddev, const char *buf, size_t len)
4145{
4146 int max;
4147 char *e;
4148 if (strncmp(buf, "system", 6)==0) {
4149 mddev->sync_speed_max = 0;
4150 return len;
4151 }
4152 max = simple_strtoul(buf, &e, 10);
4153 if (buf == e || (*e && *e != '\n') || max <= 0)
4154 return -EINVAL;
4155 mddev->sync_speed_max = max;
4156 return len;
4157}
4158
4159static struct md_sysfs_entry md_sync_max =
4160__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4161
Iustin Popd7f3d292007-10-16 23:30:54 -07004162static ssize_t
4163degraded_show(mddev_t *mddev, char *page)
4164{
4165 return sprintf(page, "%d\n", mddev->degraded);
4166}
4167static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004168
4169static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07004170sync_force_parallel_show(mddev_t *mddev, char *page)
4171{
4172 return sprintf(page, "%d\n", mddev->parallel_resync);
4173}
4174
4175static ssize_t
4176sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
4177{
4178 long n;
4179
4180 if (strict_strtol(buf, 10, &n))
4181 return -EINVAL;
4182
4183 if (n != 0 && n != 1)
4184 return -EINVAL;
4185
4186 mddev->parallel_resync = n;
4187
4188 if (mddev->sync_thread)
4189 wake_up(&resync_wait);
4190
4191 return len;
4192}
4193
4194/* force parallel resync, even with shared block devices */
4195static struct md_sysfs_entry md_sync_force_parallel =
4196__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4197 sync_force_parallel_show, sync_force_parallel_store);
4198
4199static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08004200sync_speed_show(mddev_t *mddev, char *page)
4201{
4202 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004203 if (mddev->curr_resync == 0)
4204 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004205 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4206 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004207 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004208 db = resync - mddev->resync_mark_cnt;
4209 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004210}
4211
NeilBrown80ca3a42006-07-10 04:44:18 -07004212static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004213
4214static ssize_t
4215sync_completed_show(mddev_t *mddev, char *page)
4216{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004217 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004218
NeilBrownacb180b2009-04-14 16:28:34 +10004219 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4220 return sprintf(page, "none\n");
4221
NeilBrown88202a02006-01-06 00:21:36 -08004222 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004223 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004224 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004225 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004226
NeilBrownacb180b2009-04-14 16:28:34 +10004227 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004228 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004229}
4230
NeilBrown80ca3a42006-07-10 04:44:18 -07004231static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004232
NeilBrowne464eaf2006-03-27 01:18:14 -08004233static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10004234min_sync_show(mddev_t *mddev, char *page)
4235{
4236 return sprintf(page, "%llu\n",
4237 (unsigned long long)mddev->resync_min);
4238}
4239static ssize_t
4240min_sync_store(mddev_t *mddev, const char *buf, size_t len)
4241{
4242 unsigned long long min;
4243 if (strict_strtoull(buf, 10, &min))
4244 return -EINVAL;
4245 if (min > mddev->resync_max)
4246 return -EINVAL;
4247 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4248 return -EBUSY;
4249
4250 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004251 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004252 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004253 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004254 return -EINVAL;
4255 }
4256 mddev->resync_min = min;
4257
4258 return len;
4259}
4260
4261static struct md_sysfs_entry md_min_sync =
4262__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4263
4264static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08004265max_sync_show(mddev_t *mddev, char *page)
4266{
4267 if (mddev->resync_max == MaxSector)
4268 return sprintf(page, "max\n");
4269 else
4270 return sprintf(page, "%llu\n",
4271 (unsigned long long)mddev->resync_max);
4272}
4273static ssize_t
4274max_sync_store(mddev_t *mddev, const char *buf, size_t len)
4275{
4276 if (strncmp(buf, "max", 3) == 0)
4277 mddev->resync_max = MaxSector;
4278 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004279 unsigned long long max;
4280 if (strict_strtoull(buf, 10, &max))
4281 return -EINVAL;
4282 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004283 return -EINVAL;
4284 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004285 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004286 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4287 return -EBUSY;
4288
4289 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004290 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004291 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004292 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004293 return -EINVAL;
4294 }
4295 mddev->resync_max = max;
4296 }
4297 wake_up(&mddev->recovery_wait);
4298 return len;
4299}
4300
4301static struct md_sysfs_entry md_max_sync =
4302__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4303
4304static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004305suspend_lo_show(mddev_t *mddev, char *page)
4306{
4307 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4308}
4309
4310static ssize_t
4311suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4312{
4313 char *e;
4314 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004315 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004316
NeilBrownb8d966e2009-07-01 11:14:04 +10004317 if (mddev->pers == NULL ||
4318 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004319 return -EINVAL;
4320 if (buf == e || (*e && *e != '\n'))
4321 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004322
4323 mddev->suspend_lo = new;
4324 if (new >= old)
4325 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004326 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004327 else {
4328 /* Expanding suspended region - need to wait */
4329 mddev->pers->quiesce(mddev, 1);
4330 mddev->pers->quiesce(mddev, 0);
4331 }
4332 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004333}
4334static struct md_sysfs_entry md_suspend_lo =
4335__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4336
4337
4338static ssize_t
4339suspend_hi_show(mddev_t *mddev, char *page)
4340{
4341 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4342}
4343
4344static ssize_t
4345suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4346{
4347 char *e;
4348 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004349 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004350
NeilBrownb8d966e2009-07-01 11:14:04 +10004351 if (mddev->pers == NULL ||
4352 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004353 return -EINVAL;
4354 if (buf == e || (*e && *e != '\n'))
4355 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004356
4357 mddev->suspend_hi = new;
4358 if (new <= old)
4359 /* Shrinking suspended region */
4360 mddev->pers->quiesce(mddev, 2);
4361 else {
4362 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004363 mddev->pers->quiesce(mddev, 1);
4364 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004365 }
4366 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004367}
4368static struct md_sysfs_entry md_suspend_hi =
4369__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4370
NeilBrown08a02ec2007-05-09 02:35:38 -07004371static ssize_t
4372reshape_position_show(mddev_t *mddev, char *page)
4373{
4374 if (mddev->reshape_position != MaxSector)
4375 return sprintf(page, "%llu\n",
4376 (unsigned long long)mddev->reshape_position);
4377 strcpy(page, "none\n");
4378 return 5;
4379}
4380
4381static ssize_t
4382reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4383{
4384 char *e;
4385 unsigned long long new = simple_strtoull(buf, &e, 10);
4386 if (mddev->pers)
4387 return -EBUSY;
4388 if (buf == e || (*e && *e != '\n'))
4389 return -EINVAL;
4390 mddev->reshape_position = new;
4391 mddev->delta_disks = 0;
4392 mddev->new_level = mddev->level;
4393 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004394 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004395 return len;
4396}
4397
4398static struct md_sysfs_entry md_reshape_position =
4399__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4400 reshape_position_store);
4401
Dan Williamsb522adc2009-03-31 15:00:31 +11004402static ssize_t
4403array_size_show(mddev_t *mddev, char *page)
4404{
4405 if (mddev->external_size)
4406 return sprintf(page, "%llu\n",
4407 (unsigned long long)mddev->array_sectors/2);
4408 else
4409 return sprintf(page, "default\n");
4410}
4411
4412static ssize_t
4413array_size_store(mddev_t *mddev, const char *buf, size_t len)
4414{
4415 sector_t sectors;
4416
4417 if (strncmp(buf, "default", 7) == 0) {
4418 if (mddev->pers)
4419 sectors = mddev->pers->size(mddev, 0, 0);
4420 else
4421 sectors = mddev->array_sectors;
4422
4423 mddev->external_size = 0;
4424 } else {
4425 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4426 return -EINVAL;
4427 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004428 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004429
4430 mddev->external_size = 1;
4431 }
4432
4433 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004434 if (mddev->pers) {
4435 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004436 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004437 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004438 return len;
4439}
4440
4441static struct md_sysfs_entry md_array_size =
4442__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4443 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004444
NeilBrowneae17012005-11-08 21:39:23 -08004445static struct attribute *md_default_attrs[] = {
4446 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004447 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004448 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004449 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004450 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004451 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004452 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004453 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004454 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004455 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004456 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004457 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004458 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004459 NULL,
4460};
4461
4462static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004463 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004464 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004465 &md_sync_min.attr,
4466 &md_sync_max.attr,
4467 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004468 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004469 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004470 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004471 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004472 &md_suspend_lo.attr,
4473 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004474 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004475 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004476 NULL,
4477};
NeilBrown411036f2005-11-08 21:39:40 -08004478static struct attribute_group md_redundancy_group = {
4479 .name = NULL,
4480 .attrs = md_redundancy_attrs,
4481};
4482
NeilBrowneae17012005-11-08 21:39:23 -08004483
4484static ssize_t
4485md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4486{
4487 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4488 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004489 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004490
4491 if (!entry->show)
4492 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004493 rv = mddev_lock(mddev);
4494 if (!rv) {
4495 rv = entry->show(mddev, page);
4496 mddev_unlock(mddev);
4497 }
NeilBrown96de1e62005-11-08 21:39:39 -08004498 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004499}
4500
4501static ssize_t
4502md_attr_store(struct kobject *kobj, struct attribute *attr,
4503 const char *page, size_t length)
4504{
4505 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4506 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004507 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004508
4509 if (!entry->store)
4510 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004511 if (!capable(CAP_SYS_ADMIN))
4512 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004513 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004514 if (mddev->hold_active == UNTIL_IOCTL)
4515 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004516 if (!rv) {
4517 rv = entry->store(mddev, page, length);
4518 mddev_unlock(mddev);
4519 }
NeilBrown96de1e62005-11-08 21:39:39 -08004520 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004521}
4522
4523static void md_free(struct kobject *ko)
4524{
4525 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004526
4527 if (mddev->sysfs_state)
4528 sysfs_put(mddev->sysfs_state);
4529
4530 if (mddev->gendisk) {
4531 del_gendisk(mddev->gendisk);
4532 put_disk(mddev->gendisk);
4533 }
4534 if (mddev->queue)
4535 blk_cleanup_queue(mddev->queue);
4536
NeilBrowneae17012005-11-08 21:39:23 -08004537 kfree(mddev);
4538}
4539
Emese Revfy52cf25d2010-01-19 02:58:23 +01004540static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004541 .show = md_attr_show,
4542 .store = md_attr_store,
4543};
4544static struct kobj_type md_ktype = {
4545 .release = md_free,
4546 .sysfs_ops = &md_sysfs_ops,
4547 .default_attrs = md_default_attrs,
4548};
4549
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550int mdp_major = 0;
4551
Dan Williams5fd3a172009-03-04 00:57:25 -07004552static void mddev_delayed_delete(struct work_struct *ws)
4553{
4554 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4555
NeilBrown43a70502009-12-14 12:49:55 +11004556 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004557 kobject_del(&mddev->kobj);
4558 kobject_put(&mddev->kobj);
4559}
4560
NeilBrownefeb53c2009-01-09 08:31:10 +11004561static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004563 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 mddev_t *mddev = mddev_find(dev);
4565 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004566 int partitioned;
4567 int shift;
4568 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004569 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
4571 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004572 return -ENODEV;
4573
4574 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4575 shift = partitioned ? MdpMinorShift : 0;
4576 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
Tejun Heoe804ac72010-10-15 15:36:08 +02004578 /* wait for any previous instance of this device to be
4579 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004580 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004581 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004582
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004583 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004584 error = -EEXIST;
4585 if (mddev->gendisk)
4586 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004587
4588 if (name) {
4589 /* Need to ensure that 'name' is not a duplicate.
4590 */
4591 mddev_t *mddev2;
4592 spin_lock(&all_mddevs_lock);
4593
4594 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4595 if (mddev2->gendisk &&
4596 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4597 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004598 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004599 }
4600 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 }
NeilBrown8b765392009-01-09 08:31:08 +11004602
NeilBrown0909dc42009-07-01 12:27:21 +10004603 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004604 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004605 if (!mddev->queue)
4606 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004607 mddev->queue->queuedata = mddev;
4608
NeilBrown409c57f2009-03-31 14:39:39 +11004609 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 disk = alloc_disk(1 << shift);
4612 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004613 blk_cleanup_queue(mddev->queue);
4614 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004615 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004617 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004619 if (name)
4620 strcpy(disk->disk_name, name);
4621 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004623 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 disk->fops = &md_fops;
4626 disk->private_data = mddev;
4627 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004628 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004629 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004630 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004631 * remove it now.
4632 */
4633 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004635 /* As soon as we call add_disk(), another thread could get
4636 * through to md_open, so make sure it doesn't get too far
4637 */
4638 mutex_lock(&mddev->open_mutex);
4639 add_disk(disk);
4640
Tejun Heoed9e1982008-08-25 19:56:05 +09004641 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4642 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004643 if (error) {
4644 /* This isn't possible, but as kobject_init_and_add is marked
4645 * __must_check, we must do something with the result
4646 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004647 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4648 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004649 error = 0;
4650 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004651 if (mddev->kobj.sd &&
4652 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004653 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004654 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004655 abort:
4656 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004657 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004658 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004659 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004660 }
NeilBrownd3374822009-01-09 08:31:10 +11004661 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004662 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004663}
4664
4665static struct kobject *md_probe(dev_t dev, int *part, void *data)
4666{
4667 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 return NULL;
4669}
4670
NeilBrownefeb53c2009-01-09 08:31:10 +11004671static int add_named_array(const char *val, struct kernel_param *kp)
4672{
4673 /* val must be "md_*" where * is not all digits.
4674 * We allocate an array with a large free minor number, and
4675 * set the name to val. val must not already be an active name.
4676 */
4677 int len = strlen(val);
4678 char buf[DISK_NAME_LEN];
4679
4680 while (len && val[len-1] == '\n')
4681 len--;
4682 if (len >= DISK_NAME_LEN)
4683 return -E2BIG;
4684 strlcpy(buf, val, len+1);
4685 if (strncmp(buf, "md_", 3) != 0)
4686 return -EINVAL;
4687 return md_alloc(0, buf);
4688}
4689
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690static void md_safemode_timeout(unsigned long data)
4691{
4692 mddev_t *mddev = (mddev_t *) data;
4693
Neil Brown0fd62b82008-06-28 08:31:36 +10004694 if (!atomic_read(&mddev->writes_pending)) {
4695 mddev->safemode = 1;
4696 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004697 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 md_wakeup_thread(mddev->thread);
4700}
4701
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004702static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
NeilBrown390ee602010-06-01 19:37:27 +10004704int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705{
NeilBrown2604b702006-01-06 00:20:36 -08004706 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004708 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
NeilBrowna757e642005-04-16 15:26:42 -07004710 if (list_empty(&mddev->disks))
4711 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
4714 if (mddev->pers)
4715 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004716 /* Cannot run until previous stop completes properly */
4717 if (mddev->sysfs_active)
4718 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004719
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 /*
4721 * Analyze all RAID superblock(s)
4722 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004723 if (!mddev->raid_disks) {
4724 if (!mddev->persistent)
4725 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004726 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
NeilBrownd9d166c2006-01-06 00:20:51 -08004729 if (mddev->level != LEVEL_NONE)
4730 request_module("md-level-%d", mddev->level);
4731 else if (mddev->clevel[0])
4732 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
4734 /*
4735 * Drop all container device buffers, from now on
4736 * the only valid external interface is through the md
4737 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004739 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004740 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 continue;
4742 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004743 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004744
4745 /* perform some consistency tests on the device.
4746 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004747 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004748 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004749 if (rdev->meta_bdev) {
4750 /* Nothing to check */;
4751 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004752 if (mddev->dev_sectors &&
4753 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004754 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004755 printk("md: %s: data overlaps metadata\n",
4756 mdname(mddev));
4757 return -EINVAL;
4758 }
4759 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004760 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004761 > rdev->data_offset) {
4762 printk("md: %s: metadata overlaps data\n",
4763 mdname(mddev));
4764 return -EINVAL;
4765 }
4766 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004767 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 }
4769
NeilBrowna167f662010-10-26 18:31:13 +11004770 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004771 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
4772 sizeof(mddev_t *));
NeilBrowna167f662010-10-26 18:31:13 +11004773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004775 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004776 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004778 if (mddev->level != LEVEL_NONE)
4779 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4780 mddev->level);
4781 else
4782 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4783 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 return -EINVAL;
4785 }
NeilBrown2604b702006-01-06 00:20:36 -08004786 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004788 if (mddev->level != pers->level) {
4789 mddev->level = pers->level;
4790 mddev->new_level = pers->level;
4791 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004792 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
NeilBrownf6705572006-03-27 01:18:11 -08004794 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004795 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004796 /* This personality cannot handle reshaping... */
4797 mddev->pers = NULL;
4798 module_put(pers->owner);
4799 return -EINVAL;
4800 }
4801
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004802 if (pers->sync_request) {
4803 /* Warn if this is a potentially silly
4804 * configuration.
4805 */
4806 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4807 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004808 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004809
4810 list_for_each_entry(rdev, &mddev->disks, same_set)
4811 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004812 if (rdev < rdev2 &&
4813 rdev->bdev->bd_contains ==
4814 rdev2->bdev->bd_contains) {
4815 printk(KERN_WARNING
4816 "%s: WARNING: %s appears to be"
4817 " on the same physical disk as"
4818 " %s.\n",
4819 mdname(mddev),
4820 bdevname(rdev->bdev,b),
4821 bdevname(rdev2->bdev,b2));
4822 warned = 1;
4823 }
4824 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004825
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004826 if (warned)
4827 printk(KERN_WARNING
4828 "True protection against single-disk"
4829 " failure might be compromised.\n");
4830 }
4831
NeilBrown657390d2005-08-26 18:34:16 -07004832 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004833 /* may be over-ridden by personality */
4834 mddev->resync_max_sectors = mddev->dev_sectors;
4835
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004836 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
NeilBrown0f9552b2009-12-30 12:08:50 +11004838 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004839 mddev->ro = 2; /* read-only, but switch on first write */
4840
NeilBrownb15c2e52006-01-06 00:20:16 -08004841 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004842 if (err)
4843 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004844 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4845 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4846 " but 'external_size' not in effect?\n", __func__);
4847 printk(KERN_ERR
4848 "md: invalid array_size %llu > default size %llu\n",
4849 (unsigned long long)mddev->array_sectors / 2,
4850 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4851 err = -EINVAL;
4852 mddev->pers->stop(mddev);
4853 }
4854 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004855 err = bitmap_create(mddev);
4856 if (err) {
4857 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4858 mdname(mddev), err);
4859 mddev->pers->stop(mddev);
4860 }
4861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 module_put(mddev->pers->owner);
4864 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004865 bitmap_destroy(mddev);
4866 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004868 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004869 if (mddev->kobj.sd &&
4870 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004871 printk(KERN_WARNING
4872 "md: cannot register extra attributes for %s\n",
4873 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004874 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004875 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004876 mddev->ro = 0;
4877
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004879 atomic_set(&mddev->max_corr_read_errors,
4880 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 mddev->safemode = 0;
4882 mddev->safemode_timer.function = md_safemode_timeout;
4883 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004884 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004886 smp_wmb();
4887 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004888 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004889 if (rdev->raid_disk >= 0)
4890 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004891 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
4893 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4894
NeilBrown850b2b42006-10-03 01:15:46 -07004895 if (mddev->flags)
4896 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
NeilBrownd7603b72006-01-06 00:20:30 -08004898 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004899 sysfs_notify_dirent_safe(mddev->sysfs_state);
4900 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004901 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 return 0;
4903}
NeilBrown390ee602010-06-01 19:37:27 +10004904EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
NeilBrownfe60b012010-03-29 11:10:42 +11004906static int do_md_run(mddev_t *mddev)
4907{
4908 int err;
4909
4910 err = md_run(mddev);
4911 if (err)
4912 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004913 err = bitmap_load(mddev);
4914 if (err) {
4915 bitmap_destroy(mddev);
4916 goto out;
4917 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004918
4919 md_wakeup_thread(mddev->thread);
4920 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4921
NeilBrownfe60b012010-03-29 11:10:42 +11004922 set_capacity(mddev->gendisk, mddev->array_sectors);
4923 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004924 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004925 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4926out:
4927 return err;
4928}
4929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930static int restart_array(mddev_t *mddev)
4931{
4932 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Andre Noll80fab1d2008-07-11 22:02:21 +10004934 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004936 return -ENXIO;
4937 if (!mddev->pers)
4938 return -EINVAL;
4939 if (!mddev->ro)
4940 return -EBUSY;
4941 mddev->safemode = 0;
4942 mddev->ro = 0;
4943 set_disk_ro(disk, 0);
4944 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4945 mdname(mddev));
4946 /* Kick recovery or resync if necessary */
4947 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4948 md_wakeup_thread(mddev->thread);
4949 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004950 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004951 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952}
4953
NeilBrownacc55e22006-06-26 00:27:47 -07004954/* similar to deny_write_access, but accounts for our holding a reference
4955 * to the file ourselves */
4956static int deny_bitmap_write_access(struct file * file)
4957{
4958 struct inode *inode = file->f_mapping->host;
4959
4960 spin_lock(&inode->i_lock);
4961 if (atomic_read(&inode->i_writecount) > 1) {
4962 spin_unlock(&inode->i_lock);
4963 return -ETXTBSY;
4964 }
4965 atomic_set(&inode->i_writecount, -1);
4966 spin_unlock(&inode->i_lock);
4967
4968 return 0;
4969}
4970
NeilBrown43a70502009-12-14 12:49:55 +11004971void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004972{
4973 struct inode *inode = file->f_mapping->host;
4974
4975 spin_lock(&inode->i_lock);
4976 atomic_set(&inode->i_writecount, 1);
4977 spin_unlock(&inode->i_lock);
4978}
4979
NeilBrown6177b472010-03-29 11:37:13 +11004980static void md_clean(mddev_t *mddev)
4981{
4982 mddev->array_sectors = 0;
4983 mddev->external_size = 0;
4984 mddev->dev_sectors = 0;
4985 mddev->raid_disks = 0;
4986 mddev->recovery_cp = 0;
4987 mddev->resync_min = 0;
4988 mddev->resync_max = MaxSector;
4989 mddev->reshape_position = MaxSector;
4990 mddev->external = 0;
4991 mddev->persistent = 0;
4992 mddev->level = LEVEL_NONE;
4993 mddev->clevel[0] = 0;
4994 mddev->flags = 0;
4995 mddev->ro = 0;
4996 mddev->metadata_type[0] = 0;
4997 mddev->chunk_sectors = 0;
4998 mddev->ctime = mddev->utime = 0;
4999 mddev->layout = 0;
5000 mddev->max_disks = 0;
5001 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005002 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005003 mddev->delta_disks = 0;
5004 mddev->new_level = LEVEL_NONE;
5005 mddev->new_layout = 0;
5006 mddev->new_chunk_sectors = 0;
5007 mddev->curr_resync = 0;
5008 mddev->resync_mismatches = 0;
5009 mddev->suspend_lo = mddev->suspend_hi = 0;
5010 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5011 mddev->recovery = 0;
5012 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005013 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005014 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005015 mddev->safemode = 0;
5016 mddev->bitmap_info.offset = 0;
5017 mddev->bitmap_info.default_offset = 0;
5018 mddev->bitmap_info.chunksize = 0;
5019 mddev->bitmap_info.daemon_sleep = 0;
5020 mddev->bitmap_info.max_write_behind = 0;
5021}
5022
NeilBrowndefad612011-01-14 09:14:33 +11005023static void __md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005024{
5025 if (mddev->sync_thread) {
5026 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5027 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005028 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005029 }
5030
5031 del_timer_sync(&mddev->safemode_timer);
5032
5033 bitmap_flush(mddev);
5034 md_super_wait(mddev);
5035
5036 if (!mddev->in_sync || mddev->flags) {
5037 /* mark array as shutdown cleanly */
5038 mddev->in_sync = 1;
5039 md_update_sb(mddev, 1);
5040 }
5041}
NeilBrowndefad612011-01-14 09:14:33 +11005042
5043void md_stop_writes(mddev_t *mddev)
5044{
5045 mddev_lock(mddev);
5046 __md_stop_writes(mddev);
5047 mddev_unlock(mddev);
5048}
NeilBrown390ee602010-06-01 19:37:27 +10005049EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005050
NeilBrown390ee602010-06-01 19:37:27 +10005051void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005052{
NeilBrown0ca69882011-01-14 09:14:33 +11005053 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005054 mddev->pers->stop(mddev);
5055 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5056 mddev->to_remove = &md_redundancy_group;
5057 module_put(mddev->pers->owner);
5058 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005059 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005060}
NeilBrown390ee602010-06-01 19:37:27 +10005061EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005062
NeilBrowna4bd82d2010-03-29 13:23:10 +11005063static int md_set_readonly(mddev_t *mddev, int is_open)
5064{
5065 int err = 0;
5066 mutex_lock(&mddev->open_mutex);
5067 if (atomic_read(&mddev->openers) > is_open) {
5068 printk("md: %s still in use.\n",mdname(mddev));
5069 err = -EBUSY;
5070 goto out;
5071 }
5072 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005073 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005074
5075 err = -ENXIO;
5076 if (mddev->ro==1)
5077 goto out;
5078 mddev->ro = 1;
5079 set_disk_ro(mddev->gendisk, 1);
5080 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005081 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005082 err = 0;
5083 }
5084out:
5085 mutex_unlock(&mddev->open_mutex);
5086 return err;
5087}
5088
NeilBrown9e653b62006-06-26 00:27:58 -07005089/* mode:
5090 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005091 * 2 - stop but do not disassemble array
5092 */
Neil Browndf5b20c2008-07-11 22:02:22 +10005093static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10005096 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
NeilBrownc8c00a62009-08-10 12:50:52 +10005098 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10005099 if (atomic_read(&mddev->openers) > is_open ||
5100 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005101 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005102 mutex_unlock(&mddev->open_mutex);
5103 return -EBUSY;
5104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
NeilBrown6e17b022010-08-07 21:41:19 +10005106 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005107 if (mddev->ro)
5108 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005109
NeilBrowndefad612011-01-14 09:14:33 +11005110 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005111 md_stop(mddev);
5112 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005113 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005114
NeilBrowna4bd82d2010-03-29 13:23:10 +11005115 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005116 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005117
NeilBrowna4bd82d2010-03-29 13:23:10 +11005118 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10005119 if (rdev->raid_disk >= 0)
5120 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005121
NeilBrowna4bd82d2010-03-29 13:23:10 +11005122 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005123 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005124 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005125 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005126
NeilBrowna4bd82d2010-03-29 13:23:10 +11005127 if (mddev->ro)
5128 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005129 } else
5130 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 /*
5132 * Free resources if final stop
5133 */
NeilBrown9e653b62006-06-26 00:27:58 -07005134 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5136
NeilBrown978f9462006-02-02 14:28:05 -08005137 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005138 if (mddev->bitmap_info.file) {
5139 restore_bitmap_write_access(mddev->bitmap_info.file);
5140 fput(mddev->bitmap_info.file);
5141 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005142 }
NeilBrownc3d97142009-12-14 12:49:52 +11005143 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005144
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 export_array(mddev);
5146
NeilBrown6177b472010-03-29 11:37:13 +11005147 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005148 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005149 if (mddev->hold_active == UNTIL_STOP)
5150 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005151 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005152 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005153 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005154 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156}
5157
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005158#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159static void autorun_array(mddev_t *mddev)
5160{
5161 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 int err;
5163
NeilBrowna757e642005-04-16 15:26:42 -07005164 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
5167 printk(KERN_INFO "md: running: ");
5168
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005169 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 char b[BDEVNAME_SIZE];
5171 printk("<%s>", bdevname(rdev->bdev,b));
5172 }
5173 printk("\n");
5174
NeilBrownd710e132008-10-13 11:55:12 +11005175 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 if (err) {
5177 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005178 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 }
5180}
5181
5182/*
5183 * lets try to run arrays based on all disks that have arrived
5184 * until now. (those are in pending_raid_disks)
5185 *
5186 * the method: pick the first pending disk, collect all disks with
5187 * the same UUID, remove all from the pending list and put them into
5188 * the 'same_array' list. Then order this list based on superblock
5189 * update time (freshest comes first), kick out 'old' disks and
5190 * compare superblocks. If everything's fine then run it.
5191 *
5192 * If "unit" is allocated, then bump its reference count
5193 */
5194static void autorun_devices(int part)
5195{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005196 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 mddev_t *mddev;
5198 char b[BDEVNAME_SIZE];
5199
5200 printk(KERN_INFO "md: autorun ...\n");
5201 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005202 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005204 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 rdev0 = list_entry(pending_raid_disks.next,
5206 mdk_rdev_t, same_set);
5207
5208 printk(KERN_INFO "md: considering %s ...\n",
5209 bdevname(rdev0->bdev,b));
5210 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005211 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 if (super_90_load(rdev, rdev0, 0) >= 0) {
5213 printk(KERN_INFO "md: adding %s ...\n",
5214 bdevname(rdev->bdev,b));
5215 list_move(&rdev->same_set, &candidates);
5216 }
5217 /*
5218 * now we have a set of devices, with all of them having
5219 * mostly sane superblocks. It's time to allocate the
5220 * mddev.
5221 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005222 if (part) {
5223 dev = MKDEV(mdp_major,
5224 rdev0->preferred_minor << MdpMinorShift);
5225 unit = MINOR(dev) >> MdpMinorShift;
5226 } else {
5227 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5228 unit = MINOR(dev);
5229 }
5230 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5232 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5233 break;
5234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
5236 md_probe(dev, NULL, NULL);
5237 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005238 if (!mddev || !mddev->gendisk) {
5239 if (mddev)
5240 mddev_put(mddev);
5241 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 "md: cannot allocate memory for md drive.\n");
5243 break;
5244 }
5245 if (mddev_lock(mddev))
5246 printk(KERN_WARNING "md: %s locked, cannot run\n",
5247 mdname(mddev));
5248 else if (mddev->raid_disks || mddev->major_version
5249 || !list_empty(&mddev->disks)) {
5250 printk(KERN_WARNING
5251 "md: %s already running, cannot run %s\n",
5252 mdname(mddev), bdevname(rdev0->bdev,b));
5253 mddev_unlock(mddev);
5254 } else {
5255 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005256 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005257 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 list_del_init(&rdev->same_set);
5259 if (bind_rdev_to_array(rdev, mddev))
5260 export_rdev(rdev);
5261 }
5262 autorun_array(mddev);
5263 mddev_unlock(mddev);
5264 }
5265 /* on success, candidates will be empty, on error
5266 * it won't...
5267 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005268 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005269 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 mddev_put(mddev);
5273 }
5274 printk(KERN_INFO "md: ... autorun DONE.\n");
5275}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005276#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278static int get_version(void __user * arg)
5279{
5280 mdu_version_t ver;
5281
5282 ver.major = MD_MAJOR_VERSION;
5283 ver.minor = MD_MINOR_VERSION;
5284 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5285
5286 if (copy_to_user(arg, &ver, sizeof(ver)))
5287 return -EFAULT;
5288
5289 return 0;
5290}
5291
5292static int get_array_info(mddev_t * mddev, void __user * arg)
5293{
5294 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005295 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
NeilBrowna9f326e2009-09-23 18:06:41 +10005298 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005299 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005301 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 failed++;
5303 else {
5304 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005305 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005306 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 else
5308 spare++;
5309 }
5310 }
5311
5312 info.major_version = mddev->major_version;
5313 info.minor_version = mddev->minor_version;
5314 info.patch_version = MD_PATCHLEVEL_VERSION;
5315 info.ctime = mddev->ctime;
5316 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005317 info.size = mddev->dev_sectors / 2;
5318 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005319 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 info.nr_disks = nr;
5321 info.raid_disks = mddev->raid_disks;
5322 info.md_minor = mddev->md_minor;
5323 info.not_persistent= !mddev->persistent;
5324
5325 info.utime = mddev->utime;
5326 info.state = 0;
5327 if (mddev->in_sync)
5328 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005329 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005330 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005331 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 info.working_disks = working;
5333 info.failed_disks = failed;
5334 info.spare_disks = spare;
5335
5336 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005337 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
5339 if (copy_to_user(arg, &info, sizeof(info)))
5340 return -EFAULT;
5341
5342 return 0;
5343}
5344
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005345static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005346{
5347 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5348 char *ptr, *buf = NULL;
5349 int err = -ENOMEM;
5350
Dan Williamsb5470dc2008-06-27 21:44:04 -07005351 if (md_allow_write(mddev))
5352 file = kmalloc(sizeof(*file), GFP_NOIO);
5353 else
5354 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005355
NeilBrown32a76272005-06-21 17:17:14 -07005356 if (!file)
5357 goto out;
5358
5359 /* bitmap disabled, zero the first byte and copy out */
5360 if (!mddev->bitmap || !mddev->bitmap->file) {
5361 file->pathname[0] = '\0';
5362 goto copy_out;
5363 }
5364
5365 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5366 if (!buf)
5367 goto out;
5368
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005369 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5370 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005371 goto out;
5372
5373 strcpy(file->pathname, ptr);
5374
5375copy_out:
5376 err = 0;
5377 if (copy_to_user(arg, file, sizeof(*file)))
5378 err = -EFAULT;
5379out:
5380 kfree(buf);
5381 kfree(file);
5382 return err;
5383}
5384
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385static int get_disk_info(mddev_t * mddev, void __user * arg)
5386{
5387 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 mdk_rdev_t *rdev;
5389
5390 if (copy_from_user(&info, arg, sizeof(info)))
5391 return -EFAULT;
5392
Andre Noll26ef3792008-07-11 22:02:21 +10005393 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 if (rdev) {
5395 info.major = MAJOR(rdev->bdev->bd_dev);
5396 info.minor = MINOR(rdev->bdev->bd_dev);
5397 info.raid_disk = rdev->raid_disk;
5398 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005399 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005401 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 info.state |= (1<<MD_DISK_ACTIVE);
5403 info.state |= (1<<MD_DISK_SYNC);
5404 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005405 if (test_bit(WriteMostly, &rdev->flags))
5406 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 } else {
5408 info.major = info.minor = 0;
5409 info.raid_disk = -1;
5410 info.state = (1<<MD_DISK_REMOVED);
5411 }
5412
5413 if (copy_to_user(arg, &info, sizeof(info)))
5414 return -EFAULT;
5415
5416 return 0;
5417}
5418
5419static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5420{
5421 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5422 mdk_rdev_t *rdev;
5423 dev_t dev = MKDEV(info->major,info->minor);
5424
5425 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5426 return -EOVERFLOW;
5427
5428 if (!mddev->raid_disks) {
5429 int err;
5430 /* expecting a device which has a superblock */
5431 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5432 if (IS_ERR(rdev)) {
5433 printk(KERN_WARNING
5434 "md: md_import_device returned %ld\n",
5435 PTR_ERR(rdev));
5436 return PTR_ERR(rdev);
5437 }
5438 if (!list_empty(&mddev->disks)) {
5439 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5440 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005441 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 .load_super(rdev, rdev0, mddev->minor_version);
5443 if (err < 0) {
5444 printk(KERN_WARNING
5445 "md: %s has different UUID to %s\n",
5446 bdevname(rdev->bdev,b),
5447 bdevname(rdev0->bdev,b2));
5448 export_rdev(rdev);
5449 return -EINVAL;
5450 }
5451 }
5452 err = bind_rdev_to_array(rdev, mddev);
5453 if (err)
5454 export_rdev(rdev);
5455 return err;
5456 }
5457
5458 /*
5459 * add_new_disk can be used once the array is assembled
5460 * to add "hot spares". They must already have a superblock
5461 * written
5462 */
5463 if (mddev->pers) {
5464 int err;
5465 if (!mddev->pers->hot_add_disk) {
5466 printk(KERN_WARNING
5467 "%s: personality does not support diskops!\n",
5468 mdname(mddev));
5469 return -EINVAL;
5470 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005471 if (mddev->persistent)
5472 rdev = md_import_device(dev, mddev->major_version,
5473 mddev->minor_version);
5474 else
5475 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 if (IS_ERR(rdev)) {
5477 printk(KERN_WARNING
5478 "md: md_import_device returned %ld\n",
5479 PTR_ERR(rdev));
5480 return PTR_ERR(rdev);
5481 }
NeilBrown1a855a02010-12-09 16:36:28 +11005482 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005483 if (!mddev->persistent) {
5484 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005485 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005486 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005487 set_bit(In_sync, &rdev->flags);
5488 } else
NeilBrown41158c72005-06-21 17:17:25 -07005489 rdev->raid_disk = -1;
5490 } else
5491 super_types[mddev->major_version].
5492 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005493 if ((info->state & (1<<MD_DISK_SYNC)) &&
5494 (!test_bit(In_sync, &rdev->flags) ||
5495 rdev->raid_disk != info->raid_disk)) {
5496 /* This was a hot-add request, but events doesn't
5497 * match, so reject it.
5498 */
5499 export_rdev(rdev);
5500 return -EINVAL;
5501 }
5502
NeilBrown1a855a02010-12-09 16:36:28 +11005503 if (test_bit(In_sync, &rdev->flags))
5504 rdev->saved_raid_disk = rdev->raid_disk;
5505 else
5506 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005507
NeilBrownb2d444d2005-11-08 21:39:31 -08005508 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005509 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5510 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005511 else
5512 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005513
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 rdev->raid_disk = -1;
5515 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005516 if (!err && !mddev->pers->hot_remove_disk) {
5517 /* If there is hot_add_disk but no hot_remove_disk
5518 * then added disks for geometry changes,
5519 * and should be added immediately.
5520 */
5521 super_types[mddev->major_version].
5522 validate_super(mddev, rdev);
5523 err = mddev->pers->hot_add_disk(mddev, rdev);
5524 if (err)
5525 unbind_rdev_from_array(rdev);
5526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 if (err)
5528 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005529 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005530 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005531
NeilBrown17571282006-12-10 02:20:52 -08005532 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005533 if (mddev->degraded)
5534 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005535 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005536 if (!err)
5537 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005538 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 return err;
5540 }
5541
5542 /* otherwise, add_new_disk is only allowed
5543 * for major_version==0 superblocks
5544 */
5545 if (mddev->major_version != 0) {
5546 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5547 mdname(mddev));
5548 return -EINVAL;
5549 }
5550
5551 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5552 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005553 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 if (IS_ERR(rdev)) {
5555 printk(KERN_WARNING
5556 "md: error, md_import_device() returned %ld\n",
5557 PTR_ERR(rdev));
5558 return PTR_ERR(rdev);
5559 }
5560 rdev->desc_nr = info->number;
5561 if (info->raid_disk < mddev->raid_disks)
5562 rdev->raid_disk = info->raid_disk;
5563 else
5564 rdev->raid_disk = -1;
5565
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005567 if (info->state & (1<<MD_DISK_SYNC))
5568 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005570 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5571 set_bit(WriteMostly, &rdev->flags);
5572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 if (!mddev->persistent) {
5574 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005575 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5576 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005577 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005578 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
NeilBrown2bf071b2006-01-06 00:20:55 -08005580 err = bind_rdev_to_array(rdev, mddev);
5581 if (err) {
5582 export_rdev(rdev);
5583 return err;
5584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 }
5586
5587 return 0;
5588}
5589
5590static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5591{
5592 char b[BDEVNAME_SIZE];
5593 mdk_rdev_t *rdev;
5594
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 rdev = find_rdev(mddev, dev);
5596 if (!rdev)
5597 return -ENXIO;
5598
5599 if (rdev->raid_disk >= 0)
5600 goto busy;
5601
5602 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005603 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005604 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
5606 return 0;
5607busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005608 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 bdevname(rdev->bdev,b), mdname(mddev));
5610 return -EBUSY;
5611}
5612
5613static int hot_add_disk(mddev_t * mddev, dev_t dev)
5614{
5615 char b[BDEVNAME_SIZE];
5616 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 mdk_rdev_t *rdev;
5618
5619 if (!mddev->pers)
5620 return -ENODEV;
5621
5622 if (mddev->major_version != 0) {
5623 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5624 " version-0 superblocks.\n",
5625 mdname(mddev));
5626 return -EINVAL;
5627 }
5628 if (!mddev->pers->hot_add_disk) {
5629 printk(KERN_WARNING
5630 "%s: personality does not support diskops!\n",
5631 mdname(mddev));
5632 return -EINVAL;
5633 }
5634
NeilBrownd710e132008-10-13 11:55:12 +11005635 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 if (IS_ERR(rdev)) {
5637 printk(KERN_WARNING
5638 "md: error, md_import_device() returned %ld\n",
5639 PTR_ERR(rdev));
5640 return -EINVAL;
5641 }
5642
5643 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005644 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005646 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
NeilBrown8190e752009-06-18 08:48:58 +10005648 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
NeilBrownb2d444d2005-11-08 21:39:31 -08005650 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 printk(KERN_WARNING
5652 "md: can not hot-add faulty %s disk to %s!\n",
5653 bdevname(rdev->bdev,b), mdname(mddev));
5654 err = -EINVAL;
5655 goto abort_export;
5656 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005657 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005659 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005660 err = bind_rdev_to_array(rdev, mddev);
5661 if (err)
5662 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
5664 /*
5665 * The rest should better be atomic, we can have disk failures
5666 * noticed in interrupt contexts ...
5667 */
5668
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 rdev->raid_disk = -1;
5670
NeilBrown850b2b42006-10-03 01:15:46 -07005671 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
5673 /*
5674 * Kick recovery, maybe this spare has to be added to the
5675 * array immediately.
5676 */
5677 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5678 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005679 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 return 0;
5681
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682abort_export:
5683 export_rdev(rdev);
5684 return err;
5685}
5686
NeilBrown32a76272005-06-21 17:17:14 -07005687static int set_bitmap_file(mddev_t *mddev, int fd)
5688{
5689 int err;
5690
NeilBrown36fa3062005-09-09 16:23:45 -07005691 if (mddev->pers) {
5692 if (!mddev->pers->quiesce)
5693 return -EBUSY;
5694 if (mddev->recovery || mddev->sync_thread)
5695 return -EBUSY;
5696 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005697 }
5698
NeilBrown36fa3062005-09-09 16:23:45 -07005699
5700 if (fd >= 0) {
5701 if (mddev->bitmap)
5702 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005703 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005704
NeilBrownc3d97142009-12-14 12:49:52 +11005705 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005706 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5707 mdname(mddev));
5708 return -EBADF;
5709 }
5710
NeilBrownc3d97142009-12-14 12:49:52 +11005711 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005712 if (err) {
5713 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5714 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005715 fput(mddev->bitmap_info.file);
5716 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005717 return err;
5718 }
NeilBrownc3d97142009-12-14 12:49:52 +11005719 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005720 } else if (mddev->bitmap == NULL)
5721 return -ENOENT; /* cannot remove what isn't there */
5722 err = 0;
5723 if (mddev->pers) {
5724 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005725 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005726 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005727 if (!err)
5728 err = bitmap_load(mddev);
5729 }
NeilBrownd7375ab322006-06-26 00:27:43 -07005730 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005731 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005732 fd = -1; /* make sure to put the file */
5733 }
NeilBrown36fa3062005-09-09 16:23:45 -07005734 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07005735 }
5736 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005737 if (mddev->bitmap_info.file) {
5738 restore_bitmap_write_access(mddev->bitmap_info.file);
5739 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005740 }
NeilBrownc3d97142009-12-14 12:49:52 +11005741 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005742 }
5743
NeilBrown32a76272005-06-21 17:17:14 -07005744 return err;
5745}
5746
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747/*
5748 * set_array_info is used two different ways
5749 * The original usage is when creating a new array.
5750 * In this usage, raid_disks is > 0 and it together with
5751 * level, size, not_persistent,layout,chunksize determine the
5752 * shape of the array.
5753 * This will always create an array with a type-0.90.0 superblock.
5754 * The newer usage is when assembling an array.
5755 * In this case raid_disks will be 0, and the major_version field is
5756 * use to determine which style super-blocks are to be found on the devices.
5757 * The minor and patch _version numbers are also kept incase the
5758 * super_block handler wishes to interpret them.
5759 */
5760static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5761{
5762
5763 if (info->raid_disks == 0) {
5764 /* just setting version number for superblock loading */
5765 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005766 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 super_types[info->major_version].name == NULL) {
5768 /* maybe try to auto-load a module? */
5769 printk(KERN_INFO
5770 "md: superblock version %d not known\n",
5771 info->major_version);
5772 return -EINVAL;
5773 }
5774 mddev->major_version = info->major_version;
5775 mddev->minor_version = info->minor_version;
5776 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005777 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005778 /* ensure mddev_put doesn't delete this now that there
5779 * is some minimal configuration.
5780 */
5781 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 return 0;
5783 }
5784 mddev->major_version = MD_MAJOR_VERSION;
5785 mddev->minor_version = MD_MINOR_VERSION;
5786 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5787 mddev->ctime = get_seconds();
5788
5789 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005790 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005791 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 mddev->raid_disks = info->raid_disks;
5793 /* don't set md_minor, it is determined by which /dev/md* was
5794 * openned
5795 */
5796 if (info->state & (1<<MD_SB_CLEAN))
5797 mddev->recovery_cp = MaxSector;
5798 else
5799 mddev->recovery_cp = 0;
5800 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005801 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802
5803 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005804 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
5806 mddev->max_disks = MD_SB_DISKS;
5807
NeilBrowne6910632008-02-06 01:39:51 -08005808 if (mddev->persistent)
5809 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005810 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
NeilBrownc3d97142009-12-14 12:49:52 +11005812 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5813 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005814
NeilBrownf6705572006-03-27 01:18:11 -08005815 mddev->reshape_position = MaxSector;
5816
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 /*
5818 * Generate a 128 bit UUID
5819 */
5820 get_random_bytes(mddev->uuid, 16);
5821
NeilBrownf6705572006-03-27 01:18:11 -08005822 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005823 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005824 mddev->new_layout = mddev->layout;
5825 mddev->delta_disks = 0;
5826
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 return 0;
5828}
5829
Dan Williams1f403622009-03-31 14:59:03 +11005830void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5831{
Dan Williamsb522adc2009-03-31 15:00:31 +11005832 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5833
5834 if (mddev->external_size)
5835 return;
5836
Dan Williams1f403622009-03-31 14:59:03 +11005837 mddev->array_sectors = array_sectors;
5838}
5839EXPORT_SYMBOL(md_set_array_sectors);
5840
Andre Nolld71f9f82008-07-11 22:02:22 +10005841static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005842{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005843 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005844 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005845 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005846
5847 if (mddev->pers->resize == NULL)
5848 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005849 /* The "num_sectors" is the number of sectors of each device that
5850 * is used. This can only make sense for arrays with redundancy.
5851 * linear and raid0 always use whatever space is available. We can only
5852 * consider changing this number if no resync or reconstruction is
5853 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005854 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005855 * of each device. If num_sectors is zero, we find the largest size
5856 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005857 */
5858 if (mddev->sync_thread)
5859 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005860 if (mddev->bitmap)
5861 /* Sorry, cannot grow a bitmap yet, just remove it,
5862 * grow, and re-add.
5863 */
5864 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005865 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005866 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005867
Andre Nolld71f9f82008-07-11 22:02:22 +10005868 if (fit && (num_sectors == 0 || num_sectors > avail))
5869 num_sectors = avail;
5870 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005871 return -ENOSPC;
5872 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005873 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005874 if (!rv)
5875 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005876 return rv;
5877}
5878
NeilBrownda943b992006-01-06 00:20:54 -08005879static int update_raid_disks(mddev_t *mddev, int raid_disks)
5880{
5881 int rv;
5882 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005883 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005884 return -EINVAL;
5885 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005886 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005887 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005888 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005889 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005890 mddev->delta_disks = raid_disks - mddev->raid_disks;
5891
5892 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005893 if (rv < 0)
5894 mddev->delta_disks = 0;
NeilBrownda943b992006-01-06 00:20:54 -08005895 return rv;
5896}
5897
5898
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899/*
5900 * update_array_info is used to change the configuration of an
5901 * on-line array.
5902 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5903 * fields in the info are checked against the array.
5904 * Any differences that cannot be handled will cause an error.
5905 * Normally, only one change can be managed at a time.
5906 */
5907static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5908{
5909 int rv = 0;
5910 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005911 int state = 0;
5912
5913 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005914 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005915 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
5917 if (mddev->major_version != info->major_version ||
5918 mddev->minor_version != info->minor_version ||
5919/* mddev->patch_version != info->patch_version || */
5920 mddev->ctime != info->ctime ||
5921 mddev->level != info->level ||
5922/* mddev->layout != info->layout || */
5923 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005924 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005925 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5926 ((state^info->state) & 0xfffffe00)
5927 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 return -EINVAL;
5929 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005930 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5931 cnt++;
5932 if (mddev->raid_disks != info->raid_disks)
5933 cnt++;
5934 if (mddev->layout != info->layout)
5935 cnt++;
5936 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5937 cnt++;
5938 if (cnt == 0)
5939 return 0;
5940 if (cnt > 1)
5941 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942
5943 if (mddev->layout != info->layout) {
5944 /* Change layout
5945 * we don't need to do anything at the md level, the
5946 * personality will take care of it all.
5947 */
NeilBrown50ac1682009-06-18 08:47:55 +10005948 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005950 else {
5951 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005952 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005953 if (rv)
5954 mddev->new_layout = mddev->layout;
5955 return rv;
5956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005958 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005959 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960
NeilBrownda943b992006-01-06 00:20:54 -08005961 if (mddev->raid_disks != info->raid_disks)
5962 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
NeilBrown36fa3062005-09-09 16:23:45 -07005964 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5965 if (mddev->pers->quiesce == NULL)
5966 return -EINVAL;
5967 if (mddev->recovery || mddev->sync_thread)
5968 return -EBUSY;
5969 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5970 /* add the bitmap */
5971 if (mddev->bitmap)
5972 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005973 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005974 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005975 mddev->bitmap_info.offset =
5976 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005977 mddev->pers->quiesce(mddev, 1);
5978 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005979 if (!rv)
5980 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005981 if (rv)
5982 bitmap_destroy(mddev);
5983 mddev->pers->quiesce(mddev, 0);
5984 } else {
5985 /* remove the bitmap */
5986 if (!mddev->bitmap)
5987 return -ENOENT;
5988 if (mddev->bitmap->file)
5989 return -EINVAL;
5990 mddev->pers->quiesce(mddev, 1);
5991 bitmap_destroy(mddev);
5992 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005993 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005994 }
5995 }
NeilBrown850b2b42006-10-03 01:15:46 -07005996 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 return rv;
5998}
5999
6000static int set_disk_faulty(mddev_t *mddev, dev_t dev)
6001{
6002 mdk_rdev_t *rdev;
6003
6004 if (mddev->pers == NULL)
6005 return -ENODEV;
6006
6007 rdev = find_rdev(mddev, dev);
6008 if (!rdev)
6009 return -ENODEV;
6010
6011 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006012 if (!test_bit(Faulty, &rdev->flags))
6013 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 return 0;
6015}
6016
Andre Noll2f9618c2008-04-25 18:57:58 +02006017/*
6018 * We have a problem here : there is no easy way to give a CHS
6019 * virtual geometry. We currently pretend that we have a 2 heads
6020 * 4 sectors (with a BIG number of cylinders...). This drives
6021 * dosfs just mad... ;-)
6022 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006023static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6024{
6025 mddev_t *mddev = bdev->bd_disk->private_data;
6026
6027 geo->heads = 2;
6028 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006029 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006030 return 0;
6031}
6032
Al Viroa39907f2008-03-02 10:31:15 -05006033static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 unsigned int cmd, unsigned long arg)
6035{
6036 int err = 0;
6037 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006039 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
6041 if (!capable(CAP_SYS_ADMIN))
6042 return -EACCES;
6043
6044 /*
6045 * Commands dealing with the RAID driver but not any
6046 * particular array:
6047 */
6048 switch (cmd)
6049 {
6050 case RAID_VERSION:
6051 err = get_version(argp);
6052 goto done;
6053
6054 case PRINT_RAID_DEBUG:
6055 err = 0;
6056 md_print_devices();
6057 goto done;
6058
6059#ifndef MODULE
6060 case RAID_AUTORUN:
6061 err = 0;
6062 autostart_arrays(arg);
6063 goto done;
6064#endif
6065 default:;
6066 }
6067
6068 /*
6069 * Commands creating/starting a new array:
6070 */
6071
Al Viroa39907f2008-03-02 10:31:15 -05006072 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
6074 if (!mddev) {
6075 BUG();
6076 goto abort;
6077 }
6078
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 err = mddev_lock(mddev);
6080 if (err) {
6081 printk(KERN_INFO
6082 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6083 err, cmd);
6084 goto abort;
6085 }
6086
6087 switch (cmd)
6088 {
6089 case SET_ARRAY_INFO:
6090 {
6091 mdu_array_info_t info;
6092 if (!arg)
6093 memset(&info, 0, sizeof(info));
6094 else if (copy_from_user(&info, argp, sizeof(info))) {
6095 err = -EFAULT;
6096 goto abort_unlock;
6097 }
6098 if (mddev->pers) {
6099 err = update_array_info(mddev, &info);
6100 if (err) {
6101 printk(KERN_WARNING "md: couldn't update"
6102 " array info. %d\n", err);
6103 goto abort_unlock;
6104 }
6105 goto done_unlock;
6106 }
6107 if (!list_empty(&mddev->disks)) {
6108 printk(KERN_WARNING
6109 "md: array %s already has disks!\n",
6110 mdname(mddev));
6111 err = -EBUSY;
6112 goto abort_unlock;
6113 }
6114 if (mddev->raid_disks) {
6115 printk(KERN_WARNING
6116 "md: array %s already initialised!\n",
6117 mdname(mddev));
6118 err = -EBUSY;
6119 goto abort_unlock;
6120 }
6121 err = set_array_info(mddev, &info);
6122 if (err) {
6123 printk(KERN_WARNING "md: couldn't set"
6124 " array info. %d\n", err);
6125 goto abort_unlock;
6126 }
6127 }
6128 goto done_unlock;
6129
6130 default:;
6131 }
6132
6133 /*
6134 * Commands querying/configuring an existing array:
6135 */
NeilBrown32a76272005-06-21 17:17:14 -07006136 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006137 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006138 if ((!mddev->raid_disks && !mddev->external)
6139 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6140 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6141 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 err = -ENODEV;
6143 goto abort_unlock;
6144 }
6145
6146 /*
6147 * Commands even a read-only array can execute:
6148 */
6149 switch (cmd)
6150 {
6151 case GET_ARRAY_INFO:
6152 err = get_array_info(mddev, argp);
6153 goto done_unlock;
6154
NeilBrown32a76272005-06-21 17:17:14 -07006155 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006156 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006157 goto done_unlock;
6158
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 case GET_DISK_INFO:
6160 err = get_disk_info(mddev, argp);
6161 goto done_unlock;
6162
6163 case RESTART_ARRAY_RW:
6164 err = restart_array(mddev);
6165 goto done_unlock;
6166
6167 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006168 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 goto done_unlock;
6170
6171 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11006172 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 goto done_unlock;
6174
Dan Williamse2218352010-05-12 08:25:37 +10006175 case BLKROSET:
6176 if (get_user(ro, (int __user *)(arg))) {
6177 err = -EFAULT;
6178 goto done_unlock;
6179 }
6180 err = -EINVAL;
6181
6182 /* if the bdev is going readonly the value of mddev->ro
6183 * does not matter, no writes are coming
6184 */
6185 if (ro)
6186 goto done_unlock;
6187
6188 /* are we are already prepared for writes? */
6189 if (mddev->ro != 1)
6190 goto done_unlock;
6191
6192 /* transitioning to readauto need only happen for
6193 * arrays that call md_write_start
6194 */
6195 if (mddev->pers) {
6196 err = restart_array(mddev);
6197 if (err == 0) {
6198 mddev->ro = 2;
6199 set_disk_ro(mddev->gendisk, 0);
6200 }
6201 }
6202 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 }
6204
6205 /*
6206 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006207 * superblock, so we do not allow them on read-only arrays.
6208 * However non-MD ioctls (e.g. get-size) will still come through
6209 * here and hit the 'default' below, so only disallow
6210 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006212 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006213 if (mddev->ro == 2) {
6214 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006215 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006216 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6217 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006218 } else {
6219 err = -EROFS;
6220 goto abort_unlock;
6221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 }
6223
6224 switch (cmd)
6225 {
6226 case ADD_NEW_DISK:
6227 {
6228 mdu_disk_info_t info;
6229 if (copy_from_user(&info, argp, sizeof(info)))
6230 err = -EFAULT;
6231 else
6232 err = add_new_disk(mddev, &info);
6233 goto done_unlock;
6234 }
6235
6236 case HOT_REMOVE_DISK:
6237 err = hot_remove_disk(mddev, new_decode_dev(arg));
6238 goto done_unlock;
6239
6240 case HOT_ADD_DISK:
6241 err = hot_add_disk(mddev, new_decode_dev(arg));
6242 goto done_unlock;
6243
6244 case SET_DISK_FAULTY:
6245 err = set_disk_faulty(mddev, new_decode_dev(arg));
6246 goto done_unlock;
6247
6248 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006249 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 goto done_unlock;
6251
NeilBrown32a76272005-06-21 17:17:14 -07006252 case SET_BITMAP_FILE:
6253 err = set_bitmap_file(mddev, (int)arg);
6254 goto done_unlock;
6255
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 err = -EINVAL;
6258 goto abort_unlock;
6259 }
6260
6261done_unlock:
6262abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006263 if (mddev->hold_active == UNTIL_IOCTL &&
6264 err != -EINVAL)
6265 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 mddev_unlock(mddev);
6267
6268 return err;
6269done:
6270 if (err)
6271 MD_BUG();
6272abort:
6273 return err;
6274}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006275#ifdef CONFIG_COMPAT
6276static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6277 unsigned int cmd, unsigned long arg)
6278{
6279 switch (cmd) {
6280 case HOT_REMOVE_DISK:
6281 case HOT_ADD_DISK:
6282 case SET_DISK_FAULTY:
6283 case SET_BITMAP_FILE:
6284 /* These take in integer arg, do not convert */
6285 break;
6286 default:
6287 arg = (unsigned long)compat_ptr(arg);
6288 break;
6289 }
6290
6291 return md_ioctl(bdev, mode, cmd, arg);
6292}
6293#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Al Viroa39907f2008-03-02 10:31:15 -05006295static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296{
6297 /*
6298 * Succeed if we can lock the mddev, which confirms that
6299 * it isn't being stopped right now.
6300 */
NeilBrownd3374822009-01-09 08:31:10 +11006301 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 int err;
6303
NeilBrownd3374822009-01-09 08:31:10 +11006304 if (mddev->gendisk != bdev->bd_disk) {
6305 /* we are racing with mddev_put which is discarding this
6306 * bd_disk.
6307 */
6308 mddev_put(mddev);
6309 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006310 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006311 /* Then retry the open from the top */
6312 return -ERESTARTSYS;
6313 }
6314 BUG_ON(mddev != bdev->bd_disk->private_data);
6315
NeilBrownc8c00a62009-08-10 12:50:52 +10006316 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 goto out;
6318
6319 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006320 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006321 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006323 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 out:
6325 return err;
6326}
6327
Al Viroa39907f2008-03-02 10:31:15 -05006328static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329{
Al Viroa39907f2008-03-02 10:31:15 -05006330 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006332 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006333 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 mddev_put(mddev);
6335
6336 return 0;
6337}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006338
6339static int md_media_changed(struct gendisk *disk)
6340{
6341 mddev_t *mddev = disk->private_data;
6342
6343 return mddev->changed;
6344}
6345
6346static int md_revalidate(struct gendisk *disk)
6347{
6348 mddev_t *mddev = disk->private_data;
6349
6350 mddev->changed = 0;
6351 return 0;
6352}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006353static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354{
6355 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006356 .open = md_open,
6357 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006358 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006359#ifdef CONFIG_COMPAT
6360 .compat_ioctl = md_compat_ioctl,
6361#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006362 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006363 .media_changed = md_media_changed,
6364 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365};
6366
Adrian Bunk75c96f82005-05-05 16:16:09 -07006367static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368{
6369 mdk_thread_t *thread = arg;
6370
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 /*
6372 * md_thread is a 'system-thread', it's priority should be very
6373 * high. We avoid resource deadlocks individually in each
6374 * raid personality. (RAID5 does preallocation) We also use RR and
6375 * the very same RT priority as kswapd, thus we will never get
6376 * into a priority inversion deadlock.
6377 *
6378 * we definitely have to have equal or higher priority than
6379 * bdflush, otherwise bdflush will deadlock if there are too
6380 * many dirty RAID5 blocks.
6381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
NeilBrown6985c432005-10-19 21:23:47 -07006383 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006384 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
NeilBrown93588e22005-11-15 00:09:12 -08006386 /* We need to wait INTERRUPTIBLE so that
6387 * we don't add to the load-average.
6388 * That means we need to be sure no signals are
6389 * pending
6390 */
6391 if (signal_pending(current))
6392 flush_signals(current);
6393
6394 wait_event_interruptible_timeout
6395 (thread->wqueue,
6396 test_bit(THREAD_WAKEUP, &thread->flags)
6397 || kthread_should_stop(),
6398 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399
NeilBrown6c987912011-01-14 09:13:53 +11006400 clear_bit(THREAD_WAKEUP, &thread->flags);
6401 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006402 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006404
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 return 0;
6406}
6407
6408void md_wakeup_thread(mdk_thread_t *thread)
6409{
6410 if (thread) {
6411 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6412 set_bit(THREAD_WAKEUP, &thread->flags);
6413 wake_up(&thread->wqueue);
6414 }
6415}
6416
6417mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6418 const char *name)
6419{
6420 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
NeilBrown9ffae0c2006-01-06 00:20:32 -08006422 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 if (!thread)
6424 return NULL;
6425
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 init_waitqueue_head(&thread->wqueue);
6427
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 thread->run = run;
6429 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006430 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006431 thread->tsk = kthread_run(md_thread, thread,
6432 "%s_%s",
6433 mdname(thread->mddev),
6434 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006435 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 kfree(thread);
6437 return NULL;
6438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 return thread;
6440}
6441
NeilBrown01f96c02011-09-21 15:30:20 +10006442void md_unregister_thread(mdk_thread_t **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443{
NeilBrown01f96c02011-09-21 15:30:20 +10006444 mdk_thread_t *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006445 if (!thread)
6446 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006447 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006448 /* Locking ensures that mddev_unlock does not wake_up a
6449 * non-existent thread
6450 */
6451 spin_lock(&pers_lock);
6452 *threadp = NULL;
6453 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006454
6455 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 kfree(thread);
6457}
6458
6459void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6460{
6461 if (!mddev) {
6462 MD_BUG();
6463 return;
6464 }
6465
NeilBrownb2d444d2005-11-08 21:39:31 -08006466 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006468
NeilBrownde393cd2011-07-28 11:31:48 +10006469 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 return;
6471 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006472 if (mddev->degraded)
6473 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006474 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6476 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6477 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006478 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006479 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006480 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481}
6482
6483/* seq_file implementation /proc/mdstat */
6484
6485static void status_unused(struct seq_file *seq)
6486{
6487 int i = 0;
6488 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489
6490 seq_printf(seq, "unused devices: ");
6491
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006492 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 char b[BDEVNAME_SIZE];
6494 i++;
6495 seq_printf(seq, "%s ",
6496 bdevname(rdev->bdev,b));
6497 }
6498 if (!i)
6499 seq_printf(seq, "<none>");
6500
6501 seq_printf(seq, "\n");
6502}
6503
6504
6505static void status_resync(struct seq_file *seq, mddev_t * mddev)
6506{
NeilBrowndd71cf62009-05-07 12:49:35 +10006507 sector_t max_sectors, resync, res;
6508 unsigned long dt, db;
6509 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006510 int scale;
6511 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512
NeilBrowndd71cf62009-05-07 12:49:35 +10006513 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514
6515 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006516 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006518 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519
6520 /*
6521 * Should not happen.
6522 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006523 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 MD_BUG();
6525 return;
6526 }
NeilBrown4588b422006-03-27 01:18:04 -08006527 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006528 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006529 * u32, as those are the requirements for sector_div.
6530 * Thus 'scale' must be at least 10
6531 */
6532 scale = 10;
6533 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006534 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006535 scale++;
6536 }
6537 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006538 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006539
6540 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 {
NeilBrown4588b422006-03-27 01:18:04 -08006542 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 seq_printf(seq, "[");
6544 for (i = 0; i < x; i++)
6545 seq_printf(seq, "=");
6546 seq_printf(seq, ">");
6547 for (i = 0; i < y; i++)
6548 seq_printf(seq, ".");
6549 seq_printf(seq, "] ");
6550 }
NeilBrown4588b422006-03-27 01:18:04 -08006551 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006552 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6553 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006554 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6555 "check" :
6556 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6557 "resync" : "recovery"))),
6558 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006559 (unsigned long long) resync/2,
6560 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561
6562 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 * dt: time from mark until now
6564 * db: blocks written from mark until now
6565 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006566 *
6567 * rt is a sector_t, so could be 32bit or 64bit.
6568 * So we divide before multiply in case it is 32bit and close
6569 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006570 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006571 * near the end of resync when the number of remaining sectors
6572 * is close to 'db'.
6573 * We then divide rt by 32 after multiplying by db to compensate.
6574 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 */
6576 dt = ((jiffies - mddev->resync_mark) / HZ);
6577 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006578 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6579 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
NeilBrowndd71cf62009-05-07 12:49:35 +10006581 rt = max_sectors - resync; /* number of remaining sectors */
6582 sector_div(rt, db/32+1);
6583 rt *= dt;
6584 rt >>= 5;
6585
6586 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6587 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588
NeilBrownff4e8d92006-07-10 04:44:16 -07006589 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590}
6591
6592static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6593{
6594 struct list_head *tmp;
6595 loff_t l = *pos;
6596 mddev_t *mddev;
6597
6598 if (l >= 0x10000)
6599 return NULL;
6600 if (!l--)
6601 /* header */
6602 return (void*)1;
6603
6604 spin_lock(&all_mddevs_lock);
6605 list_for_each(tmp,&all_mddevs)
6606 if (!l--) {
6607 mddev = list_entry(tmp, mddev_t, all_mddevs);
6608 mddev_get(mddev);
6609 spin_unlock(&all_mddevs_lock);
6610 return mddev;
6611 }
6612 spin_unlock(&all_mddevs_lock);
6613 if (!l--)
6614 return (void*)2;/* tail */
6615 return NULL;
6616}
6617
6618static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6619{
6620 struct list_head *tmp;
6621 mddev_t *next_mddev, *mddev = v;
6622
6623 ++*pos;
6624 if (v == (void*)2)
6625 return NULL;
6626
6627 spin_lock(&all_mddevs_lock);
6628 if (v == (void*)1)
6629 tmp = all_mddevs.next;
6630 else
6631 tmp = mddev->all_mddevs.next;
6632 if (tmp != &all_mddevs)
6633 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6634 else {
6635 next_mddev = (void*)2;
6636 *pos = 0x10000;
6637 }
6638 spin_unlock(&all_mddevs_lock);
6639
6640 if (v != (void*)1)
6641 mddev_put(mddev);
6642 return next_mddev;
6643
6644}
6645
6646static void md_seq_stop(struct seq_file *seq, void *v)
6647{
6648 mddev_t *mddev = v;
6649
6650 if (mddev && v != (void*)1 && v != (void*)2)
6651 mddev_put(mddev);
6652}
6653
6654static int md_seq_show(struct seq_file *seq, void *v)
6655{
6656 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006657 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 mdk_rdev_t *rdev;
NeilBrown32a76272005-06-21 17:17:14 -07006659 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660
6661 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006662 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 seq_printf(seq, "Personalities : ");
6664 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006665 list_for_each_entry(pers, &pers_list, list)
6666 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667
6668 spin_unlock(&pers_lock);
6669 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006670 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 return 0;
6672 }
6673 if (v == (void*)2) {
6674 status_unused(seq);
6675 return 0;
6676 }
6677
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006678 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006680
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6682 seq_printf(seq, "%s : %sactive", mdname(mddev),
6683 mddev->pers ? "" : "in");
6684 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006685 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006687 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006688 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689 seq_printf(seq, " %s", mddev->pers->name);
6690 }
6691
Andre Nolldd8ac332009-03-31 14:33:13 +11006692 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006693 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 char b[BDEVNAME_SIZE];
6695 seq_printf(seq, " %s[%d]",
6696 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006697 if (test_bit(WriteMostly, &rdev->flags))
6698 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006699 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 seq_printf(seq, "(F)");
6701 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006702 } else if (rdev->raid_disk < 0)
6703 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006704 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 }
6706
6707 if (!list_empty(&mddev->disks)) {
6708 if (mddev->pers)
6709 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006710 (unsigned long long)
6711 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 else
6713 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006714 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006716 if (mddev->persistent) {
6717 if (mddev->major_version != 0 ||
6718 mddev->minor_version != 90) {
6719 seq_printf(seq," super %d.%d",
6720 mddev->major_version,
6721 mddev->minor_version);
6722 }
NeilBrowne6910632008-02-06 01:39:51 -08006723 } else if (mddev->external)
6724 seq_printf(seq, " super external:%s",
6725 mddev->metadata_type);
6726 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006727 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728
6729 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006730 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006732 if (mddev->pers->sync_request) {
6733 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006734 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006735 seq_printf(seq, "\n ");
6736 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6737 seq_printf(seq, "\tresync=DELAYED\n ");
6738 else if (mddev->recovery_cp < MaxSector)
6739 seq_printf(seq, "\tresync=PENDING\n ");
6740 }
NeilBrown32a76272005-06-21 17:17:14 -07006741 } else
6742 seq_printf(seq, "\n ");
6743
6744 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006745 unsigned long chunk_kb;
6746 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006747 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006748 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006749 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6750 "%lu%s chunk",
6751 bitmap->pages - bitmap->missing_pages,
6752 bitmap->pages,
6753 (bitmap->pages - bitmap->missing_pages)
6754 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006755 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006756 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006757 if (bitmap->file) {
6758 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006759 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006760 }
NeilBrown78d742d2005-06-21 17:17:15 -07006761
NeilBrown32a76272005-06-21 17:17:14 -07006762 seq_printf(seq, "\n");
6763 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 }
6765
6766 seq_printf(seq, "\n");
6767 }
6768 mddev_unlock(mddev);
6769
6770 return 0;
6771}
6772
Jan Engelhardt110518b2009-05-07 12:49:37 +10006773static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 .start = md_seq_start,
6775 .next = md_seq_next,
6776 .stop = md_seq_stop,
6777 .show = md_seq_show,
6778};
6779
6780static int md_seq_open(struct inode *inode, struct file *file)
6781{
Kay Sieversf1514632011-07-12 20:48:39 +02006782 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 int error;
6784
6785 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006786 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02006787 return error;
6788
6789 seq = file->private_data;
6790 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 return error;
6792}
6793
NeilBrownd7603b72006-01-06 00:20:30 -08006794static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6795{
Kay Sieversf1514632011-07-12 20:48:39 +02006796 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08006797 int mask;
6798
6799 poll_wait(filp, &md_event_waiters, wait);
6800
6801 /* always allow read */
6802 mask = POLLIN | POLLRDNORM;
6803
Kay Sieversf1514632011-07-12 20:48:39 +02006804 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08006805 mask |= POLLERR | POLLPRI;
6806 return mask;
6807}
6808
Arjan van de Venfa027c22007-02-12 00:55:33 -08006809static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006810 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 .open = md_seq_open,
6812 .read = seq_read,
6813 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006814 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006815 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816};
6817
NeilBrown2604b702006-01-06 00:20:36 -08006818int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006821 list_add_tail(&p->list, &pers_list);
6822 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 spin_unlock(&pers_lock);
6824 return 0;
6825}
6826
NeilBrown2604b702006-01-06 00:20:36 -08006827int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828{
NeilBrown2604b702006-01-06 00:20:36 -08006829 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006831 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 spin_unlock(&pers_lock);
6833 return 0;
6834}
6835
NeilBrowneea1bf32009-03-31 14:27:02 +11006836static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837{
6838 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006840 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841
6842 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006843 rcu_read_lock();
6844 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006846 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6847 (int)part_stat_read(&disk->part0, sectors[1]) -
6848 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006849 /* sync IO will cause sync_io to increase before the disk_stats
6850 * as sync_io is counted when a request starts, and
6851 * disk_stats is counted when it completes.
6852 * So resync activity will cause curr_events to be smaller than
6853 * when there was no such activity.
6854 * non-sync IO will cause disk_stat to increase without
6855 * increasing sync_io so curr_events will (eventually)
6856 * be larger than it was before. Once it becomes
6857 * substantially larger, the test below will cause
6858 * the array to appear non-idle, and resync will slow
6859 * down.
6860 * If there is a lot of outstanding resync activity when
6861 * we set last_event to curr_events, then all that activity
6862 * completing might cause the array to appear non-idle
6863 * and resync will be slowed down even though there might
6864 * not have been non-resync activity. This will only
6865 * happen once though. 'last_events' will soon reflect
6866 * the state where there is little or no outstanding
6867 * resync requests, and further resync activity will
6868 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006869 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006871 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872 rdev->last_events = curr_events;
6873 idle = 0;
6874 }
6875 }
NeilBrown4b809912008-07-21 17:05:25 +10006876 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 return idle;
6878}
6879
6880void md_done_sync(mddev_t *mddev, int blocks, int ok)
6881{
6882 /* another "blocks" (512byte) blocks have been synced */
6883 atomic_sub(blocks, &mddev->recovery_active);
6884 wake_up(&mddev->recovery_wait);
6885 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006886 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 md_wakeup_thread(mddev->thread);
6888 // stop recovery, signal do_sync ....
6889 }
6890}
6891
6892
NeilBrown06d91a52005-06-21 17:17:12 -07006893/* md_write_start(mddev, bi)
6894 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006895 * in superblock) before writing, schedule a superblock update
6896 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006897 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006898void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899{
Neil Brown0fd62b82008-06-28 08:31:36 +10006900 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006901 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006902 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006903
NeilBrownf91de922005-11-08 21:39:36 -08006904 BUG_ON(mddev->ro == 1);
6905 if (mddev->ro == 2) {
6906 /* need to switch to read/write */
6907 mddev->ro = 0;
6908 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6909 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006910 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006911 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006912 }
NeilBrown06d91a52005-06-21 17:17:12 -07006913 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006914 if (mddev->safemode == 1)
6915 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006916 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006917 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006918 if (mddev->in_sync) {
6919 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006920 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006921 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006922 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006923 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006924 }
NeilBrowna9701a32005-11-08 21:39:34 -08006925 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006926 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006927 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006928 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006929 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006930 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931}
6932
6933void md_write_end(mddev_t *mddev)
6934{
6935 if (atomic_dec_and_test(&mddev->writes_pending)) {
6936 if (mddev->safemode == 2)
6937 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006938 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6940 }
6941}
6942
NeilBrown2a2275d2007-01-26 00:57:11 -08006943/* md_allow_write(mddev)
6944 * Calling this ensures that the array is marked 'active' so that writes
6945 * may proceed without blocking. It is important to call this before
6946 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6947 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006948 *
6949 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6950 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006951 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006952int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006953{
6954 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006955 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006956 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006957 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006958 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006959 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006960
6961 spin_lock_irq(&mddev->write_lock);
6962 if (mddev->in_sync) {
6963 mddev->in_sync = 0;
6964 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006965 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006966 if (mddev->safemode_delay &&
6967 mddev->safemode == 0)
6968 mddev->safemode = 1;
6969 spin_unlock_irq(&mddev->write_lock);
6970 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006971 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006972 } else
6973 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006974
NeilBrown070dc6d2010-08-30 17:33:34 +10006975 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006976 return -EAGAIN;
6977 else
6978 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006979}
6980EXPORT_SYMBOL_GPL(md_allow_write);
6981
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982#define SYNC_MARKS 10
6983#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006984void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985{
6986 mddev_t *mddev2;
6987 unsigned int currspeed = 0,
6988 window;
NeilBrown57afd892005-06-21 17:17:13 -07006989 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 unsigned long mark[SYNC_MARKS];
6991 sector_t mark_cnt[SYNC_MARKS];
6992 int last_mark,m;
6993 struct list_head *tmp;
6994 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006995 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006996 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006997 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998
6999 /* just incase thread restarts... */
7000 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7001 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007002 if (mddev->ro) /* never try to sync a read-only array */
7003 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004
NeilBrown61df9d92006-10-03 01:15:57 -07007005 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7006 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7007 desc = "data-check";
7008 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7009 desc = "requested-resync";
7010 else
7011 desc = "resync";
7012 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7013 desc = "reshape";
7014 else
7015 desc = "recovery";
7016
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 /* we overload curr_resync somewhat here.
7018 * 0 == not engaged in resync at all
7019 * 2 == checking that there is no conflict with another sync
7020 * 1 == like 2, but have yielded to allow conflicting resync to
7021 * commense
7022 * other == active in resync - this many blocks
7023 *
7024 * Before starting a resync we must have set curr_resync to
7025 * 2, and then checked that every "conflicting" array has curr_resync
7026 * less than ours. When we find one that is the same or higher
7027 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7028 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7029 * This will mean we have to start checking from the beginning again.
7030 *
7031 */
7032
7033 do {
7034 mddev->curr_resync = 2;
7035
7036 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007037 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007038 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007039
7040 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007042 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 if (mddev2 == mddev)
7044 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007045 if (!mddev->parallel_resync
7046 && mddev2->curr_resync
7047 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 DEFINE_WAIT(wq);
7049 if (mddev < mddev2 && mddev->curr_resync == 2) {
7050 /* arbitrarily yield */
7051 mddev->curr_resync = 1;
7052 wake_up(&resync_wait);
7053 }
7054 if (mddev > mddev2 && mddev->curr_resync == 1)
7055 /* no need to wait here, we can wait the next
7056 * time 'round when curr_resync == 2
7057 */
7058 continue;
NeilBrown97441972008-09-19 11:49:54 +10007059 /* We need to wait 'interruptible' so as not to
7060 * contribute to the load average, and not to
7061 * be caught by 'softlockup'
7062 */
7063 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007064 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007065 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007066 printk(KERN_INFO "md: delaying %s of %s"
7067 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007069 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007071 if (signal_pending(current))
7072 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 schedule();
7074 finish_wait(&resync_wait, &wq);
7075 goto try_again;
7076 }
7077 finish_wait(&resync_wait, &wq);
7078 }
7079 }
7080 } while (mddev->curr_resync < 2);
7081
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007082 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007083 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007085 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 */
7087 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007088 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007089 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007090 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7091 j = mddev->resync_min;
7092 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007093 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007094
NeilBrownccfcc3c2006-03-27 01:18:09 -08007095 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11007096 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007097 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007099 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007100 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007101 rcu_read_lock();
7102 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007103 if (rdev->raid_disk >= 0 &&
7104 !test_bit(Faulty, &rdev->flags) &&
7105 !test_bit(In_sync, &rdev->flags) &&
7106 rdev->recovery_offset < j)
7107 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007108 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110
NeilBrown61df9d92006-10-03 01:15:57 -07007111 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7112 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7113 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007114 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007115 "(but not more than %d KB/sec) for %s.\n",
7116 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117
NeilBrowneea1bf32009-03-31 14:27:02 +11007118 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007119
NeilBrown57afd892005-06-21 17:17:13 -07007120 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 for (m = 0; m < SYNC_MARKS; m++) {
7122 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007123 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 }
7125 last_mark = 0;
7126 mddev->resync_mark = mark[last_mark];
7127 mddev->resync_mark_cnt = mark_cnt[last_mark];
7128
7129 /*
7130 * Tune reconstruction:
7131 */
7132 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007133 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7134 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135
7136 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 last_check = 0;
7138
7139 if (j>2) {
7140 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007141 "md: resuming %s of %s from checkpoint.\n",
7142 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 mddev->curr_resync = j;
7144 }
NeilBrown75d3da42011-01-14 09:14:34 +11007145 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
7147 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007148 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
NeilBrown57afd892005-06-21 17:17:13 -07007150 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007151
NeilBrown7a91ee12009-05-26 12:57:21 +10007152 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7153 ((mddev->curr_resync > mddev->curr_resync_completed &&
7154 (mddev->curr_resync - mddev->curr_resync_completed)
7155 > (max_sectors >> 4)) ||
7156 (j - mddev->curr_resync_completed)*2
7157 >= mddev->resync_max - mddev->curr_resync_completed
7158 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007159 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007160 wait_event(mddev->recovery_wait,
7161 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007162 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007163 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007164 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007165 }
NeilBrownacb180b2009-04-14 16:28:34 +10007166
NeilBrowne62e58a2009-07-01 13:15:35 +10007167 while (j >= mddev->resync_max && !kthread_should_stop()) {
7168 /* As this condition is controlled by user-space,
7169 * we can block indefinitely, so use '_interruptible'
7170 * to avoid triggering warnings.
7171 */
7172 flush_signals(current); /* just in case */
7173 wait_event_interruptible(mddev->recovery_wait,
7174 mddev->resync_max > j
7175 || kthread_should_stop());
7176 }
NeilBrownacb180b2009-04-14 16:28:34 +10007177
7178 if (kthread_should_stop())
7179 goto interrupted;
7180
NeilBrown57afd892005-06-21 17:17:13 -07007181 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007182 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007183 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007184 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 goto out;
7186 }
NeilBrown57afd892005-06-21 17:17:13 -07007187
7188 if (!skipped) { /* actual IO requested */
7189 io_sectors += sectors;
7190 atomic_add(sectors, &mddev->recovery_active);
7191 }
7192
NeilBrowne875ece2011-07-28 11:39:24 +10007193 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7194 break;
7195
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 j += sectors;
7197 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007198 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007199 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007200 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007201 * visible in /proc/mdstat
7202 */
7203 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007204
7205 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 continue;
7207
NeilBrown57afd892005-06-21 17:17:13 -07007208 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 repeat:
7210 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7211 /* step marks */
7212 int next = (last_mark+1) % SYNC_MARKS;
7213
7214 mddev->resync_mark = mark[next];
7215 mddev->resync_mark_cnt = mark_cnt[next];
7216 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007217 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 last_mark = next;
7219 }
7220
7221
NeilBrownc6207272008-02-06 01:39:52 -08007222 if (kthread_should_stop())
7223 goto interrupted;
7224
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225
7226 /*
7227 * this loop exits only if either when we are slower than
7228 * the 'hard' speed limit, or the system was IO-idle for
7229 * a jiffy.
7230 * the system might be non-idle CPU-wise, but we only care
7231 * about not overloading the IO subsystem. (things like an
7232 * e2fsck being done on the RAID array should execute fast)
7233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 cond_resched();
7235
NeilBrown57afd892005-06-21 17:17:13 -07007236 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7237 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238
NeilBrown88202a02006-01-06 00:21:36 -08007239 if (currspeed > speed_min(mddev)) {
7240 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007241 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007242 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 goto repeat;
7244 }
7245 }
7246 }
NeilBrown61df9d92006-10-03 01:15:57 -07007247 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 /*
7249 * this also signals 'finished resyncing' to md_stop
7250 */
7251 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7253
7254 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007255 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256
NeilBrowndfc70642008-05-23 13:04:39 -07007257 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007258 mddev->curr_resync > 2) {
7259 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7260 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7261 if (mddev->curr_resync >= mddev->recovery_cp) {
7262 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007263 "md: checkpointing %s of %s.\n",
7264 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007265 mddev->recovery_cp = mddev->curr_resync;
7266 }
7267 } else
7268 mddev->recovery_cp = MaxSector;
7269 } else {
7270 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7271 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007272 rcu_read_lock();
7273 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007274 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007275 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007276 !test_bit(Faulty, &rdev->flags) &&
7277 !test_bit(In_sync, &rdev->flags) &&
7278 rdev->recovery_offset < mddev->curr_resync)
7279 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007280 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 }
NeilBrown17571282006-12-10 02:20:52 -08007283 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007286 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7287 /* We completed so min/max setting can be forgotten if used. */
7288 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7289 mddev->resync_min = 0;
7290 mddev->resync_max = MaxSector;
7291 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7292 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293 mddev->curr_resync = 0;
7294 wake_up(&resync_wait);
7295 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7296 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007297 return;
7298
7299 interrupted:
7300 /*
7301 * got a signal, exit.
7302 */
7303 printk(KERN_INFO
7304 "md: md_do_sync() got signal ... exiting\n");
7305 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7306 goto out;
7307
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308}
NeilBrown29269552006-03-27 01:18:10 -08007309EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007311static int remove_and_add_spares(mddev_t *mddev)
7312{
7313 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007314 int spares = 0;
7315
NeilBrown97e4f422009-03-31 14:33:13 +11007316 mddev->curr_resync_completed = 0;
7317
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007318 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007319 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007320 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007321 (test_bit(Faulty, &rdev->flags) ||
7322 ! test_bit(In_sync, &rdev->flags)) &&
7323 atomic_read(&rdev->nr_pending)==0) {
7324 if (mddev->pers->hot_remove_disk(
7325 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007326 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007327 rdev->raid_disk = -1;
7328 }
7329 }
7330
NeilBrown53890422011-07-27 11:00:36 +10007331 if (mddev->degraded) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007332 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007333 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007334 !test_bit(In_sync, &rdev->flags) &&
NeilBrownde393cd2011-07-28 11:31:48 +10007335 !test_bit(Faulty, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007336 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007337 if (rdev->raid_disk < 0
7338 && !test_bit(Faulty, &rdev->flags)) {
7339 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007340 if (mddev->pers->
7341 hot_add_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007342 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007343 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007344 spares++;
7345 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007346 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007347 } else
7348 break;
7349 }
NeilBrowndfc70642008-05-23 13:04:39 -07007350 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007351 }
7352 return spares;
7353}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007354
7355static void reap_sync_thread(mddev_t *mddev)
7356{
7357 mdk_rdev_t *rdev;
7358
7359 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007360 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007361 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7362 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7363 /* success...*/
7364 /* activate any spares */
7365 if (mddev->pers->spare_active(mddev))
7366 sysfs_notify(&mddev->kobj, NULL,
7367 "degraded");
7368 }
7369 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7370 mddev->pers->finish_reshape)
7371 mddev->pers->finish_reshape(mddev);
7372 md_update_sb(mddev, 1);
7373
7374 /* if array is no-longer degraded, then any saved_raid_disk
7375 * information must be scrapped
7376 */
7377 if (!mddev->degraded)
7378 list_for_each_entry(rdev, &mddev->disks, same_set)
7379 rdev->saved_raid_disk = -1;
7380
7381 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7382 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7383 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7384 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7385 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7386 /* flag recovery needed just to double check */
7387 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7388 sysfs_notify_dirent_safe(mddev->sysfs_action);
7389 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007390 if (mddev->event_work.func)
7391 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007392}
7393
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394/*
7395 * This routine is regularly called by all per-raid-array threads to
7396 * deal with generic issues like resync and super-block update.
7397 * Raid personalities that don't have a thread (linear/raid0) do not
7398 * need this as they never do any recovery or update the superblock.
7399 *
7400 * It does not do any resync itself, but rather "forks" off other threads
7401 * to do that as needed.
7402 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7403 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007404 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 * and wakeups up this thread which will reap the thread and finish up.
7406 * This thread also removes any faulty devices (with nr_pending == 0).
7407 *
7408 * The overall approach is:
7409 * 1/ if the superblock needs updating, update it.
7410 * 2/ If a recovery thread is running, don't do anything else.
7411 * 3/ If recovery has finished, clean up, possibly marking spares active.
7412 * 4/ If there are any faulty devices, remove them.
7413 * 5/ If array is degraded, try to add spares devices
7414 * 6/ If array has spares or is not in-sync, start a resync thread.
7415 */
7416void md_check_recovery(mddev_t *mddev)
7417{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007418 if (mddev->suspended)
7419 return;
7420
NeilBrown5f404022005-06-21 17:17:16 -07007421 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007422 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423
NeilBrownfca4d842005-06-21 17:17:11 -07007424 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007425 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007426 printk(KERN_INFO "md: %s in immediate safe mode\n",
7427 mdname(mddev));
7428 mddev->safemode = 2;
7429 }
7430 flush_signals(current);
7431 }
7432
NeilBrownc89a8ee2008-08-05 15:54:13 +10007433 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7434 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007436 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007438 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007439 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007440 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7441 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442 ))
7443 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007444
NeilBrowndf5b89b2006-03-27 01:18:20 -08007445 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007446 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007447
NeilBrownc89a8ee2008-08-05 15:54:13 +10007448 if (mddev->ro) {
7449 /* Only thing we do on a ro array is remove
7450 * failed devices.
7451 */
NeilBrowna8c42c72011-01-31 13:47:13 +11007452 mdk_rdev_t *rdev;
7453 list_for_each_entry(rdev, &mddev->disks, same_set)
7454 if (rdev->raid_disk >= 0 &&
7455 !test_bit(Blocked, &rdev->flags) &&
7456 test_bit(Faulty, &rdev->flags) &&
7457 atomic_read(&rdev->nr_pending)==0) {
7458 if (mddev->pers->hot_remove_disk(
7459 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007460 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007461 rdev->raid_disk = -1;
7462 }
7463 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007464 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7465 goto unlock;
7466 }
7467
NeilBrown31a59e32008-04-30 00:52:30 -07007468 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007469 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007470 spin_lock_irq(&mddev->write_lock);
7471 if (mddev->safemode &&
7472 !atomic_read(&mddev->writes_pending) &&
7473 !mddev->in_sync &&
7474 mddev->recovery_cp == MaxSector) {
7475 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007476 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007477 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007478 }
7479 if (mddev->safemode == 1)
7480 mddev->safemode = 0;
7481 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007482 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007483 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007484 }
NeilBrownfca4d842005-06-21 17:17:11 -07007485
NeilBrown850b2b42006-10-03 01:15:46 -07007486 if (mddev->flags)
7487 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007488
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7490 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7491 /* resync/recovery still happening */
7492 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7493 goto unlock;
7494 }
7495 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007496 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 goto unlock;
7498 }
Neil Brown72a23c22008-06-28 08:31:41 +10007499 /* Set RUNNING before clearing NEEDED to avoid
7500 * any transients in the value of "sync_action".
7501 */
7502 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7503 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007504 /* Clear some bits that don't mean anything, but
7505 * might be left set
7506 */
NeilBrown24dd4692005-11-08 21:39:26 -08007507 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7508 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007510 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7511 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 /* no recovery is running.
7513 * remove any failed drives, then
7514 * add spares if possible.
7515 * Spare are also removed and re-added, to allow
7516 * the personality to fail the re-add.
7517 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007519 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007520 if (mddev->pers->check_reshape == NULL ||
7521 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007522 /* Cannot proceed */
7523 goto unlock;
7524 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007525 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007526 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007527 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7528 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007529 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007530 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007531 } else if (mddev->recovery_cp < MaxSector) {
7532 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007533 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007534 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7535 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007537
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007539 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7540 /* We are adding a device or devices to an array
7541 * which has the bitmap stored on all devices.
7542 * So make sure all bitmap pages get written
7543 */
7544 bitmap_write_all(mddev->bitmap);
7545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 mddev->sync_thread = md_register_thread(md_do_sync,
7547 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007548 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549 if (!mddev->sync_thread) {
7550 printk(KERN_ERR "%s: could not start resync"
7551 " thread...\n",
7552 mdname(mddev));
7553 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007554 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7555 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7556 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7557 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7558 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007559 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007561 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007562 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563 }
7564 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007565 if (!mddev->sync_thread) {
7566 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7567 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7568 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007569 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007570 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 mddev_unlock(mddev);
7573 }
7574}
7575
Dan Williams6bfe0b42008-04-30 00:52:32 -07007576void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7577{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007578 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007579 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007580 !test_bit(Blocked, &rdev->flags) &&
7581 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007582 msecs_to_jiffies(5000));
7583 rdev_dec_pending(rdev, mddev);
7584}
7585EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7586
NeilBrown2230dfe2011-07-28 11:31:46 +10007587
7588/* Bad block management.
7589 * We can record which blocks on each device are 'bad' and so just
7590 * fail those blocks, or that stripe, rather than the whole device.
7591 * Entries in the bad-block table are 64bits wide. This comprises:
7592 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7593 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7594 * A 'shift' can be set so that larger blocks are tracked and
7595 * consequently larger devices can be covered.
7596 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7597 *
7598 * Locking of the bad-block table uses a seqlock so md_is_badblock
7599 * might need to retry if it is very unlucky.
7600 * We will sometimes want to check for bad blocks in a bi_end_io function,
7601 * so we use the write_seqlock_irq variant.
7602 *
7603 * When looking for a bad block we specify a range and want to
7604 * know if any block in the range is bad. So we binary-search
7605 * to the last range that starts at-or-before the given endpoint,
7606 * (or "before the sector after the target range")
7607 * then see if it ends after the given start.
7608 * We return
7609 * 0 if there are no known bad blocks in the range
7610 * 1 if there are known bad block which are all acknowledged
7611 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7612 * plus the start/length of the first bad section we overlap.
7613 */
7614int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7615 sector_t *first_bad, int *bad_sectors)
7616{
7617 int hi;
7618 int lo = 0;
7619 u64 *p = bb->page;
7620 int rv = 0;
7621 sector_t target = s + sectors;
7622 unsigned seq;
7623
7624 if (bb->shift > 0) {
7625 /* round the start down, and the end up */
7626 s >>= bb->shift;
7627 target += (1<<bb->shift) - 1;
7628 target >>= bb->shift;
7629 sectors = target - s;
7630 }
7631 /* 'target' is now the first block after the bad range */
7632
7633retry:
7634 seq = read_seqbegin(&bb->lock);
7635
7636 hi = bb->count;
7637
7638 /* Binary search between lo and hi for 'target'
7639 * i.e. for the last range that starts before 'target'
7640 */
7641 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7642 * are known not to be the last range before target.
7643 * VARIANT: hi-lo is the number of possible
7644 * ranges, and decreases until it reaches 1
7645 */
7646 while (hi - lo > 1) {
7647 int mid = (lo + hi) / 2;
7648 sector_t a = BB_OFFSET(p[mid]);
7649 if (a < target)
7650 /* This could still be the one, earlier ranges
7651 * could not. */
7652 lo = mid;
7653 else
7654 /* This and later ranges are definitely out. */
7655 hi = mid;
7656 }
7657 /* 'lo' might be the last that started before target, but 'hi' isn't */
7658 if (hi > lo) {
7659 /* need to check all range that end after 's' to see if
7660 * any are unacknowledged.
7661 */
7662 while (lo >= 0 &&
7663 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7664 if (BB_OFFSET(p[lo]) < target) {
7665 /* starts before the end, and finishes after
7666 * the start, so they must overlap
7667 */
7668 if (rv != -1 && BB_ACK(p[lo]))
7669 rv = 1;
7670 else
7671 rv = -1;
7672 *first_bad = BB_OFFSET(p[lo]);
7673 *bad_sectors = BB_LEN(p[lo]);
7674 }
7675 lo--;
7676 }
7677 }
7678
7679 if (read_seqretry(&bb->lock, seq))
7680 goto retry;
7681
7682 return rv;
7683}
7684EXPORT_SYMBOL_GPL(md_is_badblock);
7685
7686/*
7687 * Add a range of bad blocks to the table.
7688 * This might extend the table, or might contract it
7689 * if two adjacent ranges can be merged.
7690 * We binary-search to find the 'insertion' point, then
7691 * decide how best to handle it.
7692 */
7693static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7694 int acknowledged)
7695{
7696 u64 *p;
7697 int lo, hi;
7698 int rv = 1;
7699
7700 if (bb->shift < 0)
7701 /* badblocks are disabled */
7702 return 0;
7703
7704 if (bb->shift) {
7705 /* round the start down, and the end up */
7706 sector_t next = s + sectors;
7707 s >>= bb->shift;
7708 next += (1<<bb->shift) - 1;
7709 next >>= bb->shift;
7710 sectors = next - s;
7711 }
7712
7713 write_seqlock_irq(&bb->lock);
7714
7715 p = bb->page;
7716 lo = 0;
7717 hi = bb->count;
7718 /* Find the last range that starts at-or-before 's' */
7719 while (hi - lo > 1) {
7720 int mid = (lo + hi) / 2;
7721 sector_t a = BB_OFFSET(p[mid]);
7722 if (a <= s)
7723 lo = mid;
7724 else
7725 hi = mid;
7726 }
7727 if (hi > lo && BB_OFFSET(p[lo]) > s)
7728 hi = lo;
7729
7730 if (hi > lo) {
7731 /* we found a range that might merge with the start
7732 * of our new range
7733 */
7734 sector_t a = BB_OFFSET(p[lo]);
7735 sector_t e = a + BB_LEN(p[lo]);
7736 int ack = BB_ACK(p[lo]);
7737 if (e >= s) {
7738 /* Yes, we can merge with a previous range */
7739 if (s == a && s + sectors >= e)
7740 /* new range covers old */
7741 ack = acknowledged;
7742 else
7743 ack = ack && acknowledged;
7744
7745 if (e < s + sectors)
7746 e = s + sectors;
7747 if (e - a <= BB_MAX_LEN) {
7748 p[lo] = BB_MAKE(a, e-a, ack);
7749 s = e;
7750 } else {
7751 /* does not all fit in one range,
7752 * make p[lo] maximal
7753 */
7754 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7755 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7756 s = a + BB_MAX_LEN;
7757 }
7758 sectors = e - s;
7759 }
7760 }
7761 if (sectors && hi < bb->count) {
7762 /* 'hi' points to the first range that starts after 's'.
7763 * Maybe we can merge with the start of that range */
7764 sector_t a = BB_OFFSET(p[hi]);
7765 sector_t e = a + BB_LEN(p[hi]);
7766 int ack = BB_ACK(p[hi]);
7767 if (a <= s + sectors) {
7768 /* merging is possible */
7769 if (e <= s + sectors) {
7770 /* full overlap */
7771 e = s + sectors;
7772 ack = acknowledged;
7773 } else
7774 ack = ack && acknowledged;
7775
7776 a = s;
7777 if (e - a <= BB_MAX_LEN) {
7778 p[hi] = BB_MAKE(a, e-a, ack);
7779 s = e;
7780 } else {
7781 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7782 s = a + BB_MAX_LEN;
7783 }
7784 sectors = e - s;
7785 lo = hi;
7786 hi++;
7787 }
7788 }
7789 if (sectors == 0 && hi < bb->count) {
7790 /* we might be able to combine lo and hi */
7791 /* Note: 's' is at the end of 'lo' */
7792 sector_t a = BB_OFFSET(p[hi]);
7793 int lolen = BB_LEN(p[lo]);
7794 int hilen = BB_LEN(p[hi]);
7795 int newlen = lolen + hilen - (s - a);
7796 if (s >= a && newlen < BB_MAX_LEN) {
7797 /* yes, we can combine them */
7798 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7799 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7800 memmove(p + hi, p + hi + 1,
7801 (bb->count - hi - 1) * 8);
7802 bb->count--;
7803 }
7804 }
7805 while (sectors) {
7806 /* didn't merge (it all).
7807 * Need to add a range just before 'hi' */
7808 if (bb->count >= MD_MAX_BADBLOCKS) {
7809 /* No room for more */
7810 rv = 0;
7811 break;
7812 } else {
7813 int this_sectors = sectors;
7814 memmove(p + hi + 1, p + hi,
7815 (bb->count - hi) * 8);
7816 bb->count++;
7817
7818 if (this_sectors > BB_MAX_LEN)
7819 this_sectors = BB_MAX_LEN;
7820 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7821 sectors -= this_sectors;
7822 s += this_sectors;
7823 }
7824 }
7825
7826 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10007827 if (!acknowledged)
7828 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10007829 write_sequnlock_irq(&bb->lock);
7830
7831 return rv;
7832}
7833
7834int rdev_set_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors,
7835 int acknowledged)
7836{
7837 int rv = md_set_badblocks(&rdev->badblocks,
7838 s + rdev->data_offset, sectors, acknowledged);
7839 if (rv) {
7840 /* Make sure they get written out promptly */
7841 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7842 md_wakeup_thread(rdev->mddev->thread);
7843 }
7844 return rv;
7845}
7846EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7847
7848/*
7849 * Remove a range of bad blocks from the table.
7850 * This may involve extending the table if we spilt a region,
7851 * but it must not fail. So if the table becomes full, we just
7852 * drop the remove request.
7853 */
7854static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7855{
7856 u64 *p;
7857 int lo, hi;
7858 sector_t target = s + sectors;
7859 int rv = 0;
7860
7861 if (bb->shift > 0) {
7862 /* When clearing we round the start up and the end down.
7863 * This should not matter as the shift should align with
7864 * the block size and no rounding should ever be needed.
7865 * However it is better the think a block is bad when it
7866 * isn't than to think a block is not bad when it is.
7867 */
7868 s += (1<<bb->shift) - 1;
7869 s >>= bb->shift;
7870 target >>= bb->shift;
7871 sectors = target - s;
7872 }
7873
7874 write_seqlock_irq(&bb->lock);
7875
7876 p = bb->page;
7877 lo = 0;
7878 hi = bb->count;
7879 /* Find the last range that starts before 'target' */
7880 while (hi - lo > 1) {
7881 int mid = (lo + hi) / 2;
7882 sector_t a = BB_OFFSET(p[mid]);
7883 if (a < target)
7884 lo = mid;
7885 else
7886 hi = mid;
7887 }
7888 if (hi > lo) {
7889 /* p[lo] is the last range that could overlap the
7890 * current range. Earlier ranges could also overlap,
7891 * but only this one can overlap the end of the range.
7892 */
7893 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
7894 /* Partial overlap, leave the tail of this range */
7895 int ack = BB_ACK(p[lo]);
7896 sector_t a = BB_OFFSET(p[lo]);
7897 sector_t end = a + BB_LEN(p[lo]);
7898
7899 if (a < s) {
7900 /* we need to split this range */
7901 if (bb->count >= MD_MAX_BADBLOCKS) {
7902 rv = 0;
7903 goto out;
7904 }
7905 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
7906 bb->count++;
7907 p[lo] = BB_MAKE(a, s-a, ack);
7908 lo++;
7909 }
7910 p[lo] = BB_MAKE(target, end - target, ack);
7911 /* there is no longer an overlap */
7912 hi = lo;
7913 lo--;
7914 }
7915 while (lo >= 0 &&
7916 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7917 /* This range does overlap */
7918 if (BB_OFFSET(p[lo]) < s) {
7919 /* Keep the early parts of this range. */
7920 int ack = BB_ACK(p[lo]);
7921 sector_t start = BB_OFFSET(p[lo]);
7922 p[lo] = BB_MAKE(start, s - start, ack);
7923 /* now low doesn't overlap, so.. */
7924 break;
7925 }
7926 lo--;
7927 }
7928 /* 'lo' is strictly before, 'hi' is strictly after,
7929 * anything between needs to be discarded
7930 */
7931 if (hi - lo > 1) {
7932 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
7933 bb->count -= (hi - lo - 1);
7934 }
7935 }
7936
7937 bb->changed = 1;
7938out:
7939 write_sequnlock_irq(&bb->lock);
7940 return rv;
7941}
7942
7943int rdev_clear_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors)
7944{
7945 return md_clear_badblocks(&rdev->badblocks,
7946 s + rdev->data_offset,
7947 sectors);
7948}
7949EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
7950
7951/*
7952 * Acknowledge all bad blocks in a list.
7953 * This only succeeds if ->changed is clear. It is used by
7954 * in-kernel metadata updates
7955 */
7956void md_ack_all_badblocks(struct badblocks *bb)
7957{
7958 if (bb->page == NULL || bb->changed)
7959 /* no point even trying */
7960 return;
7961 write_seqlock_irq(&bb->lock);
7962
7963 if (bb->changed == 0) {
7964 u64 *p = bb->page;
7965 int i;
7966 for (i = 0; i < bb->count ; i++) {
7967 if (!BB_ACK(p[i])) {
7968 sector_t start = BB_OFFSET(p[i]);
7969 int len = BB_LEN(p[i]);
7970 p[i] = BB_MAKE(start, len, 1);
7971 }
7972 }
NeilBrownde393cd2011-07-28 11:31:48 +10007973 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10007974 }
7975 write_sequnlock_irq(&bb->lock);
7976}
7977EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
7978
NeilBrown16c791a2011-07-28 11:31:47 +10007979/* sysfs access to bad-blocks list.
7980 * We present two files.
7981 * 'bad-blocks' lists sector numbers and lengths of ranges that
7982 * are recorded as bad. The list is truncated to fit within
7983 * the one-page limit of sysfs.
7984 * Writing "sector length" to this file adds an acknowledged
7985 * bad block list.
7986 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
7987 * been acknowledged. Writing to this file adds bad blocks
7988 * without acknowledging them. This is largely for testing.
7989 */
7990
7991static ssize_t
7992badblocks_show(struct badblocks *bb, char *page, int unack)
7993{
7994 size_t len;
7995 int i;
7996 u64 *p = bb->page;
7997 unsigned seq;
7998
7999 if (bb->shift < 0)
8000 return 0;
8001
8002retry:
8003 seq = read_seqbegin(&bb->lock);
8004
8005 len = 0;
8006 i = 0;
8007
8008 while (len < PAGE_SIZE && i < bb->count) {
8009 sector_t s = BB_OFFSET(p[i]);
8010 unsigned int length = BB_LEN(p[i]);
8011 int ack = BB_ACK(p[i]);
8012 i++;
8013
8014 if (unack && ack)
8015 continue;
8016
8017 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8018 (unsigned long long)s << bb->shift,
8019 length << bb->shift);
8020 }
NeilBrownde393cd2011-07-28 11:31:48 +10008021 if (unack && len == 0)
8022 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008023
8024 if (read_seqretry(&bb->lock, seq))
8025 goto retry;
8026
8027 return len;
8028}
8029
8030#define DO_DEBUG 1
8031
8032static ssize_t
8033badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8034{
8035 unsigned long long sector;
8036 int length;
8037 char newline;
8038#ifdef DO_DEBUG
8039 /* Allow clearing via sysfs *only* for testing/debugging.
8040 * Normally only a successful write may clear a badblock
8041 */
8042 int clear = 0;
8043 if (page[0] == '-') {
8044 clear = 1;
8045 page++;
8046 }
8047#endif /* DO_DEBUG */
8048
8049 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8050 case 3:
8051 if (newline != '\n')
8052 return -EINVAL;
8053 case 2:
8054 if (length <= 0)
8055 return -EINVAL;
8056 break;
8057 default:
8058 return -EINVAL;
8059 }
8060
8061#ifdef DO_DEBUG
8062 if (clear) {
8063 md_clear_badblocks(bb, sector, length);
8064 return len;
8065 }
8066#endif /* DO_DEBUG */
8067 if (md_set_badblocks(bb, sector, length, !unack))
8068 return len;
8069 else
8070 return -ENOSPC;
8071}
8072
Adrian Bunk75c96f82005-05-05 16:16:09 -07008073static int md_notify_reboot(struct notifier_block *this,
8074 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075{
8076 struct list_head *tmp;
8077 mddev_t *mddev;
8078
8079 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
8080
8081 printk(KERN_INFO "md: stopping all md devices.\n");
8082
NeilBrown29ac4aa2008-02-06 01:39:58 -08008083 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10008084 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10008085 /* Force a switch to readonly even array
8086 * appears to still be in use. Hence
8087 * the '100'.
8088 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11008089 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10008090 mddev_unlock(mddev);
8091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092 /*
8093 * certain more exotic SCSI devices are known to be
8094 * volatile wrt too early system reboots. While the
8095 * right place to handle this issue is the given
8096 * driver, we do want to have a safe RAID driver ...
8097 */
8098 mdelay(1000*1);
8099 }
8100 return NOTIFY_DONE;
8101}
8102
Adrian Bunk75c96f82005-05-05 16:16:09 -07008103static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008104 .notifier_call = md_notify_reboot,
8105 .next = NULL,
8106 .priority = INT_MAX, /* before any real devices */
8107};
8108
8109static void md_geninit(void)
8110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008111 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
8112
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008113 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114}
8115
Adrian Bunk75c96f82005-05-05 16:16:09 -07008116static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117{
Tejun Heoe804ac72010-10-15 15:36:08 +02008118 int ret = -ENOMEM;
8119
Tejun Heoada609e2011-01-25 14:35:54 +01008120 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008121 if (!md_wq)
8122 goto err_wq;
8123
8124 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8125 if (!md_misc_wq)
8126 goto err_misc_wq;
8127
8128 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8129 goto err_md;
8130
8131 if ((ret = register_blkdev(0, "mdp")) < 0)
8132 goto err_mdp;
8133 mdp_major = ret;
8134
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008135 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008136 md_probe, NULL, NULL);
8137 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138 md_probe, NULL, NULL);
8139
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008141 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142
8143 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145
Tejun Heoe804ac72010-10-15 15:36:08 +02008146err_mdp:
8147 unregister_blkdev(MD_MAJOR, "md");
8148err_md:
8149 destroy_workqueue(md_misc_wq);
8150err_misc_wq:
8151 destroy_workqueue(md_wq);
8152err_wq:
8153 return ret;
8154}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155
8156#ifndef MODULE
8157
8158/*
8159 * Searches all registered partitions for autorun RAID arrays
8160 * at boot time.
8161 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008162
8163static LIST_HEAD(all_detected_devices);
8164struct detected_devices_node {
8165 struct list_head list;
8166 dev_t dev;
8167};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168
8169void md_autodetect_dev(dev_t dev)
8170{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008171 struct detected_devices_node *node_detected_dev;
8172
8173 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8174 if (node_detected_dev) {
8175 node_detected_dev->dev = dev;
8176 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8177 } else {
8178 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8179 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181}
8182
8183
8184static void autostart_arrays(int part)
8185{
8186 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008187 struct detected_devices_node *node_detected_dev;
8188 dev_t dev;
8189 int i_scanned, i_passed;
8190
8191 i_scanned = 0;
8192 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193
8194 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8195
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008196 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8197 i_scanned++;
8198 node_detected_dev = list_entry(all_detected_devices.next,
8199 struct detected_devices_node, list);
8200 list_del(&node_detected_dev->list);
8201 dev = node_detected_dev->dev;
8202 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008203 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008204 if (IS_ERR(rdev))
8205 continue;
8206
NeilBrownb2d444d2005-11-08 21:39:31 -08008207 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008208 MD_BUG();
8209 continue;
8210 }
NeilBrownd0fae182008-03-04 14:29:31 -08008211 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008213 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008215
8216 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8217 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218
8219 autorun_devices(part);
8220}
8221
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008222#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008223
8224static __exit void md_exit(void)
8225{
8226 mddev_t *mddev;
8227 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008228
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008229 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008230 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008232 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233 unregister_blkdev(mdp_major, "mdp");
8234 unregister_reboot_notifier(&md_notifier);
8235 unregister_sysctl_table(raid_table_header);
8236 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008237 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008239 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008241 destroy_workqueue(md_misc_wq);
8242 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243}
8244
Dan Williams685784a2007-07-09 11:56:42 -07008245subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246module_exit(md_exit)
8247
NeilBrownf91de922005-11-08 21:39:36 -08008248static int get_ro(char *buffer, struct kernel_param *kp)
8249{
8250 return sprintf(buffer, "%d", start_readonly);
8251}
8252static int set_ro(const char *val, struct kernel_param *kp)
8253{
8254 char *e;
8255 int num = simple_strtoul(val, &e, 10);
8256 if (*val && (*e == '\0' || *e == '\n')) {
8257 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008258 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008259 }
8260 return -EINVAL;
8261}
8262
NeilBrown80ca3a42006-07-10 04:44:18 -07008263module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8264module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008265
NeilBrownefeb53c2009-01-09 08:31:10 +11008266module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008267
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268EXPORT_SYMBOL(register_md_personality);
8269EXPORT_SYMBOL(unregister_md_personality);
8270EXPORT_SYMBOL(md_error);
8271EXPORT_SYMBOL(md_done_sync);
8272EXPORT_SYMBOL(md_write_start);
8273EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274EXPORT_SYMBOL(md_register_thread);
8275EXPORT_SYMBOL(md_unregister_thread);
8276EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277EXPORT_SYMBOL(md_check_recovery);
8278MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008279MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008280MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008281MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);