blob: e1f3c1715cca2ae459f0a70d7229128685ee2d70 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110042#include <linux/hdreg.h>
43#include <linux/proc_fs.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070046#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110047#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110048#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110049#include <linux/raid/md_p.h>
50#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110051#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110052#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define DEBUG 0
55#define dprintk(x...) ((void)(DEBUG && printk(x)))
56
57
58#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110059static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
NeilBrown2604b702006-01-06 00:20:36 -080062static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static DEFINE_SPINLOCK(pers_lock);
64
Adrian Bunk5e563412006-06-26 00:27:42 -070065static void md_print_devices(void);
66
Bernd Schubert90b08712008-05-23 13:04:38 -070067static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
Robert Becker1e509152009-12-14 12:49:58 +110072 * Default number of read corrections we'll attempt on an rdev
73 * before ejecting it from the array. We divide the read error
74 * count by 2 for every hour elapsed between read errors.
75 */
76#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
77/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
79 * is 1000 KB/sec, so the extra system load does not show up that much.
80 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070081 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * subsystem is idle. There is also an 'absolute maximum' reconstruction
83 * speed limit - in case reconstruction slows down your system despite
84 * idle IO detection.
85 *
86 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080087 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
89
90static int sysctl_speed_limit_min = 1000;
91static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080092static inline int speed_min(mddev_t *mddev)
93{
94 return mddev->sync_speed_min ?
95 mddev->sync_speed_min : sysctl_speed_limit_min;
96}
97
98static inline int speed_max(mddev_t *mddev)
99{
100 return mddev->sync_speed_max ?
101 mddev->sync_speed_max : sysctl_speed_limit_max;
102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104static struct ctl_table_header *raid_table_header;
105
106static ctl_table raid_table[] = {
107 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .procname = "speed_limit_min",
109 .data = &sysctl_speed_limit_min,
110 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700111 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800112 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 },
114 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .procname = "speed_limit_max",
116 .data = &sysctl_speed_limit_max,
117 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700118 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800119 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800121 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
124static ctl_table raid_dir_table[] = {
125 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .procname = "raid",
127 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700128 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .child = raid_table,
130 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800131 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
134static ctl_table raid_root_table[] = {
135 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .procname = "dev",
137 .maxlen = 0,
138 .mode = 0555,
139 .child = raid_dir_table,
140 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800141 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700144static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
NeilBrownf91de922005-11-08 21:39:36 -0800146static int start_readonly;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
NeilBrownd7603b72006-01-06 00:20:30 -0800149 * We have a system wide 'event count' that is incremented
150 * on any 'interesting' event, and readers of /proc/mdstat
151 * can use 'poll' or 'select' to find out when the event
152 * count increases.
153 *
154 * Events are:
155 * start array, stop array, error, add device, remove device,
156 * start build, activate spare
157 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800158static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800159static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800160void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800161{
162 atomic_inc(&md_event_count);
163 wake_up(&md_event_waiters);
164}
NeilBrown29269552006-03-27 01:18:10 -0800165EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800166
NeilBrownc331eb02006-05-30 21:27:13 -0700167/* Alternate version that can be called from interrupts
168 * when calling sysfs_notify isn't needed.
169 */
Adrian Bunk05381952006-06-26 00:28:01 -0700170static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700171{
172 atomic_inc(&md_event_count);
173 wake_up(&md_event_waiters);
174}
175
NeilBrownd7603b72006-01-06 00:20:30 -0800176/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * Enables to iterate over all existing md arrays
178 * all_mddevs_lock protects this list.
179 */
180static LIST_HEAD(all_mddevs);
181static DEFINE_SPINLOCK(all_mddevs_lock);
182
183
184/*
185 * iterates through all used mddevs in the system.
186 * We take care to grab the all_mddevs_lock whenever navigating
187 * the list, and to always hold a refcount when unlocked.
188 * Any code which breaks out of this loop while own
189 * a reference to the current mddev and must mddev_put it.
190 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800191#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 \
193 for (({ spin_lock(&all_mddevs_lock); \
194 tmp = all_mddevs.next; \
195 mddev = NULL;}); \
196 ({ if (tmp != &all_mddevs) \
197 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
198 spin_unlock(&all_mddevs_lock); \
199 if (mddev) mddev_put(mddev); \
200 mddev = list_entry(tmp, mddev_t, all_mddevs); \
201 tmp != &all_mddevs;}); \
202 ({ spin_lock(&all_mddevs_lock); \
203 tmp = tmp->next;}) \
204 )
205
206
NeilBrown409c57f2009-03-31 14:39:39 +1100207/* Rather than calling directly into the personality make_request function,
208 * IO requests come here first so that we can check if the device is
209 * being suspended pending a reconfiguration.
210 * We hold a refcount over the call to ->make_request. By the time that
211 * call has finished, the bio has been linked into some internal structure
212 * and so is visible to ->quiesce(), so we don't need the refcount any more.
213 */
214static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
NeilBrown409c57f2009-03-31 14:39:39 +1100216 mddev_t *mddev = q->queuedata;
217 int rv;
218 if (mddev == NULL || mddev->pers == NULL) {
219 bio_io_error(bio);
220 return 0;
221 }
222 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100223 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100224 DEFINE_WAIT(__wait);
225 for (;;) {
226 prepare_to_wait(&mddev->sb_wait, &__wait,
227 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100228 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100229 break;
230 rcu_read_unlock();
231 schedule();
232 rcu_read_lock();
233 }
234 finish_wait(&mddev->sb_wait, &__wait);
235 }
236 atomic_inc(&mddev->active_io);
237 rcu_read_unlock();
238 rv = mddev->pers->make_request(q, bio);
239 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
240 wake_up(&mddev->sb_wait);
241
242 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
NeilBrown409c57f2009-03-31 14:39:39 +1100245static void mddev_suspend(mddev_t *mddev)
246{
247 BUG_ON(mddev->suspended);
248 mddev->suspended = 1;
249 synchronize_rcu();
250 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
251 mddev->pers->quiesce(mddev, 1);
252 md_unregister_thread(mddev->thread);
253 mddev->thread = NULL;
254 /* we now know that no code is executing in the personality module,
255 * except possibly the tail end of a ->bi_end_io function, but that
256 * is certain to complete before the module has a chance to get
257 * unloaded
258 */
259}
260
261static void mddev_resume(mddev_t *mddev)
262{
263 mddev->suspended = 0;
264 wake_up(&mddev->sb_wait);
265 mddev->pers->quiesce(mddev, 0);
266}
267
NeilBrown3fa841d2009-09-23 18:10:29 +1000268int mddev_congested(mddev_t *mddev, int bits)
269{
NeilBrowna2826aa2009-12-14 12:49:49 +1100270 if (mddev->barrier)
271 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000272 return mddev->suspended;
273}
274EXPORT_SYMBOL(mddev_congested);
275
NeilBrowna2826aa2009-12-14 12:49:49 +1100276/*
277 * Generic barrier handling for md
278 */
279
280#define POST_REQUEST_BARRIER ((void*)1)
281
282static void md_end_barrier(struct bio *bio, int err)
283{
284 mdk_rdev_t *rdev = bio->bi_private;
285 mddev_t *mddev = rdev->mddev;
286 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
287 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
288
289 rdev_dec_pending(rdev, mddev);
290
291 if (atomic_dec_and_test(&mddev->flush_pending)) {
292 if (mddev->barrier == POST_REQUEST_BARRIER) {
293 /* This was a post-request barrier */
294 mddev->barrier = NULL;
295 wake_up(&mddev->sb_wait);
296 } else
297 /* The pre-request barrier has finished */
298 schedule_work(&mddev->barrier_work);
299 }
300 bio_put(bio);
301}
302
303static void submit_barriers(mddev_t *mddev)
304{
305 mdk_rdev_t *rdev;
306
307 rcu_read_lock();
308 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
309 if (rdev->raid_disk >= 0 &&
310 !test_bit(Faulty, &rdev->flags)) {
311 /* Take two references, one is dropped
312 * when request finishes, one after
313 * we reclaim rcu_read_lock
314 */
315 struct bio *bi;
316 atomic_inc(&rdev->nr_pending);
317 atomic_inc(&rdev->nr_pending);
318 rcu_read_unlock();
319 bi = bio_alloc(GFP_KERNEL, 0);
320 bi->bi_end_io = md_end_barrier;
321 bi->bi_private = rdev;
322 bi->bi_bdev = rdev->bdev;
323 atomic_inc(&mddev->flush_pending);
324 submit_bio(WRITE_BARRIER, bi);
325 rcu_read_lock();
326 rdev_dec_pending(rdev, mddev);
327 }
328 rcu_read_unlock();
329}
330
331static void md_submit_barrier(struct work_struct *ws)
332{
333 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
334 struct bio *bio = mddev->barrier;
335
336 atomic_set(&mddev->flush_pending, 1);
337
338 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
339 bio_endio(bio, -EOPNOTSUPP);
340 else if (bio->bi_size == 0)
341 /* an empty barrier - all done */
342 bio_endio(bio, 0);
343 else {
344 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
345 if (mddev->pers->make_request(mddev->queue, bio))
346 generic_make_request(bio);
347 mddev->barrier = POST_REQUEST_BARRIER;
348 submit_barriers(mddev);
349 }
350 if (atomic_dec_and_test(&mddev->flush_pending)) {
351 mddev->barrier = NULL;
352 wake_up(&mddev->sb_wait);
353 }
354}
355
356void md_barrier_request(mddev_t *mddev, struct bio *bio)
357{
358 spin_lock_irq(&mddev->write_lock);
359 wait_event_lock_irq(mddev->sb_wait,
360 !mddev->barrier,
361 mddev->write_lock, /*nothing*/);
362 mddev->barrier = bio;
363 spin_unlock_irq(&mddev->write_lock);
364
365 atomic_set(&mddev->flush_pending, 1);
366 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
367
368 submit_barriers(mddev);
369
370 if (atomic_dec_and_test(&mddev->flush_pending))
371 schedule_work(&mddev->barrier_work);
372}
373EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375static inline mddev_t *mddev_get(mddev_t *mddev)
376{
377 atomic_inc(&mddev->active);
378 return mddev;
379}
380
Dan Williams5fd3a172009-03-04 00:57:25 -0700381static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static void mddev_put(mddev_t *mddev)
384{
385 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
386 return;
NeilBrownd3374822009-01-09 08:31:10 +1100387 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
388 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100390 if (mddev->gendisk) {
391 /* we did a probe so need to clean up.
392 * Call schedule_work inside the spinlock
393 * so that flush_scheduled_work() after
394 * mddev_find will succeed in waiting for the
395 * work to be done.
396 */
397 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
398 schedule_work(&mddev->del_work);
399 } else
400 kfree(mddev);
401 }
402 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405static mddev_t * mddev_find(dev_t unit)
406{
407 mddev_t *mddev, *new = NULL;
408
409 retry:
410 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
NeilBrownefeb53c2009-01-09 08:31:10 +1100412 if (unit) {
413 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
414 if (mddev->unit == unit) {
415 mddev_get(mddev);
416 spin_unlock(&all_mddevs_lock);
417 kfree(new);
418 return mddev;
419 }
420
421 if (new) {
422 list_add(&new->all_mddevs, &all_mddevs);
423 spin_unlock(&all_mddevs_lock);
424 new->hold_active = UNTIL_IOCTL;
425 return new;
426 }
427 } else if (new) {
428 /* find an unused unit number */
429 static int next_minor = 512;
430 int start = next_minor;
431 int is_free = 0;
432 int dev = 0;
433 while (!is_free) {
434 dev = MKDEV(MD_MAJOR, next_minor);
435 next_minor++;
436 if (next_minor > MINORMASK)
437 next_minor = 0;
438 if (next_minor == start) {
439 /* Oh dear, all in use. */
440 spin_unlock(&all_mddevs_lock);
441 kfree(new);
442 return NULL;
443 }
444
445 is_free = 1;
446 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
447 if (mddev->unit == dev) {
448 is_free = 0;
449 break;
450 }
451 }
452 new->unit = dev;
453 new->md_minor = MINOR(dev);
454 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 list_add(&new->all_mddevs, &all_mddevs);
456 spin_unlock(&all_mddevs_lock);
457 return new;
458 }
459 spin_unlock(&all_mddevs_lock);
460
NeilBrown9ffae0c2006-01-06 00:20:32 -0800461 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!new)
463 return NULL;
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 new->unit = unit;
466 if (MAJOR(unit) == MD_MAJOR)
467 new->md_minor = MINOR(unit);
468 else
469 new->md_minor = MINOR(unit) >> MdpMinorShift;
470
NeilBrownc8c00a62009-08-10 12:50:52 +1000471 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800472 mutex_init(&new->reconfig_mutex);
NeilBrownc3d97142009-12-14 12:49:52 +1100473 mutex_init(&new->bitmap_info.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 INIT_LIST_HEAD(&new->disks);
475 INIT_LIST_HEAD(&new->all_mddevs);
476 init_timer(&new->safemode_timer);
477 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000478 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100479 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700480 spin_lock_init(&new->write_lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100481 atomic_set(&new->flush_pending, 0);
NeilBrown3d310eb2005-06-21 17:17:26 -0700482 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700483 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700484 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000485 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800486 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700487 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto retry;
490}
491
492static inline int mddev_lock(mddev_t * mddev)
493{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800494 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Dan Williamsb522adc2009-03-31 15:00:31 +1100497static inline int mddev_is_locked(mddev_t *mddev)
498{
499 return mutex_is_locked(&mddev->reconfig_mutex);
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502static inline int mddev_trylock(mddev_t * mddev)
503{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800504 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507static inline void mddev_unlock(mddev_t * mddev)
508{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800509 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
NeilBrown005eca52005-08-22 13:11:08 -0700511 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
NeilBrown2989ddb2006-01-06 00:20:43 -0800514static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100516 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100518 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (rdev->desc_nr == nr)
520 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return NULL;
523}
524
525static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
526{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 mdk_rdev_t *rdev;
528
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100529 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (rdev->bdev->bd_dev == dev)
531 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return NULL;
534}
535
NeilBrownd9d166c2006-01-06 00:20:51 -0800536static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800537{
538 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800539 list_for_each_entry(pers, &pers_list, list) {
540 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800541 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800542 if (strcmp(pers->name, clevel)==0)
543 return pers;
544 }
NeilBrown2604b702006-01-06 00:20:36 -0800545 return NULL;
546}
547
Andre Nollb73df2d2008-07-11 22:02:23 +1000548/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700549static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Andre Nollb73df2d2008-07-11 22:02:23 +1000551 sector_t num_sectors = bdev->bd_inode->i_size / 512;
552 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static int alloc_disk_sb(mdk_rdev_t * rdev)
556{
557 if (rdev->sb_page)
558 MD_BUG();
559
560 rdev->sb_page = alloc_page(GFP_KERNEL);
561 if (!rdev->sb_page) {
562 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000563 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
566 return 0;
567}
568
569static void free_disk_sb(mdk_rdev_t * rdev)
570{
571 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800572 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 rdev->sb_loaded = 0;
574 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000575 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100576 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578}
579
580
NeilBrown6712ecf2007-09-27 12:47:43 +0200581static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700582{
583 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800584 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700585
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700586 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
587 printk("md: super_written gets error=%d, uptodate=%d\n",
588 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
589 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800590 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700591 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700592
NeilBrowna9701a32005-11-08 21:39:34 -0800593 if (atomic_dec_and_test(&mddev->pending_writes))
594 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700595 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700596}
597
NeilBrown6712ecf2007-09-27 12:47:43 +0200598static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800599{
600 struct bio *bio2 = bio->bi_private;
601 mdk_rdev_t *rdev = bio2->bi_private;
602 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800603
604 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
605 error == -EOPNOTSUPP) {
606 unsigned long flags;
607 /* barriers don't appear to be supported :-( */
608 set_bit(BarriersNotsupp, &rdev->flags);
609 mddev->barriers_work = 0;
610 spin_lock_irqsave(&mddev->write_lock, flags);
611 bio2->bi_next = mddev->biolist;
612 mddev->biolist = bio2;
613 spin_unlock_irqrestore(&mddev->write_lock, flags);
614 wake_up(&mddev->sb_wait);
615 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200616 } else {
617 bio_put(bio2);
618 bio->bi_private = rdev;
619 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800620 }
NeilBrowna9701a32005-11-08 21:39:34 -0800621}
622
NeilBrown7bfa19f2005-06-21 17:17:28 -0700623void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
624 sector_t sector, int size, struct page *page)
625{
626 /* write first size bytes of page to sector of rdev
627 * Increment mddev->pending_writes before returning
628 * and decrement it on completion, waking up sb_wait
629 * if zero is reached.
630 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800631 *
632 * As we might need to resubmit the request if BIO_RW_BARRIER
633 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700634 */
635 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100636 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700637
638 bio->bi_bdev = rdev->bdev;
639 bio->bi_sector = sector;
640 bio_add_page(bio, page, size, 0);
641 bio->bi_private = rdev;
642 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800643 bio->bi_rw = rw;
644
NeilBrown7bfa19f2005-06-21 17:17:28 -0700645 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800646 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
647 struct bio *rbio;
648 rw |= (1<<BIO_RW_BARRIER);
649 rbio = bio_clone(bio, GFP_NOIO);
650 rbio->bi_private = bio;
651 rbio->bi_end_io = super_written_barrier;
652 submit_bio(rw, rbio);
653 } else
654 submit_bio(rw, bio);
655}
656
657void md_super_wait(mddev_t *mddev)
658{
659 /* wait for all superblock writes that were scheduled to complete.
660 * if any had to be retried (due to BARRIER problems), retry them
661 */
662 DEFINE_WAIT(wq);
663 for(;;) {
664 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
665 if (atomic_read(&mddev->pending_writes)==0)
666 break;
667 while (mddev->biolist) {
668 struct bio *bio;
669 spin_lock_irq(&mddev->write_lock);
670 bio = mddev->biolist;
671 mddev->biolist = bio->bi_next ;
672 bio->bi_next = NULL;
673 spin_unlock_irq(&mddev->write_lock);
674 submit_bio(bio->bi_rw, bio);
675 }
676 schedule();
677 }
678 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700679}
680
NeilBrown6712ecf2007-09-27 12:47:43 +0200681static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
NeilBrowna654b9d82005-06-21 17:17:27 -0700686int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct page *page, int rw)
688{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700689 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct completion event;
691 int ret;
692
Jens Axboe93dbb392009-02-16 10:25:40 +0100693 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 bio->bi_bdev = bdev;
696 bio->bi_sector = sector;
697 bio_add_page(bio, page, size, 0);
698 init_completion(&event);
699 bio->bi_private = &event;
700 bio->bi_end_io = bi_complete;
701 submit_bio(rw, bio);
702 wait_for_completion(&event);
703
704 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
705 bio_put(bio);
706 return ret;
707}
NeilBrowna8745db2006-01-06 00:20:34 -0800708EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
NeilBrown0002b272005-09-09 16:23:53 -0700710static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 char b[BDEVNAME_SIZE];
713 if (!rdev->sb_page) {
714 MD_BUG();
715 return -EINVAL;
716 }
717 if (rdev->sb_loaded)
718 return 0;
719
720
Andre Noll0f420352008-07-11 22:02:23 +1000721 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 goto fail;
723 rdev->sb_loaded = 1;
724 return 0;
725
726fail:
727 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
728 bdevname(rdev->bdev,b));
729 return -EINVAL;
730}
731
732static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
733{
Andre Noll05710462008-07-11 22:02:20 +1000734 return sb1->set_uuid0 == sb2->set_uuid0 &&
735 sb1->set_uuid1 == sb2->set_uuid1 &&
736 sb1->set_uuid2 == sb2->set_uuid2 &&
737 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
741{
742 int ret;
743 mdp_super_t *tmp1, *tmp2;
744
745 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
746 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
747
748 if (!tmp1 || !tmp2) {
749 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100750 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 goto abort;
752 }
753
754 *tmp1 = *sb1;
755 *tmp2 = *sb2;
756
757 /*
758 * nr_disks is not constant
759 */
760 tmp1->nr_disks = 0;
761 tmp2->nr_disks = 0;
762
Andre Nollce0c8e02008-07-11 22:02:20 +1000763 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700765 kfree(tmp1);
766 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return ret;
768}
769
NeilBrown4d167f02007-05-09 02:35:37 -0700770
771static u32 md_csum_fold(u32 csum)
772{
773 csum = (csum & 0xffff) + (csum >> 16);
774 return (csum & 0xffff) + (csum >> 16);
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static unsigned int calc_sb_csum(mdp_super_t * sb)
778{
NeilBrown4d167f02007-05-09 02:35:37 -0700779 u64 newcsum = 0;
780 u32 *sb32 = (u32*)sb;
781 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 unsigned int disk_csum, csum;
783
784 disk_csum = sb->sb_csum;
785 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700786
787 for (i = 0; i < MD_SB_BYTES/4 ; i++)
788 newcsum += sb32[i];
789 csum = (newcsum & 0xffffffff) + (newcsum>>32);
790
791
792#ifdef CONFIG_ALPHA
793 /* This used to use csum_partial, which was wrong for several
794 * reasons including that different results are returned on
795 * different architectures. It isn't critical that we get exactly
796 * the same return value as before (we always csum_fold before
797 * testing, and that removes any differences). However as we
798 * know that csum_partial always returned a 16bit value on
799 * alphas, do a fold to maximise conformity to previous behaviour.
800 */
801 sb->sb_csum = md_csum_fold(disk_csum);
802#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700804#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return csum;
806}
807
808
809/*
810 * Handle superblock details.
811 * We want to be able to handle multiple superblock formats
812 * so we have a common interface to them all, and an array of
813 * different handlers.
814 * We rely on user-space to write the initial superblock, and support
815 * reading and updating of superblocks.
816 * Interface methods are:
817 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
818 * loads and validates a superblock on dev.
819 * if refdev != NULL, compare superblocks on both devices
820 * Return:
821 * 0 - dev has a superblock that is compatible with refdev
822 * 1 - dev has a superblock that is compatible and newer than refdev
823 * so dev should be used as the refdev in future
824 * -EINVAL superblock incompatible or invalid
825 * -othererror e.g. -EIO
826 *
827 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
828 * Verify that dev is acceptable into mddev.
829 * The first time, mddev->raid_disks will be 0, and data from
830 * dev should be merged in. Subsequent calls check that dev
831 * is new enough. Return 0 or -EINVAL
832 *
833 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
834 * Update the superblock for rdev with data in mddev
835 * This does not write to disc.
836 *
837 */
838
839struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000840 char *name;
841 struct module *owner;
842 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
843 int minor_version);
844 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
845 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
846 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000847 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848};
849
850/*
Andre Noll0894cc32009-06-18 08:49:23 +1000851 * Check that the given mddev has no bitmap.
852 *
853 * This function is called from the run method of all personalities that do not
854 * support bitmaps. It prints an error message and returns non-zero if mddev
855 * has a bitmap. Otherwise, it returns 0.
856 *
857 */
858int md_check_no_bitmap(mddev_t *mddev)
859{
NeilBrownc3d97142009-12-14 12:49:52 +1100860 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000861 return 0;
862 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
863 mdname(mddev), mddev->pers->name);
864 return 1;
865}
866EXPORT_SYMBOL(md_check_no_bitmap);
867
868/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 * load_super for 0.90.0
870 */
871static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
872{
873 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
874 mdp_super_t *sb;
875 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 /*
Andre Noll0f420352008-07-11 22:02:23 +1000878 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 * it's at the end of the disk.
880 *
881 * It also happens to be a multiple of 4Kb.
882 */
Andre Noll0f420352008-07-11 22:02:23 +1000883 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
NeilBrown0002b272005-09-09 16:23:53 -0700885 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (ret) return ret;
887
888 ret = -EINVAL;
889
890 bdevname(rdev->bdev, b);
891 sb = (mdp_super_t*)page_address(rdev->sb_page);
892
893 if (sb->md_magic != MD_SB_MAGIC) {
894 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
895 b);
896 goto abort;
897 }
898
899 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800900 sb->minor_version < 90 ||
901 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
903 sb->major_version, sb->minor_version,
904 b);
905 goto abort;
906 }
907
908 if (sb->raid_disks <= 0)
909 goto abort;
910
NeilBrown4d167f02007-05-09 02:35:37 -0700911 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
913 b);
914 goto abort;
915 }
916
917 rdev->preferred_minor = sb->md_minor;
918 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700919 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (sb->level == LEVEL_MULTIPATH)
922 rdev->desc_nr = -1;
923 else
924 rdev->desc_nr = sb->this_disk.number;
925
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700926 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700928 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 __u64 ev1, ev2;
930 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
931 if (!uuid_equal(refsb, sb)) {
932 printk(KERN_WARNING "md: %s has different UUID to %s\n",
933 b, bdevname(refdev->bdev,b2));
934 goto abort;
935 }
936 if (!sb_equal(refsb, sb)) {
937 printk(KERN_WARNING "md: %s has same UUID"
938 " but different superblock to %s\n",
939 b, bdevname(refdev->bdev, b2));
940 goto abort;
941 }
942 ev1 = md_event(sb);
943 ev2 = md_event(refsb);
944 if (ev1 > ev2)
945 ret = 1;
946 else
947 ret = 0;
948 }
NeilBrown8190e752009-06-18 08:48:58 +1000949 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Andre Nolldd8ac332009-03-31 14:33:13 +1100951 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800952 /* "this cannot possibly happen" ... */
953 ret = -EINVAL;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 abort:
956 return ret;
957}
958
959/*
960 * validate_super for 0.90.0
961 */
962static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
963{
964 mdp_disk_t *desc;
965 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700966 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
NeilBrown41158c72005-06-21 17:17:25 -0700968 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800969 clear_bit(Faulty, &rdev->flags);
970 clear_bit(In_sync, &rdev->flags);
971 clear_bit(WriteMostly, &rdev->flags);
972 clear_bit(BarriersNotsupp, &rdev->flags);
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (mddev->raid_disks == 0) {
975 mddev->major_version = 0;
976 mddev->minor_version = sb->minor_version;
977 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800978 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000979 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 mddev->ctime = sb->ctime;
981 mddev->utime = sb->utime;
982 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800983 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 mddev->layout = sb->layout;
985 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100986 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700987 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +1100988 mddev->bitmap_info.offset = 0;
989 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
NeilBrownf6705572006-03-27 01:18:11 -0800991 if (mddev->minor_version >= 91) {
992 mddev->reshape_position = sb->reshape_position;
993 mddev->delta_disks = sb->delta_disks;
994 mddev->new_level = sb->new_level;
995 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000996 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -0800997 } else {
998 mddev->reshape_position = MaxSector;
999 mddev->delta_disks = 0;
1000 mddev->new_level = mddev->level;
1001 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001002 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001003 }
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (sb->state & (1<<MD_SB_CLEAN))
1006 mddev->recovery_cp = MaxSector;
1007 else {
1008 if (sb->events_hi == sb->cp_events_hi &&
1009 sb->events_lo == sb->cp_events_lo) {
1010 mddev->recovery_cp = sb->recovery_cp;
1011 } else
1012 mddev->recovery_cp = 0;
1013 }
1014
1015 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1016 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1017 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1018 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1019
1020 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001021
1022 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001023 mddev->bitmap_info.file == NULL)
1024 mddev->bitmap_info.offset =
1025 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001026
NeilBrown41158c72005-06-21 17:17:25 -07001027 } else if (mddev->pers == NULL) {
1028 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 ++ev1;
1030 if (ev1 < mddev->events)
1031 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001032 } else if (mddev->bitmap) {
1033 /* if adding to array with a bitmap, then we can accept an
1034 * older device ... but not too old.
1035 */
NeilBrown41158c72005-06-21 17:17:25 -07001036 if (ev1 < mddev->bitmap->events_cleared)
1037 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001038 } else {
1039 if (ev1 < mddev->events)
1040 /* just a hot-add of a new device, leave raid_disk at -1 */
1041 return 0;
1042 }
NeilBrown41158c72005-06-21 17:17:25 -07001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 desc = sb->disks + rdev->desc_nr;
1046
1047 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001048 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001049 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1050 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001051 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001053 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1054 /* active but not in sync implies recovery up to
1055 * reshape position. We don't know exactly where
1056 * that is, so set to zero for now */
1057 if (mddev->minor_version >= 91) {
1058 rdev->recovery_offset = 0;
1059 rdev->raid_disk = desc->raid_disk;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001062 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1063 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001064 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001065 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return 0;
1067}
1068
1069/*
1070 * sync_super for 0.90.0
1071 */
1072static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1073{
1074 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 mdk_rdev_t *rdev2;
1076 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 /* make rdev->sb match mddev data..
1080 *
1081 * 1/ zero out disks
1082 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1083 * 3/ any empty disks < next_spare become removed
1084 *
1085 * disks[0] gets initialised to REMOVED because
1086 * we cannot be sure from other fields if it has
1087 * been initialised or not.
1088 */
1089 int i;
1090 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1091
NeilBrown61181562005-09-09 16:24:02 -07001092 rdev->sb_size = MD_SB_BYTES;
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 sb = (mdp_super_t*)page_address(rdev->sb_page);
1095
1096 memset(sb, 0, sizeof(*sb));
1097
1098 sb->md_magic = MD_SB_MAGIC;
1099 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 sb->patch_version = mddev->patch_version;
1101 sb->gvalid_words = 0; /* ignored */
1102 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1103 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1104 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1105 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1106
1107 sb->ctime = mddev->ctime;
1108 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001109 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 sb->raid_disks = mddev->raid_disks;
1111 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001112 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 sb->utime = mddev->utime;
1114 sb->state = 0;
1115 sb->events_hi = (mddev->events>>32);
1116 sb->events_lo = (u32)mddev->events;
1117
NeilBrownf6705572006-03-27 01:18:11 -08001118 if (mddev->reshape_position == MaxSector)
1119 sb->minor_version = 90;
1120 else {
1121 sb->minor_version = 91;
1122 sb->reshape_position = mddev->reshape_position;
1123 sb->new_level = mddev->new_level;
1124 sb->delta_disks = mddev->delta_disks;
1125 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001126 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001127 }
1128 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (mddev->in_sync)
1130 {
1131 sb->recovery_cp = mddev->recovery_cp;
1132 sb->cp_events_hi = (mddev->events>>32);
1133 sb->cp_events_lo = (u32)mddev->events;
1134 if (mddev->recovery_cp == MaxSector)
1135 sb->state = (1<< MD_SB_CLEAN);
1136 } else
1137 sb->recovery_cp = 0;
1138
1139 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001140 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
NeilBrownc3d97142009-12-14 12:49:52 +11001142 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001143 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001146 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001148 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001149 int is_active = test_bit(In_sync, &rdev2->flags);
1150
1151 if (rdev2->raid_disk >= 0 &&
1152 sb->minor_version >= 91)
1153 /* we have nowhere to store the recovery_offset,
1154 * but if it is not below the reshape_position,
1155 * we can piggy-back on that.
1156 */
1157 is_active = 1;
1158 if (rdev2->raid_disk < 0 ||
1159 test_bit(Faulty, &rdev2->flags))
1160 is_active = 0;
1161 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001162 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001164 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001165 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 d = &sb->disks[rdev2->desc_nr];
1167 nr_disks++;
1168 d->number = rdev2->desc_nr;
1169 d->major = MAJOR(rdev2->bdev->bd_dev);
1170 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001171 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 d->raid_disk = rdev2->raid_disk;
1173 else
1174 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001175 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001177 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001179 if (test_bit(In_sync, &rdev2->flags))
1180 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 active++;
1182 working++;
1183 } else {
1184 d->state = 0;
1185 spare++;
1186 working++;
1187 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001188 if (test_bit(WriteMostly, &rdev2->flags))
1189 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /* now set the "removed" and "faulty" bits on any missing devices */
1192 for (i=0 ; i < mddev->raid_disks ; i++) {
1193 mdp_disk_t *d = &sb->disks[i];
1194 if (d->state == 0 && d->number == 0) {
1195 d->number = i;
1196 d->raid_disk = i;
1197 d->state = (1<<MD_DISK_REMOVED);
1198 d->state |= (1<<MD_DISK_FAULTY);
1199 failed++;
1200 }
1201 }
1202 sb->nr_disks = nr_disks;
1203 sb->active_disks = active;
1204 sb->working_disks = working;
1205 sb->failed_disks = failed;
1206 sb->spare_disks = spare;
1207
1208 sb->this_disk = sb->disks[rdev->desc_nr];
1209 sb->sb_csum = calc_sb_csum(sb);
1210}
1211
1212/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001213 * rdev_size_change for 0.90.0
1214 */
1215static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001216super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001217{
Andre Noll58c0fed2009-03-31 14:33:13 +11001218 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001219 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001220 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001221 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001222 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001223 if (!num_sectors || num_sectors > rdev->sb_start)
1224 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001225 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001226 rdev->sb_page);
1227 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001228 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001229}
1230
1231
1232/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 * version 1 superblock
1234 */
1235
NeilBrown1c05b4b2006-10-21 10:24:08 -07001236static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001238 __le32 disk_csum;
1239 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 unsigned long long newcsum;
1241 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001242 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int i;
1244
1245 disk_csum = sb->sb_csum;
1246 sb->sb_csum = 0;
1247 newcsum = 0;
1248 for (i=0; size>=4; size -= 4 )
1249 newcsum += le32_to_cpu(*isuper++);
1250
1251 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001252 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1255 sb->sb_csum = disk_csum;
1256 return cpu_to_le32(csum);
1257}
1258
1259static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1260{
1261 struct mdp_superblock_1 *sb;
1262 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001263 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001265 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /*
Andre Noll0f420352008-07-11 22:02:23 +10001268 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 * It is always aligned to a 4K boundary and
1270 * depeding on minor_version, it can be:
1271 * 0: At least 8K, but less than 12K, from end of device
1272 * 1: At start of device
1273 * 2: 4K from start of device.
1274 */
1275 switch(minor_version) {
1276 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001277 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1278 sb_start -= 8*2;
1279 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 break;
1281 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001282 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
1284 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001285 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 break;
1287 default:
1288 return -EINVAL;
1289 }
Andre Noll0f420352008-07-11 22:02:23 +10001290 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
NeilBrown0002b272005-09-09 16:23:53 -07001292 /* superblock is rarely larger than 1K, but it can be larger,
1293 * and it is safe to read 4k, so we do that
1294 */
1295 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (ret) return ret;
1297
1298
1299 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1300
1301 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1302 sb->major_version != cpu_to_le32(1) ||
1303 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001304 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001305 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return -EINVAL;
1307
1308 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1309 printk("md: invalid superblock checksum on %s\n",
1310 bdevname(rdev->bdev,b));
1311 return -EINVAL;
1312 }
1313 if (le64_to_cpu(sb->data_size) < 10) {
1314 printk("md: data_size too small on %s\n",
1315 bdevname(rdev->bdev,b));
1316 return -EINVAL;
1317 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 rdev->preferred_minor = 0xffff;
1320 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001321 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
NeilBrown0002b272005-09-09 16:23:53 -07001323 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001324 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001325 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001326 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1327
1328 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001329 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001330 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001331
NeilBrown31b65a02006-07-10 04:44:14 -07001332 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1333 rdev->desc_nr = -1;
1334 else
1335 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1336
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001337 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001338 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001339 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 __u64 ev1, ev2;
1341 struct mdp_superblock_1 *refsb =
1342 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1343
1344 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1345 sb->level != refsb->level ||
1346 sb->layout != refsb->layout ||
1347 sb->chunksize != refsb->chunksize) {
1348 printk(KERN_WARNING "md: %s has strangely different"
1349 " superblock to %s\n",
1350 bdevname(rdev->bdev,b),
1351 bdevname(refdev->bdev,b2));
1352 return -EINVAL;
1353 }
1354 ev1 = le64_to_cpu(sb->events);
1355 ev2 = le64_to_cpu(refsb->events);
1356
1357 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001358 ret = 1;
1359 else
1360 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
NeilBrowna1801f82008-03-04 14:29:31 -08001362 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001363 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1364 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001366 rdev->sectors = rdev->sb_start;
1367 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001369 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001370 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001371 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
1375static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1376{
1377 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001378 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
NeilBrown41158c72005-06-21 17:17:25 -07001380 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001381 clear_bit(Faulty, &rdev->flags);
1382 clear_bit(In_sync, &rdev->flags);
1383 clear_bit(WriteMostly, &rdev->flags);
1384 clear_bit(BarriersNotsupp, &rdev->flags);
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (mddev->raid_disks == 0) {
1387 mddev->major_version = 1;
1388 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001389 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001390 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1392 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1393 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001394 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 mddev->layout = le32_to_cpu(sb->layout);
1396 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001397 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001398 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001399 mddev->bitmap_info.offset = 0;
1400 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1403 memcpy(mddev->uuid, sb->set_uuid, 16);
1404
1405 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001406
NeilBrown71c08052005-09-09 16:23:51 -07001407 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001408 mddev->bitmap_info.file == NULL )
1409 mddev->bitmap_info.offset =
1410 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001411
NeilBrownf6705572006-03-27 01:18:11 -08001412 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1413 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1414 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1415 mddev->new_level = le32_to_cpu(sb->new_level);
1416 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001417 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001418 } else {
1419 mddev->reshape_position = MaxSector;
1420 mddev->delta_disks = 0;
1421 mddev->new_level = mddev->level;
1422 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001423 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001424 }
1425
NeilBrown41158c72005-06-21 17:17:25 -07001426 } else if (mddev->pers == NULL) {
1427 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 ++ev1;
1429 if (ev1 < mddev->events)
1430 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001431 } else if (mddev->bitmap) {
1432 /* If adding to array with a bitmap, then we can accept an
1433 * older device, but not too old.
1434 */
NeilBrown41158c72005-06-21 17:17:25 -07001435 if (ev1 < mddev->bitmap->events_cleared)
1436 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001437 } else {
1438 if (ev1 < mddev->events)
1439 /* just a hot-add of a new device, leave raid_disk at -1 */
1440 return 0;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (mddev->level != LEVEL_MULTIPATH) {
1443 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001444 if (rdev->desc_nr < 0 ||
1445 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1446 role = 0xffff;
1447 rdev->desc_nr = -1;
1448 } else
1449 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 switch(role) {
1451 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 break;
1453 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001454 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 break;
1456 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001457 if ((le32_to_cpu(sb->feature_map) &
1458 MD_FEATURE_RECOVERY_OFFSET))
1459 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1460 else
1461 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 rdev->raid_disk = role;
1463 break;
1464 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001465 if (sb->devflags & WriteMostly1)
1466 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001467 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001468 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return 0;
1471}
1472
1473static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1474{
1475 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 mdk_rdev_t *rdev2;
1477 int max_dev, i;
1478 /* make rdev->sb match mddev and rdev data. */
1479
1480 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1481
1482 sb->feature_map = 0;
1483 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001484 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 memset(sb->pad1, 0, sizeof(sb->pad1));
1486 memset(sb->pad2, 0, sizeof(sb->pad2));
1487 memset(sb->pad3, 0, sizeof(sb->pad3));
1488
1489 sb->utime = cpu_to_le64((__u64)mddev->utime);
1490 sb->events = cpu_to_le64(mddev->events);
1491 if (mddev->in_sync)
1492 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1493 else
1494 sb->resync_offset = cpu_to_le64(0);
1495
NeilBrown1c05b4b2006-10-21 10:24:08 -07001496 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001497
NeilBrownf0ca3402006-02-02 14:28:04 -08001498 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001499 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001500 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001501 sb->level = cpu_to_le32(mddev->level);
1502 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001503
NeilBrownc3d97142009-12-14 12:49:52 +11001504 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1505 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001506 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001507 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001508
1509 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001510 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001511 sb->feature_map |=
1512 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1513 sb->recovery_offset =
1514 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001515 }
1516
NeilBrownf6705572006-03-27 01:18:11 -08001517 if (mddev->reshape_position != MaxSector) {
1518 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1519 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1520 sb->new_layout = cpu_to_le32(mddev->new_layout);
1521 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1522 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001523 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001524 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001527 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if (rdev2->desc_nr+1 > max_dev)
1529 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001530
NeilBrown70471da2009-08-03 10:59:57 +10001531 if (max_dev > le32_to_cpu(sb->max_dev)) {
1532 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001533 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001534 rdev->sb_size = max_dev * 2 + 256;
1535 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1536 if (rdev->sb_size & bmask)
1537 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 for (i=0; i<max_dev;i++)
1540 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1541
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001542 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001544 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001546 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001548 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001549 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 else
1551 sb->dev_roles[i] = cpu_to_le16(0xffff);
1552 }
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 sb->sb_csum = calc_sb_1_csum(sb);
1555}
1556
Chris Webb0cd17fe2008-06-28 08:31:46 +10001557static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001558super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001559{
1560 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001561 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001562 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001563 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001564 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001565 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001566 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1567 max_sectors -= rdev->data_offset;
1568 if (!num_sectors || num_sectors > max_sectors)
1569 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001570 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001571 /* minor version 0 with bitmap we can't move */
1572 return 0;
1573 } else {
1574 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001575 sector_t sb_start;
1576 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1577 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001578 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001579 if (!num_sectors || num_sectors > max_sectors)
1580 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001581 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001582 }
1583 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001584 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001585 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001586 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001587 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001588 rdev->sb_page);
1589 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001590 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001591}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Adrian Bunk75c96f82005-05-05 16:16:09 -07001593static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 [0] = {
1595 .name = "0.90.0",
1596 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001597 .load_super = super_90_load,
1598 .validate_super = super_90_validate,
1599 .sync_super = super_90_sync,
1600 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 },
1602 [1] = {
1603 .name = "md-1",
1604 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001605 .load_super = super_1_load,
1606 .validate_super = super_1_validate,
1607 .sync_super = super_1_sync,
1608 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 },
1610};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1613{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001614 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
NeilBrown4b809912008-07-21 17:05:25 +10001616 rcu_read_lock();
1617 rdev_for_each_rcu(rdev, mddev1)
1618 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001619 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001620 rdev2->bdev->bd_contains) {
1621 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001622 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001623 }
1624 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return 0;
1626}
1627
1628static LIST_HEAD(pending_raid_disks);
1629
Andre Nollac5e7112009-08-03 10:59:47 +10001630/*
1631 * Try to register data integrity profile for an mddev
1632 *
1633 * This is called when an array is started and after a disk has been kicked
1634 * from the array. It only succeeds if all working and active component devices
1635 * are integrity capable with matching profiles.
1636 */
1637int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001638{
Andre Nollac5e7112009-08-03 10:59:47 +10001639 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001640
Andre Nollac5e7112009-08-03 10:59:47 +10001641 if (list_empty(&mddev->disks))
1642 return 0; /* nothing to do */
1643 if (blk_get_integrity(mddev->gendisk))
1644 return 0; /* already registered */
1645 list_for_each_entry(rdev, &mddev->disks, same_set) {
1646 /* skip spares and non-functional disks */
1647 if (test_bit(Faulty, &rdev->flags))
1648 continue;
1649 if (rdev->raid_disk < 0)
1650 continue;
1651 /*
1652 * If at least one rdev is not integrity capable, we can not
1653 * enable data integrity for the md device.
1654 */
1655 if (!bdev_get_integrity(rdev->bdev))
1656 return -EINVAL;
1657 if (!reference) {
1658 /* Use the first rdev as the reference */
1659 reference = rdev;
1660 continue;
1661 }
1662 /* does this rdev's profile match the reference profile? */
1663 if (blk_integrity_compare(reference->bdev->bd_disk,
1664 rdev->bdev->bd_disk) < 0)
1665 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001666 }
Andre Nollac5e7112009-08-03 10:59:47 +10001667 /*
1668 * All component devices are integrity capable and have matching
1669 * profiles, register the common profile for the md device.
1670 */
1671 if (blk_integrity_register(mddev->gendisk,
1672 bdev_get_integrity(reference->bdev)) != 0) {
1673 printk(KERN_ERR "md: failed to register integrity for %s\n",
1674 mdname(mddev));
1675 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001676 }
Andre Nollac5e7112009-08-03 10:59:47 +10001677 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1678 mdname(mddev));
1679 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001680}
Andre Nollac5e7112009-08-03 10:59:47 +10001681EXPORT_SYMBOL(md_integrity_register);
1682
1683/* Disable data integrity if non-capable/non-matching disk is being added */
1684void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1685{
1686 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1687 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1688
1689 if (!bi_mddev) /* nothing to do */
1690 return;
1691 if (rdev->raid_disk < 0) /* skip spares */
1692 return;
1693 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1694 rdev->bdev->bd_disk) >= 0)
1695 return;
1696 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1697 blk_integrity_unregister(mddev->gendisk);
1698}
1699EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1702{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001703 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001704 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001705 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001706 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 if (rdev->mddev) {
1709 MD_BUG();
1710 return -EINVAL;
1711 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001712
1713 /* prevent duplicates */
1714 if (find_rdev(mddev, rdev->bdev->bd_dev))
1715 return -EEXIST;
1716
Andre Nolldd8ac332009-03-31 14:33:13 +11001717 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1718 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1719 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001720 if (mddev->pers) {
1721 /* Cannot change size, so fail
1722 * If mddev->level <= 0, then we don't care
1723 * about aligning sizes (e.g. linear)
1724 */
1725 if (mddev->level > 0)
1726 return -ENOSPC;
1727 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001728 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 /* Verify rdev->desc_nr is unique.
1732 * If it is -1, assign a free number, else
1733 * check number is not in use
1734 */
1735 if (rdev->desc_nr < 0) {
1736 int choice = 0;
1737 if (mddev->pers) choice = mddev->raid_disks;
1738 while (find_rdev_nr(mddev, choice))
1739 choice++;
1740 rdev->desc_nr = choice;
1741 } else {
1742 if (find_rdev_nr(mddev, rdev->desc_nr))
1743 return -EBUSY;
1744 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001745 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1746 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1747 mdname(mddev), mddev->max_disks);
1748 return -EBUSY;
1749 }
NeilBrown19133a42005-11-08 21:39:35 -08001750 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001751 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001752 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001755 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001756
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001757 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001758 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001759
Tejun Heo0762b8b2008-08-25 19:56:12 +09001760 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001761 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1762 kobject_del(&rdev->kobj);
1763 goto fail;
1764 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001765 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1766
NeilBrown4b809912008-07-21 17:05:25 +10001767 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001768 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001769
1770 /* May as well allow recovery to be retried once */
1771 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001774
1775 fail:
1776 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1777 b, mdname(mddev));
1778 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
NeilBrown177a99b2008-02-06 01:39:56 -08001781static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001782{
1783 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1784 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001785 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001786}
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1789{
1790 char b[BDEVNAME_SIZE];
1791 if (!rdev->mddev) {
1792 MD_BUG();
1793 return;
1794 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001795 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001796 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1798 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001799 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001800 sysfs_put(rdev->sysfs_state);
1801 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001802 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001803 * writing to 'remove' to "dev/state". We also need
1804 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001805 */
NeilBrown4b809912008-07-21 17:05:25 +10001806 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001807 INIT_WORK(&rdev->del_work, md_delayed_delete);
1808 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001809 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
1812/*
1813 * prevent the device from being mounted, repartitioned or
1814 * otherwise reused by a RAID array (or any other kernel
1815 * subsystem), by bd_claiming the device.
1816 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001817static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
1819 int err = 0;
1820 struct block_device *bdev;
1821 char b[BDEVNAME_SIZE];
1822
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001823 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (IS_ERR(bdev)) {
1825 printk(KERN_ERR "md: could not open %s.\n",
1826 __bdevname(dev, b));
1827 return PTR_ERR(bdev);
1828 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001829 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (err) {
1831 printk(KERN_ERR "md: could not bd_claim %s.\n",
1832 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001833 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return err;
1835 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001836 if (!shared)
1837 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 rdev->bdev = bdev;
1839 return err;
1840}
1841
1842static void unlock_rdev(mdk_rdev_t *rdev)
1843{
1844 struct block_device *bdev = rdev->bdev;
1845 rdev->bdev = NULL;
1846 if (!bdev)
1847 MD_BUG();
1848 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001849 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
1852void md_autodetect_dev(dev_t dev);
1853
1854static void export_rdev(mdk_rdev_t * rdev)
1855{
1856 char b[BDEVNAME_SIZE];
1857 printk(KERN_INFO "md: export_rdev(%s)\n",
1858 bdevname(rdev->bdev,b));
1859 if (rdev->mddev)
1860 MD_BUG();
1861 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001863 if (test_bit(AutoDetected, &rdev->flags))
1864 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865#endif
1866 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001867 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
1870static void kick_rdev_from_array(mdk_rdev_t * rdev)
1871{
1872 unbind_rdev_from_array(rdev);
1873 export_rdev(rdev);
1874}
1875
1876static void export_array(mddev_t *mddev)
1877{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001878 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
NeilBrownd089c6a2008-02-06 01:39:59 -08001880 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (!rdev->mddev) {
1882 MD_BUG();
1883 continue;
1884 }
1885 kick_rdev_from_array(rdev);
1886 }
1887 if (!list_empty(&mddev->disks))
1888 MD_BUG();
1889 mddev->raid_disks = 0;
1890 mddev->major_version = 0;
1891}
1892
1893static void print_desc(mdp_disk_t *desc)
1894{
1895 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1896 desc->major,desc->minor,desc->raid_disk,desc->state);
1897}
1898
Cheng Renquancd2ac932009-01-09 08:31:08 +11001899static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 int i;
1902
1903 printk(KERN_INFO
1904 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1905 sb->major_version, sb->minor_version, sb->patch_version,
1906 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1907 sb->ctime);
1908 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1909 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1910 sb->md_minor, sb->layout, sb->chunk_size);
1911 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1912 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1913 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1914 sb->failed_disks, sb->spare_disks,
1915 sb->sb_csum, (unsigned long)sb->events_lo);
1916
1917 printk(KERN_INFO);
1918 for (i = 0; i < MD_SB_DISKS; i++) {
1919 mdp_disk_t *desc;
1920
1921 desc = sb->disks + i;
1922 if (desc->number || desc->major || desc->minor ||
1923 desc->raid_disk || (desc->state && (desc->state != 4))) {
1924 printk(" D %2d: ", i);
1925 print_desc(desc);
1926 }
1927 }
1928 printk(KERN_INFO "md: THIS: ");
1929 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Cheng Renquancd2ac932009-01-09 08:31:08 +11001932static void print_sb_1(struct mdp_superblock_1 *sb)
1933{
1934 __u8 *uuid;
1935
1936 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001937 printk(KERN_INFO
1938 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1939 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1940 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001941 le32_to_cpu(sb->major_version),
1942 le32_to_cpu(sb->feature_map),
1943 uuid[0], uuid[1], uuid[2], uuid[3],
1944 uuid[4], uuid[5], uuid[6], uuid[7],
1945 uuid[8], uuid[9], uuid[10], uuid[11],
1946 uuid[12], uuid[13], uuid[14], uuid[15],
1947 sb->set_name,
1948 (unsigned long long)le64_to_cpu(sb->ctime)
1949 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1950
1951 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001952 printk(KERN_INFO
1953 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001954 " RO:%llu\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001955 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1956 ":%02x%02x%02x%02x%02x%02x\n"
1957 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1958 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001959 le32_to_cpu(sb->level),
1960 (unsigned long long)le64_to_cpu(sb->size),
1961 le32_to_cpu(sb->raid_disks),
1962 le32_to_cpu(sb->layout),
1963 le32_to_cpu(sb->chunksize),
1964 (unsigned long long)le64_to_cpu(sb->data_offset),
1965 (unsigned long long)le64_to_cpu(sb->data_size),
1966 (unsigned long long)le64_to_cpu(sb->super_offset),
1967 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1968 le32_to_cpu(sb->dev_number),
1969 uuid[0], uuid[1], uuid[2], uuid[3],
1970 uuid[4], uuid[5], uuid[6], uuid[7],
1971 uuid[8], uuid[9], uuid[10], uuid[11],
1972 uuid[12], uuid[13], uuid[14], uuid[15],
1973 sb->devflags,
1974 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1975 (unsigned long long)le64_to_cpu(sb->events),
1976 (unsigned long long)le64_to_cpu(sb->resync_offset),
1977 le32_to_cpu(sb->sb_csum),
1978 le32_to_cpu(sb->max_dev)
1979 );
1980}
1981
1982static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001985 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1986 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001987 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1988 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001990 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1991 switch (major_version) {
1992 case 0:
1993 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1994 break;
1995 case 1:
1996 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1997 break;
1998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 } else
2000 printk(KERN_INFO "md: no rdev superblock!\n");
2001}
2002
Adrian Bunk5e563412006-06-26 00:27:42 -07002003static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002005 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 mdk_rdev_t *rdev;
2007 mddev_t *mddev;
2008 char b[BDEVNAME_SIZE];
2009
2010 printk("\n");
2011 printk("md: **********************************\n");
2012 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2013 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002014 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
NeilBrown32a76272005-06-21 17:17:14 -07002016 if (mddev->bitmap)
2017 bitmap_print_sb(mddev->bitmap);
2018 else
2019 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002020 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 printk("<%s>", bdevname(rdev->bdev,b));
2022 printk("\n");
2023
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002024 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002025 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027 printk("md: **********************************\n");
2028 printk("\n");
2029}
2030
2031
NeilBrown42543762006-06-26 00:27:57 -07002032static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
NeilBrown42543762006-06-26 00:27:57 -07002034 /* Update each superblock (in-memory image), but
2035 * if we are allowed to, skip spares which already
2036 * have the right event counter, or have one earlier
2037 * (which would mean they aren't being marked as dirty
2038 * with the rest of the array)
2039 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
NeilBrown5e865102009-11-12 12:08:04 +11002042 /* First make sure individual recovery_offsets are correct */
2043 list_for_each_entry(rdev, &mddev->disks, same_set) {
2044 if (rdev->raid_disk >= 0 &&
2045 !test_bit(In_sync, &rdev->flags) &&
2046 mddev->curr_resync_completed > rdev->recovery_offset)
2047 rdev->recovery_offset = mddev->curr_resync_completed;
2048
2049 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002050 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002051 if (rdev->sb_events == mddev->events ||
2052 (nospares &&
2053 rdev->raid_disk < 0 &&
2054 (rdev->sb_events&1)==0 &&
2055 rdev->sb_events+1 == mddev->events)) {
2056 /* Don't update this superblock */
2057 rdev->sb_loaded = 2;
2058 } else {
2059 super_types[mddev->major_version].
2060 sync_super(mddev, rdev);
2061 rdev->sb_loaded = 1;
2062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064}
2065
NeilBrown850b2b422006-10-03 01:15:46 -07002066static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002069 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002070 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
NeilBrown1b57f132009-06-18 08:48:19 +10002072 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002073 if (mddev->external)
2074 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002076 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002077
NeilBrown850b2b422006-10-03 01:15:46 -07002078 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2079 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2080 force_change = 1;
2081 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2082 /* just a clean<-> dirty transition, possibly leave spares alone,
2083 * though if events isn't the right even/odd, we will have to do
2084 * spares after all
2085 */
2086 nospares = 1;
2087 if (force_change)
2088 nospares = 0;
2089 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002090 /* If the array is degraded, then skipping spares is both
2091 * dangerous and fairly pointless.
2092 * Dangerous because a device that was removed from the array
2093 * might have a event_count that still looks up-to-date,
2094 * so it can be re-added without a resync.
2095 * Pointless because if there are any spares to skip,
2096 * then a recovery will happen and soon that array won't
2097 * be degraded any more and the spare can go back to sleep then.
2098 */
NeilBrown850b2b422006-10-03 01:15:46 -07002099 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002100
NeilBrown06d91a52005-06-21 17:17:12 -07002101 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002102
2103 /* If this is just a dirty<->clean transition, and the array is clean
2104 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002105 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002106 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002107 && (mddev->events & 1)
2108 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002109 mddev->events--;
2110 else {
2111 /* otherwise we have to go forward and ... */
2112 mddev->events ++;
2113 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002114 /* .. if the array isn't clean, an 'even' event must also go
2115 * to spares. */
2116 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002117 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002118 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002119 /* otherwise an 'odd' event must go to spares */
2120 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002121 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002122 }
2123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 if (!mddev->events) {
2126 /*
2127 * oops, this 64-bit counter should never wrap.
2128 * Either we are in around ~1 trillion A.C., assuming
2129 * 1 reboot per second, or we have a bug:
2130 */
2131 MD_BUG();
2132 mddev->events --;
2133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 /*
2136 * do not write anything to disk if using
2137 * nonpersistent superblocks
2138 */
NeilBrown06d91a52005-06-21 17:17:12 -07002139 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002140 if (!mddev->external)
2141 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2142
NeilBrowna9701a32005-11-08 21:39:34 -08002143 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002144 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002146 }
NeilBrowne6910632008-02-06 01:39:51 -08002147 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002148 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 dprintk(KERN_INFO
2151 "md: updating %s RAID superblock on device (in sync %d)\n",
2152 mdname(mddev),mddev->in_sync);
2153
NeilBrown4ad13662007-07-17 04:06:13 -07002154 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002155 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 char b[BDEVNAME_SIZE];
2157 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002158 if (rdev->sb_loaded != 1)
2159 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002160 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 dprintk("(skipping faulty ");
2162
2163 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002164 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002165 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002166 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002167 rdev->sb_page);
2168 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2169 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002170 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002171 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 } else
2174 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002175 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /* only need to write one superblock... */
2177 break;
2178 }
NeilBrowna9701a32005-11-08 21:39:34 -08002179 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002180 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002181
NeilBrowna9701a32005-11-08 21:39:34 -08002182 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002183 if (mddev->in_sync != sync_req ||
2184 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002185 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002186 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002187 goto repeat;
2188 }
NeilBrown850b2b422006-10-03 01:15:46 -07002189 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002190 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002191 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002192 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2193 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
Andre Noll7f6ce762008-03-23 18:34:54 +01002197/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002198 * We want to accept with case. For this we use cmd_match.
2199 */
2200static int cmd_match(const char *cmd, const char *str)
2201{
2202 /* See if cmd, written into a sysfs file, matches
2203 * str. They must either be the same, or cmd can
2204 * have a trailing newline
2205 */
2206 while (*cmd && *str && *cmd == *str) {
2207 cmd++;
2208 str++;
2209 }
2210 if (*cmd == '\n')
2211 cmd++;
2212 if (*str || *cmd)
2213 return 0;
2214 return 1;
2215}
2216
NeilBrown86e6ffd2005-11-08 21:39:24 -08002217struct rdev_sysfs_entry {
2218 struct attribute attr;
2219 ssize_t (*show)(mdk_rdev_t *, char *);
2220 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2221};
2222
2223static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002224state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002225{
2226 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002227 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002228
NeilBrownb2d444d2005-11-08 21:39:31 -08002229 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002230 len+= sprintf(page+len, "%sfaulty",sep);
2231 sep = ",";
2232 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002233 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002234 len += sprintf(page+len, "%sin_sync",sep);
2235 sep = ",";
2236 }
NeilBrownf6556752006-06-26 00:28:01 -07002237 if (test_bit(WriteMostly, &rdev->flags)) {
2238 len += sprintf(page+len, "%swrite_mostly",sep);
2239 sep = ",";
2240 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002241 if (test_bit(Blocked, &rdev->flags)) {
2242 len += sprintf(page+len, "%sblocked", sep);
2243 sep = ",";
2244 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002245 if (!test_bit(Faulty, &rdev->flags) &&
2246 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002247 len += sprintf(page+len, "%sspare", sep);
2248 sep = ",";
2249 }
2250 return len+sprintf(page+len, "\n");
2251}
2252
NeilBrown45dc2de2006-06-26 00:27:58 -07002253static ssize_t
2254state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2255{
2256 /* can write
2257 * faulty - simulates and error
2258 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002259 * writemostly - sets write_mostly
2260 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002261 * blocked - sets the Blocked flag
2262 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002263 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002264 */
2265 int err = -EINVAL;
2266 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2267 md_error(rdev->mddev, rdev);
2268 err = 0;
2269 } else if (cmd_match(buf, "remove")) {
2270 if (rdev->raid_disk >= 0)
2271 err = -EBUSY;
2272 else {
2273 mddev_t *mddev = rdev->mddev;
2274 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002275 if (mddev->pers)
2276 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002277 md_new_event(mddev);
2278 err = 0;
2279 }
NeilBrownf6556752006-06-26 00:28:01 -07002280 } else if (cmd_match(buf, "writemostly")) {
2281 set_bit(WriteMostly, &rdev->flags);
2282 err = 0;
2283 } else if (cmd_match(buf, "-writemostly")) {
2284 clear_bit(WriteMostly, &rdev->flags);
2285 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002286 } else if (cmd_match(buf, "blocked")) {
2287 set_bit(Blocked, &rdev->flags);
2288 err = 0;
2289 } else if (cmd_match(buf, "-blocked")) {
2290 clear_bit(Blocked, &rdev->flags);
2291 wake_up(&rdev->blocked_wait);
2292 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2293 md_wakeup_thread(rdev->mddev->thread);
2294
2295 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002296 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2297 set_bit(In_sync, &rdev->flags);
2298 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002299 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002300 if (!err && rdev->sysfs_state)
2301 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002302 return err ? err : len;
2303}
NeilBrown80ca3a42006-07-10 04:44:18 -07002304static struct rdev_sysfs_entry rdev_state =
2305__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002306
2307static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002308errors_show(mdk_rdev_t *rdev, char *page)
2309{
2310 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2311}
2312
2313static ssize_t
2314errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2315{
2316 char *e;
2317 unsigned long n = simple_strtoul(buf, &e, 10);
2318 if (*buf && (*e == 0 || *e == '\n')) {
2319 atomic_set(&rdev->corrected_errors, n);
2320 return len;
2321 }
2322 return -EINVAL;
2323}
2324static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002325__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002326
NeilBrown014236d2006-01-06 00:20:55 -08002327static ssize_t
2328slot_show(mdk_rdev_t *rdev, char *page)
2329{
2330 if (rdev->raid_disk < 0)
2331 return sprintf(page, "none\n");
2332 else
2333 return sprintf(page, "%d\n", rdev->raid_disk);
2334}
2335
2336static ssize_t
2337slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2338{
2339 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002340 int err;
2341 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002342 int slot = simple_strtoul(buf, &e, 10);
2343 if (strncmp(buf, "none", 4)==0)
2344 slot = -1;
2345 else if (e==buf || (*e && *e!= '\n'))
2346 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002347 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002348 /* Setting 'slot' on an active array requires also
2349 * updating the 'rd%d' link, and communicating
2350 * with the personality with ->hot_*_disk.
2351 * For now we only support removing
2352 * failed/spare devices. This normally happens automatically,
2353 * but not when the metadata is externally managed.
2354 */
NeilBrownc303da62008-02-06 01:39:51 -08002355 if (rdev->raid_disk == -1)
2356 return -EEXIST;
2357 /* personality does all needed checks */
2358 if (rdev->mddev->pers->hot_add_disk == NULL)
2359 return -EINVAL;
2360 err = rdev->mddev->pers->
2361 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2362 if (err)
2363 return err;
2364 sprintf(nm, "rd%d", rdev->raid_disk);
2365 sysfs_remove_link(&rdev->mddev->kobj, nm);
2366 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2367 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002368 } else if (rdev->mddev->pers) {
2369 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002370 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002371 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002372 */
2373
2374 if (rdev->raid_disk != -1)
2375 return -EBUSY;
2376
2377 if (rdev->mddev->pers->hot_add_disk == NULL)
2378 return -EINVAL;
2379
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002380 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002381 if (rdev2->raid_disk == slot)
2382 return -EEXIST;
2383
2384 rdev->raid_disk = slot;
2385 if (test_bit(In_sync, &rdev->flags))
2386 rdev->saved_raid_disk = slot;
2387 else
2388 rdev->saved_raid_disk = -1;
2389 err = rdev->mddev->pers->
2390 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002391 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002392 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002393 return err;
Neil Brown52664732008-06-28 08:31:44 +10002394 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002395 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002396 sprintf(nm, "rd%d", rdev->raid_disk);
2397 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2398 printk(KERN_WARNING
2399 "md: cannot register "
2400 "%s for %s\n",
2401 nm, mdname(rdev->mddev));
2402
2403 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002404 } else {
2405 if (slot >= rdev->mddev->raid_disks)
2406 return -ENOSPC;
2407 rdev->raid_disk = slot;
2408 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002409 clear_bit(Faulty, &rdev->flags);
2410 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002411 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002412 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002413 }
NeilBrown014236d2006-01-06 00:20:55 -08002414 return len;
2415}
2416
2417
2418static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002419__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002420
NeilBrown93c8cad2006-01-06 00:20:56 -08002421static ssize_t
2422offset_show(mdk_rdev_t *rdev, char *page)
2423{
Andrew Morton6961ece2006-01-06 00:20:59 -08002424 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002425}
2426
2427static ssize_t
2428offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2429{
2430 char *e;
2431 unsigned long long offset = simple_strtoull(buf, &e, 10);
2432 if (e==buf || (*e && *e != '\n'))
2433 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002434 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002435 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002436 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002437 /* Must set offset before size, so overlap checks
2438 * can be sane */
2439 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002440 rdev->data_offset = offset;
2441 return len;
2442}
2443
2444static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002445__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002446
NeilBrown83303b62006-01-06 00:21:06 -08002447static ssize_t
2448rdev_size_show(mdk_rdev_t *rdev, char *page)
2449{
Andre Nolldd8ac332009-03-31 14:33:13 +11002450 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002451}
2452
NeilBrownc5d79ad2008-02-06 01:39:54 -08002453static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2454{
2455 /* check if two start/length pairs overlap */
2456 if (s1+l1 <= s2)
2457 return 0;
2458 if (s2+l2 <= s1)
2459 return 0;
2460 return 1;
2461}
2462
Dan Williamsb522adc2009-03-31 15:00:31 +11002463static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2464{
2465 unsigned long long blocks;
2466 sector_t new;
2467
2468 if (strict_strtoull(buf, 10, &blocks) < 0)
2469 return -EINVAL;
2470
2471 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2472 return -EINVAL; /* sector conversion overflow */
2473
2474 new = blocks * 2;
2475 if (new != blocks * 2)
2476 return -EINVAL; /* unsigned long long to sector_t overflow */
2477
2478 *sectors = new;
2479 return 0;
2480}
2481
NeilBrown83303b62006-01-06 00:21:06 -08002482static ssize_t
2483rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2484{
NeilBrown27c529b2008-03-04 14:29:33 -08002485 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002486 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002487 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002488
Dan Williamsb522adc2009-03-31 15:00:31 +11002489 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002490 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002491 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002492 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002493 sectors = super_types[my_mddev->major_version].
2494 rdev_size_change(rdev, sectors);
2495 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002496 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002497 } else if (!sectors)
2498 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2499 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002500 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002501 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002502 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002503
Andre Nolldd8ac332009-03-31 14:33:13 +11002504 rdev->sectors = sectors;
2505 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002506 /* need to check that all other rdevs with the same ->bdev
2507 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002508 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002509 * we have to change it back, we will have the lock again.
2510 */
2511 mddev_t *mddev;
2512 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002513 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002514
NeilBrown27c529b2008-03-04 14:29:33 -08002515 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002516 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002517 mdk_rdev_t *rdev2;
2518
2519 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002520 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002521 if (test_bit(AllReserved, &rdev2->flags) ||
2522 (rdev->bdev == rdev2->bdev &&
2523 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002524 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002525 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002526 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002527 overlap = 1;
2528 break;
2529 }
2530 mddev_unlock(mddev);
2531 if (overlap) {
2532 mddev_put(mddev);
2533 break;
2534 }
2535 }
NeilBrown27c529b2008-03-04 14:29:33 -08002536 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002537 if (overlap) {
2538 /* Someone else could have slipped in a size
2539 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002540 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002541 * safe, and trust userspace not to race with
2542 * itself
2543 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002544 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002545 return -EBUSY;
2546 }
2547 }
NeilBrown83303b62006-01-06 00:21:06 -08002548 return len;
2549}
2550
2551static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002552__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002553
Dan Williams06e3c812009-12-12 21:17:12 -07002554
2555static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2556{
2557 unsigned long long recovery_start = rdev->recovery_offset;
2558
2559 if (test_bit(In_sync, &rdev->flags) ||
2560 recovery_start == MaxSector)
2561 return sprintf(page, "none\n");
2562
2563 return sprintf(page, "%llu\n", recovery_start);
2564}
2565
2566static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2567{
2568 unsigned long long recovery_start;
2569
2570 if (cmd_match(buf, "none"))
2571 recovery_start = MaxSector;
2572 else if (strict_strtoull(buf, 10, &recovery_start))
2573 return -EINVAL;
2574
2575 if (rdev->mddev->pers &&
2576 rdev->raid_disk >= 0)
2577 return -EBUSY;
2578
2579 rdev->recovery_offset = recovery_start;
2580 if (recovery_start == MaxSector)
2581 set_bit(In_sync, &rdev->flags);
2582 else
2583 clear_bit(In_sync, &rdev->flags);
2584 return len;
2585}
2586
2587static struct rdev_sysfs_entry rdev_recovery_start =
2588__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2589
NeilBrown86e6ffd2005-11-08 21:39:24 -08002590static struct attribute *rdev_default_attrs[] = {
2591 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002592 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002593 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002594 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002595 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002596 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002597 NULL,
2598};
2599static ssize_t
2600rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2601{
2602 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2603 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002604 mddev_t *mddev = rdev->mddev;
2605 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002606
2607 if (!entry->show)
2608 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002609
2610 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2611 if (!rv) {
2612 if (rdev->mddev == NULL)
2613 rv = -EBUSY;
2614 else
2615 rv = entry->show(rdev, page);
2616 mddev_unlock(mddev);
2617 }
2618 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002619}
2620
2621static ssize_t
2622rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2623 const char *page, size_t length)
2624{
2625 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2626 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002627 ssize_t rv;
2628 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002629
2630 if (!entry->store)
2631 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002632 if (!capable(CAP_SYS_ADMIN))
2633 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002634 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002635 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002636 if (rdev->mddev == NULL)
2637 rv = -EBUSY;
2638 else
2639 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002640 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002641 }
2642 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002643}
2644
2645static void rdev_free(struct kobject *ko)
2646{
2647 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2648 kfree(rdev);
2649}
2650static struct sysfs_ops rdev_sysfs_ops = {
2651 .show = rdev_attr_show,
2652 .store = rdev_attr_store,
2653};
2654static struct kobj_type rdev_ktype = {
2655 .release = rdev_free,
2656 .sysfs_ops = &rdev_sysfs_ops,
2657 .default_attrs = rdev_default_attrs,
2658};
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660/*
2661 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2662 *
2663 * mark the device faulty if:
2664 *
2665 * - the device is nonexistent (zero size)
2666 * - the device has no valid superblock
2667 *
2668 * a faulty rdev _never_ has rdev->sb set.
2669 */
2670static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2671{
2672 char b[BDEVNAME_SIZE];
2673 int err;
2674 mdk_rdev_t *rdev;
2675 sector_t size;
2676
NeilBrown9ffae0c2006-01-06 00:20:32 -08002677 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 if (!rdev) {
2679 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2680 return ERR_PTR(-ENOMEM);
2681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
2683 if ((err = alloc_disk_sb(rdev)))
2684 goto abort_free;
2685
NeilBrownc5d79ad2008-02-06 01:39:54 -08002686 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if (err)
2688 goto abort_free;
2689
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002690 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002693 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002694 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002695 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002697 rdev->sb_events = 0;
Robert Becker1e509152009-12-14 12:49:58 +11002698 rdev->last_read_error.tv_sec = 0;
2699 rdev->last_read_error.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002701 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002702 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
2704 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2705 if (!size) {
2706 printk(KERN_WARNING
2707 "md: %s has zero or unknown size, marking faulty!\n",
2708 bdevname(rdev->bdev,b));
2709 err = -EINVAL;
2710 goto abort_free;
2711 }
2712
2713 if (super_format >= 0) {
2714 err = super_types[super_format].
2715 load_super(rdev, NULL, super_minor);
2716 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002717 printk(KERN_WARNING
2718 "md: %s does not have a valid v%d.%d "
2719 "superblock, not importing!\n",
2720 bdevname(rdev->bdev,b),
2721 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 goto abort_free;
2723 }
2724 if (err < 0) {
2725 printk(KERN_WARNING
2726 "md: could not read %s's sb, not importing!\n",
2727 bdevname(rdev->bdev,b));
2728 goto abort_free;
2729 }
2730 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002733 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735 return rdev;
2736
2737abort_free:
2738 if (rdev->sb_page) {
2739 if (rdev->bdev)
2740 unlock_rdev(rdev);
2741 free_disk_sb(rdev);
2742 }
2743 kfree(rdev);
2744 return ERR_PTR(err);
2745}
2746
2747/*
2748 * Check a full RAID array for plausibility
2749 */
2750
2751
NeilBrowna757e642005-04-16 15:26:42 -07002752static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
2754 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002755 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 char b[BDEVNAME_SIZE];
2757
2758 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002759 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 switch (super_types[mddev->major_version].
2761 load_super(rdev, freshest, mddev->minor_version)) {
2762 case 1:
2763 freshest = rdev;
2764 break;
2765 case 0:
2766 break;
2767 default:
2768 printk( KERN_ERR \
2769 "md: fatal superblock inconsistency in %s"
2770 " -- removing from array\n",
2771 bdevname(rdev->bdev,b));
2772 kick_rdev_from_array(rdev);
2773 }
2774
2775
2776 super_types[mddev->major_version].
2777 validate_super(mddev, freshest);
2778
2779 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002780 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002781 if (rdev->desc_nr >= mddev->max_disks ||
2782 i > mddev->max_disks) {
2783 printk(KERN_WARNING
2784 "md: %s: %s: only %d devices permitted\n",
2785 mdname(mddev), bdevname(rdev->bdev, b),
2786 mddev->max_disks);
2787 kick_rdev_from_array(rdev);
2788 continue;
2789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if (rdev != freshest)
2791 if (super_types[mddev->major_version].
2792 validate_super(mddev, rdev)) {
2793 printk(KERN_WARNING "md: kicking non-fresh %s"
2794 " from array!\n",
2795 bdevname(rdev->bdev,b));
2796 kick_rdev_from_array(rdev);
2797 continue;
2798 }
2799 if (mddev->level == LEVEL_MULTIPATH) {
2800 rdev->desc_nr = i++;
2801 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002802 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002803 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002804 rdev->raid_disk = -1;
2805 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
2807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808}
2809
NeilBrown72e02072009-12-14 12:49:55 +11002810/* Read a fixed-point number.
2811 * Numbers in sysfs attributes should be in "standard" units where
2812 * possible, so time should be in seconds.
2813 * However we internally use a a much smaller unit such as
2814 * milliseconds or jiffies.
2815 * This function takes a decimal number with a possible fractional
2816 * component, and produces an integer which is the result of
2817 * multiplying that number by 10^'scale'.
2818 * all without any floating-point arithmetic.
2819 */
2820int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2821{
2822 unsigned long result = 0;
2823 long decimals = -1;
2824 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2825 if (*cp == '.')
2826 decimals = 0;
2827 else if (decimals < scale) {
2828 unsigned int value;
2829 value = *cp - '0';
2830 result = result * 10 + value;
2831 if (decimals >= 0)
2832 decimals++;
2833 }
2834 cp++;
2835 }
2836 if (*cp == '\n')
2837 cp++;
2838 if (*cp)
2839 return -EINVAL;
2840 if (decimals < 0)
2841 decimals = 0;
2842 while (decimals < scale) {
2843 result *= 10;
2844 decimals ++;
2845 }
2846 *res = result;
2847 return 0;
2848}
2849
2850
NeilBrown19052c02008-08-05 15:54:13 +10002851static void md_safemode_timeout(unsigned long data);
2852
NeilBrowneae17012005-11-08 21:39:23 -08002853static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002854safe_delay_show(mddev_t *mddev, char *page)
2855{
2856 int msec = (mddev->safemode_delay*1000)/HZ;
2857 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2858}
2859static ssize_t
2860safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2861{
NeilBrown16f17b32006-06-26 00:27:37 -07002862 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002863
NeilBrown72e02072009-12-14 12:49:55 +11002864 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002865 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002866 if (msec == 0)
2867 mddev->safemode_delay = 0;
2868 else {
NeilBrown19052c02008-08-05 15:54:13 +10002869 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002870 mddev->safemode_delay = (msec*HZ)/1000;
2871 if (mddev->safemode_delay == 0)
2872 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002873 if (mddev->safemode_delay < old_delay)
2874 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002875 }
2876 return len;
2877}
2878static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002879__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002880
2881static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002882level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002883{
NeilBrown2604b702006-01-06 00:20:36 -08002884 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002885 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002886 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002887 else if (mddev->clevel[0])
2888 return sprintf(page, "%s\n", mddev->clevel);
2889 else if (mddev->level != LEVEL_NONE)
2890 return sprintf(page, "%d\n", mddev->level);
2891 else
2892 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002893}
2894
NeilBrownd9d166c2006-01-06 00:20:51 -08002895static ssize_t
2896level_store(mddev_t *mddev, const char *buf, size_t len)
2897{
NeilBrown245f46c2009-03-31 14:39:39 +11002898 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002899 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002900 struct mdk_personality *pers;
2901 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002902 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002903
2904 if (mddev->pers == NULL) {
2905 if (len == 0)
2906 return 0;
2907 if (len >= sizeof(mddev->clevel))
2908 return -ENOSPC;
2909 strncpy(mddev->clevel, buf, len);
2910 if (mddev->clevel[len-1] == '\n')
2911 len--;
2912 mddev->clevel[len] = 0;
2913 mddev->level = LEVEL_NONE;
2914 return rv;
2915 }
2916
2917 /* request to change the personality. Need to ensure:
2918 * - array is not engaged in resync/recovery/reshape
2919 * - old personality can be suspended
2920 * - new personality will access other array.
2921 */
2922
2923 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002924 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002925
2926 if (!mddev->pers->quiesce) {
2927 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2928 mdname(mddev), mddev->pers->name);
2929 return -EINVAL;
2930 }
2931
2932 /* Now find the new personality */
2933 if (len == 0 || len >= sizeof(level))
2934 return -EINVAL;
2935 strncpy(level, buf, len);
2936 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002937 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002938 level[len] = 0;
2939
2940 request_module("md-%s", level);
2941 spin_lock(&pers_lock);
2942 pers = find_pers(LEVEL_NONE, level);
2943 if (!pers || !try_module_get(pers->owner)) {
2944 spin_unlock(&pers_lock);
2945 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2946 return -EINVAL;
2947 }
2948 spin_unlock(&pers_lock);
2949
2950 if (pers == mddev->pers) {
2951 /* Nothing to do! */
2952 module_put(pers->owner);
2953 return rv;
2954 }
2955 if (!pers->takeover) {
2956 module_put(pers->owner);
2957 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2958 mdname(mddev), level);
2959 return -EINVAL;
2960 }
2961
2962 /* ->takeover must set new_* and/or delta_disks
2963 * if it succeeds, and may set them when it fails.
2964 */
2965 priv = pers->takeover(mddev);
2966 if (IS_ERR(priv)) {
2967 mddev->new_level = mddev->level;
2968 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002969 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002970 mddev->raid_disks -= mddev->delta_disks;
2971 mddev->delta_disks = 0;
2972 module_put(pers->owner);
2973 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2974 mdname(mddev), level);
2975 return PTR_ERR(priv);
2976 }
2977
2978 /* Looks like we have a winner */
2979 mddev_suspend(mddev);
2980 mddev->pers->stop(mddev);
2981 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002982 /* Invalidate devices that are now superfluous */
2983 list_for_each_entry(rdev, &mddev->disks, same_set)
2984 if (rdev->raid_disk >= mddev->raid_disks) {
2985 rdev->raid_disk = -1;
2986 clear_bit(In_sync, &rdev->flags);
2987 }
NeilBrown245f46c2009-03-31 14:39:39 +11002988 mddev->pers = pers;
2989 mddev->private = priv;
2990 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2991 mddev->level = mddev->new_level;
2992 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002993 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002994 mddev->delta_disks = 0;
2995 pers->run(mddev);
2996 mddev_resume(mddev);
2997 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2998 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2999 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08003000 return rv;
3001}
3002
3003static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003004__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003005
NeilBrownd4dbd022006-06-26 00:27:59 -07003006
3007static ssize_t
3008layout_show(mddev_t *mddev, char *page)
3009{
3010 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003011 if (mddev->reshape_position != MaxSector &&
3012 mddev->layout != mddev->new_layout)
3013 return sprintf(page, "%d (%d)\n",
3014 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003015 return sprintf(page, "%d\n", mddev->layout);
3016}
3017
3018static ssize_t
3019layout_store(mddev_t *mddev, const char *buf, size_t len)
3020{
3021 char *e;
3022 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003023
3024 if (!*buf || (*e && *e != '\n'))
3025 return -EINVAL;
3026
NeilBrownb3546032009-03-31 14:56:41 +11003027 if (mddev->pers) {
3028 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003029 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003030 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003031 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003032 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003033 if (err) {
3034 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003035 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003036 }
NeilBrownb3546032009-03-31 14:56:41 +11003037 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003038 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003039 if (mddev->reshape_position == MaxSector)
3040 mddev->layout = n;
3041 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003042 return len;
3043}
3044static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003045__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003046
3047
NeilBrowneae17012005-11-08 21:39:23 -08003048static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003049raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003050{
NeilBrownbb636542005-11-08 21:39:45 -08003051 if (mddev->raid_disks == 0)
3052 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003053 if (mddev->reshape_position != MaxSector &&
3054 mddev->delta_disks != 0)
3055 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3056 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003057 return sprintf(page, "%d\n", mddev->raid_disks);
3058}
3059
NeilBrownda943b992006-01-06 00:20:54 -08003060static int update_raid_disks(mddev_t *mddev, int raid_disks);
3061
3062static ssize_t
3063raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3064{
NeilBrownda943b992006-01-06 00:20:54 -08003065 char *e;
3066 int rv = 0;
3067 unsigned long n = simple_strtoul(buf, &e, 10);
3068
3069 if (!*buf || (*e && *e != '\n'))
3070 return -EINVAL;
3071
3072 if (mddev->pers)
3073 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003074 else if (mddev->reshape_position != MaxSector) {
3075 int olddisks = mddev->raid_disks - mddev->delta_disks;
3076 mddev->delta_disks = n - olddisks;
3077 mddev->raid_disks = n;
3078 } else
NeilBrownda943b992006-01-06 00:20:54 -08003079 mddev->raid_disks = n;
3080 return rv ? rv : len;
3081}
3082static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003083__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003084
NeilBrown24dd4692005-11-08 21:39:26 -08003085static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003086chunk_size_show(mddev_t *mddev, char *page)
3087{
NeilBrown08a02ec2007-05-09 02:35:38 -07003088 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003089 mddev->chunk_sectors != mddev->new_chunk_sectors)
3090 return sprintf(page, "%d (%d)\n",
3091 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003092 mddev->chunk_sectors << 9);
3093 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003094}
3095
3096static ssize_t
3097chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3098{
NeilBrown3b343802006-01-06 00:20:47 -08003099 char *e;
3100 unsigned long n = simple_strtoul(buf, &e, 10);
3101
NeilBrown3b343802006-01-06 00:20:47 -08003102 if (!*buf || (*e && *e != '\n'))
3103 return -EINVAL;
3104
NeilBrownb3546032009-03-31 14:56:41 +11003105 if (mddev->pers) {
3106 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003107 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003108 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003109 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003110 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003111 if (err) {
3112 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003113 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003114 }
NeilBrownb3546032009-03-31 14:56:41 +11003115 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003116 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003117 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003118 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003119 }
NeilBrown3b343802006-01-06 00:20:47 -08003120 return len;
3121}
3122static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003123__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003124
NeilBrowna94213b2006-06-26 00:28:00 -07003125static ssize_t
3126resync_start_show(mddev_t *mddev, char *page)
3127{
NeilBrownd1a7c502009-03-31 15:24:32 +11003128 if (mddev->recovery_cp == MaxSector)
3129 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003130 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3131}
3132
3133static ssize_t
3134resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3135{
NeilBrowna94213b2006-06-26 00:28:00 -07003136 char *e;
3137 unsigned long long n = simple_strtoull(buf, &e, 10);
3138
3139 if (mddev->pers)
3140 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003141 if (cmd_match(buf, "none"))
3142 n = MaxSector;
3143 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003144 return -EINVAL;
3145
3146 mddev->recovery_cp = n;
3147 return len;
3148}
3149static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003150__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003151
NeilBrown9e653b62006-06-26 00:27:58 -07003152/*
3153 * The array state can be:
3154 *
3155 * clear
3156 * No devices, no size, no level
3157 * Equivalent to STOP_ARRAY ioctl
3158 * inactive
3159 * May have some settings, but array is not active
3160 * all IO results in error
3161 * When written, doesn't tear down array, but just stops it
3162 * suspended (not supported yet)
3163 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003164 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003165 * readonly
3166 * no resync can happen. no superblocks get written.
3167 * write requests fail
3168 * read-auto
3169 * like readonly, but behaves like 'clean' on a write request.
3170 *
3171 * clean - no pending writes, but otherwise active.
3172 * When written to inactive array, starts without resync
3173 * If a write request arrives then
3174 * if metadata is known, mark 'dirty' and switch to 'active'.
3175 * if not known, block and switch to write-pending
3176 * If written to an active array that has pending writes, then fails.
3177 * active
3178 * fully active: IO and resync can be happening.
3179 * When written to inactive array, starts with resync
3180 *
3181 * write-pending
3182 * clean, but writes are blocked waiting for 'active' to be written.
3183 *
3184 * active-idle
3185 * like active, but no writes have been seen for a while (100msec).
3186 *
3187 */
3188enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3189 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003190static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003191 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3192 "write-pending", "active-idle", NULL };
3193
3194static int match_word(const char *word, char **list)
3195{
3196 int n;
3197 for (n=0; list[n]; n++)
3198 if (cmd_match(word, list[n]))
3199 break;
3200 return n;
3201}
3202
3203static ssize_t
3204array_state_show(mddev_t *mddev, char *page)
3205{
3206 enum array_state st = inactive;
3207
3208 if (mddev->pers)
3209 switch(mddev->ro) {
3210 case 1:
3211 st = readonly;
3212 break;
3213 case 2:
3214 st = read_auto;
3215 break;
3216 case 0:
3217 if (mddev->in_sync)
3218 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003219 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3220 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003221 else if (mddev->safemode)
3222 st = active_idle;
3223 else
3224 st = active;
3225 }
3226 else {
3227 if (list_empty(&mddev->disks) &&
3228 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003229 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003230 st = clear;
3231 else
3232 st = inactive;
3233 }
3234 return sprintf(page, "%s\n", array_states[st]);
3235}
3236
Neil Browndf5b20c2008-07-11 22:02:22 +10003237static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003238static int do_md_run(mddev_t * mddev);
3239static int restart_array(mddev_t *mddev);
3240
3241static ssize_t
3242array_state_store(mddev_t *mddev, const char *buf, size_t len)
3243{
3244 int err = -EINVAL;
3245 enum array_state st = match_word(buf, array_states);
3246 switch(st) {
3247 case bad_word:
3248 break;
3249 case clear:
3250 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003251 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003252 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003253 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003254 break;
3255 case inactive:
3256 /* stopping an active array */
3257 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003258 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003259 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003260 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003261 } else
3262 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003263 break;
3264 case suspended:
3265 break; /* not supported yet */
3266 case readonly:
3267 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003268 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003269 else {
3270 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003271 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003272 err = do_md_run(mddev);
3273 }
3274 break;
3275 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003276 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003277 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003278 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003279 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003280 err = restart_array(mddev);
3281 if (err == 0) {
3282 mddev->ro = 2;
3283 set_disk_ro(mddev->gendisk, 0);
3284 }
NeilBrown9e653b62006-06-26 00:27:58 -07003285 } else {
3286 mddev->ro = 2;
3287 err = do_md_run(mddev);
3288 }
3289 break;
3290 case clean:
3291 if (mddev->pers) {
3292 restart_array(mddev);
3293 spin_lock_irq(&mddev->write_lock);
3294 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003295 if (mddev->in_sync == 0) {
3296 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003297 if (mddev->safemode == 1)
3298 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003299 if (mddev->persistent)
3300 set_bit(MD_CHANGE_CLEAN,
3301 &mddev->flags);
3302 }
3303 err = 0;
3304 } else
3305 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003306 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003307 } else
3308 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003309 break;
3310 case active:
3311 if (mddev->pers) {
3312 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003313 if (mddev->external)
3314 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003315 wake_up(&mddev->sb_wait);
3316 err = 0;
3317 } else {
3318 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003319 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003320 err = do_md_run(mddev);
3321 }
3322 break;
3323 case write_pending:
3324 case active_idle:
3325 /* these cannot be set */
3326 break;
3327 }
3328 if (err)
3329 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003330 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003331 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003332 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003333 }
NeilBrown9e653b62006-06-26 00:27:58 -07003334}
NeilBrown80ca3a42006-07-10 04:44:18 -07003335static struct md_sysfs_entry md_array_state =
3336__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003337
NeilBrown6d7ff7382006-01-06 00:21:16 -08003338static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003339max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3340 return sprintf(page, "%d\n",
3341 atomic_read(&mddev->max_corr_read_errors));
3342}
3343
3344static ssize_t
3345max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3346{
3347 char *e;
3348 unsigned long n = simple_strtoul(buf, &e, 10);
3349
3350 if (*buf && (*e == 0 || *e == '\n')) {
3351 atomic_set(&mddev->max_corr_read_errors, n);
3352 return len;
3353 }
3354 return -EINVAL;
3355}
3356
3357static struct md_sysfs_entry max_corr_read_errors =
3358__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3359 max_corrected_read_errors_store);
3360
3361static ssize_t
NeilBrown6d7ff7382006-01-06 00:21:16 -08003362null_show(mddev_t *mddev, char *page)
3363{
3364 return -EINVAL;
3365}
3366
3367static ssize_t
3368new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3369{
3370 /* buf must be %d:%d\n? giving major and minor numbers */
3371 /* The new device is added to the array.
3372 * If the array has a persistent superblock, we read the
3373 * superblock to initialise info and check validity.
3374 * Otherwise, only checking done is that in bind_rdev_to_array,
3375 * which mainly checks size.
3376 */
3377 char *e;
3378 int major = simple_strtoul(buf, &e, 10);
3379 int minor;
3380 dev_t dev;
3381 mdk_rdev_t *rdev;
3382 int err;
3383
3384 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3385 return -EINVAL;
3386 minor = simple_strtoul(e+1, &e, 10);
3387 if (*e && *e != '\n')
3388 return -EINVAL;
3389 dev = MKDEV(major, minor);
3390 if (major != MAJOR(dev) ||
3391 minor != MINOR(dev))
3392 return -EOVERFLOW;
3393
3394
3395 if (mddev->persistent) {
3396 rdev = md_import_device(dev, mddev->major_version,
3397 mddev->minor_version);
3398 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3399 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3400 mdk_rdev_t, same_set);
3401 err = super_types[mddev->major_version]
3402 .load_super(rdev, rdev0, mddev->minor_version);
3403 if (err < 0)
3404 goto out;
3405 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003406 } else if (mddev->external)
3407 rdev = md_import_device(dev, -2, -1);
3408 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003409 rdev = md_import_device(dev, -1, -1);
3410
3411 if (IS_ERR(rdev))
3412 return PTR_ERR(rdev);
3413 err = bind_rdev_to_array(rdev, mddev);
3414 out:
3415 if (err)
3416 export_rdev(rdev);
3417 return err ? err : len;
3418}
3419
3420static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003421__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003422
3423static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003424bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3425{
3426 char *end;
3427 unsigned long chunk, end_chunk;
3428
3429 if (!mddev->bitmap)
3430 goto out;
3431 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3432 while (*buf) {
3433 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3434 if (buf == end) break;
3435 if (*end == '-') { /* range */
3436 buf = end + 1;
3437 end_chunk = simple_strtoul(buf, &end, 0);
3438 if (buf == end) break;
3439 }
3440 if (*end && !isspace(*end)) break;
3441 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3442 buf = end;
3443 while (isspace(*buf)) buf++;
3444 }
3445 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3446out:
3447 return len;
3448}
3449
3450static struct md_sysfs_entry md_bitmap =
3451__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3452
3453static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003454size_show(mddev_t *mddev, char *page)
3455{
Andre Noll58c0fed2009-03-31 14:33:13 +11003456 return sprintf(page, "%llu\n",
3457 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003458}
3459
Andre Nolld71f9f82008-07-11 22:02:22 +10003460static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003461
3462static ssize_t
3463size_store(mddev_t *mddev, const char *buf, size_t len)
3464{
3465 /* If array is inactive, we can reduce the component size, but
3466 * not increase it (except from 0).
3467 * If array is active, we can try an on-line resize
3468 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003469 sector_t sectors;
3470 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003471
Andre Noll58c0fed2009-03-31 14:33:13 +11003472 if (err < 0)
3473 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003474 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003475 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07003476 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003477 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003478 if (mddev->dev_sectors == 0 ||
3479 mddev->dev_sectors > sectors)
3480 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003481 else
3482 err = -ENOSPC;
3483 }
3484 return err ? err : len;
3485}
3486
3487static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003488__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003489
NeilBrown8bb93aa2006-01-06 00:20:50 -08003490
3491/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003492 * This is one of
3493 * 'none' for arrays with no metadata (good luck...)
3494 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003495 * or N.M for internally known formats
3496 */
3497static ssize_t
3498metadata_show(mddev_t *mddev, char *page)
3499{
3500 if (mddev->persistent)
3501 return sprintf(page, "%d.%d\n",
3502 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003503 else if (mddev->external)
3504 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003505 else
3506 return sprintf(page, "none\n");
3507}
3508
3509static ssize_t
3510metadata_store(mddev_t *mddev, const char *buf, size_t len)
3511{
3512 int major, minor;
3513 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003514 /* Changing the details of 'external' metadata is
3515 * always permitted. Otherwise there must be
3516 * no devices attached to the array.
3517 */
3518 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3519 ;
3520 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003521 return -EBUSY;
3522
3523 if (cmd_match(buf, "none")) {
3524 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003525 mddev->external = 0;
3526 mddev->major_version = 0;
3527 mddev->minor_version = 90;
3528 return len;
3529 }
3530 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003531 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003532 if (namelen >= sizeof(mddev->metadata_type))
3533 namelen = sizeof(mddev->metadata_type)-1;
3534 strncpy(mddev->metadata_type, buf+9, namelen);
3535 mddev->metadata_type[namelen] = 0;
3536 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3537 mddev->metadata_type[--namelen] = 0;
3538 mddev->persistent = 0;
3539 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003540 mddev->major_version = 0;
3541 mddev->minor_version = 90;
3542 return len;
3543 }
3544 major = simple_strtoul(buf, &e, 10);
3545 if (e==buf || *e != '.')
3546 return -EINVAL;
3547 buf = e+1;
3548 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003549 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003550 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003551 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003552 return -ENOENT;
3553 mddev->major_version = major;
3554 mddev->minor_version = minor;
3555 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003556 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003557 return len;
3558}
3559
3560static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003561__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003562
NeilBrowna35b0d62006-01-06 00:20:49 -08003563static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003564action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003565{
NeilBrown7eec3142005-11-08 21:39:44 -08003566 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003567 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3568 type = "frozen";
3569 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003570 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003571 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3572 type = "reshape";
3573 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003574 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3575 type = "resync";
3576 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3577 type = "check";
3578 else
3579 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003580 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003581 type = "recover";
3582 }
3583 return sprintf(page, "%s\n", type);
3584}
3585
3586static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003587action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003588{
NeilBrown7eec3142005-11-08 21:39:44 -08003589 if (!mddev->pers || !mddev->pers->sync_request)
3590 return -EINVAL;
3591
NeilBrownb6a9ce62009-05-26 09:41:17 +10003592 if (cmd_match(page, "frozen"))
3593 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3594 else
3595 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3596
3597 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003598 if (mddev->sync_thread) {
3599 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3600 md_unregister_thread(mddev->sync_thread);
3601 mddev->sync_thread = NULL;
3602 mddev->recovery = 0;
3603 }
NeilBrown03c902e2006-01-06 00:20:46 -08003604 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3605 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003606 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003607 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003608 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003609 else if (cmd_match(page, "recover")) {
3610 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3611 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3612 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003613 int err;
3614 if (mddev->pers->start_reshape == NULL)
3615 return -EINVAL;
3616 err = mddev->pers->start_reshape(mddev);
3617 if (err)
3618 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003619 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003620 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003621 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003622 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003623 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003624 return -EINVAL;
3625 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3626 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003627 }
NeilBrown03c902e2006-01-06 00:20:46 -08003628 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003629 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003630 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003631 return len;
3632}
3633
NeilBrown9d888832005-11-08 21:39:26 -08003634static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003635mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003636{
3637 return sprintf(page, "%llu\n",
3638 (unsigned long long) mddev->resync_mismatches);
3639}
3640
NeilBrown80ca3a42006-07-10 04:44:18 -07003641static struct md_sysfs_entry md_scan_mode =
3642__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003643
NeilBrown96de1e62005-11-08 21:39:39 -08003644
NeilBrown80ca3a42006-07-10 04:44:18 -07003645static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003646
NeilBrown88202a02006-01-06 00:21:36 -08003647static ssize_t
3648sync_min_show(mddev_t *mddev, char *page)
3649{
3650 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3651 mddev->sync_speed_min ? "local": "system");
3652}
3653
3654static ssize_t
3655sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3656{
3657 int min;
3658 char *e;
3659 if (strncmp(buf, "system", 6)==0) {
3660 mddev->sync_speed_min = 0;
3661 return len;
3662 }
3663 min = simple_strtoul(buf, &e, 10);
3664 if (buf == e || (*e && *e != '\n') || min <= 0)
3665 return -EINVAL;
3666 mddev->sync_speed_min = min;
3667 return len;
3668}
3669
3670static struct md_sysfs_entry md_sync_min =
3671__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3672
3673static ssize_t
3674sync_max_show(mddev_t *mddev, char *page)
3675{
3676 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3677 mddev->sync_speed_max ? "local": "system");
3678}
3679
3680static ssize_t
3681sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3682{
3683 int max;
3684 char *e;
3685 if (strncmp(buf, "system", 6)==0) {
3686 mddev->sync_speed_max = 0;
3687 return len;
3688 }
3689 max = simple_strtoul(buf, &e, 10);
3690 if (buf == e || (*e && *e != '\n') || max <= 0)
3691 return -EINVAL;
3692 mddev->sync_speed_max = max;
3693 return len;
3694}
3695
3696static struct md_sysfs_entry md_sync_max =
3697__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3698
Iustin Popd7f3d292007-10-16 23:30:54 -07003699static ssize_t
3700degraded_show(mddev_t *mddev, char *page)
3701{
3702 return sprintf(page, "%d\n", mddev->degraded);
3703}
3704static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003705
3706static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003707sync_force_parallel_show(mddev_t *mddev, char *page)
3708{
3709 return sprintf(page, "%d\n", mddev->parallel_resync);
3710}
3711
3712static ssize_t
3713sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3714{
3715 long n;
3716
3717 if (strict_strtol(buf, 10, &n))
3718 return -EINVAL;
3719
3720 if (n != 0 && n != 1)
3721 return -EINVAL;
3722
3723 mddev->parallel_resync = n;
3724
3725 if (mddev->sync_thread)
3726 wake_up(&resync_wait);
3727
3728 return len;
3729}
3730
3731/* force parallel resync, even with shared block devices */
3732static struct md_sysfs_entry md_sync_force_parallel =
3733__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3734 sync_force_parallel_show, sync_force_parallel_store);
3735
3736static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003737sync_speed_show(mddev_t *mddev, char *page)
3738{
3739 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003740 if (mddev->curr_resync == 0)
3741 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003742 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3743 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003744 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003745 db = resync - mddev->resync_mark_cnt;
3746 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003747}
3748
NeilBrown80ca3a42006-07-10 04:44:18 -07003749static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003750
3751static ssize_t
3752sync_completed_show(mddev_t *mddev, char *page)
3753{
Andre Noll58c0fed2009-03-31 14:33:13 +11003754 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003755
NeilBrownacb180b2009-04-14 16:28:34 +10003756 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3757 return sprintf(page, "none\n");
3758
NeilBrown88202a02006-01-06 00:21:36 -08003759 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003760 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003761 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003762 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003763
NeilBrownacb180b2009-04-14 16:28:34 +10003764 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003765 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003766}
3767
NeilBrown80ca3a42006-07-10 04:44:18 -07003768static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003769
NeilBrowne464eaf2006-03-27 01:18:14 -08003770static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003771min_sync_show(mddev_t *mddev, char *page)
3772{
3773 return sprintf(page, "%llu\n",
3774 (unsigned long long)mddev->resync_min);
3775}
3776static ssize_t
3777min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3778{
3779 unsigned long long min;
3780 if (strict_strtoull(buf, 10, &min))
3781 return -EINVAL;
3782 if (min > mddev->resync_max)
3783 return -EINVAL;
3784 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3785 return -EBUSY;
3786
3787 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003788 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003789 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003790 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003791 return -EINVAL;
3792 }
3793 mddev->resync_min = min;
3794
3795 return len;
3796}
3797
3798static struct md_sysfs_entry md_min_sync =
3799__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3800
3801static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003802max_sync_show(mddev_t *mddev, char *page)
3803{
3804 if (mddev->resync_max == MaxSector)
3805 return sprintf(page, "max\n");
3806 else
3807 return sprintf(page, "%llu\n",
3808 (unsigned long long)mddev->resync_max);
3809}
3810static ssize_t
3811max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3812{
3813 if (strncmp(buf, "max", 3) == 0)
3814 mddev->resync_max = MaxSector;
3815 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003816 unsigned long long max;
3817 if (strict_strtoull(buf, 10, &max))
3818 return -EINVAL;
3819 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003820 return -EINVAL;
3821 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003822 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003823 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3824 return -EBUSY;
3825
3826 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003827 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003828 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003829 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003830 return -EINVAL;
3831 }
3832 mddev->resync_max = max;
3833 }
3834 wake_up(&mddev->recovery_wait);
3835 return len;
3836}
3837
3838static struct md_sysfs_entry md_max_sync =
3839__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3840
3841static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003842suspend_lo_show(mddev_t *mddev, char *page)
3843{
3844 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3845}
3846
3847static ssize_t
3848suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3849{
3850 char *e;
3851 unsigned long long new = simple_strtoull(buf, &e, 10);
3852
NeilBrownb8d966e2009-07-01 11:14:04 +10003853 if (mddev->pers == NULL ||
3854 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003855 return -EINVAL;
3856 if (buf == e || (*e && *e != '\n'))
3857 return -EINVAL;
3858 if (new >= mddev->suspend_hi ||
3859 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3860 mddev->suspend_lo = new;
3861 mddev->pers->quiesce(mddev, 2);
3862 return len;
3863 } else
3864 return -EINVAL;
3865}
3866static struct md_sysfs_entry md_suspend_lo =
3867__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3868
3869
3870static ssize_t
3871suspend_hi_show(mddev_t *mddev, char *page)
3872{
3873 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3874}
3875
3876static ssize_t
3877suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3878{
3879 char *e;
3880 unsigned long long new = simple_strtoull(buf, &e, 10);
3881
NeilBrownb8d966e2009-07-01 11:14:04 +10003882 if (mddev->pers == NULL ||
3883 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003884 return -EINVAL;
3885 if (buf == e || (*e && *e != '\n'))
3886 return -EINVAL;
3887 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3888 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3889 mddev->suspend_hi = new;
3890 mddev->pers->quiesce(mddev, 1);
3891 mddev->pers->quiesce(mddev, 0);
3892 return len;
3893 } else
3894 return -EINVAL;
3895}
3896static struct md_sysfs_entry md_suspend_hi =
3897__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3898
NeilBrown08a02ec2007-05-09 02:35:38 -07003899static ssize_t
3900reshape_position_show(mddev_t *mddev, char *page)
3901{
3902 if (mddev->reshape_position != MaxSector)
3903 return sprintf(page, "%llu\n",
3904 (unsigned long long)mddev->reshape_position);
3905 strcpy(page, "none\n");
3906 return 5;
3907}
3908
3909static ssize_t
3910reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3911{
3912 char *e;
3913 unsigned long long new = simple_strtoull(buf, &e, 10);
3914 if (mddev->pers)
3915 return -EBUSY;
3916 if (buf == e || (*e && *e != '\n'))
3917 return -EINVAL;
3918 mddev->reshape_position = new;
3919 mddev->delta_disks = 0;
3920 mddev->new_level = mddev->level;
3921 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003922 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003923 return len;
3924}
3925
3926static struct md_sysfs_entry md_reshape_position =
3927__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3928 reshape_position_store);
3929
Dan Williamsb522adc2009-03-31 15:00:31 +11003930static ssize_t
3931array_size_show(mddev_t *mddev, char *page)
3932{
3933 if (mddev->external_size)
3934 return sprintf(page, "%llu\n",
3935 (unsigned long long)mddev->array_sectors/2);
3936 else
3937 return sprintf(page, "default\n");
3938}
3939
3940static ssize_t
3941array_size_store(mddev_t *mddev, const char *buf, size_t len)
3942{
3943 sector_t sectors;
3944
3945 if (strncmp(buf, "default", 7) == 0) {
3946 if (mddev->pers)
3947 sectors = mddev->pers->size(mddev, 0, 0);
3948 else
3949 sectors = mddev->array_sectors;
3950
3951 mddev->external_size = 0;
3952 } else {
3953 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3954 return -EINVAL;
3955 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003956 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003957
3958 mddev->external_size = 1;
3959 }
3960
3961 mddev->array_sectors = sectors;
3962 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003963 if (mddev->pers)
3964 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003965
3966 return len;
3967}
3968
3969static struct md_sysfs_entry md_array_size =
3970__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3971 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003972
NeilBrowneae17012005-11-08 21:39:23 -08003973static struct attribute *md_default_attrs[] = {
3974 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003975 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003976 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003977 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003978 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003979 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003980 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003981 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003982 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003983 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003984 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003985 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11003986 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003987 NULL,
3988};
3989
3990static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003991 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003992 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003993 &md_sync_min.attr,
3994 &md_sync_max.attr,
3995 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003996 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003997 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003998 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003999 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004000 &md_suspend_lo.attr,
4001 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004002 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004003 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004004 NULL,
4005};
NeilBrown411036f2005-11-08 21:39:40 -08004006static struct attribute_group md_redundancy_group = {
4007 .name = NULL,
4008 .attrs = md_redundancy_attrs,
4009};
4010
NeilBrowneae17012005-11-08 21:39:23 -08004011
4012static ssize_t
4013md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4014{
4015 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4016 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004017 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004018
4019 if (!entry->show)
4020 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004021 rv = mddev_lock(mddev);
4022 if (!rv) {
4023 rv = entry->show(mddev, page);
4024 mddev_unlock(mddev);
4025 }
NeilBrown96de1e62005-11-08 21:39:39 -08004026 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004027}
4028
4029static ssize_t
4030md_attr_store(struct kobject *kobj, struct attribute *attr,
4031 const char *page, size_t length)
4032{
4033 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4034 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004035 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004036
4037 if (!entry->store)
4038 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004039 if (!capable(CAP_SYS_ADMIN))
4040 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004041 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004042 if (mddev->hold_active == UNTIL_IOCTL)
4043 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004044 if (!rv) {
4045 rv = entry->store(mddev, page, length);
4046 mddev_unlock(mddev);
4047 }
NeilBrown96de1e62005-11-08 21:39:39 -08004048 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004049}
4050
4051static void md_free(struct kobject *ko)
4052{
4053 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004054
4055 if (mddev->sysfs_state)
4056 sysfs_put(mddev->sysfs_state);
4057
4058 if (mddev->gendisk) {
4059 del_gendisk(mddev->gendisk);
4060 put_disk(mddev->gendisk);
4061 }
4062 if (mddev->queue)
4063 blk_cleanup_queue(mddev->queue);
4064
NeilBrowneae17012005-11-08 21:39:23 -08004065 kfree(mddev);
4066}
4067
4068static struct sysfs_ops md_sysfs_ops = {
4069 .show = md_attr_show,
4070 .store = md_attr_store,
4071};
4072static struct kobj_type md_ktype = {
4073 .release = md_free,
4074 .sysfs_ops = &md_sysfs_ops,
4075 .default_attrs = md_default_attrs,
4076};
4077
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078int mdp_major = 0;
4079
Dan Williams5fd3a172009-03-04 00:57:25 -07004080static void mddev_delayed_delete(struct work_struct *ws)
4081{
4082 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4083
4084 if (mddev->private == &md_redundancy_group) {
4085 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
4086 if (mddev->sysfs_action)
4087 sysfs_put(mddev->sysfs_action);
4088 mddev->sysfs_action = NULL;
4089 mddev->private = NULL;
4090 }
NeilBrown43a70502009-12-14 12:49:55 +11004091 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004092 kobject_del(&mddev->kobj);
4093 kobject_put(&mddev->kobj);
4094}
4095
NeilBrownefeb53c2009-01-09 08:31:10 +11004096static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004098 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 mddev_t *mddev = mddev_find(dev);
4100 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004101 int partitioned;
4102 int shift;
4103 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004104 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
4106 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004107 return -ENODEV;
4108
4109 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4110 shift = partitioned ? MdpMinorShift : 0;
4111 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
NeilBrownd3374822009-01-09 08:31:10 +11004113 /* wait for any previous instance if this device
4114 * to be completed removed (mddev_delayed_delete).
4115 */
4116 flush_scheduled_work();
4117
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004118 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004119 error = -EEXIST;
4120 if (mddev->gendisk)
4121 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004122
4123 if (name) {
4124 /* Need to ensure that 'name' is not a duplicate.
4125 */
4126 mddev_t *mddev2;
4127 spin_lock(&all_mddevs_lock);
4128
4129 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4130 if (mddev2->gendisk &&
4131 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4132 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004133 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004134 }
4135 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 }
NeilBrown8b765392009-01-09 08:31:08 +11004137
NeilBrown0909dc42009-07-01 12:27:21 +10004138 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004139 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004140 if (!mddev->queue)
4141 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004142 mddev->queue->queuedata = mddev;
4143
NeilBrown8b765392009-01-09 08:31:08 +11004144 /* Can be unlocked because the queue is new: no concurrency */
4145 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4146
NeilBrown409c57f2009-03-31 14:39:39 +11004147 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004148
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 disk = alloc_disk(1 << shift);
4150 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004151 blk_cleanup_queue(mddev->queue);
4152 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004153 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004155 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004157 if (name)
4158 strcpy(disk->disk_name, name);
4159 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004161 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 disk->fops = &md_fops;
4164 disk->private_data = mddev;
4165 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004166 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004167 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004168 * remove it now.
4169 */
4170 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 add_disk(disk);
4172 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004173 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4174 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004175 if (error) {
4176 /* This isn't possible, but as kobject_init_and_add is marked
4177 * __must_check, we must do something with the result
4178 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004179 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4180 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004181 error = 0;
4182 }
NeilBrown43a70502009-12-14 12:49:55 +11004183 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4184 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004185 abort:
4186 mutex_unlock(&disks_mutex);
4187 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004188 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004189 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4190 }
NeilBrownd3374822009-01-09 08:31:10 +11004191 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004192 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004193}
4194
4195static struct kobject *md_probe(dev_t dev, int *part, void *data)
4196{
4197 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 return NULL;
4199}
4200
NeilBrownefeb53c2009-01-09 08:31:10 +11004201static int add_named_array(const char *val, struct kernel_param *kp)
4202{
4203 /* val must be "md_*" where * is not all digits.
4204 * We allocate an array with a large free minor number, and
4205 * set the name to val. val must not already be an active name.
4206 */
4207 int len = strlen(val);
4208 char buf[DISK_NAME_LEN];
4209
4210 while (len && val[len-1] == '\n')
4211 len--;
4212 if (len >= DISK_NAME_LEN)
4213 return -E2BIG;
4214 strlcpy(buf, val, len+1);
4215 if (strncmp(buf, "md_", 3) != 0)
4216 return -EINVAL;
4217 return md_alloc(0, buf);
4218}
4219
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220static void md_safemode_timeout(unsigned long data)
4221{
4222 mddev_t *mddev = (mddev_t *) data;
4223
Neil Brown0fd62b82008-06-28 08:31:36 +10004224 if (!atomic_read(&mddev->writes_pending)) {
4225 mddev->safemode = 1;
4226 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004227 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 md_wakeup_thread(mddev->thread);
4230}
4231
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004232static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
4234static int do_md_run(mddev_t * mddev)
4235{
NeilBrown2604b702006-01-06 00:20:36 -08004236 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 mdk_rdev_t *rdev;
4238 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004239 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
NeilBrowna757e642005-04-16 15:26:42 -07004241 if (list_empty(&mddev->disks))
4242 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
4245 if (mddev->pers)
4246 return -EBUSY;
4247
4248 /*
4249 * Analyze all RAID superblock(s)
4250 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004251 if (!mddev->raid_disks) {
4252 if (!mddev->persistent)
4253 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004254 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
NeilBrownd9d166c2006-01-06 00:20:51 -08004257 if (mddev->level != LEVEL_NONE)
4258 request_module("md-level-%d", mddev->level);
4259 else if (mddev->clevel[0])
4260 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
4262 /*
4263 * Drop all container device buffers, from now on
4264 * the only valid external interface is through the md
4265 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004267 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004268 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 continue;
4270 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004271 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004272
4273 /* perform some consistency tests on the device.
4274 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004275 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004276 */
Andre Noll0f420352008-07-11 22:02:23 +10004277 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004278 if (mddev->dev_sectors &&
4279 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004280 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004281 printk("md: %s: data overlaps metadata\n",
4282 mdname(mddev));
4283 return -EINVAL;
4284 }
4285 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004286 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004287 > rdev->data_offset) {
4288 printk("md: %s: metadata overlaps data\n",
4289 mdname(mddev));
4290 return -EINVAL;
4291 }
4292 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004293 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 }
4295
4296 md_probe(mddev->unit, NULL, NULL);
4297 disk = mddev->gendisk;
4298 if (!disk)
4299 return -ENOMEM;
4300
4301 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004302 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004303 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004305 if (mddev->level != LEVEL_NONE)
4306 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4307 mddev->level);
4308 else
4309 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4310 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 return -EINVAL;
4312 }
NeilBrown2604b702006-01-06 00:20:36 -08004313 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004315 if (mddev->level != pers->level) {
4316 mddev->level = pers->level;
4317 mddev->new_level = pers->level;
4318 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004319 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
NeilBrownf6705572006-03-27 01:18:11 -08004321 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004322 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004323 /* This personality cannot handle reshaping... */
4324 mddev->pers = NULL;
4325 module_put(pers->owner);
4326 return -EINVAL;
4327 }
4328
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004329 if (pers->sync_request) {
4330 /* Warn if this is a potentially silly
4331 * configuration.
4332 */
4333 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4334 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004335 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004336
4337 list_for_each_entry(rdev, &mddev->disks, same_set)
4338 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004339 if (rdev < rdev2 &&
4340 rdev->bdev->bd_contains ==
4341 rdev2->bdev->bd_contains) {
4342 printk(KERN_WARNING
4343 "%s: WARNING: %s appears to be"
4344 " on the same physical disk as"
4345 " %s.\n",
4346 mdname(mddev),
4347 bdevname(rdev->bdev,b),
4348 bdevname(rdev2->bdev,b2));
4349 warned = 1;
4350 }
4351 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004352
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004353 if (warned)
4354 printk(KERN_WARNING
4355 "True protection against single-disk"
4356 " failure might be compromised.\n");
4357 }
4358
NeilBrown657390d2005-08-26 18:34:16 -07004359 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004360 /* may be over-ridden by personality */
4361 mddev->resync_max_sectors = mddev->dev_sectors;
4362
NeilBrowna9701a32005-11-08 21:39:34 -08004363 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004364 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
NeilBrownf91de922005-11-08 21:39:36 -08004366 if (start_readonly)
4367 mddev->ro = 2; /* read-only, but switch on first write */
4368
NeilBrownb15c2e52006-01-06 00:20:16 -08004369 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004370 if (err)
4371 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004372 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4373 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4374 " but 'external_size' not in effect?\n", __func__);
4375 printk(KERN_ERR
4376 "md: invalid array_size %llu > default size %llu\n",
4377 (unsigned long long)mddev->array_sectors / 2,
4378 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4379 err = -EINVAL;
4380 mddev->pers->stop(mddev);
4381 }
4382 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004383 err = bitmap_create(mddev);
4384 if (err) {
4385 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4386 mdname(mddev), err);
4387 mddev->pers->stop(mddev);
4388 }
4389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 module_put(mddev->pers->owner);
4392 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004393 bitmap_destroy(mddev);
4394 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004396 if (mddev->pers->sync_request) {
4397 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4398 printk(KERN_WARNING
4399 "md: cannot register extra attributes for %s\n",
4400 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004401 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004402 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004403 mddev->ro = 0;
4404
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004406 atomic_set(&mddev->max_corr_read_errors,
4407 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 mddev->safemode = 0;
4409 mddev->safemode_timer.function = md_safemode_timeout;
4410 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004411 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004413
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004414 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004415 if (rdev->raid_disk >= 0) {
4416 char nm[20];
4417 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004418 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4419 printk("md: cannot register %s for %s\n",
4420 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
4423 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4424
NeilBrown850b2b422006-10-03 01:15:46 -07004425 if (mddev->flags)
4426 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Andre Nollf233ea52008-07-21 17:05:22 +10004428 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004430 /* If there is a partially-recovered drive we need to
4431 * start recovery here. If we leave it to md_check_recovery,
4432 * it will remove the drives and not do the right thing
4433 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004434 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004435 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004436 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004437 if (rdev->raid_disk >= 0 &&
4438 !test_bit(In_sync, &rdev->flags) &&
4439 !test_bit(Faulty, &rdev->flags))
4440 /* complete an interrupted recovery */
4441 spares++;
4442 if (spares && mddev->pers->sync_request) {
4443 mddev->recovery = 0;
4444 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4445 mddev->sync_thread = md_register_thread(md_do_sync,
4446 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004447 "resync");
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004448 if (!mddev->sync_thread) {
4449 printk(KERN_ERR "%s: could not start resync"
4450 " thread...\n",
4451 mdname(mddev));
4452 /* leave the spares where they are, it shouldn't hurt */
4453 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004454 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004455 }
4456 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004457 md_wakeup_thread(mddev->thread);
4458 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004459
NeilBrown449aad32009-08-03 10:59:58 +10004460 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004461 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004462 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004463 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004464 if (mddev->sysfs_action)
4465 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004466 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004467 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 return 0;
4469}
4470
4471static int restart_array(mddev_t *mddev)
4472{
4473 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
Andre Noll80fab1d2008-07-11 22:02:21 +10004475 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004477 return -ENXIO;
4478 if (!mddev->pers)
4479 return -EINVAL;
4480 if (!mddev->ro)
4481 return -EBUSY;
4482 mddev->safemode = 0;
4483 mddev->ro = 0;
4484 set_disk_ro(disk, 0);
4485 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4486 mdname(mddev));
4487 /* Kick recovery or resync if necessary */
4488 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4489 md_wakeup_thread(mddev->thread);
4490 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004491 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493}
4494
NeilBrownacc55e22006-06-26 00:27:47 -07004495/* similar to deny_write_access, but accounts for our holding a reference
4496 * to the file ourselves */
4497static int deny_bitmap_write_access(struct file * file)
4498{
4499 struct inode *inode = file->f_mapping->host;
4500
4501 spin_lock(&inode->i_lock);
4502 if (atomic_read(&inode->i_writecount) > 1) {
4503 spin_unlock(&inode->i_lock);
4504 return -ETXTBSY;
4505 }
4506 atomic_set(&inode->i_writecount, -1);
4507 spin_unlock(&inode->i_lock);
4508
4509 return 0;
4510}
4511
NeilBrown43a70502009-12-14 12:49:55 +11004512void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004513{
4514 struct inode *inode = file->f_mapping->host;
4515
4516 spin_lock(&inode->i_lock);
4517 atomic_set(&inode->i_writecount, 1);
4518 spin_unlock(&inode->i_lock);
4519}
4520
NeilBrown9e653b62006-06-26 00:27:58 -07004521/* mode:
4522 * 0 - completely stop and dis-assemble array
4523 * 1 - switch to readonly
4524 * 2 - stop but do not disassemble array
4525 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004526static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
4528 int err = 0;
4529 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004530 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
NeilBrownc8c00a62009-08-10 12:50:52 +10004532 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004533 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004534 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004535 err = -EBUSY;
4536 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
4538 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004539 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4541 md_unregister_thread(mddev->sync_thread);
4542 mddev->sync_thread = NULL;
4543 }
4544
4545 del_timer_sync(&mddev->safemode_timer);
4546
NeilBrown9e653b62006-06-26 00:27:58 -07004547 switch(mode) {
4548 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004550 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 goto out;
4552 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004553 break;
4554 case 0: /* disassemble */
4555 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004556 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004557 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 if (mddev->ro)
4559 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004562 mddev->queue->merge_bvec_fn = NULL;
4563 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004564 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004566 if (mddev->pers->sync_request)
4567 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004569 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004570 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004571
NeilBrownc4647292009-05-07 12:51:06 +10004572 list_for_each_entry(rdev, &mddev->disks, same_set)
4573 if (rdev->raid_disk >= 0) {
4574 char nm[20];
4575 sprintf(nm, "rd%d", rdev->raid_disk);
4576 sysfs_remove_link(&mddev->kobj, nm);
4577 }
4578
NeilBrown0d4ca602006-12-10 02:20:44 -08004579 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004580 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (mddev->ro)
4583 mddev->ro = 0;
4584 }
NeilBrown850b2b422006-10-03 01:15:46 -07004585 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 /* mark array as shutdown cleanly */
4587 mddev->in_sync = 1;
NeilBrown850b2b422006-10-03 01:15:46 -07004588 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 }
NeilBrown9e653b62006-06-26 00:27:58 -07004590 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004592 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004593 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004595out:
4596 mutex_unlock(&mddev->open_mutex);
4597 if (err)
4598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 /*
4600 * Free resources if final stop
4601 */
NeilBrown9e653b62006-06-26 00:27:58 -07004602 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4605
NeilBrown978f9462006-02-02 14:28:05 -08004606 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004607 if (mddev->bitmap_info.file) {
4608 restore_bitmap_write_access(mddev->bitmap_info.file);
4609 fput(mddev->bitmap_info.file);
4610 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004611 }
NeilBrownc3d97142009-12-14 12:49:52 +11004612 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004613
NeilBrown177a99b2008-02-06 01:39:56 -08004614 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004615 flush_scheduled_work();
4616
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 export_array(mddev);
4618
Andre Nollf233ea52008-07-21 17:05:22 +10004619 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004620 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004621 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004622 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004623 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004624 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004625 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004626 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004627 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004628 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004629 mddev->level = LEVEL_NONE;
4630 mddev->clevel[0] = 0;
4631 mddev->flags = 0;
4632 mddev->ro = 0;
4633 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004634 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004635 mddev->ctime = mddev->utime = 0;
4636 mddev->layout = 0;
4637 mddev->max_disks = 0;
4638 mddev->events = 0;
4639 mddev->delta_disks = 0;
4640 mddev->new_level = LEVEL_NONE;
4641 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004642 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004643 mddev->curr_resync = 0;
4644 mddev->resync_mismatches = 0;
4645 mddev->suspend_lo = mddev->suspend_hi = 0;
4646 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4647 mddev->recovery = 0;
4648 mddev->in_sync = 0;
4649 mddev->changed = 0;
4650 mddev->degraded = 0;
4651 mddev->barriers_work = 0;
4652 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004653 mddev->bitmap_info.offset = 0;
4654 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004655 mddev->bitmap_info.chunksize = 0;
4656 mddev->bitmap_info.daemon_sleep = 0;
4657 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004658 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004659 if (mddev->hold_active == UNTIL_STOP)
4660 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004661
NeilBrowna8a55c32006-06-26 00:27:39 -07004662 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4664 mdname(mddev));
4665 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004666 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004667 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004668 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 return err;
4670}
4671
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004672#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673static void autorun_array(mddev_t *mddev)
4674{
4675 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 int err;
4677
NeilBrowna757e642005-04-16 15:26:42 -07004678 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
4681 printk(KERN_INFO "md: running: ");
4682
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004683 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 char b[BDEVNAME_SIZE];
4685 printk("<%s>", bdevname(rdev->bdev,b));
4686 }
4687 printk("\n");
4688
NeilBrownd710e132008-10-13 11:55:12 +11004689 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 if (err) {
4691 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004692 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 }
4694}
4695
4696/*
4697 * lets try to run arrays based on all disks that have arrived
4698 * until now. (those are in pending_raid_disks)
4699 *
4700 * the method: pick the first pending disk, collect all disks with
4701 * the same UUID, remove all from the pending list and put them into
4702 * the 'same_array' list. Then order this list based on superblock
4703 * update time (freshest comes first), kick out 'old' disks and
4704 * compare superblocks. If everything's fine then run it.
4705 *
4706 * If "unit" is allocated, then bump its reference count
4707 */
4708static void autorun_devices(int part)
4709{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004710 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 mddev_t *mddev;
4712 char b[BDEVNAME_SIZE];
4713
4714 printk(KERN_INFO "md: autorun ...\n");
4715 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004716 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004718 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 rdev0 = list_entry(pending_raid_disks.next,
4720 mdk_rdev_t, same_set);
4721
4722 printk(KERN_INFO "md: considering %s ...\n",
4723 bdevname(rdev0->bdev,b));
4724 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004725 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 if (super_90_load(rdev, rdev0, 0) >= 0) {
4727 printk(KERN_INFO "md: adding %s ...\n",
4728 bdevname(rdev->bdev,b));
4729 list_move(&rdev->same_set, &candidates);
4730 }
4731 /*
4732 * now we have a set of devices, with all of them having
4733 * mostly sane superblocks. It's time to allocate the
4734 * mddev.
4735 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004736 if (part) {
4737 dev = MKDEV(mdp_major,
4738 rdev0->preferred_minor << MdpMinorShift);
4739 unit = MINOR(dev) >> MdpMinorShift;
4740 } else {
4741 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4742 unit = MINOR(dev);
4743 }
4744 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4746 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4747 break;
4748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
4750 md_probe(dev, NULL, NULL);
4751 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004752 if (!mddev || !mddev->gendisk) {
4753 if (mddev)
4754 mddev_put(mddev);
4755 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 "md: cannot allocate memory for md drive.\n");
4757 break;
4758 }
4759 if (mddev_lock(mddev))
4760 printk(KERN_WARNING "md: %s locked, cannot run\n",
4761 mdname(mddev));
4762 else if (mddev->raid_disks || mddev->major_version
4763 || !list_empty(&mddev->disks)) {
4764 printk(KERN_WARNING
4765 "md: %s already running, cannot run %s\n",
4766 mdname(mddev), bdevname(rdev0->bdev,b));
4767 mddev_unlock(mddev);
4768 } else {
4769 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004770 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004771 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 list_del_init(&rdev->same_set);
4773 if (bind_rdev_to_array(rdev, mddev))
4774 export_rdev(rdev);
4775 }
4776 autorun_array(mddev);
4777 mddev_unlock(mddev);
4778 }
4779 /* on success, candidates will be empty, on error
4780 * it won't...
4781 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004782 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004783 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 mddev_put(mddev);
4787 }
4788 printk(KERN_INFO "md: ... autorun DONE.\n");
4789}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004790#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792static int get_version(void __user * arg)
4793{
4794 mdu_version_t ver;
4795
4796 ver.major = MD_MAJOR_VERSION;
4797 ver.minor = MD_MINOR_VERSION;
4798 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4799
4800 if (copy_to_user(arg, &ver, sizeof(ver)))
4801 return -EFAULT;
4802
4803 return 0;
4804}
4805
4806static int get_array_info(mddev_t * mddev, void __user * arg)
4807{
4808 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004809 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
NeilBrowna9f326e2009-09-23 18:06:41 +10004812 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004813 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004815 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 failed++;
4817 else {
4818 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004819 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004820 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 else
4822 spare++;
4823 }
4824 }
4825
4826 info.major_version = mddev->major_version;
4827 info.minor_version = mddev->minor_version;
4828 info.patch_version = MD_PATCHLEVEL_VERSION;
4829 info.ctime = mddev->ctime;
4830 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004831 info.size = mddev->dev_sectors / 2;
4832 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004833 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 info.nr_disks = nr;
4835 info.raid_disks = mddev->raid_disks;
4836 info.md_minor = mddev->md_minor;
4837 info.not_persistent= !mddev->persistent;
4838
4839 info.utime = mddev->utime;
4840 info.state = 0;
4841 if (mddev->in_sync)
4842 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004843 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004844 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004845 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 info.working_disks = working;
4847 info.failed_disks = failed;
4848 info.spare_disks = spare;
4849
4850 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004851 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
4853 if (copy_to_user(arg, &info, sizeof(info)))
4854 return -EFAULT;
4855
4856 return 0;
4857}
4858
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004859static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004860{
4861 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4862 char *ptr, *buf = NULL;
4863 int err = -ENOMEM;
4864
Dan Williamsb5470dc2008-06-27 21:44:04 -07004865 if (md_allow_write(mddev))
4866 file = kmalloc(sizeof(*file), GFP_NOIO);
4867 else
4868 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004869
NeilBrown32a76272005-06-21 17:17:14 -07004870 if (!file)
4871 goto out;
4872
4873 /* bitmap disabled, zero the first byte and copy out */
4874 if (!mddev->bitmap || !mddev->bitmap->file) {
4875 file->pathname[0] = '\0';
4876 goto copy_out;
4877 }
4878
4879 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4880 if (!buf)
4881 goto out;
4882
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004883 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4884 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004885 goto out;
4886
4887 strcpy(file->pathname, ptr);
4888
4889copy_out:
4890 err = 0;
4891 if (copy_to_user(arg, file, sizeof(*file)))
4892 err = -EFAULT;
4893out:
4894 kfree(buf);
4895 kfree(file);
4896 return err;
4897}
4898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899static int get_disk_info(mddev_t * mddev, void __user * arg)
4900{
4901 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 mdk_rdev_t *rdev;
4903
4904 if (copy_from_user(&info, arg, sizeof(info)))
4905 return -EFAULT;
4906
Andre Noll26ef3792008-07-11 22:02:21 +10004907 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 if (rdev) {
4909 info.major = MAJOR(rdev->bdev->bd_dev);
4910 info.minor = MINOR(rdev->bdev->bd_dev);
4911 info.raid_disk = rdev->raid_disk;
4912 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004913 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004915 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 info.state |= (1<<MD_DISK_ACTIVE);
4917 info.state |= (1<<MD_DISK_SYNC);
4918 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004919 if (test_bit(WriteMostly, &rdev->flags))
4920 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 } else {
4922 info.major = info.minor = 0;
4923 info.raid_disk = -1;
4924 info.state = (1<<MD_DISK_REMOVED);
4925 }
4926
4927 if (copy_to_user(arg, &info, sizeof(info)))
4928 return -EFAULT;
4929
4930 return 0;
4931}
4932
4933static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4934{
4935 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4936 mdk_rdev_t *rdev;
4937 dev_t dev = MKDEV(info->major,info->minor);
4938
4939 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4940 return -EOVERFLOW;
4941
4942 if (!mddev->raid_disks) {
4943 int err;
4944 /* expecting a device which has a superblock */
4945 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4946 if (IS_ERR(rdev)) {
4947 printk(KERN_WARNING
4948 "md: md_import_device returned %ld\n",
4949 PTR_ERR(rdev));
4950 return PTR_ERR(rdev);
4951 }
4952 if (!list_empty(&mddev->disks)) {
4953 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4954 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004955 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 .load_super(rdev, rdev0, mddev->minor_version);
4957 if (err < 0) {
4958 printk(KERN_WARNING
4959 "md: %s has different UUID to %s\n",
4960 bdevname(rdev->bdev,b),
4961 bdevname(rdev0->bdev,b2));
4962 export_rdev(rdev);
4963 return -EINVAL;
4964 }
4965 }
4966 err = bind_rdev_to_array(rdev, mddev);
4967 if (err)
4968 export_rdev(rdev);
4969 return err;
4970 }
4971
4972 /*
4973 * add_new_disk can be used once the array is assembled
4974 * to add "hot spares". They must already have a superblock
4975 * written
4976 */
4977 if (mddev->pers) {
4978 int err;
4979 if (!mddev->pers->hot_add_disk) {
4980 printk(KERN_WARNING
4981 "%s: personality does not support diskops!\n",
4982 mdname(mddev));
4983 return -EINVAL;
4984 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004985 if (mddev->persistent)
4986 rdev = md_import_device(dev, mddev->major_version,
4987 mddev->minor_version);
4988 else
4989 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 if (IS_ERR(rdev)) {
4991 printk(KERN_WARNING
4992 "md: md_import_device returned %ld\n",
4993 PTR_ERR(rdev));
4994 return PTR_ERR(rdev);
4995 }
NeilBrown41158c72005-06-21 17:17:25 -07004996 /* set save_raid_disk if appropriate */
4997 if (!mddev->persistent) {
4998 if (info->state & (1<<MD_DISK_SYNC) &&
4999 info->raid_disk < mddev->raid_disks)
5000 rdev->raid_disk = info->raid_disk;
5001 else
5002 rdev->raid_disk = -1;
5003 } else
5004 super_types[mddev->major_version].
5005 validate_super(mddev, rdev);
5006 rdev->saved_raid_disk = rdev->raid_disk;
5007
NeilBrownb2d444d2005-11-08 21:39:31 -08005008 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005009 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5010 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005011 else
5012 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 rdev->raid_disk = -1;
5015 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005016 if (!err && !mddev->pers->hot_remove_disk) {
5017 /* If there is hot_add_disk but no hot_remove_disk
5018 * then added disks for geometry changes,
5019 * and should be added immediately.
5020 */
5021 super_types[mddev->major_version].
5022 validate_super(mddev, rdev);
5023 err = mddev->pers->hot_add_disk(mddev, rdev);
5024 if (err)
5025 unbind_rdev_from_array(rdev);
5026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 if (err)
5028 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005029 else
NeilBrown3c0ee632008-10-21 13:25:28 +11005030 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005031
NeilBrown17571282006-12-10 02:20:52 -08005032 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005033 if (mddev->degraded)
5034 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005035 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005036 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 return err;
5038 }
5039
5040 /* otherwise, add_new_disk is only allowed
5041 * for major_version==0 superblocks
5042 */
5043 if (mddev->major_version != 0) {
5044 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5045 mdname(mddev));
5046 return -EINVAL;
5047 }
5048
5049 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5050 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005051 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 if (IS_ERR(rdev)) {
5053 printk(KERN_WARNING
5054 "md: error, md_import_device() returned %ld\n",
5055 PTR_ERR(rdev));
5056 return PTR_ERR(rdev);
5057 }
5058 rdev->desc_nr = info->number;
5059 if (info->raid_disk < mddev->raid_disks)
5060 rdev->raid_disk = info->raid_disk;
5061 else
5062 rdev->raid_disk = -1;
5063
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005065 if (info->state & (1<<MD_DISK_SYNC))
5066 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005068 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5069 set_bit(WriteMostly, &rdev->flags);
5070
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 if (!mddev->persistent) {
5072 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005073 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 } else
Andre Noll0f420352008-07-11 22:02:23 +10005075 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005076 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
NeilBrown2bf071b2006-01-06 00:20:55 -08005078 err = bind_rdev_to_array(rdev, mddev);
5079 if (err) {
5080 export_rdev(rdev);
5081 return err;
5082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 }
5084
5085 return 0;
5086}
5087
5088static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5089{
5090 char b[BDEVNAME_SIZE];
5091 mdk_rdev_t *rdev;
5092
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 rdev = find_rdev(mddev, dev);
5094 if (!rdev)
5095 return -ENXIO;
5096
5097 if (rdev->raid_disk >= 0)
5098 goto busy;
5099
5100 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005101 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005102 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
5104 return 0;
5105busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005106 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 bdevname(rdev->bdev,b), mdname(mddev));
5108 return -EBUSY;
5109}
5110
5111static int hot_add_disk(mddev_t * mddev, dev_t dev)
5112{
5113 char b[BDEVNAME_SIZE];
5114 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 mdk_rdev_t *rdev;
5116
5117 if (!mddev->pers)
5118 return -ENODEV;
5119
5120 if (mddev->major_version != 0) {
5121 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5122 " version-0 superblocks.\n",
5123 mdname(mddev));
5124 return -EINVAL;
5125 }
5126 if (!mddev->pers->hot_add_disk) {
5127 printk(KERN_WARNING
5128 "%s: personality does not support diskops!\n",
5129 mdname(mddev));
5130 return -EINVAL;
5131 }
5132
NeilBrownd710e132008-10-13 11:55:12 +11005133 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 if (IS_ERR(rdev)) {
5135 printk(KERN_WARNING
5136 "md: error, md_import_device() returned %ld\n",
5137 PTR_ERR(rdev));
5138 return -EINVAL;
5139 }
5140
5141 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005142 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 else
Andre Noll0f420352008-07-11 22:02:23 +10005144 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
NeilBrown8190e752009-06-18 08:48:58 +10005146 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
NeilBrownb2d444d2005-11-08 21:39:31 -08005148 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 printk(KERN_WARNING
5150 "md: can not hot-add faulty %s disk to %s!\n",
5151 bdevname(rdev->bdev,b), mdname(mddev));
5152 err = -EINVAL;
5153 goto abort_export;
5154 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005155 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005157 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005158 err = bind_rdev_to_array(rdev, mddev);
5159 if (err)
5160 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
5162 /*
5163 * The rest should better be atomic, we can have disk failures
5164 * noticed in interrupt contexts ...
5165 */
5166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 rdev->raid_disk = -1;
5168
NeilBrown850b2b422006-10-03 01:15:46 -07005169 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170
5171 /*
5172 * Kick recovery, maybe this spare has to be added to the
5173 * array immediately.
5174 */
5175 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5176 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005177 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 return 0;
5179
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180abort_export:
5181 export_rdev(rdev);
5182 return err;
5183}
5184
NeilBrown32a76272005-06-21 17:17:14 -07005185static int set_bitmap_file(mddev_t *mddev, int fd)
5186{
5187 int err;
5188
NeilBrown36fa3062005-09-09 16:23:45 -07005189 if (mddev->pers) {
5190 if (!mddev->pers->quiesce)
5191 return -EBUSY;
5192 if (mddev->recovery || mddev->sync_thread)
5193 return -EBUSY;
5194 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005195 }
5196
NeilBrown36fa3062005-09-09 16:23:45 -07005197
5198 if (fd >= 0) {
5199 if (mddev->bitmap)
5200 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005201 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005202
NeilBrownc3d97142009-12-14 12:49:52 +11005203 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005204 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5205 mdname(mddev));
5206 return -EBADF;
5207 }
5208
NeilBrownc3d97142009-12-14 12:49:52 +11005209 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005210 if (err) {
5211 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5212 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005213 fput(mddev->bitmap_info.file);
5214 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005215 return err;
5216 }
NeilBrownc3d97142009-12-14 12:49:52 +11005217 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005218 } else if (mddev->bitmap == NULL)
5219 return -ENOENT; /* cannot remove what isn't there */
5220 err = 0;
5221 if (mddev->pers) {
5222 mddev->pers->quiesce(mddev, 1);
5223 if (fd >= 0)
5224 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005225 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005226 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005227 fd = -1; /* make sure to put the file */
5228 }
NeilBrown36fa3062005-09-09 16:23:45 -07005229 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005230 }
5231 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005232 if (mddev->bitmap_info.file) {
5233 restore_bitmap_write_access(mddev->bitmap_info.file);
5234 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005235 }
NeilBrownc3d97142009-12-14 12:49:52 +11005236 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005237 }
5238
NeilBrown32a76272005-06-21 17:17:14 -07005239 return err;
5240}
5241
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242/*
5243 * set_array_info is used two different ways
5244 * The original usage is when creating a new array.
5245 * In this usage, raid_disks is > 0 and it together with
5246 * level, size, not_persistent,layout,chunksize determine the
5247 * shape of the array.
5248 * This will always create an array with a type-0.90.0 superblock.
5249 * The newer usage is when assembling an array.
5250 * In this case raid_disks will be 0, and the major_version field is
5251 * use to determine which style super-blocks are to be found on the devices.
5252 * The minor and patch _version numbers are also kept incase the
5253 * super_block handler wishes to interpret them.
5254 */
5255static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5256{
5257
5258 if (info->raid_disks == 0) {
5259 /* just setting version number for superblock loading */
5260 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005261 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 super_types[info->major_version].name == NULL) {
5263 /* maybe try to auto-load a module? */
5264 printk(KERN_INFO
5265 "md: superblock version %d not known\n",
5266 info->major_version);
5267 return -EINVAL;
5268 }
5269 mddev->major_version = info->major_version;
5270 mddev->minor_version = info->minor_version;
5271 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005272 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 return 0;
5274 }
5275 mddev->major_version = MD_MAJOR_VERSION;
5276 mddev->minor_version = MD_MINOR_VERSION;
5277 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5278 mddev->ctime = get_seconds();
5279
5280 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005281 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005282 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 mddev->raid_disks = info->raid_disks;
5284 /* don't set md_minor, it is determined by which /dev/md* was
5285 * openned
5286 */
5287 if (info->state & (1<<MD_SB_CLEAN))
5288 mddev->recovery_cp = MaxSector;
5289 else
5290 mddev->recovery_cp = 0;
5291 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005292 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
5294 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005295 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
5297 mddev->max_disks = MD_SB_DISKS;
5298
NeilBrowne6910632008-02-06 01:39:51 -08005299 if (mddev->persistent)
5300 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005301 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
NeilBrownc3d97142009-12-14 12:49:52 +11005303 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5304 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005305
NeilBrownf6705572006-03-27 01:18:11 -08005306 mddev->reshape_position = MaxSector;
5307
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 /*
5309 * Generate a 128 bit UUID
5310 */
5311 get_random_bytes(mddev->uuid, 16);
5312
NeilBrownf6705572006-03-27 01:18:11 -08005313 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005314 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005315 mddev->new_layout = mddev->layout;
5316 mddev->delta_disks = 0;
5317
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 return 0;
5319}
5320
Dan Williams1f403622009-03-31 14:59:03 +11005321void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5322{
Dan Williamsb522adc2009-03-31 15:00:31 +11005323 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5324
5325 if (mddev->external_size)
5326 return;
5327
Dan Williams1f403622009-03-31 14:59:03 +11005328 mddev->array_sectors = array_sectors;
5329}
5330EXPORT_SYMBOL(md_set_array_sectors);
5331
Andre Nolld71f9f82008-07-11 22:02:22 +10005332static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005333{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005334 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005335 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005336 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005337
5338 if (mddev->pers->resize == NULL)
5339 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005340 /* The "num_sectors" is the number of sectors of each device that
5341 * is used. This can only make sense for arrays with redundancy.
5342 * linear and raid0 always use whatever space is available. We can only
5343 * consider changing this number if no resync or reconstruction is
5344 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005345 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005346 * of each device. If num_sectors is zero, we find the largest size
5347 * that fits.
5348
NeilBrowna35b0d62006-01-06 00:20:49 -08005349 */
5350 if (mddev->sync_thread)
5351 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005352 if (mddev->bitmap)
5353 /* Sorry, cannot grow a bitmap yet, just remove it,
5354 * grow, and re-add.
5355 */
5356 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005357 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005358 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005359
Andre Nolld71f9f82008-07-11 22:02:22 +10005360 if (fit && (num_sectors == 0 || num_sectors > avail))
5361 num_sectors = avail;
5362 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005363 return -ENOSPC;
5364 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005365 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005366 if (!rv)
5367 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005368 return rv;
5369}
5370
NeilBrownda943b992006-01-06 00:20:54 -08005371static int update_raid_disks(mddev_t *mddev, int raid_disks)
5372{
5373 int rv;
5374 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005375 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005376 return -EINVAL;
5377 if (raid_disks <= 0 ||
5378 raid_disks >= mddev->max_disks)
5379 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005380 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005381 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005382 mddev->delta_disks = raid_disks - mddev->raid_disks;
5383
5384 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005385 return rv;
5386}
5387
5388
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389/*
5390 * update_array_info is used to change the configuration of an
5391 * on-line array.
5392 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5393 * fields in the info are checked against the array.
5394 * Any differences that cannot be handled will cause an error.
5395 * Normally, only one change can be managed at a time.
5396 */
5397static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5398{
5399 int rv = 0;
5400 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005401 int state = 0;
5402
5403 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005404 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005405 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
5407 if (mddev->major_version != info->major_version ||
5408 mddev->minor_version != info->minor_version ||
5409/* mddev->patch_version != info->patch_version || */
5410 mddev->ctime != info->ctime ||
5411 mddev->level != info->level ||
5412/* mddev->layout != info->layout || */
5413 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005414 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005415 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5416 ((state^info->state) & 0xfffffe00)
5417 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 return -EINVAL;
5419 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005420 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5421 cnt++;
5422 if (mddev->raid_disks != info->raid_disks)
5423 cnt++;
5424 if (mddev->layout != info->layout)
5425 cnt++;
5426 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5427 cnt++;
5428 if (cnt == 0)
5429 return 0;
5430 if (cnt > 1)
5431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
5433 if (mddev->layout != info->layout) {
5434 /* Change layout
5435 * we don't need to do anything at the md level, the
5436 * personality will take care of it all.
5437 */
NeilBrown50ac1682009-06-18 08:47:55 +10005438 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005440 else {
5441 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005442 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005443 if (rv)
5444 mddev->new_layout = mddev->layout;
5445 return rv;
5446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005448 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005449 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
NeilBrownda943b992006-01-06 00:20:54 -08005451 if (mddev->raid_disks != info->raid_disks)
5452 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
NeilBrown36fa3062005-09-09 16:23:45 -07005454 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5455 if (mddev->pers->quiesce == NULL)
5456 return -EINVAL;
5457 if (mddev->recovery || mddev->sync_thread)
5458 return -EBUSY;
5459 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5460 /* add the bitmap */
5461 if (mddev->bitmap)
5462 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005463 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005464 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005465 mddev->bitmap_info.offset =
5466 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005467 mddev->pers->quiesce(mddev, 1);
5468 rv = bitmap_create(mddev);
5469 if (rv)
5470 bitmap_destroy(mddev);
5471 mddev->pers->quiesce(mddev, 0);
5472 } else {
5473 /* remove the bitmap */
5474 if (!mddev->bitmap)
5475 return -ENOENT;
5476 if (mddev->bitmap->file)
5477 return -EINVAL;
5478 mddev->pers->quiesce(mddev, 1);
5479 bitmap_destroy(mddev);
5480 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005481 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005482 }
5483 }
NeilBrown850b2b422006-10-03 01:15:46 -07005484 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 return rv;
5486}
5487
5488static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5489{
5490 mdk_rdev_t *rdev;
5491
5492 if (mddev->pers == NULL)
5493 return -ENODEV;
5494
5495 rdev = find_rdev(mddev, dev);
5496 if (!rdev)
5497 return -ENODEV;
5498
5499 md_error(mddev, rdev);
5500 return 0;
5501}
5502
Andre Noll2f9618c2008-04-25 18:57:58 +02005503/*
5504 * We have a problem here : there is no easy way to give a CHS
5505 * virtual geometry. We currently pretend that we have a 2 heads
5506 * 4 sectors (with a BIG number of cylinders...). This drives
5507 * dosfs just mad... ;-)
5508 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005509static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5510{
5511 mddev_t *mddev = bdev->bd_disk->private_data;
5512
5513 geo->heads = 2;
5514 geo->sectors = 4;
5515 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5516 return 0;
5517}
5518
Al Viroa39907f2008-03-02 10:31:15 -05005519static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 unsigned int cmd, unsigned long arg)
5521{
5522 int err = 0;
5523 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 mddev_t *mddev = NULL;
5525
5526 if (!capable(CAP_SYS_ADMIN))
5527 return -EACCES;
5528
5529 /*
5530 * Commands dealing with the RAID driver but not any
5531 * particular array:
5532 */
5533 switch (cmd)
5534 {
5535 case RAID_VERSION:
5536 err = get_version(argp);
5537 goto done;
5538
5539 case PRINT_RAID_DEBUG:
5540 err = 0;
5541 md_print_devices();
5542 goto done;
5543
5544#ifndef MODULE
5545 case RAID_AUTORUN:
5546 err = 0;
5547 autostart_arrays(arg);
5548 goto done;
5549#endif
5550 default:;
5551 }
5552
5553 /*
5554 * Commands creating/starting a new array:
5555 */
5556
Al Viroa39907f2008-03-02 10:31:15 -05005557 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
5559 if (!mddev) {
5560 BUG();
5561 goto abort;
5562 }
5563
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 err = mddev_lock(mddev);
5565 if (err) {
5566 printk(KERN_INFO
5567 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5568 err, cmd);
5569 goto abort;
5570 }
5571
5572 switch (cmd)
5573 {
5574 case SET_ARRAY_INFO:
5575 {
5576 mdu_array_info_t info;
5577 if (!arg)
5578 memset(&info, 0, sizeof(info));
5579 else if (copy_from_user(&info, argp, sizeof(info))) {
5580 err = -EFAULT;
5581 goto abort_unlock;
5582 }
5583 if (mddev->pers) {
5584 err = update_array_info(mddev, &info);
5585 if (err) {
5586 printk(KERN_WARNING "md: couldn't update"
5587 " array info. %d\n", err);
5588 goto abort_unlock;
5589 }
5590 goto done_unlock;
5591 }
5592 if (!list_empty(&mddev->disks)) {
5593 printk(KERN_WARNING
5594 "md: array %s already has disks!\n",
5595 mdname(mddev));
5596 err = -EBUSY;
5597 goto abort_unlock;
5598 }
5599 if (mddev->raid_disks) {
5600 printk(KERN_WARNING
5601 "md: array %s already initialised!\n",
5602 mdname(mddev));
5603 err = -EBUSY;
5604 goto abort_unlock;
5605 }
5606 err = set_array_info(mddev, &info);
5607 if (err) {
5608 printk(KERN_WARNING "md: couldn't set"
5609 " array info. %d\n", err);
5610 goto abort_unlock;
5611 }
5612 }
5613 goto done_unlock;
5614
5615 default:;
5616 }
5617
5618 /*
5619 * Commands querying/configuring an existing array:
5620 */
NeilBrown32a76272005-06-21 17:17:14 -07005621 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005622 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005623 if ((!mddev->raid_disks && !mddev->external)
5624 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5625 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5626 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 err = -ENODEV;
5628 goto abort_unlock;
5629 }
5630
5631 /*
5632 * Commands even a read-only array can execute:
5633 */
5634 switch (cmd)
5635 {
5636 case GET_ARRAY_INFO:
5637 err = get_array_info(mddev, argp);
5638 goto done_unlock;
5639
NeilBrown32a76272005-06-21 17:17:14 -07005640 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005641 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005642 goto done_unlock;
5643
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 case GET_DISK_INFO:
5645 err = get_disk_info(mddev, argp);
5646 goto done_unlock;
5647
5648 case RESTART_ARRAY_RW:
5649 err = restart_array(mddev);
5650 goto done_unlock;
5651
5652 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005653 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 goto done_unlock;
5655
5656 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005657 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 goto done_unlock;
5659
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 }
5661
5662 /*
5663 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005664 * superblock, so we do not allow them on read-only arrays.
5665 * However non-MD ioctls (e.g. get-size) will still come through
5666 * here and hit the 'default' below, so only disallow
5667 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005669 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005670 if (mddev->ro == 2) {
5671 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005672 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005673 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5674 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005675 } else {
5676 err = -EROFS;
5677 goto abort_unlock;
5678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 }
5680
5681 switch (cmd)
5682 {
5683 case ADD_NEW_DISK:
5684 {
5685 mdu_disk_info_t info;
5686 if (copy_from_user(&info, argp, sizeof(info)))
5687 err = -EFAULT;
5688 else
5689 err = add_new_disk(mddev, &info);
5690 goto done_unlock;
5691 }
5692
5693 case HOT_REMOVE_DISK:
5694 err = hot_remove_disk(mddev, new_decode_dev(arg));
5695 goto done_unlock;
5696
5697 case HOT_ADD_DISK:
5698 err = hot_add_disk(mddev, new_decode_dev(arg));
5699 goto done_unlock;
5700
5701 case SET_DISK_FAULTY:
5702 err = set_disk_faulty(mddev, new_decode_dev(arg));
5703 goto done_unlock;
5704
5705 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005706 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 goto done_unlock;
5708
NeilBrown32a76272005-06-21 17:17:14 -07005709 case SET_BITMAP_FILE:
5710 err = set_bitmap_file(mddev, (int)arg);
5711 goto done_unlock;
5712
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 err = -EINVAL;
5715 goto abort_unlock;
5716 }
5717
5718done_unlock:
5719abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005720 if (mddev->hold_active == UNTIL_IOCTL &&
5721 err != -EINVAL)
5722 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 mddev_unlock(mddev);
5724
5725 return err;
5726done:
5727 if (err)
5728 MD_BUG();
5729abort:
5730 return err;
5731}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005732#ifdef CONFIG_COMPAT
5733static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5734 unsigned int cmd, unsigned long arg)
5735{
5736 switch (cmd) {
5737 case HOT_REMOVE_DISK:
5738 case HOT_ADD_DISK:
5739 case SET_DISK_FAULTY:
5740 case SET_BITMAP_FILE:
5741 /* These take in integer arg, do not convert */
5742 break;
5743 default:
5744 arg = (unsigned long)compat_ptr(arg);
5745 break;
5746 }
5747
5748 return md_ioctl(bdev, mode, cmd, arg);
5749}
5750#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
Al Viroa39907f2008-03-02 10:31:15 -05005752static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753{
5754 /*
5755 * Succeed if we can lock the mddev, which confirms that
5756 * it isn't being stopped right now.
5757 */
NeilBrownd3374822009-01-09 08:31:10 +11005758 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 int err;
5760
NeilBrownd3374822009-01-09 08:31:10 +11005761 if (mddev->gendisk != bdev->bd_disk) {
5762 /* we are racing with mddev_put which is discarding this
5763 * bd_disk.
5764 */
5765 mddev_put(mddev);
5766 /* Wait until bdev->bd_disk is definitely gone */
5767 flush_scheduled_work();
5768 /* Then retry the open from the top */
5769 return -ERESTARTSYS;
5770 }
5771 BUG_ON(mddev != bdev->bd_disk->private_data);
5772
NeilBrownc8c00a62009-08-10 12:50:52 +10005773 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 goto out;
5775
5776 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005777 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005778 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
Al Viroa39907f2008-03-02 10:31:15 -05005780 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 out:
5782 return err;
5783}
5784
Al Viroa39907f2008-03-02 10:31:15 -05005785static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786{
Al Viroa39907f2008-03-02 10:31:15 -05005787 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005789 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005790 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 mddev_put(mddev);
5792
5793 return 0;
5794}
5795
Linus Torvalds44ce62942007-05-09 18:51:36 -07005796static int md_media_changed(struct gendisk *disk)
5797{
5798 mddev_t *mddev = disk->private_data;
5799
5800 return mddev->changed;
5801}
5802
5803static int md_revalidate(struct gendisk *disk)
5804{
5805 mddev_t *mddev = disk->private_data;
5806
5807 mddev->changed = 0;
5808 return 0;
5809}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005810static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811{
5812 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005813 .open = md_open,
5814 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005815 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005816#ifdef CONFIG_COMPAT
5817 .compat_ioctl = md_compat_ioctl,
5818#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005819 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005820 .media_changed = md_media_changed,
5821 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822};
5823
Adrian Bunk75c96f82005-05-05 16:16:09 -07005824static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825{
5826 mdk_thread_t *thread = arg;
5827
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 /*
5829 * md_thread is a 'system-thread', it's priority should be very
5830 * high. We avoid resource deadlocks individually in each
5831 * raid personality. (RAID5 does preallocation) We also use RR and
5832 * the very same RT priority as kswapd, thus we will never get
5833 * into a priority inversion deadlock.
5834 *
5835 * we definitely have to have equal or higher priority than
5836 * bdflush, otherwise bdflush will deadlock if there are too
5837 * many dirty RAID5 blocks.
5838 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
NeilBrown6985c432005-10-19 21:23:47 -07005840 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005841 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
NeilBrown93588e22005-11-15 00:09:12 -08005843 /* We need to wait INTERRUPTIBLE so that
5844 * we don't add to the load-average.
5845 * That means we need to be sure no signals are
5846 * pending
5847 */
5848 if (signal_pending(current))
5849 flush_signals(current);
5850
5851 wait_event_interruptible_timeout
5852 (thread->wqueue,
5853 test_bit(THREAD_WAKEUP, &thread->flags)
5854 || kthread_should_stop(),
5855 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
5857 clear_bit(THREAD_WAKEUP, &thread->flags);
5858
NeilBrown787453c2005-11-08 21:39:43 -08005859 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005861
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 return 0;
5863}
5864
5865void md_wakeup_thread(mdk_thread_t *thread)
5866{
5867 if (thread) {
5868 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5869 set_bit(THREAD_WAKEUP, &thread->flags);
5870 wake_up(&thread->wqueue);
5871 }
5872}
5873
5874mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5875 const char *name)
5876{
5877 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878
NeilBrown9ffae0c2006-01-06 00:20:32 -08005879 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 if (!thread)
5881 return NULL;
5882
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 init_waitqueue_head(&thread->wqueue);
5884
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 thread->run = run;
5886 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005887 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005888 thread->tsk = kthread_run(md_thread, thread,
5889 "%s_%s",
5890 mdname(thread->mddev),
5891 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005892 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 kfree(thread);
5894 return NULL;
5895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 return thread;
5897}
5898
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899void md_unregister_thread(mdk_thread_t *thread)
5900{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005901 if (!thread)
5902 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005903 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005904
5905 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 kfree(thread);
5907}
5908
5909void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5910{
5911 if (!mddev) {
5912 MD_BUG();
5913 return;
5914 }
5915
NeilBrownb2d444d2005-11-08 21:39:31 -08005916 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005918
5919 if (mddev->external)
5920 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005921/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5923 mdname(mddev),
5924 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5925 __builtin_return_address(0),__builtin_return_address(1),
5926 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005927*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005928 if (!mddev->pers)
5929 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 if (!mddev->pers->error_handler)
5931 return;
5932 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005933 if (mddev->degraded)
5934 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005935 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5937 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5938 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005939 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940}
5941
5942/* seq_file implementation /proc/mdstat */
5943
5944static void status_unused(struct seq_file *seq)
5945{
5946 int i = 0;
5947 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
5949 seq_printf(seq, "unused devices: ");
5950
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005951 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 char b[BDEVNAME_SIZE];
5953 i++;
5954 seq_printf(seq, "%s ",
5955 bdevname(rdev->bdev,b));
5956 }
5957 if (!i)
5958 seq_printf(seq, "<none>");
5959
5960 seq_printf(seq, "\n");
5961}
5962
5963
5964static void status_resync(struct seq_file *seq, mddev_t * mddev)
5965{
NeilBrowndd71cf62009-05-07 12:49:35 +10005966 sector_t max_sectors, resync, res;
5967 unsigned long dt, db;
5968 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005969 int scale;
5970 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
NeilBrowndd71cf62009-05-07 12:49:35 +10005972 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973
5974 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005975 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005977 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978
5979 /*
5980 * Should not happen.
5981 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005982 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 MD_BUG();
5984 return;
5985 }
NeilBrown4588b422006-03-27 01:18:04 -08005986 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005987 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005988 * u32, as those are the requirements for sector_div.
5989 * Thus 'scale' must be at least 10
5990 */
5991 scale = 10;
5992 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005993 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005994 scale++;
5995 }
5996 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005997 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005998
5999 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 {
NeilBrown4588b422006-03-27 01:18:04 -08006001 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 seq_printf(seq, "[");
6003 for (i = 0; i < x; i++)
6004 seq_printf(seq, "=");
6005 seq_printf(seq, ">");
6006 for (i = 0; i < y; i++)
6007 seq_printf(seq, ".");
6008 seq_printf(seq, "] ");
6009 }
NeilBrown4588b422006-03-27 01:18:04 -08006010 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006011 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6012 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006013 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6014 "check" :
6015 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6016 "resync" : "recovery"))),
6017 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006018 (unsigned long long) resync/2,
6019 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
6021 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 * dt: time from mark until now
6023 * db: blocks written from mark until now
6024 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006025 *
6026 * rt is a sector_t, so could be 32bit or 64bit.
6027 * So we divide before multiply in case it is 32bit and close
6028 * to the limit.
6029 * We scale the divisor (db) by 32 to avoid loosing precision
6030 * near the end of resync when the number of remaining sectors
6031 * is close to 'db'.
6032 * We then divide rt by 32 after multiplying by db to compensate.
6033 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 */
6035 dt = ((jiffies - mddev->resync_mark) / HZ);
6036 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006037 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6038 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
NeilBrowndd71cf62009-05-07 12:49:35 +10006040 rt = max_sectors - resync; /* number of remaining sectors */
6041 sector_div(rt, db/32+1);
6042 rt *= dt;
6043 rt >>= 5;
6044
6045 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6046 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
NeilBrownff4e8d92006-07-10 04:44:16 -07006048 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049}
6050
6051static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6052{
6053 struct list_head *tmp;
6054 loff_t l = *pos;
6055 mddev_t *mddev;
6056
6057 if (l >= 0x10000)
6058 return NULL;
6059 if (!l--)
6060 /* header */
6061 return (void*)1;
6062
6063 spin_lock(&all_mddevs_lock);
6064 list_for_each(tmp,&all_mddevs)
6065 if (!l--) {
6066 mddev = list_entry(tmp, mddev_t, all_mddevs);
6067 mddev_get(mddev);
6068 spin_unlock(&all_mddevs_lock);
6069 return mddev;
6070 }
6071 spin_unlock(&all_mddevs_lock);
6072 if (!l--)
6073 return (void*)2;/* tail */
6074 return NULL;
6075}
6076
6077static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6078{
6079 struct list_head *tmp;
6080 mddev_t *next_mddev, *mddev = v;
6081
6082 ++*pos;
6083 if (v == (void*)2)
6084 return NULL;
6085
6086 spin_lock(&all_mddevs_lock);
6087 if (v == (void*)1)
6088 tmp = all_mddevs.next;
6089 else
6090 tmp = mddev->all_mddevs.next;
6091 if (tmp != &all_mddevs)
6092 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6093 else {
6094 next_mddev = (void*)2;
6095 *pos = 0x10000;
6096 }
6097 spin_unlock(&all_mddevs_lock);
6098
6099 if (v != (void*)1)
6100 mddev_put(mddev);
6101 return next_mddev;
6102
6103}
6104
6105static void md_seq_stop(struct seq_file *seq, void *v)
6106{
6107 mddev_t *mddev = v;
6108
6109 if (mddev && v != (void*)1 && v != (void*)2)
6110 mddev_put(mddev);
6111}
6112
NeilBrownd7603b72006-01-06 00:20:30 -08006113struct mdstat_info {
6114 int event;
6115};
6116
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117static int md_seq_show(struct seq_file *seq, void *v)
6118{
6119 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006120 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006122 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006123 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
6125 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006126 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 seq_printf(seq, "Personalities : ");
6128 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006129 list_for_each_entry(pers, &pers_list, list)
6130 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
6132 spin_unlock(&pers_lock);
6133 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006134 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 return 0;
6136 }
6137 if (v == (void*)2) {
6138 status_unused(seq);
6139 return 0;
6140 }
6141
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006142 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006144
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6146 seq_printf(seq, "%s : %sactive", mdname(mddev),
6147 mddev->pers ? "" : "in");
6148 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006149 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006151 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006152 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 seq_printf(seq, " %s", mddev->pers->name);
6154 }
6155
Andre Nolldd8ac332009-03-31 14:33:13 +11006156 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006157 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 char b[BDEVNAME_SIZE];
6159 seq_printf(seq, " %s[%d]",
6160 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006161 if (test_bit(WriteMostly, &rdev->flags))
6162 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006163 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 seq_printf(seq, "(F)");
6165 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006166 } else if (rdev->raid_disk < 0)
6167 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006168 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 }
6170
6171 if (!list_empty(&mddev->disks)) {
6172 if (mddev->pers)
6173 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006174 (unsigned long long)
6175 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 else
6177 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006178 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006180 if (mddev->persistent) {
6181 if (mddev->major_version != 0 ||
6182 mddev->minor_version != 90) {
6183 seq_printf(seq," super %d.%d",
6184 mddev->major_version,
6185 mddev->minor_version);
6186 }
NeilBrowne6910632008-02-06 01:39:51 -08006187 } else if (mddev->external)
6188 seq_printf(seq, " super external:%s",
6189 mddev->metadata_type);
6190 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006191 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192
6193 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006194 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006196 if (mddev->pers->sync_request) {
6197 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006198 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006199 seq_printf(seq, "\n ");
6200 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6201 seq_printf(seq, "\tresync=DELAYED\n ");
6202 else if (mddev->recovery_cp < MaxSector)
6203 seq_printf(seq, "\tresync=PENDING\n ");
6204 }
NeilBrown32a76272005-06-21 17:17:14 -07006205 } else
6206 seq_printf(seq, "\n ");
6207
6208 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006209 unsigned long chunk_kb;
6210 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006211 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006212 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006213 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6214 "%lu%s chunk",
6215 bitmap->pages - bitmap->missing_pages,
6216 bitmap->pages,
6217 (bitmap->pages - bitmap->missing_pages)
6218 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006219 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006220 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006221 if (bitmap->file) {
6222 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006223 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006224 }
NeilBrown78d742d2005-06-21 17:17:15 -07006225
NeilBrown32a76272005-06-21 17:17:14 -07006226 seq_printf(seq, "\n");
6227 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 }
6229
6230 seq_printf(seq, "\n");
6231 }
6232 mddev_unlock(mddev);
6233
6234 return 0;
6235}
6236
Jan Engelhardt110518b2009-05-07 12:49:37 +10006237static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 .start = md_seq_start,
6239 .next = md_seq_next,
6240 .stop = md_seq_stop,
6241 .show = md_seq_show,
6242};
6243
6244static int md_seq_open(struct inode *inode, struct file *file)
6245{
6246 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006247 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6248 if (mi == NULL)
6249 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
6251 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006252 if (error)
6253 kfree(mi);
6254 else {
6255 struct seq_file *p = file->private_data;
6256 p->private = mi;
6257 mi->event = atomic_read(&md_event_count);
6258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 return error;
6260}
6261
NeilBrownd7603b72006-01-06 00:20:30 -08006262static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6263{
6264 struct seq_file *m = filp->private_data;
6265 struct mdstat_info *mi = m->private;
6266 int mask;
6267
6268 poll_wait(filp, &md_event_waiters, wait);
6269
6270 /* always allow read */
6271 mask = POLLIN | POLLRDNORM;
6272
6273 if (mi->event != atomic_read(&md_event_count))
6274 mask |= POLLERR | POLLPRI;
6275 return mask;
6276}
6277
Arjan van de Venfa027c22007-02-12 00:55:33 -08006278static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006279 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 .open = md_seq_open,
6281 .read = seq_read,
6282 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006283 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006284 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285};
6286
NeilBrown2604b702006-01-06 00:20:36 -08006287int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006290 list_add_tail(&p->list, &pers_list);
6291 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 spin_unlock(&pers_lock);
6293 return 0;
6294}
6295
NeilBrown2604b702006-01-06 00:20:36 -08006296int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297{
NeilBrown2604b702006-01-06 00:20:36 -08006298 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006300 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 spin_unlock(&pers_lock);
6302 return 0;
6303}
6304
NeilBrowneea1bf32009-03-31 14:27:02 +11006305static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306{
6307 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006309 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310
6311 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006312 rcu_read_lock();
6313 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006315 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6316 (int)part_stat_read(&disk->part0, sectors[1]) -
6317 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006318 /* sync IO will cause sync_io to increase before the disk_stats
6319 * as sync_io is counted when a request starts, and
6320 * disk_stats is counted when it completes.
6321 * So resync activity will cause curr_events to be smaller than
6322 * when there was no such activity.
6323 * non-sync IO will cause disk_stat to increase without
6324 * increasing sync_io so curr_events will (eventually)
6325 * be larger than it was before. Once it becomes
6326 * substantially larger, the test below will cause
6327 * the array to appear non-idle, and resync will slow
6328 * down.
6329 * If there is a lot of outstanding resync activity when
6330 * we set last_event to curr_events, then all that activity
6331 * completing might cause the array to appear non-idle
6332 * and resync will be slowed down even though there might
6333 * not have been non-resync activity. This will only
6334 * happen once though. 'last_events' will soon reflect
6335 * the state where there is little or no outstanding
6336 * resync requests, and further resync activity will
6337 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006338 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006340 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 rdev->last_events = curr_events;
6342 idle = 0;
6343 }
6344 }
NeilBrown4b809912008-07-21 17:05:25 +10006345 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 return idle;
6347}
6348
6349void md_done_sync(mddev_t *mddev, int blocks, int ok)
6350{
6351 /* another "blocks" (512byte) blocks have been synced */
6352 atomic_sub(blocks, &mddev->recovery_active);
6353 wake_up(&mddev->recovery_wait);
6354 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006355 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 md_wakeup_thread(mddev->thread);
6357 // stop recovery, signal do_sync ....
6358 }
6359}
6360
6361
NeilBrown06d91a52005-06-21 17:17:12 -07006362/* md_write_start(mddev, bi)
6363 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006364 * in superblock) before writing, schedule a superblock update
6365 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006366 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006367void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368{
Neil Brown0fd62b82008-06-28 08:31:36 +10006369 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006370 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006371 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006372
NeilBrownf91de922005-11-08 21:39:36 -08006373 BUG_ON(mddev->ro == 1);
6374 if (mddev->ro == 2) {
6375 /* need to switch to read/write */
6376 mddev->ro = 0;
6377 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6378 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006379 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006380 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006381 }
NeilBrown06d91a52005-06-21 17:17:12 -07006382 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006383 if (mddev->safemode == 1)
6384 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006385 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006386 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006387 if (mddev->in_sync) {
6388 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006389 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006390 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006391 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006392 }
NeilBrowna9701a32005-11-08 21:39:34 -08006393 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006394 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006395 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006396 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006397 wait_event(mddev->sb_wait,
6398 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6399 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400}
6401
6402void md_write_end(mddev_t *mddev)
6403{
6404 if (atomic_dec_and_test(&mddev->writes_pending)) {
6405 if (mddev->safemode == 2)
6406 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006407 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6409 }
6410}
6411
NeilBrown2a2275d2007-01-26 00:57:11 -08006412/* md_allow_write(mddev)
6413 * Calling this ensures that the array is marked 'active' so that writes
6414 * may proceed without blocking. It is important to call this before
6415 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6416 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006417 *
6418 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6419 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006420 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006421int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006422{
6423 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006424 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006425 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006426 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006427 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006428 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006429
6430 spin_lock_irq(&mddev->write_lock);
6431 if (mddev->in_sync) {
6432 mddev->in_sync = 0;
6433 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6434 if (mddev->safemode_delay &&
6435 mddev->safemode == 0)
6436 mddev->safemode = 1;
6437 spin_unlock_irq(&mddev->write_lock);
6438 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006439 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006440 } else
6441 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006442
6443 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6444 return -EAGAIN;
6445 else
6446 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006447}
6448EXPORT_SYMBOL_GPL(md_allow_write);
6449
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450#define SYNC_MARKS 10
6451#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006452void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453{
6454 mddev_t *mddev2;
6455 unsigned int currspeed = 0,
6456 window;
NeilBrown57afd892005-06-21 17:17:13 -07006457 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 unsigned long mark[SYNC_MARKS];
6459 sector_t mark_cnt[SYNC_MARKS];
6460 int last_mark,m;
6461 struct list_head *tmp;
6462 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006463 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006464 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006465 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466
6467 /* just incase thread restarts... */
6468 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6469 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006470 if (mddev->ro) /* never try to sync a read-only array */
6471 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472
NeilBrown61df9d92006-10-03 01:15:57 -07006473 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6474 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6475 desc = "data-check";
6476 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6477 desc = "requested-resync";
6478 else
6479 desc = "resync";
6480 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6481 desc = "reshape";
6482 else
6483 desc = "recovery";
6484
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 /* we overload curr_resync somewhat here.
6486 * 0 == not engaged in resync at all
6487 * 2 == checking that there is no conflict with another sync
6488 * 1 == like 2, but have yielded to allow conflicting resync to
6489 * commense
6490 * other == active in resync - this many blocks
6491 *
6492 * Before starting a resync we must have set curr_resync to
6493 * 2, and then checked that every "conflicting" array has curr_resync
6494 * less than ours. When we find one that is the same or higher
6495 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6496 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6497 * This will mean we have to start checking from the beginning again.
6498 *
6499 */
6500
6501 do {
6502 mddev->curr_resync = 2;
6503
6504 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006505 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006506 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 goto skip;
6508 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006509 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 if (mddev2 == mddev)
6511 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006512 if (!mddev->parallel_resync
6513 && mddev2->curr_resync
6514 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 DEFINE_WAIT(wq);
6516 if (mddev < mddev2 && mddev->curr_resync == 2) {
6517 /* arbitrarily yield */
6518 mddev->curr_resync = 1;
6519 wake_up(&resync_wait);
6520 }
6521 if (mddev > mddev2 && mddev->curr_resync == 1)
6522 /* no need to wait here, we can wait the next
6523 * time 'round when curr_resync == 2
6524 */
6525 continue;
NeilBrown97441972008-09-19 11:49:54 +10006526 /* We need to wait 'interruptible' so as not to
6527 * contribute to the load average, and not to
6528 * be caught by 'softlockup'
6529 */
6530 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006531 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006532 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006533 printk(KERN_INFO "md: delaying %s of %s"
6534 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006536 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006538 if (signal_pending(current))
6539 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 schedule();
6541 finish_wait(&resync_wait, &wq);
6542 goto try_again;
6543 }
6544 finish_wait(&resync_wait, &wq);
6545 }
6546 }
6547 } while (mddev->curr_resync < 2);
6548
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006549 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006550 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006552 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553 */
6554 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006555 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006556 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006557 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6558 j = mddev->resync_min;
6559 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006560 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006561
NeilBrownccfcc3c2006-03-27 01:18:09 -08006562 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006563 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006564 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006566 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006567 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006568 rcu_read_lock();
6569 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006570 if (rdev->raid_disk >= 0 &&
6571 !test_bit(Faulty, &rdev->flags) &&
6572 !test_bit(In_sync, &rdev->flags) &&
6573 rdev->recovery_offset < j)
6574 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006575 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577
NeilBrown61df9d92006-10-03 01:15:57 -07006578 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6579 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6580 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006581 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006582 "(but not more than %d KB/sec) for %s.\n",
6583 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584
NeilBrowneea1bf32009-03-31 14:27:02 +11006585 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006586
NeilBrown57afd892005-06-21 17:17:13 -07006587 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 for (m = 0; m < SYNC_MARKS; m++) {
6589 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006590 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 }
6592 last_mark = 0;
6593 mddev->resync_mark = mark[last_mark];
6594 mddev->resync_mark_cnt = mark_cnt[last_mark];
6595
6596 /*
6597 * Tune reconstruction:
6598 */
6599 window = 32*(PAGE_SIZE/512);
6600 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6601 window/2,(unsigned long long) max_sectors/2);
6602
6603 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 last_check = 0;
6605
6606 if (j>2) {
6607 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006608 "md: resuming %s of %s from checkpoint.\n",
6609 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 mddev->curr_resync = j;
6611 }
NeilBrownefa59332009-12-14 12:49:49 +11006612 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613
6614 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006615 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616
NeilBrown57afd892005-06-21 17:17:13 -07006617 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006618
NeilBrown7a91ee12009-05-26 12:57:21 +10006619 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6620 ((mddev->curr_resync > mddev->curr_resync_completed &&
6621 (mddev->curr_resync - mddev->curr_resync_completed)
6622 > (max_sectors >> 4)) ||
6623 (j - mddev->curr_resync_completed)*2
6624 >= mddev->resync_max - mddev->curr_resync_completed
6625 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006626 /* time to update curr_resync_completed */
6627 blk_unplug(mddev->queue);
6628 wait_event(mddev->recovery_wait,
6629 atomic_read(&mddev->recovery_active) == 0);
6630 mddev->curr_resync_completed =
6631 mddev->curr_resync;
6632 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006633 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006634 }
NeilBrownacb180b2009-04-14 16:28:34 +10006635
NeilBrowne62e58a2009-07-01 13:15:35 +10006636 while (j >= mddev->resync_max && !kthread_should_stop()) {
6637 /* As this condition is controlled by user-space,
6638 * we can block indefinitely, so use '_interruptible'
6639 * to avoid triggering warnings.
6640 */
6641 flush_signals(current); /* just in case */
6642 wait_event_interruptible(mddev->recovery_wait,
6643 mddev->resync_max > j
6644 || kthread_should_stop());
6645 }
NeilBrownacb180b2009-04-14 16:28:34 +10006646
6647 if (kthread_should_stop())
6648 goto interrupted;
6649
NeilBrown57afd892005-06-21 17:17:13 -07006650 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006651 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006652 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006653 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 goto out;
6655 }
NeilBrown57afd892005-06-21 17:17:13 -07006656
6657 if (!skipped) { /* actual IO requested */
6658 io_sectors += sectors;
6659 atomic_add(sectors, &mddev->recovery_active);
6660 }
6661
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 j += sectors;
6663 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006664 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006665 if (last_check == 0)
6666 /* this is the earliers that rebuilt will be
6667 * visible in /proc/mdstat
6668 */
6669 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006670
6671 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 continue;
6673
NeilBrown57afd892005-06-21 17:17:13 -07006674 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675
NeilBrowndfc70642008-05-23 13:04:39 -07006676 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 break;
6678
6679 repeat:
6680 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6681 /* step marks */
6682 int next = (last_mark+1) % SYNC_MARKS;
6683
6684 mddev->resync_mark = mark[next];
6685 mddev->resync_mark_cnt = mark_cnt[next];
6686 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006687 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 last_mark = next;
6689 }
6690
6691
NeilBrownc6207272008-02-06 01:39:52 -08006692 if (kthread_should_stop())
6693 goto interrupted;
6694
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695
6696 /*
6697 * this loop exits only if either when we are slower than
6698 * the 'hard' speed limit, or the system was IO-idle for
6699 * a jiffy.
6700 * the system might be non-idle CPU-wise, but we only care
6701 * about not overloading the IO subsystem. (things like an
6702 * e2fsck being done on the RAID array should execute fast)
6703 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006704 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 cond_resched();
6706
NeilBrown57afd892005-06-21 17:17:13 -07006707 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6708 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709
NeilBrown88202a02006-01-06 00:21:36 -08006710 if (currspeed > speed_min(mddev)) {
6711 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006712 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006713 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 goto repeat;
6715 }
6716 }
6717 }
NeilBrown61df9d92006-10-03 01:15:57 -07006718 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 /*
6720 * this also signals 'finished resyncing' to md_stop
6721 */
6722 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006723 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724
6725 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6726
6727 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006728 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
NeilBrowndfc70642008-05-23 13:04:39 -07006730 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006731 mddev->curr_resync > 2) {
6732 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6733 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6734 if (mddev->curr_resync >= mddev->recovery_cp) {
6735 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006736 "md: checkpointing %s of %s.\n",
6737 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006738 mddev->recovery_cp = mddev->curr_resync;
6739 }
6740 } else
6741 mddev->recovery_cp = MaxSector;
6742 } else {
6743 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6744 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006745 rcu_read_lock();
6746 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006747 if (rdev->raid_disk >= 0 &&
6748 !test_bit(Faulty, &rdev->flags) &&
6749 !test_bit(In_sync, &rdev->flags) &&
6750 rdev->recovery_offset < mddev->curr_resync)
6751 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006752 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 }
NeilBrown17571282006-12-10 02:20:52 -08006755 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006758 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6759 /* We completed so min/max setting can be forgotten if used. */
6760 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6761 mddev->resync_min = 0;
6762 mddev->resync_max = MaxSector;
6763 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6764 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006766 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6767 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006768 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 wake_up(&resync_wait);
6770 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6771 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006772 return;
6773
6774 interrupted:
6775 /*
6776 * got a signal, exit.
6777 */
6778 printk(KERN_INFO
6779 "md: md_do_sync() got signal ... exiting\n");
6780 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6781 goto out;
6782
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783}
NeilBrown29269552006-03-27 01:18:10 -08006784EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785
6786
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006787static int remove_and_add_spares(mddev_t *mddev)
6788{
6789 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006790 int spares = 0;
6791
NeilBrown97e4f422009-03-31 14:33:13 +11006792 mddev->curr_resync_completed = 0;
6793
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006794 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006795 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006796 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006797 (test_bit(Faulty, &rdev->flags) ||
6798 ! test_bit(In_sync, &rdev->flags)) &&
6799 atomic_read(&rdev->nr_pending)==0) {
6800 if (mddev->pers->hot_remove_disk(
6801 mddev, rdev->raid_disk)==0) {
6802 char nm[20];
6803 sprintf(nm,"rd%d", rdev->raid_disk);
6804 sysfs_remove_link(&mddev->kobj, nm);
6805 rdev->raid_disk = -1;
6806 }
6807 }
6808
NeilBrown4044ba52009-01-09 08:31:11 +11006809 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006810 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006811 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006812 !test_bit(In_sync, &rdev->flags) &&
6813 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006814 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006815 if (rdev->raid_disk < 0
6816 && !test_bit(Faulty, &rdev->flags)) {
6817 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006818 if (mddev->pers->
6819 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006820 char nm[20];
6821 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006822 if (sysfs_create_link(&mddev->kobj,
6823 &rdev->kobj, nm))
6824 printk(KERN_WARNING
6825 "md: cannot register "
6826 "%s for %s\n",
6827 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006828 spares++;
6829 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006830 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006831 } else
6832 break;
6833 }
NeilBrowndfc70642008-05-23 13:04:39 -07006834 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006835 }
6836 return spares;
6837}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838/*
6839 * This routine is regularly called by all per-raid-array threads to
6840 * deal with generic issues like resync and super-block update.
6841 * Raid personalities that don't have a thread (linear/raid0) do not
6842 * need this as they never do any recovery or update the superblock.
6843 *
6844 * It does not do any resync itself, but rather "forks" off other threads
6845 * to do that as needed.
6846 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6847 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006848 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 * and wakeups up this thread which will reap the thread and finish up.
6850 * This thread also removes any faulty devices (with nr_pending == 0).
6851 *
6852 * The overall approach is:
6853 * 1/ if the superblock needs updating, update it.
6854 * 2/ If a recovery thread is running, don't do anything else.
6855 * 3/ If recovery has finished, clean up, possibly marking spares active.
6856 * 4/ If there are any faulty devices, remove them.
6857 * 5/ If array is degraded, try to add spares devices
6858 * 6/ If array has spares or is not in-sync, start a resync thread.
6859 */
6860void md_check_recovery(mddev_t *mddev)
6861{
6862 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863
6864
NeilBrown5f404022005-06-21 17:17:16 -07006865 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006866 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867
6868 if (mddev->ro)
6869 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006870
6871 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006872 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006873 printk(KERN_INFO "md: %s in immediate safe mode\n",
6874 mdname(mddev));
6875 mddev->safemode = 2;
6876 }
6877 flush_signals(current);
6878 }
6879
NeilBrownc89a8ee2008-08-05 15:54:13 +10006880 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6881 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006883 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006885 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006886 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006887 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6888 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 ))
6890 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006891
NeilBrowndf5b89b2006-03-27 01:18:20 -08006892 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006893 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006894
NeilBrownc89a8ee2008-08-05 15:54:13 +10006895 if (mddev->ro) {
6896 /* Only thing we do on a ro array is remove
6897 * failed devices.
6898 */
6899 remove_and_add_spares(mddev);
6900 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6901 goto unlock;
6902 }
6903
NeilBrown31a59e32008-04-30 00:52:30 -07006904 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006905 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006906 spin_lock_irq(&mddev->write_lock);
6907 if (mddev->safemode &&
6908 !atomic_read(&mddev->writes_pending) &&
6909 !mddev->in_sync &&
6910 mddev->recovery_cp == MaxSector) {
6911 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006912 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006913 if (mddev->persistent)
6914 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6915 }
6916 if (mddev->safemode == 1)
6917 mddev->safemode = 0;
6918 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006919 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006920 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006921 }
NeilBrownfca4d842005-06-21 17:17:11 -07006922
NeilBrown850b2b422006-10-03 01:15:46 -07006923 if (mddev->flags)
6924 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006925
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006926 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006927 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006928 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006929
NeilBrown06d91a52005-06-21 17:17:12 -07006930
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6932 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6933 /* resync/recovery still happening */
6934 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6935 goto unlock;
6936 }
6937 if (mddev->sync_thread) {
6938 /* resync has finished, collect result */
6939 md_unregister_thread(mddev->sync_thread);
6940 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006941 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6942 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 /* success...*/
6944 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006945 if (mddev->pers->spare_active(mddev))
6946 sysfs_notify(&mddev->kobj, NULL,
6947 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 }
NeilBrowncea9c222009-03-31 15:15:05 +11006949 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6950 mddev->pers->finish_reshape)
6951 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07006952 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006953
6954 /* if array is no-longer degraded, then any saved_raid_disk
6955 * information must be scrapped
6956 */
6957 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006958 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006959 rdev->saved_raid_disk = -1;
6960
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 mddev->recovery = 0;
6962 /* flag recovery needed just to double check */
6963 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006964 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006965 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 goto unlock;
6967 }
Neil Brown72a23c22008-06-28 08:31:41 +10006968 /* Set RUNNING before clearing NEEDED to avoid
6969 * any transients in the value of "sync_action".
6970 */
6971 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6972 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006973 /* Clear some bits that don't mean anything, but
6974 * might be left set
6975 */
NeilBrown24dd4692005-11-08 21:39:26 -08006976 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6977 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006979 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6980 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 /* no recovery is running.
6982 * remove any failed drives, then
6983 * add spares if possible.
6984 * Spare are also removed and re-added, to allow
6985 * the personality to fail the re-add.
6986 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006988 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006989 if (mddev->pers->check_reshape == NULL ||
6990 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006991 /* Cannot proceed */
6992 goto unlock;
6993 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006994 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006995 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006996 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6997 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006998 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006999 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007000 } else if (mddev->recovery_cp < MaxSector) {
7001 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007002 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007003 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7004 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007008 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7009 /* We are adding a device or devices to an array
7010 * which has the bitmap stored on all devices.
7011 * So make sure all bitmap pages get written
7012 */
7013 bitmap_write_all(mddev->bitmap);
7014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 mddev->sync_thread = md_register_thread(md_do_sync,
7016 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007017 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 if (!mddev->sync_thread) {
7019 printk(KERN_ERR "%s: could not start resync"
7020 " thread...\n",
7021 mdname(mddev));
7022 /* leave the spares where they are, it shouldn't hurt */
7023 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007024 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11007026 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007027 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 }
7029 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007030 if (!mddev->sync_thread) {
7031 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7032 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7033 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007034 if (mddev->sysfs_action)
7035 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 mddev_unlock(mddev);
7038 }
7039}
7040
Dan Williams6bfe0b42008-04-30 00:52:32 -07007041void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7042{
NeilBrown3c0ee632008-10-21 13:25:28 +11007043 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007044 wait_event_timeout(rdev->blocked_wait,
7045 !test_bit(Blocked, &rdev->flags),
7046 msecs_to_jiffies(5000));
7047 rdev_dec_pending(rdev, mddev);
7048}
7049EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7050
Adrian Bunk75c96f82005-05-05 16:16:09 -07007051static int md_notify_reboot(struct notifier_block *this,
7052 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053{
7054 struct list_head *tmp;
7055 mddev_t *mddev;
7056
7057 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7058
7059 printk(KERN_INFO "md: stopping all md devices.\n");
7060
NeilBrown29ac4aa2008-02-06 01:39:58 -08007061 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007062 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007063 /* Force a switch to readonly even array
7064 * appears to still be in use. Hence
7065 * the '100'.
7066 */
NeilBrownd710e132008-10-13 11:55:12 +11007067 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007068 mddev_unlock(mddev);
7069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 /*
7071 * certain more exotic SCSI devices are known to be
7072 * volatile wrt too early system reboots. While the
7073 * right place to handle this issue is the given
7074 * driver, we do want to have a safe RAID driver ...
7075 */
7076 mdelay(1000*1);
7077 }
7078 return NOTIFY_DONE;
7079}
7080
Adrian Bunk75c96f82005-05-05 16:16:09 -07007081static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 .notifier_call = md_notify_reboot,
7083 .next = NULL,
7084 .priority = INT_MAX, /* before any real devices */
7085};
7086
7087static void md_geninit(void)
7088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7090
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007091 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092}
7093
Adrian Bunk75c96f82005-05-05 16:16:09 -07007094static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007096 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 return -1;
7098 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007099 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100 return -1;
7101 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007102 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007103 md_probe, NULL, NULL);
7104 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 md_probe, NULL, NULL);
7106
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007108 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109
7110 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112}
7113
7114
7115#ifndef MODULE
7116
7117/*
7118 * Searches all registered partitions for autorun RAID arrays
7119 * at boot time.
7120 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007121
7122static LIST_HEAD(all_detected_devices);
7123struct detected_devices_node {
7124 struct list_head list;
7125 dev_t dev;
7126};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127
7128void md_autodetect_dev(dev_t dev)
7129{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007130 struct detected_devices_node *node_detected_dev;
7131
7132 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7133 if (node_detected_dev) {
7134 node_detected_dev->dev = dev;
7135 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7136 } else {
7137 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7138 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140}
7141
7142
7143static void autostart_arrays(int part)
7144{
7145 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007146 struct detected_devices_node *node_detected_dev;
7147 dev_t dev;
7148 int i_scanned, i_passed;
7149
7150 i_scanned = 0;
7151 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152
7153 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7154
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007155 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7156 i_scanned++;
7157 node_detected_dev = list_entry(all_detected_devices.next,
7158 struct detected_devices_node, list);
7159 list_del(&node_detected_dev->list);
7160 dev = node_detected_dev->dev;
7161 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007162 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 if (IS_ERR(rdev))
7164 continue;
7165
NeilBrownb2d444d2005-11-08 21:39:31 -08007166 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 MD_BUG();
7168 continue;
7169 }
NeilBrownd0fae182008-03-04 14:29:31 -08007170 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007172 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007174
7175 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7176 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177
7178 autorun_devices(part);
7179}
7180
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007181#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182
7183static __exit void md_exit(void)
7184{
7185 mddev_t *mddev;
7186 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007187
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007188 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007189 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007191 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 unregister_blkdev(mdp_major, "mdp");
7193 unregister_reboot_notifier(&md_notifier);
7194 unregister_sysctl_table(raid_table_header);
7195 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007196 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007198 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 }
7200}
7201
Dan Williams685784a2007-07-09 11:56:42 -07007202subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203module_exit(md_exit)
7204
NeilBrownf91de922005-11-08 21:39:36 -08007205static int get_ro(char *buffer, struct kernel_param *kp)
7206{
7207 return sprintf(buffer, "%d", start_readonly);
7208}
7209static int set_ro(const char *val, struct kernel_param *kp)
7210{
7211 char *e;
7212 int num = simple_strtoul(val, &e, 10);
7213 if (*val && (*e == '\0' || *e == '\n')) {
7214 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007215 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007216 }
7217 return -EINVAL;
7218}
7219
NeilBrown80ca3a42006-07-10 04:44:18 -07007220module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7221module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007222
NeilBrownefeb53c2009-01-09 08:31:10 +11007223module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007224
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225EXPORT_SYMBOL(register_md_personality);
7226EXPORT_SYMBOL(unregister_md_personality);
7227EXPORT_SYMBOL(md_error);
7228EXPORT_SYMBOL(md_done_sync);
7229EXPORT_SYMBOL(md_write_start);
7230EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231EXPORT_SYMBOL(md_register_thread);
7232EXPORT_SYMBOL(md_unregister_thread);
7233EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234EXPORT_SYMBOL(md_check_recovery);
7235MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007236MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007237MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007238MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);