]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/md/md.c
md: allow suspend_lo and suspend_hi to decrease as well as increase.
[mirror_ubuntu-artful-kernel.git] / drivers / md / md.c
CommitLineData
1da177e4
LT
1/*
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
32a7627c
N
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
1da177e4
LT
25 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
a6fb0934 35#include <linux/kthread.h>
bff61975 36#include <linux/blkdev.h>
1da177e4 37#include <linux/sysctl.h>
bff61975 38#include <linux/seq_file.h>
2a48fc0a 39#include <linux/mutex.h>
1da177e4 40#include <linux/buffer_head.h> /* for invalidate_bdev */
d7603b7e 41#include <linux/poll.h>
16f17b39 42#include <linux/ctype.h>
e7d2860b 43#include <linux/string.h>
fb4d8c76
N
44#include <linux/hdreg.h>
45#include <linux/proc_fs.h>
46#include <linux/random.h>
47#include <linux/reboot.h>
32a7627c 48#include <linux/file.h>
aa98aa31 49#include <linux/compat.h>
25570727 50#include <linux/delay.h>
bff61975
N
51#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
5a0e3ad6 53#include <linux/slab.h>
43b2e5d8 54#include "md.h"
ef740c37 55#include "bitmap.h"
1da177e4
LT
56
57#define DEBUG 0
58#define dprintk(x...) ((void)(DEBUG && printk(x)))
59
1da177e4 60#ifndef MODULE
d710e138 61static void autostart_arrays(int part);
1da177e4
LT
62#endif
63
2604b703 64static LIST_HEAD(pers_list);
1da177e4
LT
65static DEFINE_SPINLOCK(pers_lock);
66
5e56341d
AB
67static void md_print_devices(void);
68
90b08710 69static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
e804ac78
TH
70static struct workqueue_struct *md_wq;
71static struct workqueue_struct *md_misc_wq;
90b08710 72
5e56341d
AB
73#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
74
1e50915f
RB
75/*
76 * Default number of read corrections we'll attempt on an rdev
77 * before ejecting it from the array. We divide the read error
78 * count by 2 for every hour elapsed between read errors.
79 */
80#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
1da177e4
LT
81/*
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
338cec32 85 * the RAID driver will use the maximum available bandwidth if the IO
1da177e4
LT
86 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
88 * idle IO detection.
89 *
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
88202a0c 91 * or /sys/block/mdX/md/sync_speed_{min,max}
1da177e4
LT
92 */
93
94static int sysctl_speed_limit_min = 1000;
95static int sysctl_speed_limit_max = 200000;
88202a0c
N
96static inline int speed_min(mddev_t *mddev)
97{
98 return mddev->sync_speed_min ?
99 mddev->sync_speed_min : sysctl_speed_limit_min;
100}
101
102static inline int speed_max(mddev_t *mddev)
103{
104 return mddev->sync_speed_max ?
105 mddev->sync_speed_max : sysctl_speed_limit_max;
106}
1da177e4
LT
107
108static struct ctl_table_header *raid_table_header;
109
110static ctl_table raid_table[] = {
111 {
1da177e4
LT
112 .procname = "speed_limit_min",
113 .data = &sysctl_speed_limit_min,
114 .maxlen = sizeof(int),
80ca3a44 115 .mode = S_IRUGO|S_IWUSR,
6d456111 116 .proc_handler = proc_dointvec,
1da177e4
LT
117 },
118 {
1da177e4
LT
119 .procname = "speed_limit_max",
120 .data = &sysctl_speed_limit_max,
121 .maxlen = sizeof(int),
80ca3a44 122 .mode = S_IRUGO|S_IWUSR,
6d456111 123 .proc_handler = proc_dointvec,
1da177e4 124 },
894d2491 125 { }
1da177e4
LT
126};
127
128static ctl_table raid_dir_table[] = {
129 {
1da177e4
LT
130 .procname = "raid",
131 .maxlen = 0,
80ca3a44 132 .mode = S_IRUGO|S_IXUGO,
1da177e4
LT
133 .child = raid_table,
134 },
894d2491 135 { }
1da177e4
LT
136};
137
138static ctl_table raid_root_table[] = {
139 {
1da177e4
LT
140 .procname = "dev",
141 .maxlen = 0,
142 .mode = 0555,
143 .child = raid_dir_table,
144 },
894d2491 145 { }
1da177e4
LT
146};
147
83d5cde4 148static const struct block_device_operations md_fops;
1da177e4 149
f91de92e
N
150static int start_readonly;
151
a167f663
N
152/* bio_clone_mddev
153 * like bio_clone, but with a local bio set
154 */
155
156static void mddev_bio_destructor(struct bio *bio)
157{
158 mddev_t *mddev, **mddevp;
159
160 mddevp = (void*)bio;
161 mddev = mddevp[-1];
162
163 bio_free(bio, mddev->bio_set);
164}
165
166struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
167 mddev_t *mddev)
168{
169 struct bio *b;
170 mddev_t **mddevp;
171
172 if (!mddev || !mddev->bio_set)
173 return bio_alloc(gfp_mask, nr_iovecs);
174
175 b = bio_alloc_bioset(gfp_mask, nr_iovecs,
176 mddev->bio_set);
177 if (!b)
178 return NULL;
179 mddevp = (void*)b;
180 mddevp[-1] = mddev;
181 b->bi_destructor = mddev_bio_destructor;
182 return b;
183}
184EXPORT_SYMBOL_GPL(bio_alloc_mddev);
185
186struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
187 mddev_t *mddev)
188{
189 struct bio *b;
190 mddev_t **mddevp;
191
192 if (!mddev || !mddev->bio_set)
193 return bio_clone(bio, gfp_mask);
194
195 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
196 mddev->bio_set);
197 if (!b)
198 return NULL;
199 mddevp = (void*)b;
200 mddevp[-1] = mddev;
201 b->bi_destructor = mddev_bio_destructor;
202 __bio_clone(b, bio);
203 if (bio_integrity(bio)) {
204 int ret;
205
206 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
207
208 if (ret < 0) {
209 bio_put(b);
210 return NULL;
211 }
212 }
213
214 return b;
215}
216EXPORT_SYMBOL_GPL(bio_clone_mddev);
217
d7603b7e
N
218/*
219 * We have a system wide 'event count' that is incremented
220 * on any 'interesting' event, and readers of /proc/mdstat
221 * can use 'poll' or 'select' to find out when the event
222 * count increases.
223 *
224 * Events are:
225 * start array, stop array, error, add device, remove device,
226 * start build, activate spare
227 */
2989ddbd 228static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
d7603b7e 229static atomic_t md_event_count;
29269553 230void md_new_event(mddev_t *mddev)
d7603b7e
N
231{
232 atomic_inc(&md_event_count);
233 wake_up(&md_event_waiters);
234}
29269553 235EXPORT_SYMBOL_GPL(md_new_event);
d7603b7e 236
c331eb04
N
237/* Alternate version that can be called from interrupts
238 * when calling sysfs_notify isn't needed.
239 */
05381954 240static void md_new_event_inintr(mddev_t *mddev)
c331eb04
N
241{
242 atomic_inc(&md_event_count);
243 wake_up(&md_event_waiters);
244}
245
1da177e4
LT
246/*
247 * Enables to iterate over all existing md arrays
248 * all_mddevs_lock protects this list.
249 */
250static LIST_HEAD(all_mddevs);
251static DEFINE_SPINLOCK(all_mddevs_lock);
252
253
254/*
255 * iterates through all used mddevs in the system.
256 * We take care to grab the all_mddevs_lock whenever navigating
257 * the list, and to always hold a refcount when unlocked.
258 * Any code which breaks out of this loop while own
259 * a reference to the current mddev and must mddev_put it.
260 */
29ac4aa3 261#define for_each_mddev(mddev,tmp) \
1da177e4
LT
262 \
263 for (({ spin_lock(&all_mddevs_lock); \
264 tmp = all_mddevs.next; \
265 mddev = NULL;}); \
266 ({ if (tmp != &all_mddevs) \
267 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
268 spin_unlock(&all_mddevs_lock); \
269 if (mddev) mddev_put(mddev); \
270 mddev = list_entry(tmp, mddev_t, all_mddevs); \
271 tmp != &all_mddevs;}); \
272 ({ spin_lock(&all_mddevs_lock); \
273 tmp = tmp->next;}) \
274 )
275
276
409c57f3
N
277/* Rather than calling directly into the personality make_request function,
278 * IO requests come here first so that we can check if the device is
279 * being suspended pending a reconfiguration.
280 * We hold a refcount over the call to ->make_request. By the time that
281 * call has finished, the bio has been linked into some internal structure
282 * and so is visible to ->quiesce(), so we don't need the refcount any more.
283 */
284static int md_make_request(struct request_queue *q, struct bio *bio)
1da177e4 285{
49077326 286 const int rw = bio_data_dir(bio);
409c57f3
N
287 mddev_t *mddev = q->queuedata;
288 int rv;
49077326
N
289 int cpu;
290
0ca69886
N
291 if (mddev == NULL || mddev->pers == NULL
292 || !mddev->ready) {
409c57f3
N
293 bio_io_error(bio);
294 return 0;
295 }
0ca69886 296 smp_rmb(); /* Ensure implications of 'active' are visible */
409c57f3 297 rcu_read_lock();
e9c7469b 298 if (mddev->suspended) {
409c57f3
N
299 DEFINE_WAIT(__wait);
300 for (;;) {
301 prepare_to_wait(&mddev->sb_wait, &__wait,
302 TASK_UNINTERRUPTIBLE);
e9c7469b 303 if (!mddev->suspended)
409c57f3
N
304 break;
305 rcu_read_unlock();
306 schedule();
307 rcu_read_lock();
308 }
309 finish_wait(&mddev->sb_wait, &__wait);
310 }
311 atomic_inc(&mddev->active_io);
312 rcu_read_unlock();
49077326 313
21a52c6d 314 rv = mddev->pers->make_request(mddev, bio);
49077326
N
315
316 cpu = part_stat_lock();
317 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
318 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
319 bio_sectors(bio));
320 part_stat_unlock();
321
409c57f3
N
322 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
323 wake_up(&mddev->sb_wait);
324
325 return rv;
326}
327
9e35b99c
N
328/* mddev_suspend makes sure no new requests are submitted
329 * to the device, and that any requests that have been submitted
330 * are completely handled.
331 * Once ->stop is called and completes, the module will be completely
332 * unused.
333 */
390ee602 334void mddev_suspend(mddev_t *mddev)
409c57f3
N
335{
336 BUG_ON(mddev->suspended);
337 mddev->suspended = 1;
338 synchronize_rcu();
339 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
340 mddev->pers->quiesce(mddev, 1);
409c57f3 341}
390ee602 342EXPORT_SYMBOL_GPL(mddev_suspend);
409c57f3 343
390ee602 344void mddev_resume(mddev_t *mddev)
409c57f3
N
345{
346 mddev->suspended = 0;
347 wake_up(&mddev->sb_wait);
348 mddev->pers->quiesce(mddev, 0);
1da177e4 349}
390ee602 350EXPORT_SYMBOL_GPL(mddev_resume);
1da177e4 351
3fa841d7
N
352int mddev_congested(mddev_t *mddev, int bits)
353{
354 return mddev->suspended;
355}
356EXPORT_SYMBOL(mddev_congested);
357
a2826aa9 358/*
e9c7469b 359 * Generic flush handling for md
a2826aa9
N
360 */
361
e9c7469b 362static void md_end_flush(struct bio *bio, int err)
a2826aa9
N
363{
364 mdk_rdev_t *rdev = bio->bi_private;
365 mddev_t *mddev = rdev->mddev;
a2826aa9
N
366
367 rdev_dec_pending(rdev, mddev);
368
369 if (atomic_dec_and_test(&mddev->flush_pending)) {
e9c7469b 370 /* The pre-request flush has finished */
e804ac78 371 queue_work(md_wq, &mddev->flush_work);
a2826aa9
N
372 }
373 bio_put(bio);
374}
375
a7a07e69
N
376static void md_submit_flush_data(struct work_struct *ws);
377
a035fc3e 378static void submit_flushes(struct work_struct *ws)
a2826aa9 379{
a035fc3e 380 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
a2826aa9
N
381 mdk_rdev_t *rdev;
382
a7a07e69
N
383 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
384 atomic_set(&mddev->flush_pending, 1);
a2826aa9
N
385 rcu_read_lock();
386 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
387 if (rdev->raid_disk >= 0 &&
388 !test_bit(Faulty, &rdev->flags)) {
389 /* Take two references, one is dropped
390 * when request finishes, one after
391 * we reclaim rcu_read_lock
392 */
393 struct bio *bi;
394 atomic_inc(&rdev->nr_pending);
395 atomic_inc(&rdev->nr_pending);
396 rcu_read_unlock();
a167f663 397 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
e9c7469b 398 bi->bi_end_io = md_end_flush;
a2826aa9
N
399 bi->bi_private = rdev;
400 bi->bi_bdev = rdev->bdev;
401 atomic_inc(&mddev->flush_pending);
e9c7469b 402 submit_bio(WRITE_FLUSH, bi);
a2826aa9
N
403 rcu_read_lock();
404 rdev_dec_pending(rdev, mddev);
405 }
406 rcu_read_unlock();
a7a07e69
N
407 if (atomic_dec_and_test(&mddev->flush_pending))
408 queue_work(md_wq, &mddev->flush_work);
a2826aa9
N
409}
410
e9c7469b 411static void md_submit_flush_data(struct work_struct *ws)
a2826aa9 412{
e9c7469b
TH
413 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
414 struct bio *bio = mddev->flush_bio;
a2826aa9 415
e9c7469b 416 if (bio->bi_size == 0)
a2826aa9
N
417 /* an empty barrier - all done */
418 bio_endio(bio, 0);
419 else {
e9c7469b 420 bio->bi_rw &= ~REQ_FLUSH;
21a52c6d 421 if (mddev->pers->make_request(mddev, bio))
a2826aa9 422 generic_make_request(bio);
a2826aa9 423 }
2b74e12e
N
424
425 mddev->flush_bio = NULL;
426 wake_up(&mddev->sb_wait);
a2826aa9
N
427}
428
e9c7469b 429void md_flush_request(mddev_t *mddev, struct bio *bio)
a2826aa9
N
430{
431 spin_lock_irq(&mddev->write_lock);
432 wait_event_lock_irq(mddev->sb_wait,
e9c7469b 433 !mddev->flush_bio,
a2826aa9 434 mddev->write_lock, /*nothing*/);
e9c7469b 435 mddev->flush_bio = bio;
a2826aa9
N
436 spin_unlock_irq(&mddev->write_lock);
437
a035fc3e
N
438 INIT_WORK(&mddev->flush_work, submit_flushes);
439 queue_work(md_wq, &mddev->flush_work);
a2826aa9 440}
e9c7469b 441EXPORT_SYMBOL(md_flush_request);
409c57f3 442
2ac87401
N
443/* Support for plugging.
444 * This mirrors the plugging support in request_queue, but does not
445 * require having a whole queue
446 */
447static void plugger_work(struct work_struct *work)
448{
449 struct plug_handle *plug =
450 container_of(work, struct plug_handle, unplug_work);
451 plug->unplug_fn(plug);
452}
453static void plugger_timeout(unsigned long data)
454{
455 struct plug_handle *plug = (void *)data;
456 kblockd_schedule_work(NULL, &plug->unplug_work);
457}
458void plugger_init(struct plug_handle *plug,
459 void (*unplug_fn)(struct plug_handle *))
460{
461 plug->unplug_flag = 0;
462 plug->unplug_fn = unplug_fn;
463 init_timer(&plug->unplug_timer);
464 plug->unplug_timer.function = plugger_timeout;
465 plug->unplug_timer.data = (unsigned long)plug;
466 INIT_WORK(&plug->unplug_work, plugger_work);
467}
468EXPORT_SYMBOL_GPL(plugger_init);
469
470void plugger_set_plug(struct plug_handle *plug)
471{
472 if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag))
473 mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1);
474}
475EXPORT_SYMBOL_GPL(plugger_set_plug);
476
477int plugger_remove_plug(struct plug_handle *plug)
478{
479 if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) {
480 del_timer(&plug->unplug_timer);
481 return 1;
482 } else
483 return 0;
484}
485EXPORT_SYMBOL_GPL(plugger_remove_plug);
486
487
1da177e4
LT
488static inline mddev_t *mddev_get(mddev_t *mddev)
489{
490 atomic_inc(&mddev->active);
491 return mddev;
492}
493
5fd3a17e 494static void mddev_delayed_delete(struct work_struct *ws);
d3374825 495
1da177e4
LT
496static void mddev_put(mddev_t *mddev)
497{
a167f663
N
498 struct bio_set *bs = NULL;
499
1da177e4
LT
500 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
501 return;
d3374825 502 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
cbd19983
N
503 mddev->ctime == 0 && !mddev->hold_active) {
504 /* Array is not configured at all, and not held active,
505 * so destroy it */
1da177e4 506 list_del(&mddev->all_mddevs);
a167f663
N
507 bs = mddev->bio_set;
508 mddev->bio_set = NULL;
d3374825 509 if (mddev->gendisk) {
e804ac78
TH
510 /* We did a probe so need to clean up. Call
511 * queue_work inside the spinlock so that
512 * flush_workqueue() after mddev_find will
513 * succeed in waiting for the work to be done.
d3374825
N
514 */
515 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
e804ac78 516 queue_work(md_misc_wq, &mddev->del_work);
d3374825
N
517 } else
518 kfree(mddev);
519 }
520 spin_unlock(&all_mddevs_lock);
a167f663
N
521 if (bs)
522 bioset_free(bs);
1da177e4
LT
523}
524
390ee602 525void mddev_init(mddev_t *mddev)
fafd7fb0
N
526{
527 mutex_init(&mddev->open_mutex);
528 mutex_init(&mddev->reconfig_mutex);
529 mutex_init(&mddev->bitmap_info.mutex);
530 INIT_LIST_HEAD(&mddev->disks);
531 INIT_LIST_HEAD(&mddev->all_mddevs);
532 init_timer(&mddev->safemode_timer);
533 atomic_set(&mddev->active, 1);
534 atomic_set(&mddev->openers, 0);
535 atomic_set(&mddev->active_io, 0);
536 spin_lock_init(&mddev->write_lock);
537 atomic_set(&mddev->flush_pending, 0);
538 init_waitqueue_head(&mddev->sb_wait);
539 init_waitqueue_head(&mddev->recovery_wait);
540 mddev->reshape_position = MaxSector;
541 mddev->resync_min = 0;
542 mddev->resync_max = MaxSector;
543 mddev->level = LEVEL_NONE;
544}
390ee602 545EXPORT_SYMBOL_GPL(mddev_init);
fafd7fb0 546
1da177e4
LT
547static mddev_t * mddev_find(dev_t unit)
548{
549 mddev_t *mddev, *new = NULL;
550
551 retry:
552 spin_lock(&all_mddevs_lock);
efeb53c0
N
553
554 if (unit) {
555 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
556 if (mddev->unit == unit) {
557 mddev_get(mddev);
558 spin_unlock(&all_mddevs_lock);
559 kfree(new);
560 return mddev;
561 }
562
563 if (new) {
564 list_add(&new->all_mddevs, &all_mddevs);
1da177e4 565 spin_unlock(&all_mddevs_lock);
efeb53c0
N
566 new->hold_active = UNTIL_IOCTL;
567 return new;
1da177e4 568 }
efeb53c0
N
569 } else if (new) {
570 /* find an unused unit number */
571 static int next_minor = 512;
572 int start = next_minor;
573 int is_free = 0;
574 int dev = 0;
575 while (!is_free) {
576 dev = MKDEV(MD_MAJOR, next_minor);
577 next_minor++;
578 if (next_minor > MINORMASK)
579 next_minor = 0;
580 if (next_minor == start) {
581 /* Oh dear, all in use. */
582 spin_unlock(&all_mddevs_lock);
583 kfree(new);
584 return NULL;
585 }
586
587 is_free = 1;
588 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
589 if (mddev->unit == dev) {
590 is_free = 0;
591 break;
592 }
593 }
594 new->unit = dev;
595 new->md_minor = MINOR(dev);
596 new->hold_active = UNTIL_STOP;
1da177e4
LT
597 list_add(&new->all_mddevs, &all_mddevs);
598 spin_unlock(&all_mddevs_lock);
599 return new;
600 }
601 spin_unlock(&all_mddevs_lock);
602
9ffae0cf 603 new = kzalloc(sizeof(*new), GFP_KERNEL);
1da177e4
LT
604 if (!new)
605 return NULL;
606
1da177e4
LT
607 new->unit = unit;
608 if (MAJOR(unit) == MD_MAJOR)
609 new->md_minor = MINOR(unit);
610 else
611 new->md_minor = MINOR(unit) >> MdpMinorShift;
612
fafd7fb0 613 mddev_init(new);
1da177e4 614
1da177e4
LT
615 goto retry;
616}
617
618static inline int mddev_lock(mddev_t * mddev)
619{
df5b89b3 620 return mutex_lock_interruptible(&mddev->reconfig_mutex);
1da177e4
LT
621}
622
b522adcd
DW
623static inline int mddev_is_locked(mddev_t *mddev)
624{
625 return mutex_is_locked(&mddev->reconfig_mutex);
626}
627
1da177e4
LT
628static inline int mddev_trylock(mddev_t * mddev)
629{
df5b89b3 630 return mutex_trylock(&mddev->reconfig_mutex);
1da177e4
LT
631}
632
b6eb127d
N
633static struct attribute_group md_redundancy_group;
634
a64c876f 635static void mddev_unlock(mddev_t * mddev)
1da177e4 636{
a64c876f 637 if (mddev->to_remove) {
b6eb127d
N
638 /* These cannot be removed under reconfig_mutex as
639 * an access to the files will try to take reconfig_mutex
640 * while holding the file unremovable, which leads to
641 * a deadlock.
bb4f1e9d
N
642 * So hold set sysfs_active while the remove in happeing,
643 * and anything else which might set ->to_remove or my
644 * otherwise change the sysfs namespace will fail with
645 * -EBUSY if sysfs_active is still set.
646 * We set sysfs_active under reconfig_mutex and elsewhere
647 * test it under the same mutex to ensure its correct value
648 * is seen.
b6eb127d 649 */
a64c876f
N
650 struct attribute_group *to_remove = mddev->to_remove;
651 mddev->to_remove = NULL;
bb4f1e9d 652 mddev->sysfs_active = 1;
b6eb127d
N
653 mutex_unlock(&mddev->reconfig_mutex);
654
00bcb4ac
N
655 if (mddev->kobj.sd) {
656 if (to_remove != &md_redundancy_group)
657 sysfs_remove_group(&mddev->kobj, to_remove);
658 if (mddev->pers == NULL ||
659 mddev->pers->sync_request == NULL) {
660 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
661 if (mddev->sysfs_action)
662 sysfs_put(mddev->sysfs_action);
663 mddev->sysfs_action = NULL;
664 }
a64c876f 665 }
bb4f1e9d 666 mddev->sysfs_active = 0;
b6eb127d
N
667 } else
668 mutex_unlock(&mddev->reconfig_mutex);
1da177e4 669
005eca5e 670 md_wakeup_thread(mddev->thread);
1da177e4
LT
671}
672
2989ddbd 673static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
1da177e4 674{
159ec1fc 675 mdk_rdev_t *rdev;
1da177e4 676
159ec1fc 677 list_for_each_entry(rdev, &mddev->disks, same_set)
1da177e4
LT
678 if (rdev->desc_nr == nr)
679 return rdev;
159ec1fc 680
1da177e4
LT
681 return NULL;
682}
683
684static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
685{
1da177e4
LT
686 mdk_rdev_t *rdev;
687
159ec1fc 688 list_for_each_entry(rdev, &mddev->disks, same_set)
1da177e4
LT
689 if (rdev->bdev->bd_dev == dev)
690 return rdev;
159ec1fc 691
1da177e4
LT
692 return NULL;
693}
694
d9d166c2 695static struct mdk_personality *find_pers(int level, char *clevel)
2604b703
N
696{
697 struct mdk_personality *pers;
d9d166c2
N
698 list_for_each_entry(pers, &pers_list, list) {
699 if (level != LEVEL_NONE && pers->level == level)
2604b703 700 return pers;
d9d166c2
N
701 if (strcmp(pers->name, clevel)==0)
702 return pers;
703 }
2604b703
N
704 return NULL;
705}
706
b73df2d3 707/* return the offset of the super block in 512byte sectors */
57b2caa3 708static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev)
1da177e4 709{
57b2caa3 710 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
b73df2d3 711 return MD_NEW_SIZE_SECTORS(num_sectors);
1da177e4
LT
712}
713
1da177e4
LT
714static int alloc_disk_sb(mdk_rdev_t * rdev)
715{
716 if (rdev->sb_page)
717 MD_BUG();
718
719 rdev->sb_page = alloc_page(GFP_KERNEL);
720 if (!rdev->sb_page) {
721 printk(KERN_ALERT "md: out of memory.\n");
ebc24337 722 return -ENOMEM;
1da177e4
LT
723 }
724
725 return 0;
726}
727
728static void free_disk_sb(mdk_rdev_t * rdev)
729{
730 if (rdev->sb_page) {
2d1f3b5d 731 put_page(rdev->sb_page);
1da177e4
LT
732 rdev->sb_loaded = 0;
733 rdev->sb_page = NULL;
0f420358 734 rdev->sb_start = 0;
dd8ac336 735 rdev->sectors = 0;
1da177e4
LT
736 }
737}
738
739
6712ecf8 740static void super_written(struct bio *bio, int error)
7bfa19f2
N
741{
742 mdk_rdev_t *rdev = bio->bi_private;
a9701a30 743 mddev_t *mddev = rdev->mddev;
7bfa19f2 744
3a0f5bbb
N
745 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
746 printk("md: super_written gets error=%d, uptodate=%d\n",
747 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
748 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
a9701a30 749 md_error(mddev, rdev);
3a0f5bbb 750 }
7bfa19f2 751
a9701a30
N
752 if (atomic_dec_and_test(&mddev->pending_writes))
753 wake_up(&mddev->sb_wait);
f8b58edf 754 bio_put(bio);
7bfa19f2
N
755}
756
757void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
758 sector_t sector, int size, struct page *page)
759{
760 /* write first size bytes of page to sector of rdev
761 * Increment mddev->pending_writes before returning
762 * and decrement it on completion, waking up sb_wait
763 * if zero is reached.
764 * If an error occurred, call md_error
765 */
a167f663 766 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
7bfa19f2 767
a6ff7e08 768 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
7bfa19f2
N
769 bio->bi_sector = sector;
770 bio_add_page(bio, page, size, 0);
771 bio->bi_private = rdev;
772 bio->bi_end_io = super_written;
a9701a30 773
7bfa19f2 774 atomic_inc(&mddev->pending_writes);
e9c7469b
TH
775 submit_bio(REQ_WRITE | REQ_SYNC | REQ_UNPLUG | REQ_FLUSH | REQ_FUA,
776 bio);
a9701a30
N
777}
778
779void md_super_wait(mddev_t *mddev)
780{
e9c7469b 781 /* wait for all superblock writes that were scheduled to complete */
a9701a30
N
782 DEFINE_WAIT(wq);
783 for(;;) {
784 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
785 if (atomic_read(&mddev->pending_writes)==0)
786 break;
a9701a30
N
787 schedule();
788 }
789 finish_wait(&mddev->sb_wait, &wq);
7bfa19f2
N
790}
791
6712ecf8 792static void bi_complete(struct bio *bio, int error)
1da177e4 793{
1da177e4 794 complete((struct completion*)bio->bi_private);
1da177e4
LT
795}
796
2b193363 797int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
ccebd4c4 798 struct page *page, int rw, bool metadata_op)
1da177e4 799{
a167f663 800 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
1da177e4
LT
801 struct completion event;
802 int ret;
803
7b6d91da 804 rw |= REQ_SYNC | REQ_UNPLUG;
1da177e4 805
a6ff7e08
JB
806 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
807 rdev->meta_bdev : rdev->bdev;
ccebd4c4
JB
808 if (metadata_op)
809 bio->bi_sector = sector + rdev->sb_start;
810 else
811 bio->bi_sector = sector + rdev->data_offset;
1da177e4
LT
812 bio_add_page(bio, page, size, 0);
813 init_completion(&event);
814 bio->bi_private = &event;
815 bio->bi_end_io = bi_complete;
816 submit_bio(rw, bio);
817 wait_for_completion(&event);
818
819 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
820 bio_put(bio);
821 return ret;
822}
a8745db2 823EXPORT_SYMBOL_GPL(sync_page_io);
1da177e4 824
0002b271 825static int read_disk_sb(mdk_rdev_t * rdev, int size)
1da177e4
LT
826{
827 char b[BDEVNAME_SIZE];
828 if (!rdev->sb_page) {
829 MD_BUG();
830 return -EINVAL;
831 }
832 if (rdev->sb_loaded)
833 return 0;
834
835
ccebd4c4 836 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
1da177e4
LT
837 goto fail;
838 rdev->sb_loaded = 1;
839 return 0;
840
841fail:
842 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
843 bdevname(rdev->bdev,b));
844 return -EINVAL;
845}
846
847static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
848{
05710466
AN
849 return sb1->set_uuid0 == sb2->set_uuid0 &&
850 sb1->set_uuid1 == sb2->set_uuid1 &&
851 sb1->set_uuid2 == sb2->set_uuid2 &&
852 sb1->set_uuid3 == sb2->set_uuid3;
1da177e4
LT
853}
854
1da177e4
LT
855static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
856{
857 int ret;
858 mdp_super_t *tmp1, *tmp2;
859
860 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
861 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
862
863 if (!tmp1 || !tmp2) {
864 ret = 0;
35020f1a 865 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
1da177e4
LT
866 goto abort;
867 }
868
869 *tmp1 = *sb1;
870 *tmp2 = *sb2;
871
872 /*
873 * nr_disks is not constant
874 */
875 tmp1->nr_disks = 0;
876 tmp2->nr_disks = 0;
877
ce0c8e05 878 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
1da177e4 879abort:
990a8baf
JJ
880 kfree(tmp1);
881 kfree(tmp2);
1da177e4
LT
882 return ret;
883}
884
4d167f09
N
885
886static u32 md_csum_fold(u32 csum)
887{
888 csum = (csum & 0xffff) + (csum >> 16);
889 return (csum & 0xffff) + (csum >> 16);
890}
891
1da177e4
LT
892static unsigned int calc_sb_csum(mdp_super_t * sb)
893{
4d167f09
N
894 u64 newcsum = 0;
895 u32 *sb32 = (u32*)sb;
896 int i;
1da177e4
LT
897 unsigned int disk_csum, csum;
898
899 disk_csum = sb->sb_csum;
900 sb->sb_csum = 0;
4d167f09
N
901
902 for (i = 0; i < MD_SB_BYTES/4 ; i++)
903 newcsum += sb32[i];
904 csum = (newcsum & 0xffffffff) + (newcsum>>32);
905
906
907#ifdef CONFIG_ALPHA
908 /* This used to use csum_partial, which was wrong for several
909 * reasons including that different results are returned on
910 * different architectures. It isn't critical that we get exactly
911 * the same return value as before (we always csum_fold before
912 * testing, and that removes any differences). However as we
913 * know that csum_partial always returned a 16bit value on
914 * alphas, do a fold to maximise conformity to previous behaviour.
915 */
916 sb->sb_csum = md_csum_fold(disk_csum);
917#else
1da177e4 918 sb->sb_csum = disk_csum;
4d167f09 919#endif
1da177e4
LT
920 return csum;
921}
922
923
924/*
925 * Handle superblock details.
926 * We want to be able to handle multiple superblock formats
927 * so we have a common interface to them all, and an array of
928 * different handlers.
929 * We rely on user-space to write the initial superblock, and support
930 * reading and updating of superblocks.
931 * Interface methods are:
932 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
933 * loads and validates a superblock on dev.
934 * if refdev != NULL, compare superblocks on both devices
935 * Return:
936 * 0 - dev has a superblock that is compatible with refdev
937 * 1 - dev has a superblock that is compatible and newer than refdev
938 * so dev should be used as the refdev in future
939 * -EINVAL superblock incompatible or invalid
940 * -othererror e.g. -EIO
941 *
942 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
943 * Verify that dev is acceptable into mddev.
944 * The first time, mddev->raid_disks will be 0, and data from
945 * dev should be merged in. Subsequent calls check that dev
946 * is new enough. Return 0 or -EINVAL
947 *
948 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
949 * Update the superblock for rdev with data in mddev
950 * This does not write to disc.
951 *
952 */
953
954struct super_type {
0cd17fec
CW
955 char *name;
956 struct module *owner;
957 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
958 int minor_version);
959 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
960 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
961 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
15f4a5fd 962 sector_t num_sectors);
1da177e4
LT
963};
964
0894cc30
AN
965/*
966 * Check that the given mddev has no bitmap.
967 *
968 * This function is called from the run method of all personalities that do not
969 * support bitmaps. It prints an error message and returns non-zero if mddev
970 * has a bitmap. Otherwise, it returns 0.
971 *
972 */
973int md_check_no_bitmap(mddev_t *mddev)
974{
c3d9714e 975 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
0894cc30
AN
976 return 0;
977 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
978 mdname(mddev), mddev->pers->name);
979 return 1;
980}
981EXPORT_SYMBOL(md_check_no_bitmap);
982
1da177e4
LT
983/*
984 * load_super for 0.90.0
985 */
986static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
987{
988 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
989 mdp_super_t *sb;
990 int ret;
1da177e4
LT
991
992 /*
0f420358 993 * Calculate the position of the superblock (512byte sectors),
1da177e4
LT
994 * it's at the end of the disk.
995 *
996 * It also happens to be a multiple of 4Kb.
997 */
57b2caa3 998 rdev->sb_start = calc_dev_sboffset(rdev);
1da177e4 999
0002b271 1000 ret = read_disk_sb(rdev, MD_SB_BYTES);
1da177e4
LT
1001 if (ret) return ret;
1002
1003 ret = -EINVAL;
1004
1005 bdevname(rdev->bdev, b);
1006 sb = (mdp_super_t*)page_address(rdev->sb_page);
1007
1008 if (sb->md_magic != MD_SB_MAGIC) {
1009 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1010 b);
1011 goto abort;
1012 }
1013
1014 if (sb->major_version != 0 ||
f6705578
N
1015 sb->minor_version < 90 ||
1016 sb->minor_version > 91) {
1da177e4
LT
1017 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1018 sb->major_version, sb->minor_version,
1019 b);
1020 goto abort;
1021 }
1022
1023 if (sb->raid_disks <= 0)
1024 goto abort;
1025
4d167f09 1026 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1da177e4
LT
1027 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1028 b);
1029 goto abort;
1030 }
1031
1032 rdev->preferred_minor = sb->md_minor;
1033 rdev->data_offset = 0;
0002b271 1034 rdev->sb_size = MD_SB_BYTES;
1da177e4
LT
1035
1036 if (sb->level == LEVEL_MULTIPATH)
1037 rdev->desc_nr = -1;
1038 else
1039 rdev->desc_nr = sb->this_disk.number;
1040
9a7b2b0f 1041 if (!refdev) {
1da177e4 1042 ret = 1;
9a7b2b0f 1043 } else {
1da177e4
LT
1044 __u64 ev1, ev2;
1045 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
1046 if (!uuid_equal(refsb, sb)) {
1047 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1048 b, bdevname(refdev->bdev,b2));
1049 goto abort;
1050 }
1051 if (!sb_equal(refsb, sb)) {
1052 printk(KERN_WARNING "md: %s has same UUID"
1053 " but different superblock to %s\n",
1054 b, bdevname(refdev->bdev, b2));
1055 goto abort;
1056 }
1057 ev1 = md_event(sb);
1058 ev2 = md_event(refsb);
1059 if (ev1 > ev2)
1060 ret = 1;
1061 else
1062 ret = 0;
1063 }
8190e754 1064 rdev->sectors = rdev->sb_start;
1da177e4 1065
dd8ac336 1066 if (rdev->sectors < sb->size * 2 && sb->level > 1)
2bf071bf
N
1067 /* "this cannot possibly happen" ... */
1068 ret = -EINVAL;
1069
1da177e4
LT
1070 abort:
1071 return ret;
1072}
1073
1074/*
1075 * validate_super for 0.90.0
1076 */
1077static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1078{
1079 mdp_disk_t *desc;
1080 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
07d84d10 1081 __u64 ev1 = md_event(sb);
1da177e4 1082
41158c7e 1083 rdev->raid_disk = -1;
c5d79adb
N
1084 clear_bit(Faulty, &rdev->flags);
1085 clear_bit(In_sync, &rdev->flags);
1086 clear_bit(WriteMostly, &rdev->flags);
c5d79adb 1087
1da177e4
LT
1088 if (mddev->raid_disks == 0) {
1089 mddev->major_version = 0;
1090 mddev->minor_version = sb->minor_version;
1091 mddev->patch_version = sb->patch_version;
e691063a 1092 mddev->external = 0;
9d8f0363 1093 mddev->chunk_sectors = sb->chunk_size >> 9;
1da177e4
LT
1094 mddev->ctime = sb->ctime;
1095 mddev->utime = sb->utime;
1096 mddev->level = sb->level;
d9d166c2 1097 mddev->clevel[0] = 0;
1da177e4
LT
1098 mddev->layout = sb->layout;
1099 mddev->raid_disks = sb->raid_disks;
58c0fed4 1100 mddev->dev_sectors = sb->size * 2;
07d84d10 1101 mddev->events = ev1;
c3d9714e
N
1102 mddev->bitmap_info.offset = 0;
1103 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1da177e4 1104
f6705578
N
1105 if (mddev->minor_version >= 91) {
1106 mddev->reshape_position = sb->reshape_position;
1107 mddev->delta_disks = sb->delta_disks;
1108 mddev->new_level = sb->new_level;
1109 mddev->new_layout = sb->new_layout;
664e7c41 1110 mddev->new_chunk_sectors = sb->new_chunk >> 9;
f6705578
N
1111 } else {
1112 mddev->reshape_position = MaxSector;
1113 mddev->delta_disks = 0;
1114 mddev->new_level = mddev->level;
1115 mddev->new_layout = mddev->layout;
664e7c41 1116 mddev->new_chunk_sectors = mddev->chunk_sectors;
f6705578
N
1117 }
1118
1da177e4
LT
1119 if (sb->state & (1<<MD_SB_CLEAN))
1120 mddev->recovery_cp = MaxSector;
1121 else {
1122 if (sb->events_hi == sb->cp_events_hi &&
1123 sb->events_lo == sb->cp_events_lo) {
1124 mddev->recovery_cp = sb->recovery_cp;
1125 } else
1126 mddev->recovery_cp = 0;
1127 }
1128
1129 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1130 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1131 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1132 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1133
1134 mddev->max_disks = MD_SB_DISKS;
a654b9d8
N
1135
1136 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
c3d9714e
N
1137 mddev->bitmap_info.file == NULL)
1138 mddev->bitmap_info.offset =
1139 mddev->bitmap_info.default_offset;
a654b9d8 1140
41158c7e 1141 } else if (mddev->pers == NULL) {
be6800a7
N
1142 /* Insist on good event counter while assembling, except
1143 * for spares (which don't need an event count) */
1da177e4 1144 ++ev1;
be6800a7
N
1145 if (sb->disks[rdev->desc_nr].state & (
1146 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1147 if (ev1 < mddev->events)
1148 return -EINVAL;
41158c7e
N
1149 } else if (mddev->bitmap) {
1150 /* if adding to array with a bitmap, then we can accept an
1151 * older device ... but not too old.
1152 */
41158c7e
N
1153 if (ev1 < mddev->bitmap->events_cleared)
1154 return 0;
07d84d10
N
1155 } else {
1156 if (ev1 < mddev->events)
1157 /* just a hot-add of a new device, leave raid_disk at -1 */
1158 return 0;
1159 }
41158c7e 1160
1da177e4 1161 if (mddev->level != LEVEL_MULTIPATH) {
1da177e4
LT
1162 desc = sb->disks + rdev->desc_nr;
1163
1164 if (desc->state & (1<<MD_DISK_FAULTY))
b2d444d7 1165 set_bit(Faulty, &rdev->flags);
7c7546cc
N
1166 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1167 desc->raid_disk < mddev->raid_disks */) {
b2d444d7 1168 set_bit(In_sync, &rdev->flags);
1da177e4 1169 rdev->raid_disk = desc->raid_disk;
0261cd9f
N
1170 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1171 /* active but not in sync implies recovery up to
1172 * reshape position. We don't know exactly where
1173 * that is, so set to zero for now */
1174 if (mddev->minor_version >= 91) {
1175 rdev->recovery_offset = 0;
1176 rdev->raid_disk = desc->raid_disk;
1177 }
1da177e4 1178 }
8ddf9efe
N
1179 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1180 set_bit(WriteMostly, &rdev->flags);
41158c7e 1181 } else /* MULTIPATH are always insync */
b2d444d7 1182 set_bit(In_sync, &rdev->flags);
1da177e4
LT
1183 return 0;
1184}
1185
1186/*
1187 * sync_super for 0.90.0
1188 */
1189static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1190{
1191 mdp_super_t *sb;
1da177e4
LT
1192 mdk_rdev_t *rdev2;
1193 int next_spare = mddev->raid_disks;
19133a42 1194
1da177e4
LT
1195
1196 /* make rdev->sb match mddev data..
1197 *
1198 * 1/ zero out disks
1199 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1200 * 3/ any empty disks < next_spare become removed
1201 *
1202 * disks[0] gets initialised to REMOVED because
1203 * we cannot be sure from other fields if it has
1204 * been initialised or not.
1205 */
1206 int i;
1207 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1208
61181565
N
1209 rdev->sb_size = MD_SB_BYTES;
1210
1da177e4
LT
1211 sb = (mdp_super_t*)page_address(rdev->sb_page);
1212
1213 memset(sb, 0, sizeof(*sb));
1214
1215 sb->md_magic = MD_SB_MAGIC;
1216 sb->major_version = mddev->major_version;
1da177e4
LT
1217 sb->patch_version = mddev->patch_version;
1218 sb->gvalid_words = 0; /* ignored */
1219 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1220 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1221 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1222 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1223
1224 sb->ctime = mddev->ctime;
1225 sb->level = mddev->level;
58c0fed4 1226 sb->size = mddev->dev_sectors / 2;
1da177e4
LT
1227 sb->raid_disks = mddev->raid_disks;
1228 sb->md_minor = mddev->md_minor;
e691063a 1229 sb->not_persistent = 0;
1da177e4
LT
1230 sb->utime = mddev->utime;
1231 sb->state = 0;
1232 sb->events_hi = (mddev->events>>32);
1233 sb->events_lo = (u32)mddev->events;
1234
f6705578
N
1235 if (mddev->reshape_position == MaxSector)
1236 sb->minor_version = 90;
1237 else {
1238 sb->minor_version = 91;
1239 sb->reshape_position = mddev->reshape_position;
1240 sb->new_level = mddev->new_level;
1241 sb->delta_disks = mddev->delta_disks;
1242 sb->new_layout = mddev->new_layout;
664e7c41 1243 sb->new_chunk = mddev->new_chunk_sectors << 9;
f6705578
N
1244 }
1245 mddev->minor_version = sb->minor_version;
1da177e4
LT
1246 if (mddev->in_sync)
1247 {
1248 sb->recovery_cp = mddev->recovery_cp;
1249 sb->cp_events_hi = (mddev->events>>32);
1250 sb->cp_events_lo = (u32)mddev->events;
1251 if (mddev->recovery_cp == MaxSector)
1252 sb->state = (1<< MD_SB_CLEAN);
1253 } else
1254 sb->recovery_cp = 0;
1255
1256 sb->layout = mddev->layout;
9d8f0363 1257 sb->chunk_size = mddev->chunk_sectors << 9;
1da177e4 1258
c3d9714e 1259 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
a654b9d8
N
1260 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1261
1da177e4 1262 sb->disks[0].state = (1<<MD_DISK_REMOVED);
159ec1fc 1263 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1da177e4 1264 mdp_disk_t *d;
86e6ffdd 1265 int desc_nr;
0261cd9f
N
1266 int is_active = test_bit(In_sync, &rdev2->flags);
1267
1268 if (rdev2->raid_disk >= 0 &&
1269 sb->minor_version >= 91)
1270 /* we have nowhere to store the recovery_offset,
1271 * but if it is not below the reshape_position,
1272 * we can piggy-back on that.
1273 */
1274 is_active = 1;
1275 if (rdev2->raid_disk < 0 ||
1276 test_bit(Faulty, &rdev2->flags))
1277 is_active = 0;
1278 if (is_active)
86e6ffdd 1279 desc_nr = rdev2->raid_disk;
1da177e4 1280 else
86e6ffdd 1281 desc_nr = next_spare++;
19133a42 1282 rdev2->desc_nr = desc_nr;
1da177e4
LT
1283 d = &sb->disks[rdev2->desc_nr];
1284 nr_disks++;
1285 d->number = rdev2->desc_nr;
1286 d->major = MAJOR(rdev2->bdev->bd_dev);
1287 d->minor = MINOR(rdev2->bdev->bd_dev);
0261cd9f 1288 if (is_active)
1da177e4
LT
1289 d->raid_disk = rdev2->raid_disk;
1290 else
1291 d->raid_disk = rdev2->desc_nr; /* compatibility */
1be7892f 1292 if (test_bit(Faulty, &rdev2->flags))
1da177e4 1293 d->state = (1<<MD_DISK_FAULTY);
0261cd9f 1294 else if (is_active) {
1da177e4 1295 d->state = (1<<MD_DISK_ACTIVE);
0261cd9f
N
1296 if (test_bit(In_sync, &rdev2->flags))
1297 d->state |= (1<<MD_DISK_SYNC);
1da177e4
LT
1298 active++;
1299 working++;
1300 } else {
1301 d->state = 0;
1302 spare++;
1303 working++;
1304 }
8ddf9efe
N
1305 if (test_bit(WriteMostly, &rdev2->flags))
1306 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1da177e4 1307 }
1da177e4
LT
1308 /* now set the "removed" and "faulty" bits on any missing devices */
1309 for (i=0 ; i < mddev->raid_disks ; i++) {
1310 mdp_disk_t *d = &sb->disks[i];
1311 if (d->state == 0 && d->number == 0) {
1312 d->number = i;
1313 d->raid_disk = i;
1314 d->state = (1<<MD_DISK_REMOVED);
1315 d->state |= (1<<MD_DISK_FAULTY);
1316 failed++;
1317 }
1318 }
1319 sb->nr_disks = nr_disks;
1320 sb->active_disks = active;
1321 sb->working_disks = working;
1322 sb->failed_disks = failed;
1323 sb->spare_disks = spare;
1324
1325 sb->this_disk = sb->disks[rdev->desc_nr];
1326 sb->sb_csum = calc_sb_csum(sb);
1327}
1328
0cd17fec
CW
1329/*
1330 * rdev_size_change for 0.90.0
1331 */
1332static unsigned long long
15f4a5fd 1333super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
0cd17fec 1334{
58c0fed4 1335 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
0cd17fec 1336 return 0; /* component must fit device */
c3d9714e 1337 if (rdev->mddev->bitmap_info.offset)
0cd17fec 1338 return 0; /* can't move bitmap */
57b2caa3 1339 rdev->sb_start = calc_dev_sboffset(rdev);
15f4a5fd
AN
1340 if (!num_sectors || num_sectors > rdev->sb_start)
1341 num_sectors = rdev->sb_start;
0f420358 1342 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
0cd17fec
CW
1343 rdev->sb_page);
1344 md_super_wait(rdev->mddev);
c26a44ed 1345 return num_sectors;
0cd17fec
CW
1346}
1347
1348
1da177e4
LT
1349/*
1350 * version 1 superblock
1351 */
1352
1c05b4bc 1353static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1da177e4 1354{
1c05b4bc
N
1355 __le32 disk_csum;
1356 u32 csum;
1da177e4
LT
1357 unsigned long long newcsum;
1358 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1c05b4bc 1359 __le32 *isuper = (__le32*)sb;
1da177e4
LT
1360 int i;
1361
1362 disk_csum = sb->sb_csum;
1363 sb->sb_csum = 0;
1364 newcsum = 0;
1365 for (i=0; size>=4; size -= 4 )
1366 newcsum += le32_to_cpu(*isuper++);
1367
1368 if (size == 2)
1c05b4bc 1369 newcsum += le16_to_cpu(*(__le16*) isuper);
1da177e4
LT
1370
1371 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1372 sb->sb_csum = disk_csum;
1373 return cpu_to_le32(csum);
1374}
1375
1376static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1377{
1378 struct mdp_superblock_1 *sb;
1379 int ret;
0f420358 1380 sector_t sb_start;
1da177e4 1381 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
0002b271 1382 int bmask;
1da177e4
LT
1383
1384 /*
0f420358 1385 * Calculate the position of the superblock in 512byte sectors.
1da177e4
LT
1386 * It is always aligned to a 4K boundary and
1387 * depeding on minor_version, it can be:
1388 * 0: At least 8K, but less than 12K, from end of device
1389 * 1: At start of device
1390 * 2: 4K from start of device.
1391 */
1392 switch(minor_version) {
1393 case 0:
77304d2a 1394 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
0f420358
AN
1395 sb_start -= 8*2;
1396 sb_start &= ~(sector_t)(4*2-1);
1da177e4
LT
1397 break;
1398 case 1:
0f420358 1399 sb_start = 0;
1da177e4
LT
1400 break;
1401 case 2:
0f420358 1402 sb_start = 8;
1da177e4
LT
1403 break;
1404 default:
1405 return -EINVAL;
1406 }
0f420358 1407 rdev->sb_start = sb_start;
1da177e4 1408
0002b271
N
1409 /* superblock is rarely larger than 1K, but it can be larger,
1410 * and it is safe to read 4k, so we do that
1411 */
1412 ret = read_disk_sb(rdev, 4096);
1da177e4
LT
1413 if (ret) return ret;
1414
1415
1416 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1417
1418 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1419 sb->major_version != cpu_to_le32(1) ||
1420 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
0f420358 1421 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
71c0805c 1422 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1da177e4
LT
1423 return -EINVAL;
1424
1425 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1426 printk("md: invalid superblock checksum on %s\n",
1427 bdevname(rdev->bdev,b));
1428 return -EINVAL;
1429 }
1430 if (le64_to_cpu(sb->data_size) < 10) {
1431 printk("md: data_size too small on %s\n",
1432 bdevname(rdev->bdev,b));
1433 return -EINVAL;
1434 }
e11e93fa 1435
1da177e4
LT
1436 rdev->preferred_minor = 0xffff;
1437 rdev->data_offset = le64_to_cpu(sb->data_offset);
4dbcdc75 1438 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1da177e4 1439
0002b271 1440 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
e1defc4f 1441 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
0002b271 1442 if (rdev->sb_size & bmask)
a1801f85
N
1443 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1444
1445 if (minor_version
0f420358 1446 && rdev->data_offset < sb_start + (rdev->sb_size/512))
a1801f85 1447 return -EINVAL;
0002b271 1448
31b65a0d
N
1449 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1450 rdev->desc_nr = -1;
1451 else
1452 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1453
9a7b2b0f 1454 if (!refdev) {
8ed75463 1455 ret = 1;
9a7b2b0f 1456 } else {
1da177e4
LT
1457 __u64 ev1, ev2;
1458 struct mdp_superblock_1 *refsb =
1459 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1460
1461 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1462 sb->level != refsb->level ||
1463 sb->layout != refsb->layout ||
1464 sb->chunksize != refsb->chunksize) {
1465 printk(KERN_WARNING "md: %s has strangely different"
1466 " superblock to %s\n",
1467 bdevname(rdev->bdev,b),
1468 bdevname(refdev->bdev,b2));
1469 return -EINVAL;
1470 }
1471 ev1 = le64_to_cpu(sb->events);
1472 ev2 = le64_to_cpu(refsb->events);
1473
1474 if (ev1 > ev2)
8ed75463
N
1475 ret = 1;
1476 else
1477 ret = 0;
1da177e4 1478 }
a1801f85 1479 if (minor_version)
77304d2a 1480 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
dd8ac336 1481 le64_to_cpu(sb->data_offset);
1da177e4 1482 else
dd8ac336
AN
1483 rdev->sectors = rdev->sb_start;
1484 if (rdev->sectors < le64_to_cpu(sb->data_size))
1da177e4 1485 return -EINVAL;
dd8ac336 1486 rdev->sectors = le64_to_cpu(sb->data_size);
dd8ac336 1487 if (le64_to_cpu(sb->size) > rdev->sectors)
2bf071bf 1488 return -EINVAL;
8ed75463 1489 return ret;
1da177e4
LT
1490}
1491
1492static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1493{
1494 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
07d84d10 1495 __u64 ev1 = le64_to_cpu(sb->events);
1da177e4 1496
41158c7e 1497 rdev->raid_disk = -1;
c5d79adb
N
1498 clear_bit(Faulty, &rdev->flags);
1499 clear_bit(In_sync, &rdev->flags);
1500 clear_bit(WriteMostly, &rdev->flags);
c5d79adb 1501
1da177e4
LT
1502 if (mddev->raid_disks == 0) {
1503 mddev->major_version = 1;
1504 mddev->patch_version = 0;
e691063a 1505 mddev->external = 0;
9d8f0363 1506 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1da177e4
LT
1507 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1508 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1509 mddev->level = le32_to_cpu(sb->level);
d9d166c2 1510 mddev->clevel[0] = 0;
1da177e4
LT
1511 mddev->layout = le32_to_cpu(sb->layout);
1512 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
58c0fed4 1513 mddev->dev_sectors = le64_to_cpu(sb->size);
07d84d10 1514 mddev->events = ev1;
c3d9714e
N
1515 mddev->bitmap_info.offset = 0;
1516 mddev->bitmap_info.default_offset = 1024 >> 9;
1da177e4
LT
1517
1518 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1519 memcpy(mddev->uuid, sb->set_uuid, 16);
1520
1521 mddev->max_disks = (4096-256)/2;
a654b9d8 1522
71c0805c 1523 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
c3d9714e
N
1524 mddev->bitmap_info.file == NULL )
1525 mddev->bitmap_info.offset =
1526 (__s32)le32_to_cpu(sb->bitmap_offset);
e11e93fa 1527
f6705578
N
1528 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1529 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1530 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1531 mddev->new_level = le32_to_cpu(sb->new_level);
1532 mddev->new_layout = le32_to_cpu(sb->new_layout);
664e7c41 1533 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
f6705578
N
1534 } else {
1535 mddev->reshape_position = MaxSector;
1536 mddev->delta_disks = 0;
1537 mddev->new_level = mddev->level;
1538 mddev->new_layout = mddev->layout;
664e7c41 1539 mddev->new_chunk_sectors = mddev->chunk_sectors;
f6705578
N
1540 }
1541
41158c7e 1542 } else if (mddev->pers == NULL) {
be6800a7
N
1543 /* Insist of good event counter while assembling, except for
1544 * spares (which don't need an event count) */
1da177e4 1545 ++ev1;
be6800a7
N
1546 if (rdev->desc_nr >= 0 &&
1547 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1548 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1549 if (ev1 < mddev->events)
1550 return -EINVAL;
41158c7e
N
1551 } else if (mddev->bitmap) {
1552 /* If adding to array with a bitmap, then we can accept an
1553 * older device, but not too old.
1554 */
41158c7e
N
1555 if (ev1 < mddev->bitmap->events_cleared)
1556 return 0;
07d84d10
N
1557 } else {
1558 if (ev1 < mddev->events)
1559 /* just a hot-add of a new device, leave raid_disk at -1 */
1560 return 0;
1561 }
1da177e4
LT
1562 if (mddev->level != LEVEL_MULTIPATH) {
1563 int role;
3673f305
N
1564 if (rdev->desc_nr < 0 ||
1565 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1566 role = 0xffff;
1567 rdev->desc_nr = -1;
1568 } else
1569 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1da177e4
LT
1570 switch(role) {
1571 case 0xffff: /* spare */
1da177e4
LT
1572 break;
1573 case 0xfffe: /* faulty */
b2d444d7 1574 set_bit(Faulty, &rdev->flags);
1da177e4
LT
1575 break;
1576 default:
5fd6c1dc
N
1577 if ((le32_to_cpu(sb->feature_map) &
1578 MD_FEATURE_RECOVERY_OFFSET))
1579 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1580 else
1581 set_bit(In_sync, &rdev->flags);
1da177e4
LT
1582 rdev->raid_disk = role;
1583 break;
1584 }
8ddf9efe
N
1585 if (sb->devflags & WriteMostly1)
1586 set_bit(WriteMostly, &rdev->flags);
41158c7e 1587 } else /* MULTIPATH are always insync */
b2d444d7 1588 set_bit(In_sync, &rdev->flags);
41158c7e 1589
1da177e4
LT
1590 return 0;
1591}
1592
1593static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1594{
1595 struct mdp_superblock_1 *sb;
1da177e4
LT
1596 mdk_rdev_t *rdev2;
1597 int max_dev, i;
1598 /* make rdev->sb match mddev and rdev data. */
1599
1600 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1601
1602 sb->feature_map = 0;
1603 sb->pad0 = 0;
5fd6c1dc 1604 sb->recovery_offset = cpu_to_le64(0);
1da177e4
LT
1605 memset(sb->pad1, 0, sizeof(sb->pad1));
1606 memset(sb->pad2, 0, sizeof(sb->pad2));
1607 memset(sb->pad3, 0, sizeof(sb->pad3));
1608
1609 sb->utime = cpu_to_le64((__u64)mddev->utime);
1610 sb->events = cpu_to_le64(mddev->events);
1611 if (mddev->in_sync)
1612 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1613 else
1614 sb->resync_offset = cpu_to_le64(0);
1615
1c05b4bc 1616 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
4dbcdc75 1617
f0ca340c 1618 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
58c0fed4 1619 sb->size = cpu_to_le64(mddev->dev_sectors);
9d8f0363 1620 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
62e1e389
N
1621 sb->level = cpu_to_le32(mddev->level);
1622 sb->layout = cpu_to_le32(mddev->layout);
f0ca340c 1623
c3d9714e
N
1624 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1625 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
71c0805c 1626 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
a654b9d8 1627 }
5fd6c1dc
N
1628
1629 if (rdev->raid_disk >= 0 &&
97e4f42d 1630 !test_bit(In_sync, &rdev->flags)) {
93be75ff
N
1631 sb->feature_map |=
1632 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1633 sb->recovery_offset =
1634 cpu_to_le64(rdev->recovery_offset);
5fd6c1dc
N
1635 }
1636
f6705578
N
1637 if (mddev->reshape_position != MaxSector) {
1638 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1639 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1640 sb->new_layout = cpu_to_le32(mddev->new_layout);
1641 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1642 sb->new_level = cpu_to_le32(mddev->new_level);
664e7c41 1643 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
f6705578 1644 }
a654b9d8 1645
1da177e4 1646 max_dev = 0;
159ec1fc 1647 list_for_each_entry(rdev2, &mddev->disks, same_set)
1da177e4
LT
1648 if (rdev2->desc_nr+1 > max_dev)
1649 max_dev = rdev2->desc_nr+1;
a778b73f 1650
70471daf
N
1651 if (max_dev > le32_to_cpu(sb->max_dev)) {
1652 int bmask;
a778b73f 1653 sb->max_dev = cpu_to_le32(max_dev);
70471daf
N
1654 rdev->sb_size = max_dev * 2 + 256;
1655 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1656 if (rdev->sb_size & bmask)
1657 rdev->sb_size = (rdev->sb_size | bmask) + 1;
ddcf3522
N
1658 } else
1659 max_dev = le32_to_cpu(sb->max_dev);
1660
1da177e4
LT
1661 for (i=0; i<max_dev;i++)
1662 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1663
159ec1fc 1664 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1da177e4 1665 i = rdev2->desc_nr;
b2d444d7 1666 if (test_bit(Faulty, &rdev2->flags))
1da177e4 1667 sb->dev_roles[i] = cpu_to_le16(0xfffe);
b2d444d7 1668 else if (test_bit(In_sync, &rdev2->flags))
1da177e4 1669 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
93be75ff 1670 else if (rdev2->raid_disk >= 0)
5fd6c1dc 1671 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1da177e4
LT
1672 else
1673 sb->dev_roles[i] = cpu_to_le16(0xffff);
1674 }
1675
1da177e4
LT
1676 sb->sb_csum = calc_sb_1_csum(sb);
1677}
1678
0cd17fec 1679static unsigned long long
15f4a5fd 1680super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
0cd17fec
CW
1681{
1682 struct mdp_superblock_1 *sb;
15f4a5fd 1683 sector_t max_sectors;
58c0fed4 1684 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
0cd17fec 1685 return 0; /* component must fit device */
0f420358 1686 if (rdev->sb_start < rdev->data_offset) {
0cd17fec 1687 /* minor versions 1 and 2; superblock before data */
77304d2a 1688 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
15f4a5fd
AN
1689 max_sectors -= rdev->data_offset;
1690 if (!num_sectors || num_sectors > max_sectors)
1691 num_sectors = max_sectors;
c3d9714e 1692 } else if (rdev->mddev->bitmap_info.offset) {
0cd17fec
CW
1693 /* minor version 0 with bitmap we can't move */
1694 return 0;
1695 } else {
1696 /* minor version 0; superblock after data */
0f420358 1697 sector_t sb_start;
77304d2a 1698 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
0f420358 1699 sb_start &= ~(sector_t)(4*2 - 1);
dd8ac336 1700 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
15f4a5fd
AN
1701 if (!num_sectors || num_sectors > max_sectors)
1702 num_sectors = max_sectors;
0f420358 1703 rdev->sb_start = sb_start;
0cd17fec
CW
1704 }
1705 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
15f4a5fd 1706 sb->data_size = cpu_to_le64(num_sectors);
0f420358 1707 sb->super_offset = rdev->sb_start;
0cd17fec 1708 sb->sb_csum = calc_sb_1_csum(sb);
0f420358 1709 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
0cd17fec
CW
1710 rdev->sb_page);
1711 md_super_wait(rdev->mddev);
c26a44ed 1712 return num_sectors;
0cd17fec 1713}
1da177e4 1714
75c96f85 1715static struct super_type super_types[] = {
1da177e4
LT
1716 [0] = {
1717 .name = "0.90.0",
1718 .owner = THIS_MODULE,
0cd17fec
CW
1719 .load_super = super_90_load,
1720 .validate_super = super_90_validate,
1721 .sync_super = super_90_sync,
1722 .rdev_size_change = super_90_rdev_size_change,
1da177e4
LT
1723 },
1724 [1] = {
1725 .name = "md-1",
1726 .owner = THIS_MODULE,
0cd17fec
CW
1727 .load_super = super_1_load,
1728 .validate_super = super_1_validate,
1729 .sync_super = super_1_sync,
1730 .rdev_size_change = super_1_rdev_size_change,
1da177e4
LT
1731 },
1732};
1da177e4
LT
1733
1734static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1735{
7dd5e7c3 1736 mdk_rdev_t *rdev, *rdev2;
1da177e4 1737
4b80991c
N
1738 rcu_read_lock();
1739 rdev_for_each_rcu(rdev, mddev1)
1740 rdev_for_each_rcu(rdev2, mddev2)
7dd5e7c3 1741 if (rdev->bdev->bd_contains ==
4b80991c
N
1742 rdev2->bdev->bd_contains) {
1743 rcu_read_unlock();
7dd5e7c3 1744 return 1;
4b80991c
N
1745 }
1746 rcu_read_unlock();
1da177e4
LT
1747 return 0;
1748}
1749
1750static LIST_HEAD(pending_raid_disks);
1751
ac5e7113
AN
1752/*
1753 * Try to register data integrity profile for an mddev
1754 *
1755 * This is called when an array is started and after a disk has been kicked
1756 * from the array. It only succeeds if all working and active component devices
1757 * are integrity capable with matching profiles.
1758 */
1759int md_integrity_register(mddev_t *mddev)
1760{
1761 mdk_rdev_t *rdev, *reference = NULL;
1762
1763 if (list_empty(&mddev->disks))
1764 return 0; /* nothing to do */
1765 if (blk_get_integrity(mddev->gendisk))
1766 return 0; /* already registered */
1767 list_for_each_entry(rdev, &mddev->disks, same_set) {
1768 /* skip spares and non-functional disks */
1769 if (test_bit(Faulty, &rdev->flags))
1770 continue;
1771 if (rdev->raid_disk < 0)
1772 continue;
1773 /*
1774 * If at least one rdev is not integrity capable, we can not
1775 * enable data integrity for the md device.
1776 */
1777 if (!bdev_get_integrity(rdev->bdev))
1778 return -EINVAL;
1779 if (!reference) {
1780 /* Use the first rdev as the reference */
1781 reference = rdev;
1782 continue;
1783 }
1784 /* does this rdev's profile match the reference profile? */
1785 if (blk_integrity_compare(reference->bdev->bd_disk,
1786 rdev->bdev->bd_disk) < 0)
1787 return -EINVAL;
1788 }
1789 /*
1790 * All component devices are integrity capable and have matching
1791 * profiles, register the common profile for the md device.
1792 */
1793 if (blk_integrity_register(mddev->gendisk,
1794 bdev_get_integrity(reference->bdev)) != 0) {
1795 printk(KERN_ERR "md: failed to register integrity for %s\n",
1796 mdname(mddev));
1797 return -EINVAL;
1798 }
1799 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1800 mdname(mddev));
1801 return 0;
1802}
1803EXPORT_SYMBOL(md_integrity_register);
1804
1805/* Disable data integrity if non-capable/non-matching disk is being added */
1806void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
3f9d99c1 1807{
3f9d99c1 1808 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
ac5e7113 1809 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
3f9d99c1 1810
ac5e7113 1811 if (!bi_mddev) /* nothing to do */
3f9d99c1 1812 return;
ac5e7113 1813 if (rdev->raid_disk < 0) /* skip spares */
3f9d99c1 1814 return;
ac5e7113
AN
1815 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1816 rdev->bdev->bd_disk) >= 0)
1817 return;
1818 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1819 blk_integrity_unregister(mddev->gendisk);
3f9d99c1 1820}
ac5e7113 1821EXPORT_SYMBOL(md_integrity_add_rdev);
3f9d99c1 1822
1da177e4
LT
1823static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1824{
7dd5e7c3 1825 char b[BDEVNAME_SIZE];
f637b9f9 1826 struct kobject *ko;
1edf80d3 1827 char *s;
5e55e2f5 1828 int err;
1da177e4
LT
1829
1830 if (rdev->mddev) {
1831 MD_BUG();
1832 return -EINVAL;
1833 }
11e2ede0
DW
1834
1835 /* prevent duplicates */
1836 if (find_rdev(mddev, rdev->bdev->bd_dev))
1837 return -EEXIST;
1838
dd8ac336
AN
1839 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1840 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1841 rdev->sectors < mddev->dev_sectors)) {
a778b73f
N
1842 if (mddev->pers) {
1843 /* Cannot change size, so fail
1844 * If mddev->level <= 0, then we don't care
1845 * about aligning sizes (e.g. linear)
1846 */
1847 if (mddev->level > 0)
1848 return -ENOSPC;
1849 } else
dd8ac336 1850 mddev->dev_sectors = rdev->sectors;
2bf071bf 1851 }
1da177e4
LT
1852
1853 /* Verify rdev->desc_nr is unique.
1854 * If it is -1, assign a free number, else
1855 * check number is not in use
1856 */
1857 if (rdev->desc_nr < 0) {
1858 int choice = 0;
1859 if (mddev->pers) choice = mddev->raid_disks;
1860 while (find_rdev_nr(mddev, choice))
1861 choice++;
1862 rdev->desc_nr = choice;
1863 } else {
1864 if (find_rdev_nr(mddev, rdev->desc_nr))
1865 return -EBUSY;
1866 }
de01dfad
N
1867 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1868 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1869 mdname(mddev), mddev->max_disks);
1870 return -EBUSY;
1871 }
19133a42 1872 bdevname(rdev->bdev,b);
649316b2 1873 while ( (s=strchr(b, '/')) != NULL)
1edf80d3 1874 *s = '!';
649316b2 1875
1da177e4 1876 rdev->mddev = mddev;
19133a42 1877 printk(KERN_INFO "md: bind<%s>\n", b);
86e6ffdd 1878
b2d6db58 1879 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
5e55e2f5 1880 goto fail;
86e6ffdd 1881
0762b8bd 1882 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
00bcb4ac
N
1883 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1884 /* failure here is OK */;
1885 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
3c0ee63a 1886
4b80991c 1887 list_add_rcu(&rdev->same_set, &mddev->disks);
c5d79adb 1888 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
4044ba58
N
1889
1890 /* May as well allow recovery to be retried once */
1891 mddev->recovery_disabled = 0;
3f9d99c1 1892
1da177e4 1893 return 0;
5e55e2f5
N
1894
1895 fail:
1896 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1897 b, mdname(mddev));
1898 return err;
1da177e4
LT
1899}
1900
177a99b2 1901static void md_delayed_delete(struct work_struct *ws)
5792a285
N
1902{
1903 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1904 kobject_del(&rdev->kobj);
177a99b2 1905 kobject_put(&rdev->kobj);
5792a285
N
1906}
1907
1da177e4
LT
1908static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1909{
1910 char b[BDEVNAME_SIZE];
1911 if (!rdev->mddev) {
1912 MD_BUG();
1913 return;
1914 }
5463c790 1915 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
4b80991c 1916 list_del_rcu(&rdev->same_set);
1da177e4
LT
1917 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1918 rdev->mddev = NULL;
86e6ffdd 1919 sysfs_remove_link(&rdev->kobj, "block");
3c0ee63a
N
1920 sysfs_put(rdev->sysfs_state);
1921 rdev->sysfs_state = NULL;
5792a285 1922 /* We need to delay this, otherwise we can deadlock when
4b80991c
N
1923 * writing to 'remove' to "dev/state". We also need
1924 * to delay it due to rcu usage.
5792a285 1925 */
4b80991c 1926 synchronize_rcu();
177a99b2
N
1927 INIT_WORK(&rdev->del_work, md_delayed_delete);
1928 kobject_get(&rdev->kobj);
e804ac78 1929 queue_work(md_misc_wq, &rdev->del_work);
1da177e4
LT
1930}
1931
1932/*
1933 * prevent the device from being mounted, repartitioned or
1934 * otherwise reused by a RAID array (or any other kernel
1935 * subsystem), by bd_claiming the device.
1936 */
c5d79adb 1937static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1da177e4
LT
1938{
1939 int err = 0;
1940 struct block_device *bdev;
1941 char b[BDEVNAME_SIZE];
1942
2e7b651d 1943 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1da177e4
LT
1944 if (IS_ERR(bdev)) {
1945 printk(KERN_ERR "md: could not open %s.\n",
1946 __bdevname(dev, b));
1947 return PTR_ERR(bdev);
1948 }
c5d79adb 1949 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1da177e4
LT
1950 if (err) {
1951 printk(KERN_ERR "md: could not bd_claim %s.\n",
1952 bdevname(bdev, b));
9a1c3542 1953 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1da177e4
LT
1954 return err;
1955 }
c5d79adb
N
1956 if (!shared)
1957 set_bit(AllReserved, &rdev->flags);
1da177e4
LT
1958 rdev->bdev = bdev;
1959 return err;
1960}
1961
1962static void unlock_rdev(mdk_rdev_t *rdev)
1963{
1964 struct block_device *bdev = rdev->bdev;
1965 rdev->bdev = NULL;
1966 if (!bdev)
1967 MD_BUG();
1968 bd_release(bdev);
9a1c3542 1969 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1da177e4
LT
1970}
1971
1972void md_autodetect_dev(dev_t dev);
1973
1974static void export_rdev(mdk_rdev_t * rdev)
1975{
1976 char b[BDEVNAME_SIZE];
1977 printk(KERN_INFO "md: export_rdev(%s)\n",
1978 bdevname(rdev->bdev,b));
1979 if (rdev->mddev)
1980 MD_BUG();
1981 free_disk_sb(rdev);
1da177e4 1982#ifndef MODULE
d0fae18f
N
1983 if (test_bit(AutoDetected, &rdev->flags))
1984 md_autodetect_dev(rdev->bdev->bd_dev);
1da177e4
LT
1985#endif
1986 unlock_rdev(rdev);
86e6ffdd 1987 kobject_put(&rdev->kobj);
1da177e4
LT
1988}
1989
1990static void kick_rdev_from_array(mdk_rdev_t * rdev)
1991{
1992 unbind_rdev_from_array(rdev);
1993 export_rdev(rdev);
1994}
1995
1996static void export_array(mddev_t *mddev)
1997{
159ec1fc 1998 mdk_rdev_t *rdev, *tmp;
1da177e4 1999
d089c6af 2000 rdev_for_each(rdev, tmp, mddev) {
1da177e4
LT
2001 if (!rdev->mddev) {
2002 MD_BUG();
2003 continue;
2004 }
2005 kick_rdev_from_array(rdev);
2006 }
2007 if (!list_empty(&mddev->disks))
2008 MD_BUG();
2009 mddev->raid_disks = 0;
2010 mddev->major_version = 0;
2011}
2012
2013static void print_desc(mdp_disk_t *desc)
2014{
2015 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2016 desc->major,desc->minor,desc->raid_disk,desc->state);
2017}
2018
cd2ac932 2019static void print_sb_90(mdp_super_t *sb)
1da177e4
LT
2020{
2021 int i;
2022
2023 printk(KERN_INFO
2024 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2025 sb->major_version, sb->minor_version, sb->patch_version,
2026 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2027 sb->ctime);
2028 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2029 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2030 sb->md_minor, sb->layout, sb->chunk_size);
2031 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2032 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2033 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2034 sb->failed_disks, sb->spare_disks,
2035 sb->sb_csum, (unsigned long)sb->events_lo);
2036
2037 printk(KERN_INFO);
2038 for (i = 0; i < MD_SB_DISKS; i++) {
2039 mdp_disk_t *desc;
2040
2041 desc = sb->disks + i;
2042 if (desc->number || desc->major || desc->minor ||
2043 desc->raid_disk || (desc->state && (desc->state != 4))) {
2044 printk(" D %2d: ", i);
2045 print_desc(desc);
2046 }
2047 }
2048 printk(KERN_INFO "md: THIS: ");
2049 print_desc(&sb->this_disk);
cd2ac932 2050}
1da177e4 2051
cd2ac932
CR
2052static void print_sb_1(struct mdp_superblock_1 *sb)
2053{
2054 __u8 *uuid;
2055
2056 uuid = sb->set_uuid;
ad361c98 2057 printk(KERN_INFO
7b75c2f8 2058 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
ad361c98 2059 "md: Name: \"%s\" CT:%llu\n",
cd2ac932
CR
2060 le32_to_cpu(sb->major_version),
2061 le32_to_cpu(sb->feature_map),
7b75c2f8 2062 uuid,
cd2ac932
CR
2063 sb->set_name,
2064 (unsigned long long)le64_to_cpu(sb->ctime)
2065 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2066
2067 uuid = sb->device_uuid;
ad361c98
JP
2068 printk(KERN_INFO
2069 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
cd2ac932 2070 " RO:%llu\n"
7b75c2f8 2071 "md: Dev:%08x UUID: %pU\n"
ad361c98
JP
2072 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2073 "md: (MaxDev:%u) \n",
cd2ac932
CR
2074 le32_to_cpu(sb->level),
2075 (unsigned long long)le64_to_cpu(sb->size),
2076 le32_to_cpu(sb->raid_disks),
2077 le32_to_cpu(sb->layout),
2078 le32_to_cpu(sb->chunksize),
2079 (unsigned long long)le64_to_cpu(sb->data_offset),
2080 (unsigned long long)le64_to_cpu(sb->data_size),
2081 (unsigned long long)le64_to_cpu(sb->super_offset),
2082 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2083 le32_to_cpu(sb->dev_number),
7b75c2f8 2084 uuid,
cd2ac932
CR
2085 sb->devflags,
2086 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2087 (unsigned long long)le64_to_cpu(sb->events),
2088 (unsigned long long)le64_to_cpu(sb->resync_offset),
2089 le32_to_cpu(sb->sb_csum),
2090 le32_to_cpu(sb->max_dev)
2091 );
1da177e4
LT
2092}
2093
cd2ac932 2094static void print_rdev(mdk_rdev_t *rdev, int major_version)
1da177e4
LT
2095{
2096 char b[BDEVNAME_SIZE];
dd8ac336
AN
2097 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2098 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
b2d444d7
N
2099 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2100 rdev->desc_nr);
1da177e4 2101 if (rdev->sb_loaded) {
cd2ac932
CR
2102 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2103 switch (major_version) {
2104 case 0:
2105 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2106 break;
2107 case 1:
2108 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2109 break;
2110 }
1da177e4
LT
2111 } else
2112 printk(KERN_INFO "md: no rdev superblock!\n");
2113}
2114
5e56341d 2115static void md_print_devices(void)
1da177e4 2116{
159ec1fc 2117 struct list_head *tmp;
1da177e4
LT
2118 mdk_rdev_t *rdev;
2119 mddev_t *mddev;
2120 char b[BDEVNAME_SIZE];
2121
2122 printk("\n");
2123 printk("md: **********************************\n");
2124 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2125 printk("md: **********************************\n");
29ac4aa3 2126 for_each_mddev(mddev, tmp) {
1da177e4 2127
32a7627c
N
2128 if (mddev->bitmap)
2129 bitmap_print_sb(mddev->bitmap);
2130 else
2131 printk("%s: ", mdname(mddev));
159ec1fc 2132 list_for_each_entry(rdev, &mddev->disks, same_set)
1da177e4
LT
2133 printk("<%s>", bdevname(rdev->bdev,b));
2134 printk("\n");
2135
159ec1fc 2136 list_for_each_entry(rdev, &mddev->disks, same_set)
cd2ac932 2137 print_rdev(rdev, mddev->major_version);
1da177e4
LT
2138 }
2139 printk("md: **********************************\n");
2140 printk("\n");
2141}
2142
2143
42543769 2144static void sync_sbs(mddev_t * mddev, int nospares)
1da177e4 2145{
42543769
N
2146 /* Update each superblock (in-memory image), but
2147 * if we are allowed to, skip spares which already
2148 * have the right event counter, or have one earlier
2149 * (which would mean they aren't being marked as dirty
2150 * with the rest of the array)
2151 */
1da177e4 2152 mdk_rdev_t *rdev;
159ec1fc 2153 list_for_each_entry(rdev, &mddev->disks, same_set) {
42543769
N
2154 if (rdev->sb_events == mddev->events ||
2155 (nospares &&
2156 rdev->raid_disk < 0 &&
42543769
N
2157 rdev->sb_events+1 == mddev->events)) {
2158 /* Don't update this superblock */
2159 rdev->sb_loaded = 2;
2160 } else {
2161 super_types[mddev->major_version].
2162 sync_super(mddev, rdev);
2163 rdev->sb_loaded = 1;
2164 }
1da177e4
LT
2165 }
2166}
2167
850b2b42 2168static void md_update_sb(mddev_t * mddev, int force_change)
1da177e4 2169{
1da177e4 2170 mdk_rdev_t *rdev;
06d91a5f 2171 int sync_req;
42543769 2172 int nospares = 0;
1da177e4 2173
1da177e4 2174repeat:
3a3a5ddb
N
2175 /* First make sure individual recovery_offsets are correct */
2176 list_for_each_entry(rdev, &mddev->disks, same_set) {
2177 if (rdev->raid_disk >= 0 &&
2178 mddev->delta_disks >= 0 &&
2179 !test_bit(In_sync, &rdev->flags) &&
2180 mddev->curr_resync_completed > rdev->recovery_offset)
2181 rdev->recovery_offset = mddev->curr_resync_completed;
2182
2183 }
bd52b746 2184 if (!mddev->persistent) {
070dc6dd 2185 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
3a3a5ddb 2186 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
d97a41dc
N
2187 if (!mddev->external)
2188 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3a3a5ddb
N
2189 wake_up(&mddev->sb_wait);
2190 return;
2191 }
2192
a9701a30 2193 spin_lock_irq(&mddev->write_lock);
84692195 2194
3a3a5ddb
N
2195 mddev->utime = get_seconds();
2196
850b2b42
N
2197 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2198 force_change = 1;
2199 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2200 /* just a clean<-> dirty transition, possibly leave spares alone,
2201 * though if events isn't the right even/odd, we will have to do
2202 * spares after all
2203 */
2204 nospares = 1;
2205 if (force_change)
2206 nospares = 0;
2207 if (mddev->degraded)
84692195
N
2208 /* If the array is degraded, then skipping spares is both
2209 * dangerous and fairly pointless.
2210 * Dangerous because a device that was removed from the array
2211 * might have a event_count that still looks up-to-date,
2212 * so it can be re-added without a resync.
2213 * Pointless because if there are any spares to skip,
2214 * then a recovery will happen and soon that array won't
2215 * be degraded any more and the spare can go back to sleep then.
2216 */
850b2b42 2217 nospares = 0;
84692195 2218
06d91a5f 2219 sync_req = mddev->in_sync;
42543769
N
2220
2221 /* If this is just a dirty<->clean transition, and the array is clean
2222 * and 'events' is odd, we can roll back to the previous clean state */
850b2b42 2223 if (nospares
42543769 2224 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
a8707c08
N
2225 && mddev->can_decrease_events
2226 && mddev->events != 1) {
42543769 2227 mddev->events--;
a8707c08
N
2228 mddev->can_decrease_events = 0;
2229 } else {
42543769
N
2230 /* otherwise we have to go forward and ... */
2231 mddev->events ++;
a8707c08 2232 mddev->can_decrease_events = nospares;
42543769 2233 }
1da177e4
LT
2234
2235 if (!mddev->events) {
2236 /*
2237 * oops, this 64-bit counter should never wrap.
2238 * Either we are in around ~1 trillion A.C., assuming
2239 * 1 reboot per second, or we have a bug:
2240 */
2241 MD_BUG();
2242 mddev->events --;
2243 }
e691063a 2244 sync_sbs(mddev, nospares);
a9701a30 2245 spin_unlock_irq(&mddev->write_lock);
1da177e4
LT
2246
2247 dprintk(KERN_INFO
2248 "md: updating %s RAID superblock on device (in sync %d)\n",
2249 mdname(mddev),mddev->in_sync);
2250
4ad13663 2251 bitmap_update_sb(mddev->bitmap);
159ec1fc 2252 list_for_each_entry(rdev, &mddev->disks, same_set) {
1da177e4
LT
2253 char b[BDEVNAME_SIZE];
2254 dprintk(KERN_INFO "md: ");
42543769
N
2255 if (rdev->sb_loaded != 1)
2256 continue; /* no noise on spare devices */
b2d444d7 2257 if (test_bit(Faulty, &rdev->flags))
1da177e4
LT
2258 dprintk("(skipping faulty ");
2259
2260 dprintk("%s ", bdevname(rdev->bdev,b));
b2d444d7 2261 if (!test_bit(Faulty, &rdev->flags)) {
7bfa19f2 2262 md_super_write(mddev,rdev,
0f420358 2263 rdev->sb_start, rdev->sb_size,
7bfa19f2
N
2264 rdev->sb_page);
2265 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2266 bdevname(rdev->bdev,b),
0f420358 2267 (unsigned long long)rdev->sb_start);
42543769 2268 rdev->sb_events = mddev->events;
7bfa19f2 2269
1da177e4
LT
2270 } else
2271 dprintk(")\n");
7bfa19f2 2272 if (mddev->level == LEVEL_MULTIPATH)
1da177e4
LT
2273 /* only need to write one superblock... */
2274 break;
2275 }
a9701a30 2276 md_super_wait(mddev);
850b2b42 2277 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
7bfa19f2 2278
a9701a30 2279 spin_lock_irq(&mddev->write_lock);
850b2b42
N
2280 if (mddev->in_sync != sync_req ||
2281 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
06d91a5f 2282 /* have to write it out again */
a9701a30 2283 spin_unlock_irq(&mddev->write_lock);
06d91a5f
N
2284 goto repeat;
2285 }
850b2b42 2286 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
a9701a30 2287 spin_unlock_irq(&mddev->write_lock);
3d310eb7 2288 wake_up(&mddev->sb_wait);
acb180b0
N
2289 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2290 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
06d91a5f 2291
1da177e4
LT
2292}
2293
7f6ce769 2294/* words written to sysfs files may, or may not, be \n terminated.
bce74dac
N
2295 * We want to accept with case. For this we use cmd_match.
2296 */
2297static int cmd_match(const char *cmd, const char *str)
2298{
2299 /* See if cmd, written into a sysfs file, matches
2300 * str. They must either be the same, or cmd can
2301 * have a trailing newline
2302 */
2303 while (*cmd && *str && *cmd == *str) {
2304 cmd++;
2305 str++;
2306 }
2307 if (*cmd == '\n')
2308 cmd++;
2309 if (*str || *cmd)
2310 return 0;
2311 return 1;
2312}
2313
86e6ffdd
N
2314struct rdev_sysfs_entry {
2315 struct attribute attr;
2316 ssize_t (*show)(mdk_rdev_t *, char *);
2317 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2318};
2319
2320static ssize_t
96de1e66 2321state_show(mdk_rdev_t *rdev, char *page)
86e6ffdd
N
2322{
2323 char *sep = "";
20a49ff6 2324 size_t len = 0;
86e6ffdd 2325
b2d444d7 2326 if (test_bit(Faulty, &rdev->flags)) {
86e6ffdd
N
2327 len+= sprintf(page+len, "%sfaulty",sep);
2328 sep = ",";
2329 }
b2d444d7 2330 if (test_bit(In_sync, &rdev->flags)) {
86e6ffdd
N
2331 len += sprintf(page+len, "%sin_sync",sep);
2332 sep = ",";
2333 }
f655675b
N
2334 if (test_bit(WriteMostly, &rdev->flags)) {
2335 len += sprintf(page+len, "%swrite_mostly",sep);
2336 sep = ",";
2337 }
6bfe0b49
DW
2338 if (test_bit(Blocked, &rdev->flags)) {
2339 len += sprintf(page+len, "%sblocked", sep);
2340 sep = ",";
2341 }
b2d444d7
N
2342 if (!test_bit(Faulty, &rdev->flags) &&
2343 !test_bit(In_sync, &rdev->flags)) {
86e6ffdd
N
2344 len += sprintf(page+len, "%sspare", sep);
2345 sep = ",";
2346 }
2347 return len+sprintf(page+len, "\n");
2348}
2349
45dc2de1
N
2350static ssize_t
2351state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2352{
2353 /* can write
2354 * faulty - simulates and error
2355 * remove - disconnects the device
f655675b
N
2356 * writemostly - sets write_mostly
2357 * -writemostly - clears write_mostly
6bfe0b49
DW
2358 * blocked - sets the Blocked flag
2359 * -blocked - clears the Blocked flag
6d56e278 2360 * insync - sets Insync providing device isn't active
45dc2de1
N
2361 */
2362 int err = -EINVAL;
2363 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2364 md_error(rdev->mddev, rdev);
2365 err = 0;
2366 } else if (cmd_match(buf, "remove")) {
2367 if (rdev->raid_disk >= 0)
2368 err = -EBUSY;
2369 else {
2370 mddev_t *mddev = rdev->mddev;
2371 kick_rdev_from_array(rdev);
3f9d7b0d
N
2372 if (mddev->pers)
2373 md_update_sb(mddev, 1);
45dc2de1
N
2374 md_new_event(mddev);
2375 err = 0;
2376 }
f655675b
N
2377 } else if (cmd_match(buf, "writemostly")) {
2378 set_bit(WriteMostly, &rdev->flags);
2379 err = 0;
2380 } else if (cmd_match(buf, "-writemostly")) {
2381 clear_bit(WriteMostly, &rdev->flags);
6bfe0b49
DW
2382 err = 0;
2383 } else if (cmd_match(buf, "blocked")) {
2384 set_bit(Blocked, &rdev->flags);
2385 err = 0;
2386 } else if (cmd_match(buf, "-blocked")) {
2387 clear_bit(Blocked, &rdev->flags);
2388 wake_up(&rdev->blocked_wait);
2389 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2390 md_wakeup_thread(rdev->mddev->thread);
2391
6d56e278
N
2392 err = 0;
2393 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2394 set_bit(In_sync, &rdev->flags);
f655675b 2395 err = 0;
45dc2de1 2396 }
00bcb4ac
N
2397 if (!err)
2398 sysfs_notify_dirent_safe(rdev->sysfs_state);
45dc2de1
N
2399 return err ? err : len;
2400}
80ca3a44
N
2401static struct rdev_sysfs_entry rdev_state =
2402__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
86e6ffdd 2403
4dbcdc75
N
2404static ssize_t
2405errors_show(mdk_rdev_t *rdev, char *page)
2406{
2407 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2408}
2409
2410static ssize_t
2411errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2412{
2413 char *e;
2414 unsigned long n = simple_strtoul(buf, &e, 10);
2415 if (*buf && (*e == 0 || *e == '\n')) {
2416 atomic_set(&rdev->corrected_errors, n);
2417 return len;
2418 }
2419 return -EINVAL;
2420}
2421static struct rdev_sysfs_entry rdev_errors =
80ca3a44 2422__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
4dbcdc75 2423
014236d2
N
2424static ssize_t
2425slot_show(mdk_rdev_t *rdev, char *page)
2426{
2427 if (rdev->raid_disk < 0)
2428 return sprintf(page, "none\n");
2429 else
2430 return sprintf(page, "%d\n", rdev->raid_disk);
2431}
2432
2433static ssize_t
2434slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2435{
2436 char *e;
c303da6d
N
2437 int err;
2438 char nm[20];
014236d2
N
2439 int slot = simple_strtoul(buf, &e, 10);
2440 if (strncmp(buf, "none", 4)==0)
2441 slot = -1;
2442 else if (e==buf || (*e && *e!= '\n'))
2443 return -EINVAL;
6c2fce2e 2444 if (rdev->mddev->pers && slot == -1) {
c303da6d
N
2445 /* Setting 'slot' on an active array requires also
2446 * updating the 'rd%d' link, and communicating
2447 * with the personality with ->hot_*_disk.
2448 * For now we only support removing
2449 * failed/spare devices. This normally happens automatically,
2450 * but not when the metadata is externally managed.
2451 */
c303da6d
N
2452 if (rdev->raid_disk == -1)
2453 return -EEXIST;
2454 /* personality does all needed checks */
2455 if (rdev->mddev->pers->hot_add_disk == NULL)
2456 return -EINVAL;
2457 err = rdev->mddev->pers->
2458 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2459 if (err)
2460 return err;
2461 sprintf(nm, "rd%d", rdev->raid_disk);
2462 sysfs_remove_link(&rdev->mddev->kobj, nm);
b7103107 2463 rdev->raid_disk = -1;
c303da6d
N
2464 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2465 md_wakeup_thread(rdev->mddev->thread);
6c2fce2e
NB
2466 } else if (rdev->mddev->pers) {
2467 mdk_rdev_t *rdev2;
6c2fce2e 2468 /* Activating a spare .. or possibly reactivating
6d56e278 2469 * if we ever get bitmaps working here.
6c2fce2e
NB
2470 */
2471
2472 if (rdev->raid_disk != -1)
2473 return -EBUSY;
2474
2475 if (rdev->mddev->pers->hot_add_disk == NULL)
2476 return -EINVAL;
2477
159ec1fc 2478 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
6c2fce2e
NB
2479 if (rdev2->raid_disk == slot)
2480 return -EEXIST;
2481
2482 rdev->raid_disk = slot;
2483 if (test_bit(In_sync, &rdev->flags))
2484 rdev->saved_raid_disk = slot;
2485 else
2486 rdev->saved_raid_disk = -1;
2487 err = rdev->mddev->pers->
2488 hot_add_disk(rdev->mddev, rdev);
199050ea 2489 if (err) {
6c2fce2e 2490 rdev->raid_disk = -1;
6c2fce2e 2491 return err;
52664732 2492 } else
00bcb4ac 2493 sysfs_notify_dirent_safe(rdev->sysfs_state);
6c2fce2e
NB
2494 sprintf(nm, "rd%d", rdev->raid_disk);
2495 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
00bcb4ac 2496 /* failure here is OK */;
6c2fce2e 2497 /* don't wakeup anyone, leave that to userspace. */
c303da6d
N
2498 } else {
2499 if (slot >= rdev->mddev->raid_disks)
2500 return -ENOSPC;
2501 rdev->raid_disk = slot;
2502 /* assume it is working */
c5d79adb
N
2503 clear_bit(Faulty, &rdev->flags);
2504 clear_bit(WriteMostly, &rdev->flags);
c303da6d 2505 set_bit(In_sync, &rdev->flags);
00bcb4ac 2506 sysfs_notify_dirent_safe(rdev->sysfs_state);
c303da6d 2507 }
014236d2
N
2508 return len;
2509}
2510
2511
2512static struct rdev_sysfs_entry rdev_slot =
80ca3a44 2513__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
014236d2 2514
93c8cad0
N
2515static ssize_t
2516offset_show(mdk_rdev_t *rdev, char *page)
2517{
6961ece4 2518 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
93c8cad0
N
2519}
2520
2521static ssize_t
2522offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2523{
2524 char *e;
2525 unsigned long long offset = simple_strtoull(buf, &e, 10);
2526 if (e==buf || (*e && *e != '\n'))
2527 return -EINVAL;
8ed0a521 2528 if (rdev->mddev->pers && rdev->raid_disk >= 0)
93c8cad0 2529 return -EBUSY;
dd8ac336 2530 if (rdev->sectors && rdev->mddev->external)
c5d79adb
N
2531 /* Must set offset before size, so overlap checks
2532 * can be sane */
2533 return -EBUSY;
93c8cad0
N
2534 rdev->data_offset = offset;
2535 return len;
2536}
2537
2538static struct rdev_sysfs_entry rdev_offset =
80ca3a44 2539__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
93c8cad0 2540
83303b61
N
2541static ssize_t
2542rdev_size_show(mdk_rdev_t *rdev, char *page)
2543{
dd8ac336 2544 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
83303b61
N
2545}
2546
c5d79adb
N
2547static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2548{
2549 /* check if two start/length pairs overlap */
2550 if (s1+l1 <= s2)
2551 return 0;
2552 if (s2+l2 <= s1)
2553 return 0;
2554 return 1;
2555}
2556
b522adcd
DW
2557static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2558{
2559 unsigned long long blocks;
2560 sector_t new;
2561
2562 if (strict_strtoull(buf, 10, &blocks) < 0)
2563 return -EINVAL;
2564
2565 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2566 return -EINVAL; /* sector conversion overflow */
2567
2568 new = blocks * 2;
2569 if (new != blocks * 2)
2570 return -EINVAL; /* unsigned long long to sector_t overflow */
2571
2572 *sectors = new;
2573 return 0;
2574}
2575
83303b61
N
2576static ssize_t
2577rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2578{
27c529bb 2579 mddev_t *my_mddev = rdev->mddev;
dd8ac336 2580 sector_t oldsectors = rdev->sectors;
b522adcd 2581 sector_t sectors;
27c529bb 2582
b522adcd 2583 if (strict_blocks_to_sectors(buf, &sectors) < 0)
d7027458 2584 return -EINVAL;
0cd17fec 2585 if (my_mddev->pers && rdev->raid_disk >= 0) {
d7027458 2586 if (my_mddev->persistent) {
dd8ac336
AN
2587 sectors = super_types[my_mddev->major_version].
2588 rdev_size_change(rdev, sectors);
2589 if (!sectors)
0cd17fec 2590 return -EBUSY;
dd8ac336 2591 } else if (!sectors)
77304d2a 2592 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
dd8ac336 2593 rdev->data_offset;
0cd17fec 2594 }
dd8ac336 2595 if (sectors < my_mddev->dev_sectors)
7d3c6f87 2596 return -EINVAL; /* component must fit device */
0cd17fec 2597
dd8ac336
AN
2598 rdev->sectors = sectors;
2599 if (sectors > oldsectors && my_mddev->external) {
c5d79adb
N
2600 /* need to check that all other rdevs with the same ->bdev
2601 * do not overlap. We need to unlock the mddev to avoid
dd8ac336 2602 * a deadlock. We have already changed rdev->sectors, and if
c5d79adb
N
2603 * we have to change it back, we will have the lock again.
2604 */
2605 mddev_t *mddev;
2606 int overlap = 0;
159ec1fc 2607 struct list_head *tmp;
c5d79adb 2608
27c529bb 2609 mddev_unlock(my_mddev);
29ac4aa3 2610 for_each_mddev(mddev, tmp) {
c5d79adb
N
2611 mdk_rdev_t *rdev2;
2612
2613 mddev_lock(mddev);
159ec1fc 2614 list_for_each_entry(rdev2, &mddev->disks, same_set)
c5d79adb
N
2615 if (test_bit(AllReserved, &rdev2->flags) ||
2616 (rdev->bdev == rdev2->bdev &&
2617 rdev != rdev2 &&
dd8ac336 2618 overlaps(rdev->data_offset, rdev->sectors,
d07bd3bc 2619 rdev2->data_offset,
dd8ac336 2620 rdev2->sectors))) {
c5d79adb
N
2621 overlap = 1;
2622 break;
2623 }
2624 mddev_unlock(mddev);
2625 if (overlap) {
2626 mddev_put(mddev);
2627 break;
2628 }
2629 }
27c529bb 2630 mddev_lock(my_mddev);
c5d79adb
N
2631 if (overlap) {
2632 /* Someone else could have slipped in a size
2633 * change here, but doing so is just silly.
dd8ac336 2634 * We put oldsectors back because we *know* it is
c5d79adb
N
2635 * safe, and trust userspace not to race with
2636 * itself
2637 */
dd8ac336 2638 rdev->sectors = oldsectors;
c5d79adb
N
2639 return -EBUSY;
2640 }
2641 }
83303b61
N
2642 return len;
2643}
2644
2645static struct rdev_sysfs_entry rdev_size =
80ca3a44 2646__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
83303b61 2647
06e3c817
DW
2648
2649static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2650{
2651 unsigned long long recovery_start = rdev->recovery_offset;
2652
2653 if (test_bit(In_sync, &rdev->flags) ||
2654 recovery_start == MaxSector)
2655 return sprintf(page, "none\n");
2656
2657 return sprintf(page, "%llu\n", recovery_start);
2658}
2659
2660static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2661{
2662 unsigned long long recovery_start;
2663
2664 if (cmd_match(buf, "none"))
2665 recovery_start = MaxSector;
2666 else if (strict_strtoull(buf, 10, &recovery_start))
2667 return -EINVAL;
2668
2669 if (rdev->mddev->pers &&
2670 rdev->raid_disk >= 0)
2671 return -EBUSY;
2672
2673 rdev->recovery_offset = recovery_start;
2674 if (recovery_start == MaxSector)
2675 set_bit(In_sync, &rdev->flags);
2676 else
2677 clear_bit(In_sync, &rdev->flags);
2678 return len;
2679}
2680
2681static struct rdev_sysfs_entry rdev_recovery_start =
2682__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2683
86e6ffdd
N
2684static struct attribute *rdev_default_attrs[] = {
2685 &rdev_state.attr,
4dbcdc75 2686 &rdev_errors.attr,
014236d2 2687 &rdev_slot.attr,
93c8cad0 2688 &rdev_offset.attr,
83303b61 2689 &rdev_size.attr,
06e3c817 2690 &rdev_recovery_start.attr,
86e6ffdd
N
2691 NULL,
2692};
2693static ssize_t
2694rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2695{
2696 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2697 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
27c529bb
N
2698 mddev_t *mddev = rdev->mddev;
2699 ssize_t rv;
86e6ffdd
N
2700
2701 if (!entry->show)
2702 return -EIO;
27c529bb
N
2703
2704 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2705 if (!rv) {
2706 if (rdev->mddev == NULL)
2707 rv = -EBUSY;
2708 else
2709 rv = entry->show(rdev, page);
2710 mddev_unlock(mddev);
2711 }
2712 return rv;
86e6ffdd
N
2713}
2714
2715static ssize_t
2716rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2717 const char *page, size_t length)
2718{
2719 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2720 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
27c529bb
N
2721 ssize_t rv;
2722 mddev_t *mddev = rdev->mddev;
86e6ffdd
N
2723
2724 if (!entry->store)
2725 return -EIO;
67463acb
N
2726 if (!capable(CAP_SYS_ADMIN))
2727 return -EACCES;
27c529bb 2728 rv = mddev ? mddev_lock(mddev): -EBUSY;
ca388059 2729 if (!rv) {
27c529bb
N
2730 if (rdev->mddev == NULL)
2731 rv = -EBUSY;
2732 else
2733 rv = entry->store(rdev, page, length);
6a51830e 2734 mddev_unlock(mddev);
ca388059
N
2735 }
2736 return rv;
86e6ffdd
N
2737}
2738
2739static void rdev_free(struct kobject *ko)
2740{
2741 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2742 kfree(rdev);
2743}
52cf25d0 2744static const struct sysfs_ops rdev_sysfs_ops = {
86e6ffdd
N
2745 .show = rdev_attr_show,
2746 .store = rdev_attr_store,
2747};
2748static struct kobj_type rdev_ktype = {
2749 .release = rdev_free,
2750 .sysfs_ops = &rdev_sysfs_ops,
2751 .default_attrs = rdev_default_attrs,
2752};
2753
e8bb9a83
N
2754void md_rdev_init(mdk_rdev_t *rdev)
2755{
2756 rdev->desc_nr = -1;
2757 rdev->saved_raid_disk = -1;
2758 rdev->raid_disk = -1;
2759 rdev->flags = 0;
2760 rdev->data_offset = 0;
2761 rdev->sb_events = 0;
2762 rdev->last_read_error.tv_sec = 0;
2763 rdev->last_read_error.tv_nsec = 0;
2764 atomic_set(&rdev->nr_pending, 0);
2765 atomic_set(&rdev->read_errors, 0);
2766 atomic_set(&rdev->corrected_errors, 0);
2767
2768 INIT_LIST_HEAD(&rdev->same_set);
2769 init_waitqueue_head(&rdev->blocked_wait);
2770}
2771EXPORT_SYMBOL_GPL(md_rdev_init);
1da177e4
LT
2772/*
2773 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2774 *
2775 * mark the device faulty if:
2776 *
2777 * - the device is nonexistent (zero size)
2778 * - the device has no valid superblock
2779 *
2780 * a faulty rdev _never_ has rdev->sb set.
2781 */
2782static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2783{
2784 char b[BDEVNAME_SIZE];
2785 int err;
2786 mdk_rdev_t *rdev;
2787 sector_t size;
2788
9ffae0cf 2789 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1da177e4
LT
2790 if (!rdev) {
2791 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2792 return ERR_PTR(-ENOMEM);
2793 }
1da177e4 2794
e8bb9a83 2795 md_rdev_init(rdev);
1da177e4
LT
2796 if ((err = alloc_disk_sb(rdev)))
2797 goto abort_free;
2798
c5d79adb 2799 err = lock_rdev(rdev, newdev, super_format == -2);
1da177e4
LT
2800 if (err)
2801 goto abort_free;
2802
f9cb074b 2803 kobject_init(&rdev->kobj, &rdev_ktype);
86e6ffdd 2804
77304d2a 2805 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
1da177e4
LT
2806 if (!size) {
2807 printk(KERN_WARNING
2808 "md: %s has zero or unknown size, marking faulty!\n",
2809 bdevname(rdev->bdev,b));
2810 err = -EINVAL;
2811 goto abort_free;
2812 }
2813
2814 if (super_format >= 0) {
2815 err = super_types[super_format].
2816 load_super(rdev, NULL, super_minor);
2817 if (err == -EINVAL) {
df968c4e
N
2818 printk(KERN_WARNING
2819 "md: %s does not have a valid v%d.%d "
2820 "superblock, not importing!\n",
2821 bdevname(rdev->bdev,b),
2822 super_format, super_minor);
1da177e4
LT
2823 goto abort_free;
2824 }
2825 if (err < 0) {
2826 printk(KERN_WARNING
2827 "md: could not read %s's sb, not importing!\n",
2828 bdevname(rdev->bdev,b));
2829 goto abort_free;
2830 }
2831 }
6bfe0b49 2832
1da177e4
LT
2833 return rdev;
2834
2835abort_free:
2836 if (rdev->sb_page) {
2837 if (rdev->bdev)
2838 unlock_rdev(rdev);
2839 free_disk_sb(rdev);
2840 }
2841 kfree(rdev);
2842 return ERR_PTR(err);
2843}
2844
2845/*
2846 * Check a full RAID array for plausibility
2847 */
2848
2849
a757e64c 2850static void analyze_sbs(mddev_t * mddev)
1da177e4
LT
2851{
2852 int i;
159ec1fc 2853 mdk_rdev_t *rdev, *freshest, *tmp;
1da177e4
LT
2854 char b[BDEVNAME_SIZE];
2855
2856 freshest = NULL;
d089c6af 2857 rdev_for_each(rdev, tmp, mddev)
1da177e4
LT
2858 switch (super_types[mddev->major_version].
2859 load_super(rdev, freshest, mddev->minor_version)) {
2860 case 1:
2861 freshest = rdev;
2862 break;
2863 case 0:
2864 break;
2865 default:
2866 printk( KERN_ERR \
2867 "md: fatal superblock inconsistency in %s"
2868 " -- removing from array\n",
2869 bdevname(rdev->bdev,b));
2870 kick_rdev_from_array(rdev);
2871 }
2872
2873
2874 super_types[mddev->major_version].
2875 validate_super(mddev, freshest);
2876
2877 i = 0;
d089c6af 2878 rdev_for_each(rdev, tmp, mddev) {
233fca36
N
2879 if (mddev->max_disks &&
2880 (rdev->desc_nr >= mddev->max_disks ||
2881 i > mddev->max_disks)) {
de01dfad
N
2882 printk(KERN_WARNING
2883 "md: %s: %s: only %d devices permitted\n",
2884 mdname(mddev), bdevname(rdev->bdev, b),
2885 mddev->max_disks);
2886 kick_rdev_from_array(rdev);
2887 continue;
2888 }
1da177e4
LT
2889 if (rdev != freshest)
2890 if (super_types[mddev->major_version].
2891 validate_super(mddev, rdev)) {
2892 printk(KERN_WARNING "md: kicking non-fresh %s"
2893 " from array!\n",
2894 bdevname(rdev->bdev,b));
2895 kick_rdev_from_array(rdev);
2896 continue;
2897 }
2898 if (mddev->level == LEVEL_MULTIPATH) {
2899 rdev->desc_nr = i++;
2900 rdev->raid_disk = rdev->desc_nr;
b2d444d7 2901 set_bit(In_sync, &rdev->flags);
5e5e3e78 2902 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
a778b73f
N
2903 rdev->raid_disk = -1;
2904 clear_bit(In_sync, &rdev->flags);
1da177e4
LT
2905 }
2906 }
1da177e4
LT
2907}
2908
72e02075
N
2909/* Read a fixed-point number.
2910 * Numbers in sysfs attributes should be in "standard" units where
2911 * possible, so time should be in seconds.
2912 * However we internally use a a much smaller unit such as
2913 * milliseconds or jiffies.
2914 * This function takes a decimal number with a possible fractional
2915 * component, and produces an integer which is the result of
2916 * multiplying that number by 10^'scale'.
2917 * all without any floating-point arithmetic.
2918 */
2919int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2920{
2921 unsigned long result = 0;
2922 long decimals = -1;
2923 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2924 if (*cp == '.')
2925 decimals = 0;
2926 else if (decimals < scale) {
2927 unsigned int value;
2928 value = *cp - '0';
2929 result = result * 10 + value;
2930 if (decimals >= 0)
2931 decimals++;
2932 }
2933 cp++;
2934 }
2935 if (*cp == '\n')
2936 cp++;
2937 if (*cp)
2938 return -EINVAL;
2939 if (decimals < 0)
2940 decimals = 0;
2941 while (decimals < scale) {
2942 result *= 10;
2943 decimals ++;
2944 }
2945 *res = result;
2946 return 0;
2947}
2948
2949
19052c0e
N
2950static void md_safemode_timeout(unsigned long data);
2951
16f17b39
N
2952static ssize_t
2953safe_delay_show(mddev_t *mddev, char *page)
2954{
2955 int msec = (mddev->safemode_delay*1000)/HZ;
2956 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2957}
2958static ssize_t
2959safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2960{
16f17b39 2961 unsigned long msec;
97ce0a7f 2962
72e02075 2963 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
16f17b39 2964 return -EINVAL;
16f17b39
N
2965 if (msec == 0)
2966 mddev->safemode_delay = 0;
2967 else {
19052c0e 2968 unsigned long old_delay = mddev->safemode_delay;
16f17b39
N
2969 mddev->safemode_delay = (msec*HZ)/1000;
2970 if (mddev->safemode_delay == 0)
2971 mddev->safemode_delay = 1;
19052c0e
N
2972 if (mddev->safemode_delay < old_delay)
2973 md_safemode_timeout((unsigned long)mddev);
16f17b39
N
2974 }
2975 return len;
2976}
2977static struct md_sysfs_entry md_safe_delay =
80ca3a44 2978__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
16f17b39 2979
eae1701f 2980static ssize_t
96de1e66 2981level_show(mddev_t *mddev, char *page)
eae1701f 2982{
2604b703 2983 struct mdk_personality *p = mddev->pers;
d9d166c2 2984 if (p)
eae1701f 2985 return sprintf(page, "%s\n", p->name);
d9d166c2
N
2986 else if (mddev->clevel[0])
2987 return sprintf(page, "%s\n", mddev->clevel);
2988 else if (mddev->level != LEVEL_NONE)
2989 return sprintf(page, "%d\n", mddev->level);
2990 else
2991 return 0;
eae1701f
N
2992}
2993
d9d166c2
N
2994static ssize_t
2995level_store(mddev_t *mddev, const char *buf, size_t len)
2996{
f2859af6 2997 char clevel[16];
20a49ff6 2998 ssize_t rv = len;
245f46c2 2999 struct mdk_personality *pers;
f2859af6 3000 long level;
245f46c2 3001 void *priv;
3a981b03 3002 mdk_rdev_t *rdev;
245f46c2
N
3003
3004 if (mddev->pers == NULL) {
3005 if (len == 0)
3006 return 0;
3007 if (len >= sizeof(mddev->clevel))
3008 return -ENOSPC;
3009 strncpy(mddev->clevel, buf, len);
3010 if (mddev->clevel[len-1] == '\n')
3011 len--;
3012 mddev->clevel[len] = 0;
3013 mddev->level = LEVEL_NONE;
3014 return rv;
3015 }
3016
3017 /* request to change the personality. Need to ensure:
3018 * - array is not engaged in resync/recovery/reshape
3019 * - old personality can be suspended
3020 * - new personality will access other array.
3021 */
3022
bb4f1e9d
N
3023 if (mddev->sync_thread ||
3024 mddev->reshape_position != MaxSector ||
3025 mddev->sysfs_active)
d9d166c2 3026 return -EBUSY;
245f46c2
N
3027
3028 if (!mddev->pers->quiesce) {
3029 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3030 mdname(mddev), mddev->pers->name);
3031 return -EINVAL;
3032 }
3033
3034 /* Now find the new personality */
f2859af6 3035 if (len == 0 || len >= sizeof(clevel))
245f46c2 3036 return -EINVAL;
f2859af6
DW
3037 strncpy(clevel, buf, len);
3038 if (clevel[len-1] == '\n')
d9d166c2 3039 len--;
f2859af6
DW
3040 clevel[len] = 0;
3041 if (strict_strtol(clevel, 10, &level))
3042 level = LEVEL_NONE;
245f46c2 3043
f2859af6
DW
3044 if (request_module("md-%s", clevel) != 0)
3045 request_module("md-level-%s", clevel);
245f46c2 3046 spin_lock(&pers_lock);
f2859af6 3047 pers = find_pers(level, clevel);
245f46c2
N
3048 if (!pers || !try_module_get(pers->owner)) {
3049 spin_unlock(&pers_lock);
f2859af6 3050 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
245f46c2
N
3051 return -EINVAL;
3052 }
3053 spin_unlock(&pers_lock);
3054
3055 if (pers == mddev->pers) {
3056 /* Nothing to do! */
3057 module_put(pers->owner);
3058 return rv;
3059 }
3060 if (!pers->takeover) {
3061 module_put(pers->owner);
3062 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
f2859af6 3063 mdname(mddev), clevel);
245f46c2
N
3064 return -EINVAL;
3065 }
3066
e93f68a1
N
3067 list_for_each_entry(rdev, &mddev->disks, same_set)
3068 rdev->new_raid_disk = rdev->raid_disk;
3069
245f46c2
N
3070 /* ->takeover must set new_* and/or delta_disks
3071 * if it succeeds, and may set them when it fails.
3072 */
3073 priv = pers->takeover(mddev);
3074 if (IS_ERR(priv)) {
3075 mddev->new_level = mddev->level;
3076 mddev->new_layout = mddev->layout;
664e7c41 3077 mddev->new_chunk_sectors = mddev->chunk_sectors;
245f46c2
N
3078 mddev->raid_disks -= mddev->delta_disks;
3079 mddev->delta_disks = 0;
3080 module_put(pers->owner);
3081 printk(KERN_WARNING "md: %s: %s would not accept array\n",
f2859af6 3082 mdname(mddev), clevel);
245f46c2
N
3083 return PTR_ERR(priv);
3084 }
3085
3086 /* Looks like we have a winner */
3087 mddev_suspend(mddev);
3088 mddev->pers->stop(mddev);
a64c876f
N
3089
3090 if (mddev->pers->sync_request == NULL &&
3091 pers->sync_request != NULL) {
3092 /* need to add the md_redundancy_group */
3093 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3094 printk(KERN_WARNING
3095 "md: cannot register extra attributes for %s\n",
3096 mdname(mddev));
19fdb9ee 3097 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
a64c876f
N
3098 }
3099 if (mddev->pers->sync_request != NULL &&
3100 pers->sync_request == NULL) {
3101 /* need to remove the md_redundancy_group */
3102 if (mddev->to_remove == NULL)
3103 mddev->to_remove = &md_redundancy_group;
3104 }
3105
54071b38
TM
3106 if (mddev->pers->sync_request == NULL &&
3107 mddev->external) {
3108 /* We are converting from a no-redundancy array
3109 * to a redundancy array and metadata is managed
3110 * externally so we need to be sure that writes
3111 * won't block due to a need to transition
3112 * clean->dirty
3113 * until external management is started.
3114 */
3115 mddev->in_sync = 0;
3116 mddev->safemode_delay = 0;
3117 mddev->safemode = 0;
3118 }
3119
e93f68a1
N
3120 list_for_each_entry(rdev, &mddev->disks, same_set) {
3121 char nm[20];
3122 if (rdev->raid_disk < 0)
3123 continue;
3124 if (rdev->new_raid_disk > mddev->raid_disks)
3125 rdev->new_raid_disk = -1;
3126 if (rdev->new_raid_disk == rdev->raid_disk)
3127 continue;
3128 sprintf(nm, "rd%d", rdev->raid_disk);
3129 sysfs_remove_link(&mddev->kobj, nm);
3130 }
3131 list_for_each_entry(rdev, &mddev->disks, same_set) {
3132 if (rdev->raid_disk < 0)
3133 continue;
3134 if (rdev->new_raid_disk == rdev->raid_disk)
3135 continue;
3136 rdev->raid_disk = rdev->new_raid_disk;
3137 if (rdev->raid_disk < 0)
3a981b03 3138 clear_bit(In_sync, &rdev->flags);
e93f68a1
N
3139 else {
3140 char nm[20];
3141 sprintf(nm, "rd%d", rdev->raid_disk);
3142 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3143 printk("md: cannot register %s for %s after level change\n",
3144 nm, mdname(mddev));
3a981b03 3145 }
e93f68a1
N
3146 }
3147
3148 module_put(mddev->pers->owner);
245f46c2
N
3149 mddev->pers = pers;
3150 mddev->private = priv;
3151 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3152 mddev->level = mddev->new_level;
3153 mddev->layout = mddev->new_layout;
664e7c41 3154 mddev->chunk_sectors = mddev->new_chunk_sectors;
245f46c2 3155 mddev->delta_disks = 0;
9af204cf
TM
3156 if (mddev->pers->sync_request == NULL) {
3157 /* this is now an array without redundancy, so
3158 * it must always be in_sync
3159 */
3160 mddev->in_sync = 1;
3161 del_timer_sync(&mddev->safemode_timer);
3162 }
245f46c2
N
3163 pers->run(mddev);
3164 mddev_resume(mddev);
3165 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3166 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3167 md_wakeup_thread(mddev->thread);
5cac7861 3168 sysfs_notify(&mddev->kobj, NULL, "level");
bb7f8d22 3169 md_new_event(mddev);
d9d166c2
N
3170 return rv;
3171}
3172
3173static struct md_sysfs_entry md_level =
80ca3a44 3174__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
eae1701f 3175
d4dbd025
N
3176
3177static ssize_t
3178layout_show(mddev_t *mddev, char *page)
3179{
3180 /* just a number, not meaningful for all levels */
08a02ecd
N
3181 if (mddev->reshape_position != MaxSector &&
3182 mddev->layout != mddev->new_layout)
3183 return sprintf(page, "%d (%d)\n",
3184 mddev->new_layout, mddev->layout);
d4dbd025
N
3185 return sprintf(page, "%d\n", mddev->layout);
3186}
3187
3188static ssize_t
3189layout_store(mddev_t *mddev, const char *buf, size_t len)
3190{
3191 char *e;
3192 unsigned long n = simple_strtoul(buf, &e, 10);
d4dbd025
N
3193
3194 if (!*buf || (*e && *e != '\n'))
3195 return -EINVAL;
3196
b3546035
N
3197 if (mddev->pers) {
3198 int err;
50ac168a 3199 if (mddev->pers->check_reshape == NULL)
b3546035 3200 return -EBUSY;
597a711b 3201 mddev->new_layout = n;
50ac168a 3202 err = mddev->pers->check_reshape(mddev);
597a711b
N
3203 if (err) {
3204 mddev->new_layout = mddev->layout;
b3546035 3205 return err;
597a711b 3206 }
b3546035 3207 } else {
08a02ecd 3208 mddev->new_layout = n;
b3546035
N
3209 if (mddev->reshape_position == MaxSector)
3210 mddev->layout = n;
3211 }
d4dbd025
N
3212 return len;
3213}
3214static struct md_sysfs_entry md_layout =
80ca3a44 3215__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
d4dbd025
N
3216
3217
eae1701f 3218static ssize_t
96de1e66 3219raid_disks_show(mddev_t *mddev, char *page)
eae1701f 3220{
bb636547
N
3221 if (mddev->raid_disks == 0)
3222 return 0;
08a02ecd
N
3223 if (mddev->reshape_position != MaxSector &&
3224 mddev->delta_disks != 0)
3225 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3226 mddev->raid_disks - mddev->delta_disks);
eae1701f
N
3227 return sprintf(page, "%d\n", mddev->raid_disks);
3228}
3229
da943b99
N
3230static int update_raid_disks(mddev_t *mddev, int raid_disks);
3231
3232static ssize_t
3233raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3234{
da943b99
N
3235 char *e;
3236 int rv = 0;
3237 unsigned long n = simple_strtoul(buf, &e, 10);
3238
3239 if (!*buf || (*e && *e != '\n'))
3240 return -EINVAL;
3241
3242 if (mddev->pers)
3243 rv = update_raid_disks(mddev, n);
08a02ecd
N
3244 else if (mddev->reshape_position != MaxSector) {
3245 int olddisks = mddev->raid_disks - mddev->delta_disks;
3246 mddev->delta_disks = n - olddisks;
3247 mddev->raid_disks = n;
3248 } else
da943b99
N
3249 mddev->raid_disks = n;
3250 return rv ? rv : len;
3251}
3252static struct md_sysfs_entry md_raid_disks =
80ca3a44 3253__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
eae1701f 3254
3b34380a
N
3255static ssize_t
3256chunk_size_show(mddev_t *mddev, char *page)
3257{
08a02ecd 3258 if (mddev->reshape_position != MaxSector &&
664e7c41
AN
3259 mddev->chunk_sectors != mddev->new_chunk_sectors)
3260 return sprintf(page, "%d (%d)\n",
3261 mddev->new_chunk_sectors << 9,
9d8f0363
AN
3262 mddev->chunk_sectors << 9);
3263 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3b34380a
N
3264}
3265
3266static ssize_t
3267chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3268{
3b34380a
N
3269 char *e;
3270 unsigned long n = simple_strtoul(buf, &e, 10);
3271
3b34380a
N
3272 if (!*buf || (*e && *e != '\n'))
3273 return -EINVAL;
3274
b3546035
N
3275 if (mddev->pers) {
3276 int err;
50ac168a 3277 if (mddev->pers->check_reshape == NULL)
b3546035 3278 return -EBUSY;
597a711b 3279 mddev->new_chunk_sectors = n >> 9;
50ac168a 3280 err = mddev->pers->check_reshape(mddev);
597a711b
N
3281 if (err) {
3282 mddev->new_chunk_sectors = mddev->chunk_sectors;
b3546035 3283 return err;
597a711b 3284 }
b3546035 3285 } else {
664e7c41 3286 mddev->new_chunk_sectors = n >> 9;
b3546035 3287 if (mddev->reshape_position == MaxSector)
9d8f0363 3288 mddev->chunk_sectors = n >> 9;
b3546035 3289 }
3b34380a
N
3290 return len;
3291}
3292static struct md_sysfs_entry md_chunk_size =
80ca3a44 3293__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3b34380a 3294
a94213b1
N
3295static ssize_t
3296resync_start_show(mddev_t *mddev, char *page)
3297{
d1a7c503
N
3298 if (mddev->recovery_cp == MaxSector)
3299 return sprintf(page, "none\n");
a94213b1
N
3300 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3301}
3302
3303static ssize_t
3304resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3305{
a94213b1
N
3306 char *e;
3307 unsigned long long n = simple_strtoull(buf, &e, 10);
3308
3309 if (mddev->pers)
3310 return -EBUSY;
06e3c817
DW
3311 if (cmd_match(buf, "none"))
3312 n = MaxSector;
3313 else if (!*buf || (*e && *e != '\n'))
a94213b1
N
3314 return -EINVAL;
3315
3316 mddev->recovery_cp = n;
3317 return len;
3318}
3319static struct md_sysfs_entry md_resync_start =
80ca3a44 3320__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
a94213b1 3321
9e653b63
N
3322/*
3323 * The array state can be:
3324 *
3325 * clear
3326 * No devices, no size, no level
3327 * Equivalent to STOP_ARRAY ioctl
3328 * inactive
3329 * May have some settings, but array is not active
3330 * all IO results in error
3331 * When written, doesn't tear down array, but just stops it
3332 * suspended (not supported yet)
3333 * All IO requests will block. The array can be reconfigured.
910d8cb3 3334 * Writing this, if accepted, will block until array is quiescent
9e653b63
N
3335 * readonly
3336 * no resync can happen. no superblocks get written.
3337 * write requests fail
3338 * read-auto
3339 * like readonly, but behaves like 'clean' on a write request.
3340 *
3341 * clean - no pending writes, but otherwise active.
3342 * When written to inactive array, starts without resync
3343 * If a write request arrives then
3344 * if metadata is known, mark 'dirty' and switch to 'active'.
3345 * if not known, block and switch to write-pending
3346 * If written to an active array that has pending writes, then fails.
3347 * active
3348 * fully active: IO and resync can be happening.
3349 * When written to inactive array, starts with resync
3350 *
3351 * write-pending
3352 * clean, but writes are blocked waiting for 'active' to be written.
3353 *
3354 * active-idle
3355 * like active, but no writes have been seen for a while (100msec).
3356 *
3357 */
3358enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3359 write_pending, active_idle, bad_word};
05381954 3360static char *array_states[] = {
9e653b63
N
3361 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3362 "write-pending", "active-idle", NULL };
3363
3364static int match_word(const char *word, char **list)
3365{
3366 int n;
3367 for (n=0; list[n]; n++)
3368 if (cmd_match(word, list[n]))
3369 break;
3370 return n;
3371}
3372
3373static ssize_t
3374array_state_show(mddev_t *mddev, char *page)
3375{
3376 enum array_state st = inactive;
3377
3378 if (mddev->pers)
3379 switch(mddev->ro) {
3380 case 1:
3381 st = readonly;
3382 break;
3383 case 2:
3384 st = read_auto;
3385 break;
3386 case 0:
3387 if (mddev->in_sync)
3388 st = clean;
070dc6dd 3389 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
e691063a 3390 st = write_pending;
9e653b63
N
3391 else if (mddev->safemode)
3392 st = active_idle;
3393 else
3394 st = active;
3395 }
3396 else {
3397 if (list_empty(&mddev->disks) &&
3398 mddev->raid_disks == 0 &&
58c0fed4 3399 mddev->dev_sectors == 0)
9e653b63
N
3400 st = clear;
3401 else
3402 st = inactive;
3403 }
3404 return sprintf(page, "%s\n", array_states[st]);
3405}
3406
df5b20cf 3407static int do_md_stop(mddev_t * mddev, int ro, int is_open);
a4bd82d0 3408static int md_set_readonly(mddev_t * mddev, int is_open);
9e653b63
N
3409static int do_md_run(mddev_t * mddev);
3410static int restart_array(mddev_t *mddev);
3411
3412static ssize_t
3413array_state_store(mddev_t *mddev, const char *buf, size_t len)
3414{
3415 int err = -EINVAL;
3416 enum array_state st = match_word(buf, array_states);
3417 switch(st) {
3418 case bad_word:
3419 break;
3420 case clear:
3421 /* stopping an active array */
f2ea68cf 3422 if (atomic_read(&mddev->openers) > 0)
e691063a 3423 return -EBUSY;
df5b20cf 3424 err = do_md_stop(mddev, 0, 0);
9e653b63
N
3425 break;
3426 case inactive:
3427 /* stopping an active array */
3428 if (mddev->pers) {
f2ea68cf 3429 if (atomic_read(&mddev->openers) > 0)
9e653b63 3430 return -EBUSY;
df5b20cf 3431 err = do_md_stop(mddev, 2, 0);
e691063a
N
3432 } else
3433 err = 0; /* already inactive */
9e653b63
N
3434 break;
3435 case suspended:
3436 break; /* not supported yet */
3437 case readonly:
3438 if (mddev->pers)
a4bd82d0 3439 err = md_set_readonly(mddev, 0);
9e653b63
N
3440 else {
3441 mddev->ro = 1;
648b629e 3442 set_disk_ro(mddev->gendisk, 1);
9e653b63
N
3443 err = do_md_run(mddev);
3444 }
3445 break;
3446 case read_auto:
9e653b63 3447 if (mddev->pers) {
80268ee9 3448 if (mddev->ro == 0)
a4bd82d0 3449 err = md_set_readonly(mddev, 0);
80268ee9 3450 else if (mddev->ro == 1)
648b629e
N
3451 err = restart_array(mddev);
3452 if (err == 0) {
3453 mddev->ro = 2;
3454 set_disk_ro(mddev->gendisk, 0);
3455 }
9e653b63
N
3456 } else {
3457 mddev->ro = 2;
3458 err = do_md_run(mddev);
3459 }
3460 break;
3461 case clean:
3462 if (mddev->pers) {
3463 restart_array(mddev);
3464 spin_lock_irq(&mddev->write_lock);
3465 if (atomic_read(&mddev->writes_pending) == 0) {
e691063a
N
3466 if (mddev->in_sync == 0) {
3467 mddev->in_sync = 1;
31a59e34
N
3468 if (mddev->safemode == 1)
3469 mddev->safemode = 0;
070dc6dd 3470 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
e691063a
N
3471 }
3472 err = 0;
3473 } else
3474 err = -EBUSY;
9e653b63 3475 spin_unlock_irq(&mddev->write_lock);
5bf29597
N
3476 } else
3477 err = -EINVAL;
9e653b63
N
3478 break;
3479 case active:
3480 if (mddev->pers) {
3481 restart_array(mddev);
070dc6dd 3482 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
9e653b63
N
3483 wake_up(&mddev->sb_wait);
3484 err = 0;
3485 } else {
3486 mddev->ro = 0;
648b629e 3487 set_disk_ro(mddev->gendisk, 0);
9e653b63
N
3488 err = do_md_run(mddev);
3489 }
3490 break;
3491 case write_pending:
3492 case active_idle:
3493 /* these cannot be set */
3494 break;
3495 }
3496 if (err)
3497 return err;
0fd62b86 3498 else {
00bcb4ac 3499 sysfs_notify_dirent_safe(mddev->sysfs_state);
9e653b63 3500 return len;
0fd62b86 3501 }
9e653b63 3502}
80ca3a44
N
3503static struct md_sysfs_entry md_array_state =
3504__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
9e653b63 3505
1e50915f
RB
3506static ssize_t
3507max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3508 return sprintf(page, "%d\n",
3509 atomic_read(&mddev->max_corr_read_errors));
3510}
3511
3512static ssize_t
3513max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3514{
3515 char *e;
3516 unsigned long n = simple_strtoul(buf, &e, 10);
3517
3518 if (*buf && (*e == 0 || *e == '\n')) {
3519 atomic_set(&mddev->max_corr_read_errors, n);
3520 return len;
3521 }
3522 return -EINVAL;
3523}
3524
3525static struct md_sysfs_entry max_corr_read_errors =
3526__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3527 max_corrected_read_errors_store);
3528
6d7ff738
N
3529static ssize_t
3530null_show(mddev_t *mddev, char *page)
3531{
3532 return -EINVAL;
3533}
3534
3535static ssize_t
3536new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3537{
3538 /* buf must be %d:%d\n? giving major and minor numbers */
3539 /* The new device is added to the array.
3540 * If the array has a persistent superblock, we read the
3541 * superblock to initialise info and check validity.
3542 * Otherwise, only checking done is that in bind_rdev_to_array,
3543 * which mainly checks size.
3544 */
3545 char *e;
3546 int major = simple_strtoul(buf, &e, 10);
3547 int minor;
3548 dev_t dev;
3549 mdk_rdev_t *rdev;
3550 int err;
3551
3552 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3553 return -EINVAL;
3554 minor = simple_strtoul(e+1, &e, 10);
3555 if (*e && *e != '\n')
3556 return -EINVAL;
3557 dev = MKDEV(major, minor);
3558 if (major != MAJOR(dev) ||
3559 minor != MINOR(dev))
3560 return -EOVERFLOW;
3561
3562
3563 if (mddev->persistent) {
3564 rdev = md_import_device(dev, mddev->major_version,
3565 mddev->minor_version);
3566 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3567 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3568 mdk_rdev_t, same_set);
3569 err = super_types[mddev->major_version]
3570 .load_super(rdev, rdev0, mddev->minor_version);
3571 if (err < 0)
3572 goto out;
3573 }
c5d79adb
N
3574 } else if (mddev->external)
3575 rdev = md_import_device(dev, -2, -1);
3576 else
6d7ff738
N
3577 rdev = md_import_device(dev, -1, -1);
3578
3579 if (IS_ERR(rdev))
3580 return PTR_ERR(rdev);
3581 err = bind_rdev_to_array(rdev, mddev);
3582 out:
3583 if (err)
3584 export_rdev(rdev);
3585 return err ? err : len;
3586}
3587
3588static struct md_sysfs_entry md_new_device =
80ca3a44 3589__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
3b34380a 3590
9b1d1dac
PC
3591static ssize_t
3592bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3593{
3594 char *end;
3595 unsigned long chunk, end_chunk;
3596
3597 if (!mddev->bitmap)
3598 goto out;
3599 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3600 while (*buf) {
3601 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3602 if (buf == end) break;
3603 if (*end == '-') { /* range */
3604 buf = end + 1;
3605 end_chunk = simple_strtoul(buf, &end, 0);
3606 if (buf == end) break;
3607 }
3608 if (*end && !isspace(*end)) break;
3609 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
e7d2860b 3610 buf = skip_spaces(end);
9b1d1dac
PC
3611 }
3612 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3613out:
3614 return len;
3615}
3616
3617static struct md_sysfs_entry md_bitmap =
3618__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3619
a35b0d69
N
3620static ssize_t
3621size_show(mddev_t *mddev, char *page)
3622{
58c0fed4
AN
3623 return sprintf(page, "%llu\n",
3624 (unsigned long long)mddev->dev_sectors / 2);
a35b0d69
N
3625}
3626
d71f9f88 3627static int update_size(mddev_t *mddev, sector_t num_sectors);
a35b0d69
N
3628
3629static ssize_t
3630size_store(mddev_t *mddev, const char *buf, size_t len)
3631{
3632 /* If array is inactive, we can reduce the component size, but
3633 * not increase it (except from 0).
3634 * If array is active, we can try an on-line resize
3635 */
b522adcd
DW
3636 sector_t sectors;
3637 int err = strict_blocks_to_sectors(buf, &sectors);
a35b0d69 3638
58c0fed4
AN
3639 if (err < 0)
3640 return err;
a35b0d69 3641 if (mddev->pers) {
58c0fed4 3642 err = update_size(mddev, sectors);
850b2b42 3643 md_update_sb(mddev, 1);
a35b0d69 3644 } else {
58c0fed4
AN
3645 if (mddev->dev_sectors == 0 ||
3646 mddev->dev_sectors > sectors)
3647 mddev->dev_sectors = sectors;
a35b0d69
N
3648 else
3649 err = -ENOSPC;
3650 }
3651 return err ? err : len;
3652}
3653
3654static struct md_sysfs_entry md_size =
80ca3a44 3655__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
a35b0d69 3656
8bb93aac
N
3657
3658/* Metdata version.
e691063a
N
3659 * This is one of
3660 * 'none' for arrays with no metadata (good luck...)
3661 * 'external' for arrays with externally managed metadata,
8bb93aac
N
3662 * or N.M for internally known formats
3663 */
3664static ssize_t
3665metadata_show(mddev_t *mddev, char *page)
3666{
3667 if (mddev->persistent)
3668 return sprintf(page, "%d.%d\n",
3669 mddev->major_version, mddev->minor_version);
e691063a
N
3670 else if (mddev->external)
3671 return sprintf(page, "external:%s\n", mddev->metadata_type);
8bb93aac
N
3672 else
3673 return sprintf(page, "none\n");
3674}
3675
3676static ssize_t
3677metadata_store(mddev_t *mddev, const char *buf, size_t len)
3678{
3679 int major, minor;
3680 char *e;
ea43ddd8
N
3681 /* Changing the details of 'external' metadata is
3682 * always permitted. Otherwise there must be
3683 * no devices attached to the array.
3684 */
3685 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3686 ;
3687 else if (!list_empty(&mddev->disks))
8bb93aac
N
3688 return -EBUSY;
3689
3690 if (cmd_match(buf, "none")) {
3691 mddev->persistent = 0;
e691063a
N
3692 mddev->external = 0;
3693 mddev->major_version = 0;
3694 mddev->minor_version = 90;
3695 return len;
3696 }
3697 if (strncmp(buf, "external:", 9) == 0) {
20a49ff6 3698 size_t namelen = len-9;
e691063a
N
3699 if (namelen >= sizeof(mddev->metadata_type))
3700 namelen = sizeof(mddev->metadata_type)-1;
3701 strncpy(mddev->metadata_type, buf+9, namelen);
3702 mddev->metadata_type[namelen] = 0;
3703 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3704 mddev->metadata_type[--namelen] = 0;
3705 mddev->persistent = 0;
3706 mddev->external = 1;
8bb93aac
N
3707 mddev->major_version = 0;
3708 mddev->minor_version = 90;
3709 return len;
3710 }
3711 major = simple_strtoul(buf, &e, 10);
3712 if (e==buf || *e != '.')
3713 return -EINVAL;
3714 buf = e+1;
3715 minor = simple_strtoul(buf, &e, 10);
3f9d7b0d 3716 if (e==buf || (*e && *e != '\n') )
8bb93aac 3717 return -EINVAL;
50511da3 3718 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
8bb93aac
N
3719 return -ENOENT;
3720 mddev->major_version = major;
3721 mddev->minor_version = minor;
3722 mddev->persistent = 1;
e691063a 3723 mddev->external = 0;
8bb93aac
N
3724 return len;
3725}
3726
3727static struct md_sysfs_entry md_metadata =
80ca3a44 3728__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
8bb93aac 3729
24dd469d 3730static ssize_t
7eec314d 3731action_show(mddev_t *mddev, char *page)
24dd469d 3732{
7eec314d 3733 char *type = "idle";
b6a9ce68
N
3734 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3735 type = "frozen";
3736 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2b12ab6d 3737 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
ccfcc3c1
N
3738 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3739 type = "reshape";
3740 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
24dd469d
N
3741 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3742 type = "resync";
3743 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3744 type = "check";
3745 else
3746 type = "repair";
72a23c21 3747 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
24dd469d
N
3748 type = "recover";
3749 }
3750 return sprintf(page, "%s\n", type);
3751}
3752
7ebc0be7
N
3753static void reap_sync_thread(mddev_t *mddev);
3754
24dd469d 3755static ssize_t
7eec314d 3756action_store(mddev_t *mddev, const char *page, size_t len)
24dd469d 3757{
7eec314d
N
3758 if (!mddev->pers || !mddev->pers->sync_request)
3759 return -EINVAL;
3760
b6a9ce68
N
3761 if (cmd_match(page, "frozen"))
3762 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3763 else
3764 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3765
3766 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
7eec314d
N
3767 if (mddev->sync_thread) {
3768 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7ebc0be7 3769 reap_sync_thread(mddev);
7eec314d 3770 }
03c902e1
N
3771 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3772 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
24dd469d 3773 return -EBUSY;
72a23c21
NB
3774 else if (cmd_match(page, "resync"))
3775 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3776 else if (cmd_match(page, "recover")) {
3777 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7eec314d 3778 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
72a23c21 3779 } else if (cmd_match(page, "reshape")) {
16484bf5
N
3780 int err;
3781 if (mddev->pers->start_reshape == NULL)
3782 return -EINVAL;
3783 err = mddev->pers->start_reshape(mddev);
3784 if (err)
3785 return err;
a99ac971 3786 sysfs_notify(&mddev->kobj, NULL, "degraded");
16484bf5 3787 } else {
bce74dac 3788 if (cmd_match(page, "check"))
7eec314d 3789 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2adc7d47 3790 else if (!cmd_match(page, "repair"))
7eec314d
N
3791 return -EINVAL;
3792 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3793 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7eec314d 3794 }
03c902e1 3795 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
24dd469d 3796 md_wakeup_thread(mddev->thread);
00bcb4ac 3797 sysfs_notify_dirent_safe(mddev->sysfs_action);
24dd469d
N
3798 return len;
3799}
3800
9d88883e 3801static ssize_t
96de1e66 3802mismatch_cnt_show(mddev_t *mddev, char *page)
9d88883e
N
3803{
3804 return sprintf(page, "%llu\n",
3805 (unsigned long long) mddev->resync_mismatches);
3806}
3807
80ca3a44
N
3808static struct md_sysfs_entry md_scan_mode =
3809__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
24dd469d 3810
96de1e66 3811
80ca3a44 3812static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
9d88883e 3813
88202a0c
N
3814static ssize_t
3815sync_min_show(mddev_t *mddev, char *page)
3816{
3817 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3818 mddev->sync_speed_min ? "local": "system");
3819}
3820
3821static ssize_t
3822sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3823{
3824 int min;
3825 char *e;
3826 if (strncmp(buf, "system", 6)==0) {
3827 mddev->sync_speed_min = 0;
3828 return len;
3829 }
3830 min = simple_strtoul(buf, &e, 10);
3831 if (buf == e || (*e && *e != '\n') || min <= 0)
3832 return -EINVAL;
3833 mddev->sync_speed_min = min;
3834 return len;
3835}
3836
3837static struct md_sysfs_entry md_sync_min =
3838__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3839
3840static ssize_t
3841sync_max_show(mddev_t *mddev, char *page)
3842{
3843 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3844 mddev->sync_speed_max ? "local": "system");
3845}
3846
3847static ssize_t
3848sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3849{
3850 int max;
3851 char *e;
3852 if (strncmp(buf, "system", 6)==0) {
3853 mddev->sync_speed_max = 0;
3854 return len;
3855 }
3856 max = simple_strtoul(buf, &e, 10);
3857 if (buf == e || (*e && *e != '\n') || max <= 0)
3858 return -EINVAL;
3859 mddev->sync_speed_max = max;
3860 return len;
3861}
3862
3863static struct md_sysfs_entry md_sync_max =
3864__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3865
d7f3d291
IP
3866static ssize_t
3867degraded_show(mddev_t *mddev, char *page)
3868{
3869 return sprintf(page, "%d\n", mddev->degraded);
3870}
3871static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
88202a0c 3872
90b08710
BS
3873static ssize_t
3874sync_force_parallel_show(mddev_t *mddev, char *page)
3875{
3876 return sprintf(page, "%d\n", mddev->parallel_resync);
3877}
3878
3879static ssize_t
3880sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3881{
3882 long n;
3883
3884 if (strict_strtol(buf, 10, &n))
3885 return -EINVAL;
3886
3887 if (n != 0 && n != 1)
3888 return -EINVAL;
3889
3890 mddev->parallel_resync = n;
3891
3892 if (mddev->sync_thread)
3893 wake_up(&resync_wait);
3894
3895 return len;
3896}
3897
3898/* force parallel resync, even with shared block devices */
3899static struct md_sysfs_entry md_sync_force_parallel =
3900__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3901 sync_force_parallel_show, sync_force_parallel_store);
3902
88202a0c
N
3903static ssize_t
3904sync_speed_show(mddev_t *mddev, char *page)
3905{
3906 unsigned long resync, dt, db;
d1a7c503
N
3907 if (mddev->curr_resync == 0)
3908 return sprintf(page, "none\n");
9687a60c
AN
3909 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3910 dt = (jiffies - mddev->resync_mark) / HZ;
88202a0c 3911 if (!dt) dt++;
9687a60c
AN
3912 db = resync - mddev->resync_mark_cnt;
3913 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
88202a0c
N
3914}
3915
80ca3a44 3916static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
88202a0c
N
3917
3918static ssize_t
3919sync_completed_show(mddev_t *mddev, char *page)
3920{
58c0fed4 3921 unsigned long max_sectors, resync;
88202a0c 3922
acb180b0
N
3923 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3924 return sprintf(page, "none\n");
3925
88202a0c 3926 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
58c0fed4 3927 max_sectors = mddev->resync_max_sectors;
88202a0c 3928 else
58c0fed4 3929 max_sectors = mddev->dev_sectors;
88202a0c 3930
acb180b0 3931 resync = mddev->curr_resync_completed;
58c0fed4 3932 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
88202a0c
N
3933}
3934
80ca3a44 3935static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
88202a0c 3936
5e96ee65
NB
3937static ssize_t
3938min_sync_show(mddev_t *mddev, char *page)
3939{
3940 return sprintf(page, "%llu\n",
3941 (unsigned long long)mddev->resync_min);
3942}
3943static ssize_t
3944min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3945{
3946 unsigned long long min;
3947 if (strict_strtoull(buf, 10, &min))
3948 return -EINVAL;
3949 if (min > mddev->resync_max)
3950 return -EINVAL;
3951 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3952 return -EBUSY;
3953
3954 /* Must be a multiple of chunk_size */
9d8f0363 3955 if (mddev->chunk_sectors) {
2ac06c33 3956 sector_t temp = min;
9d8f0363 3957 if (sector_div(temp, mddev->chunk_sectors))
5e96ee65
NB
3958 return -EINVAL;
3959 }
3960 mddev->resync_min = min;
3961
3962 return len;
3963}
3964
3965static struct md_sysfs_entry md_min_sync =
3966__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3967
c6207277
N
3968static ssize_t
3969max_sync_show(mddev_t *mddev, char *page)
3970{
3971 if (mddev->resync_max == MaxSector)
3972 return sprintf(page, "max\n");
3973 else
3974 return sprintf(page, "%llu\n",
3975 (unsigned long long)mddev->resync_max);
3976}
3977static ssize_t
3978max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3979{
3980 if (strncmp(buf, "max", 3) == 0)
3981 mddev->resync_max = MaxSector;
3982 else {
5e96ee65
NB
3983 unsigned long long max;
3984 if (strict_strtoull(buf, 10, &max))
3985 return -EINVAL;
3986 if (max < mddev->resync_min)
c6207277
N
3987 return -EINVAL;
3988 if (max < mddev->resync_max &&
4d484a4a 3989 mddev->ro == 0 &&
c6207277
N
3990 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3991 return -EBUSY;
3992
3993 /* Must be a multiple of chunk_size */
9d8f0363 3994 if (mddev->chunk_sectors) {
2ac06c33 3995 sector_t temp = max;
9d8f0363 3996 if (sector_div(temp, mddev->chunk_sectors))
c6207277
N
3997 return -EINVAL;
3998 }
3999 mddev->resync_max = max;
4000 }
4001 wake_up(&mddev->recovery_wait);
4002 return len;
4003}
4004
4005static struct md_sysfs_entry md_max_sync =
4006__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4007
e464eafd
N
4008static ssize_t
4009suspend_lo_show(mddev_t *mddev, char *page)
4010{
4011 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4012}
4013
4014static ssize_t
4015suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4016{
4017 char *e;
4018 unsigned long long new = simple_strtoull(buf, &e, 10);
23ddff37 4019 unsigned long long old = mddev->suspend_lo;
e464eafd 4020
b8d966ef
N
4021 if (mddev->pers == NULL ||
4022 mddev->pers->quiesce == NULL)
e464eafd
N
4023 return -EINVAL;
4024 if (buf == e || (*e && *e != '\n'))
4025 return -EINVAL;
23ddff37
N
4026
4027 mddev->suspend_lo = new;
4028 if (new >= old)
4029 /* Shrinking suspended region */
e464eafd 4030 mddev->pers->quiesce(mddev, 2);
23ddff37
N
4031 else {
4032 /* Expanding suspended region - need to wait */
4033 mddev->pers->quiesce(mddev, 1);
4034 mddev->pers->quiesce(mddev, 0);
4035 }
4036 return len;
e464eafd
N
4037}
4038static struct md_sysfs_entry md_suspend_lo =
4039__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4040
4041
4042static ssize_t
4043suspend_hi_show(mddev_t *mddev, char *page)
4044{
4045 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4046}
4047
4048static ssize_t
4049suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4050{
4051 char *e;
4052 unsigned long long new = simple_strtoull(buf, &e, 10);
23ddff37 4053 unsigned long long old = mddev->suspend_hi;
e464eafd 4054
b8d966ef
N
4055 if (mddev->pers == NULL ||
4056 mddev->pers->quiesce == NULL)
e464eafd
N
4057 return -EINVAL;
4058 if (buf == e || (*e && *e != '\n'))
4059 return -EINVAL;
23ddff37
N
4060
4061 mddev->suspend_hi = new;
4062 if (new <= old)
4063 /* Shrinking suspended region */
4064 mddev->pers->quiesce(mddev, 2);
4065 else {
4066 /* Expanding suspended region - need to wait */
e464eafd
N
4067 mddev->pers->quiesce(mddev, 1);
4068 mddev->pers->quiesce(mddev, 0);
23ddff37
N
4069 }
4070 return len;
e464eafd
N
4071}
4072static struct md_sysfs_entry md_suspend_hi =
4073__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4074
08a02ecd
N
4075static ssize_t
4076reshape_position_show(mddev_t *mddev, char *page)
4077{
4078 if (mddev->reshape_position != MaxSector)
4079 return sprintf(page, "%llu\n",
4080 (unsigned long long)mddev->reshape_position);
4081 strcpy(page, "none\n");
4082 return 5;
4083}
4084
4085static ssize_t
4086reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4087{
4088 char *e;
4089 unsigned long long new = simple_strtoull(buf, &e, 10);
4090 if (mddev->pers)
4091 return -EBUSY;
4092 if (buf == e || (*e && *e != '\n'))
4093 return -EINVAL;
4094 mddev->reshape_position = new;
4095 mddev->delta_disks = 0;
4096 mddev->new_level = mddev->level;
4097 mddev->new_layout = mddev->layout;
664e7c41 4098 mddev->new_chunk_sectors = mddev->chunk_sectors;
08a02ecd
N
4099 return len;
4100}
4101
4102static struct md_sysfs_entry md_reshape_position =
4103__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4104 reshape_position_store);
4105
b522adcd
DW
4106static ssize_t
4107array_size_show(mddev_t *mddev, char *page)
4108{
4109 if (mddev->external_size)
4110 return sprintf(page, "%llu\n",
4111 (unsigned long long)mddev->array_sectors/2);
4112 else
4113 return sprintf(page, "default\n");
4114}
4115
4116static ssize_t
4117array_size_store(mddev_t *mddev, const char *buf, size_t len)
4118{
4119 sector_t sectors;
4120
4121 if (strncmp(buf, "default", 7) == 0) {
4122 if (mddev->pers)
4123 sectors = mddev->pers->size(mddev, 0, 0);
4124 else
4125 sectors = mddev->array_sectors;
4126
4127 mddev->external_size = 0;
4128 } else {
4129 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4130 return -EINVAL;
4131 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
2b69c839 4132 return -E2BIG;
b522adcd
DW
4133
4134 mddev->external_size = 1;
4135 }
4136
4137 mddev->array_sectors = sectors;
4138 set_capacity(mddev->gendisk, mddev->array_sectors);
449aad3e
N
4139 if (mddev->pers)
4140 revalidate_disk(mddev->gendisk);
b522adcd
DW
4141
4142 return len;
4143}
4144
4145static struct md_sysfs_entry md_array_size =
4146__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4147 array_size_store);
e464eafd 4148
eae1701f
N
4149static struct attribute *md_default_attrs[] = {
4150 &md_level.attr,
d4dbd025 4151 &md_layout.attr,
eae1701f 4152 &md_raid_disks.attr,
3b34380a 4153 &md_chunk_size.attr,
a35b0d69 4154 &md_size.attr,
a94213b1 4155 &md_resync_start.attr,
8bb93aac 4156 &md_metadata.attr,
6d7ff738 4157 &md_new_device.attr,
16f17b39 4158 &md_safe_delay.attr,
9e653b63 4159 &md_array_state.attr,
08a02ecd 4160 &md_reshape_position.attr,
b522adcd 4161 &md_array_size.attr,
1e50915f 4162 &max_corr_read_errors.attr,
411036fa
N
4163 NULL,
4164};
4165
4166static struct attribute *md_redundancy_attrs[] = {
24dd469d 4167 &md_scan_mode.attr,
9d88883e 4168 &md_mismatches.attr,
88202a0c
N
4169 &md_sync_min.attr,
4170 &md_sync_max.attr,
4171 &md_sync_speed.attr,
90b08710 4172 &md_sync_force_parallel.attr,
88202a0c 4173 &md_sync_completed.attr,
5e96ee65 4174 &md_min_sync.attr,
c6207277 4175 &md_max_sync.attr,
e464eafd
N
4176 &md_suspend_lo.attr,
4177 &md_suspend_hi.attr,
9b1d1dac 4178 &md_bitmap.attr,
d7f3d291 4179 &md_degraded.attr,
eae1701f
N
4180 NULL,
4181};
411036fa
N
4182static struct attribute_group md_redundancy_group = {
4183 .name = NULL,
4184 .attrs = md_redundancy_attrs,
4185};
4186
eae1701f
N
4187
4188static ssize_t
4189md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4190{
4191 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4192 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
96de1e66 4193 ssize_t rv;
eae1701f
N
4194
4195 if (!entry->show)
4196 return -EIO;
5dc5cf7d
IM
4197 rv = mddev_lock(mddev);
4198 if (!rv) {
4199 rv = entry->show(mddev, page);
4200 mddev_unlock(mddev);
4201 }
96de1e66 4202 return rv;
eae1701f
N
4203}
4204
4205static ssize_t
4206md_attr_store(struct kobject *kobj, struct attribute *attr,
4207 const char *page, size_t length)
4208{
4209 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4210 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
96de1e66 4211 ssize_t rv;
eae1701f
N
4212
4213 if (!entry->store)
4214 return -EIO;
67463acb
N
4215 if (!capable(CAP_SYS_ADMIN))
4216 return -EACCES;
5dc5cf7d 4217 rv = mddev_lock(mddev);
d3374825
N
4218 if (mddev->hold_active == UNTIL_IOCTL)
4219 mddev->hold_active = 0;
5dc5cf7d
IM
4220 if (!rv) {
4221 rv = entry->store(mddev, page, length);
4222 mddev_unlock(mddev);
4223 }
96de1e66 4224 return rv;
eae1701f
N
4225}
4226
4227static void md_free(struct kobject *ko)
4228{
4229 mddev_t *mddev = container_of(ko, mddev_t, kobj);
a21d1504
N
4230
4231 if (mddev->sysfs_state)
4232 sysfs_put(mddev->sysfs_state);
4233
4234 if (mddev->gendisk) {
4235 del_gendisk(mddev->gendisk);
4236 put_disk(mddev->gendisk);
4237 }
4238 if (mddev->queue)
4239 blk_cleanup_queue(mddev->queue);
4240
eae1701f
N
4241 kfree(mddev);
4242}
4243
52cf25d0 4244static const struct sysfs_ops md_sysfs_ops = {
eae1701f
N
4245 .show = md_attr_show,
4246 .store = md_attr_store,
4247};
4248static struct kobj_type md_ktype = {
4249 .release = md_free,
4250 .sysfs_ops = &md_sysfs_ops,
4251 .default_attrs = md_default_attrs,
4252};
4253
1da177e4
LT
4254int mdp_major = 0;
4255
5fd3a17e
DW
4256static void mddev_delayed_delete(struct work_struct *ws)
4257{
4258 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4259
43a70507 4260 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
5fd3a17e
DW
4261 kobject_del(&mddev->kobj);
4262 kobject_put(&mddev->kobj);
4263}
4264
efeb53c0 4265static int md_alloc(dev_t dev, char *name)
1da177e4 4266{
48c9c27b 4267 static DEFINE_MUTEX(disks_mutex);
1da177e4
LT
4268 mddev_t *mddev = mddev_find(dev);
4269 struct gendisk *disk;
efeb53c0
N
4270 int partitioned;
4271 int shift;
4272 int unit;
3830c62f 4273 int error;
1da177e4
LT
4274
4275 if (!mddev)
efeb53c0
N
4276 return -ENODEV;
4277
4278 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4279 shift = partitioned ? MdpMinorShift : 0;
4280 unit = MINOR(mddev->unit) >> shift;
1da177e4 4281
e804ac78
TH
4282 /* wait for any previous instance of this device to be
4283 * completely removed (mddev_delayed_delete).
d3374825 4284 */
e804ac78 4285 flush_workqueue(md_misc_wq);
d3374825 4286
48c9c27b 4287 mutex_lock(&disks_mutex);
0909dc44
N
4288 error = -EEXIST;
4289 if (mddev->gendisk)
4290 goto abort;
efeb53c0
N
4291
4292 if (name) {
4293 /* Need to ensure that 'name' is not a duplicate.
4294 */
4295 mddev_t *mddev2;
4296 spin_lock(&all_mddevs_lock);
4297
4298 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4299 if (mddev2->gendisk &&
4300 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4301 spin_unlock(&all_mddevs_lock);
0909dc44 4302 goto abort;
efeb53c0
N
4303 }
4304 spin_unlock(&all_mddevs_lock);
1da177e4 4305 }
8b765398 4306
0909dc44 4307 error = -ENOMEM;
8b765398 4308 mddev->queue = blk_alloc_queue(GFP_KERNEL);
0909dc44
N
4309 if (!mddev->queue)
4310 goto abort;
409c57f3
N
4311 mddev->queue->queuedata = mddev;
4312
409c57f3 4313 blk_queue_make_request(mddev->queue, md_make_request);
8b765398 4314
1da177e4
LT
4315 disk = alloc_disk(1 << shift);
4316 if (!disk) {
8b765398
N
4317 blk_cleanup_queue(mddev->queue);
4318 mddev->queue = NULL;
0909dc44 4319 goto abort;
1da177e4 4320 }
efeb53c0 4321 disk->major = MAJOR(mddev->unit);
1da177e4 4322 disk->first_minor = unit << shift;
efeb53c0
N
4323 if (name)
4324 strcpy(disk->disk_name, name);
4325 else if (partitioned)
1da177e4 4326 sprintf(disk->disk_name, "md_d%d", unit);
ce7b0f46 4327 else
1da177e4 4328 sprintf(disk->disk_name, "md%d", unit);
1da177e4
LT
4329 disk->fops = &md_fops;
4330 disk->private_data = mddev;
4331 disk->queue = mddev->queue;
92850bbd 4332 /* Allow extended partitions. This makes the
d3374825 4333 * 'mdp' device redundant, but we can't really
92850bbd
N
4334 * remove it now.
4335 */
4336 disk->flags |= GENHD_FL_EXT_DEVT;
1da177e4
LT
4337 add_disk(disk);
4338 mddev->gendisk = disk;
ed9e1982
TH
4339 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4340 &disk_to_dev(disk)->kobj, "%s", "md");
0909dc44
N
4341 if (error) {
4342 /* This isn't possible, but as kobject_init_and_add is marked
4343 * __must_check, we must do something with the result
4344 */
5e55e2f5
N
4345 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4346 disk->disk_name);
0909dc44
N
4347 error = 0;
4348 }
00bcb4ac
N
4349 if (mddev->kobj.sd &&
4350 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
43a70507 4351 printk(KERN_DEBUG "pointless warning\n");
be20e6c6
DW
4352
4353 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
0909dc44
N
4354 abort:
4355 mutex_unlock(&disks_mutex);
00bcb4ac 4356 if (!error && mddev->kobj.sd) {
3830c62f 4357 kobject_uevent(&mddev->kobj, KOBJ_ADD);
00bcb4ac 4358 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
b62b7590 4359 }
d3374825 4360 mddev_put(mddev);
0909dc44 4361 return error;
efeb53c0
N
4362}
4363
4364static struct kobject *md_probe(dev_t dev, int *part, void *data)
4365{
4366 md_alloc(dev, NULL);
1da177e4
LT
4367 return NULL;
4368}
4369
efeb53c0
N
4370static int add_named_array(const char *val, struct kernel_param *kp)
4371{
4372 /* val must be "md_*" where * is not all digits.
4373 * We allocate an array with a large free minor number, and
4374 * set the name to val. val must not already be an active name.
4375 */
4376 int len = strlen(val);
4377 char buf[DISK_NAME_LEN];
4378
4379 while (len && val[len-1] == '\n')
4380 len--;
4381 if (len >= DISK_NAME_LEN)
4382 return -E2BIG;
4383 strlcpy(buf, val, len+1);
4384 if (strncmp(buf, "md_", 3) != 0)
4385 return -EINVAL;
4386 return md_alloc(0, buf);
4387}
4388
1da177e4
LT
4389static void md_safemode_timeout(unsigned long data)
4390{
4391 mddev_t *mddev = (mddev_t *) data;
4392
0fd62b86
NB
4393 if (!atomic_read(&mddev->writes_pending)) {
4394 mddev->safemode = 1;
4395 if (mddev->external)
00bcb4ac 4396 sysfs_notify_dirent_safe(mddev->sysfs_state);
0fd62b86 4397 }
1da177e4
LT
4398 md_wakeup_thread(mddev->thread);
4399}
4400
6ff8d8ec 4401static int start_dirty_degraded;
1da177e4 4402
390ee602 4403int md_run(mddev_t *mddev)
1da177e4 4404{
2604b703 4405 int err;
1da177e4 4406 mdk_rdev_t *rdev;
2604b703 4407 struct mdk_personality *pers;
1da177e4 4408
a757e64c
N
4409 if (list_empty(&mddev->disks))
4410 /* cannot run an array with no devices.. */
1da177e4 4411 return -EINVAL;
1da177e4
LT
4412
4413 if (mddev->pers)
4414 return -EBUSY;
bb4f1e9d
N
4415 /* Cannot run until previous stop completes properly */
4416 if (mddev->sysfs_active)
4417 return -EBUSY;
b6eb127d 4418
1da177e4
LT
4419 /*
4420 * Analyze all RAID superblock(s)
4421 */
1ec4a939
N
4422 if (!mddev->raid_disks) {
4423 if (!mddev->persistent)
4424 return -EINVAL;
a757e64c 4425 analyze_sbs(mddev);
1ec4a939 4426 }
1da177e4 4427
d9d166c2
N
4428 if (mddev->level != LEVEL_NONE)
4429 request_module("md-level-%d", mddev->level);
4430 else if (mddev->clevel[0])
4431 request_module("md-%s", mddev->clevel);
1da177e4
LT
4432
4433 /*
4434 * Drop all container device buffers, from now on
4435 * the only valid external interface is through the md
4436 * device.
1da177e4 4437 */
159ec1fc 4438 list_for_each_entry(rdev, &mddev->disks, same_set) {
b2d444d7 4439 if (test_bit(Faulty, &rdev->flags))
1da177e4
LT
4440 continue;
4441 sync_blockdev(rdev->bdev);
f98393a6 4442 invalidate_bdev(rdev->bdev);
f0d76d70
N
4443
4444 /* perform some consistency tests on the device.
4445 * We don't want the data to overlap the metadata,
58c0fed4 4446 * Internal Bitmap issues have been handled elsewhere.
f0d76d70 4447 */
a6ff7e08
JB
4448 if (rdev->meta_bdev) {
4449 /* Nothing to check */;
4450 } else if (rdev->data_offset < rdev->sb_start) {
58c0fed4
AN
4451 if (mddev->dev_sectors &&
4452 rdev->data_offset + mddev->dev_sectors
0f420358 4453 > rdev->sb_start) {
f0d76d70
N
4454 printk("md: %s: data overlaps metadata\n",
4455 mdname(mddev));
4456 return -EINVAL;
4457 }
4458 } else {
0f420358 4459 if (rdev->sb_start + rdev->sb_size/512
f0d76d70
N
4460 > rdev->data_offset) {
4461 printk("md: %s: metadata overlaps data\n",
4462 mdname(mddev));
4463 return -EINVAL;
4464 }
4465 }
00bcb4ac 4466 sysfs_notify_dirent_safe(rdev->sysfs_state);
1da177e4
LT
4467 }
4468
a167f663
N
4469 if (mddev->bio_set == NULL)
4470 mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev));
4471
1da177e4 4472 spin_lock(&pers_lock);
d9d166c2 4473 pers = find_pers(mddev->level, mddev->clevel);
2604b703 4474 if (!pers || !try_module_get(pers->owner)) {
1da177e4 4475 spin_unlock(&pers_lock);
d9d166c2
N
4476 if (mddev->level != LEVEL_NONE)
4477 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4478 mddev->level);
4479 else
4480 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4481 mddev->clevel);
1da177e4
LT
4482 return -EINVAL;
4483 }
2604b703 4484 mddev->pers = pers;
1da177e4 4485 spin_unlock(&pers_lock);
34817e8c
N
4486 if (mddev->level != pers->level) {
4487 mddev->level = pers->level;
4488 mddev->new_level = pers->level;
4489 }
d9d166c2 4490 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
1da177e4 4491
f6705578 4492 if (mddev->reshape_position != MaxSector &&
63c70c4f 4493 pers->start_reshape == NULL) {
f6705578
N
4494 /* This personality cannot handle reshaping... */
4495 mddev->pers = NULL;
4496 module_put(pers->owner);
4497 return -EINVAL;
4498 }
4499
7dd5e7c3
N
4500 if (pers->sync_request) {
4501 /* Warn if this is a potentially silly
4502 * configuration.
4503 */
4504 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4505 mdk_rdev_t *rdev2;
7dd5e7c3 4506 int warned = 0;
159ec1fc
CR
4507
4508 list_for_each_entry(rdev, &mddev->disks, same_set)
4509 list_for_each_entry(rdev2, &mddev->disks, same_set) {
7dd5e7c3
N
4510 if (rdev < rdev2 &&
4511 rdev->bdev->bd_contains ==
4512 rdev2->bdev->bd_contains) {
4513 printk(KERN_WARNING
4514 "%s: WARNING: %s appears to be"
4515 " on the same physical disk as"
4516 " %s.\n",
4517 mdname(mddev),
4518 bdevname(rdev->bdev,b),
4519 bdevname(rdev2->bdev,b2));
4520 warned = 1;
4521 }
4522 }
159ec1fc 4523
7dd5e7c3
N
4524 if (warned)
4525 printk(KERN_WARNING
4526 "True protection against single-disk"
4527 " failure might be compromised.\n");
4528 }
4529
657390d2 4530 mddev->recovery = 0;
58c0fed4
AN
4531 /* may be over-ridden by personality */
4532 mddev->resync_max_sectors = mddev->dev_sectors;
4533
6ff8d8ec 4534 mddev->ok_start_degraded = start_dirty_degraded;
1da177e4 4535
0f9552b5 4536 if (start_readonly && mddev->ro == 0)
f91de92e
N
4537 mddev->ro = 2; /* read-only, but switch on first write */
4538
b15c2e57 4539 err = mddev->pers->run(mddev);
13e53df3
AN
4540 if (err)
4541 printk(KERN_ERR "md: pers->run() failed ...\n");
b522adcd
DW
4542 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4543 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4544 " but 'external_size' not in effect?\n", __func__);
4545 printk(KERN_ERR
4546 "md: invalid array_size %llu > default size %llu\n",
4547 (unsigned long long)mddev->array_sectors / 2,
4548 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4549 err = -EINVAL;
4550 mddev->pers->stop(mddev);
4551 }
4552 if (err == 0 && mddev->pers->sync_request) {
b15c2e57
N
4553 err = bitmap_create(mddev);
4554 if (err) {
4555 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4556 mdname(mddev), err);
4557 mddev->pers->stop(mddev);
4558 }
4559 }
1da177e4 4560 if (err) {
1da177e4
LT
4561 module_put(mddev->pers->owner);
4562 mddev->pers = NULL;
32a7627c
N
4563 bitmap_destroy(mddev);
4564 return err;
1da177e4 4565 }
5e55e2f5 4566 if (mddev->pers->sync_request) {
00bcb4ac
N
4567 if (mddev->kobj.sd &&
4568 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5e55e2f5
N
4569 printk(KERN_WARNING
4570 "md: cannot register extra attributes for %s\n",
4571 mdname(mddev));
00bcb4ac 4572 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5e55e2f5 4573 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
fd9d49ca
N
4574 mddev->ro = 0;
4575
1da177e4 4576 atomic_set(&mddev->writes_pending,0);
1e50915f
RB
4577 atomic_set(&mddev->max_corr_read_errors,
4578 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
1da177e4
LT
4579 mddev->safemode = 0;
4580 mddev->safemode_timer.function = md_safemode_timeout;
4581 mddev->safemode_timer.data = (unsigned long) mddev;
16f17b39 4582 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
1da177e4 4583 mddev->in_sync = 1;
0ca69886
N
4584 smp_wmb();
4585 mddev->ready = 1;
159ec1fc 4586 list_for_each_entry(rdev, &mddev->disks, same_set)
86e6ffdd
N
4587 if (rdev->raid_disk >= 0) {
4588 char nm[20];
4589 sprintf(nm, "rd%d", rdev->raid_disk);
5e55e2f5 4590 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
00bcb4ac 4591 /* failure here is OK */;
86e6ffdd 4592 }
1da177e4
LT
4593
4594 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4595
850b2b42
N
4596 if (mddev->flags)
4597 md_update_sb(mddev, 0);
1da177e4 4598
0b8c9de0
N
4599 md_wakeup_thread(mddev->thread);
4600 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5fd6c1dc 4601
d7603b7e 4602 md_new_event(mddev);
00bcb4ac
N
4603 sysfs_notify_dirent_safe(mddev->sysfs_state);
4604 sysfs_notify_dirent_safe(mddev->sysfs_action);
a99ac971 4605 sysfs_notify(&mddev->kobj, NULL, "degraded");
1da177e4
LT
4606 return 0;
4607}
390ee602 4608EXPORT_SYMBOL_GPL(md_run);
1da177e4 4609
fe60b014
N
4610static int do_md_run(mddev_t *mddev)
4611{
4612 int err;
4613
4614 err = md_run(mddev);
4615 if (err)
4616 goto out;
69e51b44
N
4617 err = bitmap_load(mddev);
4618 if (err) {
4619 bitmap_destroy(mddev);
4620 goto out;
4621 }
fe60b014
N
4622 set_capacity(mddev->gendisk, mddev->array_sectors);
4623 revalidate_disk(mddev->gendisk);
4624 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4625out:
4626 return err;
4627}
4628
1da177e4
LT
4629static int restart_array(mddev_t *mddev)
4630{
4631 struct gendisk *disk = mddev->gendisk;
1da177e4 4632
80fab1d7 4633 /* Complain if it has no devices */
1da177e4 4634 if (list_empty(&mddev->disks))
80fab1d7
AN
4635 return -ENXIO;
4636 if (!mddev->pers)
4637 return -EINVAL;
4638 if (!mddev->ro)
4639 return -EBUSY;
4640 mddev->safemode = 0;
4641 mddev->ro = 0;
4642 set_disk_ro(disk, 0);
4643 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4644 mdname(mddev));
4645 /* Kick recovery or resync if necessary */
4646 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4647 md_wakeup_thread(mddev->thread);
4648 md_wakeup_thread(mddev->sync_thread);
00bcb4ac 4649 sysfs_notify_dirent_safe(mddev->sysfs_state);
80fab1d7 4650 return 0;
1da177e4
LT
4651}
4652
acc55e22
N
4653/* similar to deny_write_access, but accounts for our holding a reference
4654 * to the file ourselves */
4655static int deny_bitmap_write_access(struct file * file)
4656{
4657 struct inode *inode = file->f_mapping->host;
4658
4659 spin_lock(&inode->i_lock);
4660 if (atomic_read(&inode->i_writecount) > 1) {
4661 spin_unlock(&inode->i_lock);
4662 return -ETXTBSY;
4663 }
4664 atomic_set(&inode->i_writecount, -1);
4665 spin_unlock(&inode->i_lock);
4666
4667 return 0;
4668}
4669
43a70507 4670void restore_bitmap_write_access(struct file *file)
acc55e22
N
4671{
4672 struct inode *inode = file->f_mapping->host;
4673
4674 spin_lock(&inode->i_lock);
4675 atomic_set(&inode->i_writecount, 1);
4676 spin_unlock(&inode->i_lock);
4677}
4678
6177b472
N
4679static void md_clean(mddev_t *mddev)
4680{
4681 mddev->array_sectors = 0;
4682 mddev->external_size = 0;
4683 mddev->dev_sectors = 0;
4684 mddev->raid_disks = 0;
4685 mddev->recovery_cp = 0;
4686 mddev->resync_min = 0;
4687 mddev->resync_max = MaxSector;
4688 mddev->reshape_position = MaxSector;
4689 mddev->external = 0;
4690 mddev->persistent = 0;
4691 mddev->level = LEVEL_NONE;
4692 mddev->clevel[0] = 0;
4693 mddev->flags = 0;
4694 mddev->ro = 0;
4695 mddev->metadata_type[0] = 0;
4696 mddev->chunk_sectors = 0;
4697 mddev->ctime = mddev->utime = 0;
4698 mddev->layout = 0;
4699 mddev->max_disks = 0;
4700 mddev->events = 0;
a8707c08 4701 mddev->can_decrease_events = 0;
6177b472
N
4702 mddev->delta_disks = 0;
4703 mddev->new_level = LEVEL_NONE;
4704 mddev->new_layout = 0;
4705 mddev->new_chunk_sectors = 0;
4706 mddev->curr_resync = 0;
4707 mddev->resync_mismatches = 0;
4708 mddev->suspend_lo = mddev->suspend_hi = 0;
4709 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4710 mddev->recovery = 0;
4711 mddev->in_sync = 0;
4712 mddev->degraded = 0;
6177b472
N
4713 mddev->safemode = 0;
4714 mddev->bitmap_info.offset = 0;
4715 mddev->bitmap_info.default_offset = 0;
4716 mddev->bitmap_info.chunksize = 0;
4717 mddev->bitmap_info.daemon_sleep = 0;
4718 mddev->bitmap_info.max_write_behind = 0;
252ac522 4719 mddev->plug = NULL;
6177b472
N
4720}
4721
defad61a 4722static void __md_stop_writes(mddev_t *mddev)
a047e125
N
4723{
4724 if (mddev->sync_thread) {
4725 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4726 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7ebc0be7 4727 reap_sync_thread(mddev);
a047e125
N
4728 }
4729
4730 del_timer_sync(&mddev->safemode_timer);
4731
4732 bitmap_flush(mddev);
4733 md_super_wait(mddev);
4734
4735 if (!mddev->in_sync || mddev->flags) {
4736 /* mark array as shutdown cleanly */
4737 mddev->in_sync = 1;
4738 md_update_sb(mddev, 1);
4739 }
4740}
defad61a
N
4741
4742void md_stop_writes(mddev_t *mddev)
4743{
4744 mddev_lock(mddev);
4745 __md_stop_writes(mddev);
4746 mddev_unlock(mddev);
4747}
390ee602 4748EXPORT_SYMBOL_GPL(md_stop_writes);
a047e125 4749
390ee602 4750void md_stop(mddev_t *mddev)
6177b472 4751{
0ca69886 4752 mddev->ready = 0;
6177b472
N
4753 mddev->pers->stop(mddev);
4754 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4755 mddev->to_remove = &md_redundancy_group;
4756 module_put(mddev->pers->owner);
4757 mddev->pers = NULL;
cca9cf90 4758 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6177b472 4759}
390ee602 4760EXPORT_SYMBOL_GPL(md_stop);
6177b472 4761
a4bd82d0
N
4762static int md_set_readonly(mddev_t *mddev, int is_open)
4763{
4764 int err = 0;
4765 mutex_lock(&mddev->open_mutex);
4766 if (atomic_read(&mddev->openers) > is_open) {
4767 printk("md: %s still in use.\n",mdname(mddev));
4768 err = -EBUSY;
4769 goto out;
4770 }
4771 if (mddev->pers) {
defad61a 4772 __md_stop_writes(mddev);
a4bd82d0
N
4773
4774 err = -ENXIO;
4775 if (mddev->ro==1)
4776 goto out;
4777 mddev->ro = 1;
4778 set_disk_ro(mddev->gendisk, 1);
4779 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
00bcb4ac 4780 sysfs_notify_dirent_safe(mddev->sysfs_state);
a4bd82d0
N
4781 err = 0;
4782 }
4783out:
4784 mutex_unlock(&mddev->open_mutex);
4785 return err;
4786}
4787
9e653b63
N
4788/* mode:
4789 * 0 - completely stop and dis-assemble array
9e653b63
N
4790 * 2 - stop but do not disassemble array
4791 */
df5b20cf 4792static int do_md_stop(mddev_t * mddev, int mode, int is_open)
1da177e4 4793{
1da177e4 4794 struct gendisk *disk = mddev->gendisk;
c4647292 4795 mdk_rdev_t *rdev;
1da177e4 4796
c8c00a69 4797 mutex_lock(&mddev->open_mutex);
bb4f1e9d
N
4798 if (atomic_read(&mddev->openers) > is_open ||
4799 mddev->sysfs_active) {
df5b20cf 4800 printk("md: %s still in use.\n",mdname(mddev));
6e17b027
N
4801 mutex_unlock(&mddev->open_mutex);
4802 return -EBUSY;
4803 }
1da177e4 4804
6e17b027 4805 if (mddev->pers) {
a4bd82d0
N
4806 if (mddev->ro)
4807 set_disk_ro(disk, 0);
409c57f3 4808
defad61a 4809 __md_stop_writes(mddev);
a4bd82d0
N
4810 md_stop(mddev);
4811 mddev->queue->merge_bvec_fn = NULL;
4812 mddev->queue->unplug_fn = NULL;
4813 mddev->queue->backing_dev_info.congested_fn = NULL;
6177b472 4814
a4bd82d0 4815 /* tell userspace to handle 'inactive' */
00bcb4ac 4816 sysfs_notify_dirent_safe(mddev->sysfs_state);
0d4ca600 4817
a4bd82d0
N
4818 list_for_each_entry(rdev, &mddev->disks, same_set)
4819 if (rdev->raid_disk >= 0) {
4820 char nm[20];
4821 sprintf(nm, "rd%d", rdev->raid_disk);
4822 sysfs_remove_link(&mddev->kobj, nm);
4823 }
c4647292 4824
a4bd82d0 4825 set_capacity(disk, 0);
6e17b027 4826 mutex_unlock(&mddev->open_mutex);
a4bd82d0 4827 revalidate_disk(disk);
0d4ca600 4828
a4bd82d0
N
4829 if (mddev->ro)
4830 mddev->ro = 0;
6e17b027
N
4831 } else
4832 mutex_unlock(&mddev->open_mutex);
1da177e4
LT
4833 /*
4834 * Free resources if final stop
4835 */
9e653b63 4836 if (mode == 0) {
1da177e4
LT
4837 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4838
978f946b 4839 bitmap_destroy(mddev);
c3d9714e
N
4840 if (mddev->bitmap_info.file) {
4841 restore_bitmap_write_access(mddev->bitmap_info.file);
4842 fput(mddev->bitmap_info.file);
4843 mddev->bitmap_info.file = NULL;
978f946b 4844 }
c3d9714e 4845 mddev->bitmap_info.offset = 0;
978f946b 4846
1da177e4
LT
4847 export_array(mddev);
4848
6177b472 4849 md_clean(mddev);
934d9c23 4850 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
efeb53c0
N
4851 if (mddev->hold_active == UNTIL_STOP)
4852 mddev->hold_active = 0;
a4bd82d0 4853 }
3f9d99c1 4854 blk_integrity_unregister(disk);
d7603b7e 4855 md_new_event(mddev);
00bcb4ac 4856 sysfs_notify_dirent_safe(mddev->sysfs_state);
6e17b027 4857 return 0;
1da177e4
LT
4858}
4859
fdee8ae4 4860#ifndef MODULE
1da177e4
LT
4861static void autorun_array(mddev_t *mddev)
4862{
4863 mdk_rdev_t *rdev;
1da177e4
LT
4864 int err;
4865
a757e64c 4866 if (list_empty(&mddev->disks))
1da177e4 4867 return;
1da177e4
LT
4868
4869 printk(KERN_INFO "md: running: ");
4870
159ec1fc 4871 list_for_each_entry(rdev, &mddev->disks, same_set) {
1da177e4
LT
4872 char b[BDEVNAME_SIZE];
4873 printk("<%s>", bdevname(rdev->bdev,b));
4874 }
4875 printk("\n");
4876
d710e138 4877 err = do_md_run(mddev);
1da177e4
LT
4878 if (err) {
4879 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
d710e138 4880 do_md_stop(mddev, 0, 0);
1da177e4
LT
4881 }
4882}
4883
4884/*
4885 * lets try to run arrays based on all disks that have arrived
4886 * until now. (those are in pending_raid_disks)
4887 *
4888 * the method: pick the first pending disk, collect all disks with
4889 * the same UUID, remove all from the pending list and put them into
4890 * the 'same_array' list. Then order this list based on superblock
4891 * update time (freshest comes first), kick out 'old' disks and
4892 * compare superblocks. If everything's fine then run it.
4893 *
4894 * If "unit" is allocated, then bump its reference count
4895 */
4896static void autorun_devices(int part)
4897{
159ec1fc 4898 mdk_rdev_t *rdev0, *rdev, *tmp;
1da177e4
LT
4899 mddev_t *mddev;
4900 char b[BDEVNAME_SIZE];
4901
4902 printk(KERN_INFO "md: autorun ...\n");
4903 while (!list_empty(&pending_raid_disks)) {
e8703fe1 4904 int unit;
1da177e4 4905 dev_t dev;
ad01c9e3 4906 LIST_HEAD(candidates);
1da177e4
LT
4907 rdev0 = list_entry(pending_raid_disks.next,
4908 mdk_rdev_t, same_set);
4909
4910 printk(KERN_INFO "md: considering %s ...\n",
4911 bdevname(rdev0->bdev,b));
4912 INIT_LIST_HEAD(&candidates);
159ec1fc 4913 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
1da177e4
LT
4914 if (super_90_load(rdev, rdev0, 0) >= 0) {
4915 printk(KERN_INFO "md: adding %s ...\n",
4916 bdevname(rdev->bdev,b));
4917 list_move(&rdev->same_set, &candidates);
4918 }
4919 /*
4920 * now we have a set of devices, with all of them having
4921 * mostly sane superblocks. It's time to allocate the
4922 * mddev.
4923 */
e8703fe1
N
4924 if (part) {
4925 dev = MKDEV(mdp_major,
4926 rdev0->preferred_minor << MdpMinorShift);
4927 unit = MINOR(dev) >> MdpMinorShift;
4928 } else {
4929 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4930 unit = MINOR(dev);
4931 }
4932 if (rdev0->preferred_minor != unit) {
1da177e4
LT
4933 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4934 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4935 break;
4936 }
1da177e4
LT
4937
4938 md_probe(dev, NULL, NULL);
4939 mddev = mddev_find(dev);
9bbbca3a
NB
4940 if (!mddev || !mddev->gendisk) {
4941 if (mddev)
4942 mddev_put(mddev);
4943 printk(KERN_ERR
1da177e4
LT
4944 "md: cannot allocate memory for md drive.\n");
4945 break;
4946 }
4947 if (mddev_lock(mddev))
4948 printk(KERN_WARNING "md: %s locked, cannot run\n",
4949 mdname(mddev));
4950 else if (mddev->raid_disks || mddev->major_version
4951 || !list_empty(&mddev->disks)) {
4952 printk(KERN_WARNING
4953 "md: %s already running, cannot run %s\n",
4954 mdname(mddev), bdevname(rdev0->bdev,b));
4955 mddev_unlock(mddev);
4956 } else {
4957 printk(KERN_INFO "md: created %s\n", mdname(mddev));
1ec4a939 4958 mddev->persistent = 1;
159ec1fc 4959 rdev_for_each_list(rdev, tmp, &candidates) {
1da177e4
LT
4960 list_del_init(&rdev->same_set);
4961 if (bind_rdev_to_array(rdev, mddev))
4962 export_rdev(rdev);
4963 }
4964 autorun_array(mddev);
4965 mddev_unlock(mddev);
4966 }
4967 /* on success, candidates will be empty, on error
4968 * it won't...
4969 */
159ec1fc 4970 rdev_for_each_list(rdev, tmp, &candidates) {
4b80991c 4971 list_del_init(&rdev->same_set);
1da177e4 4972 export_rdev(rdev);
4b80991c 4973 }
1da177e4
LT
4974 mddev_put(mddev);
4975 }
4976 printk(KERN_INFO "md: ... autorun DONE.\n");
4977}
fdee8ae4 4978#endif /* !MODULE */
1da177e4 4979
1da177e4
LT
4980static int get_version(void __user * arg)
4981{
4982 mdu_version_t ver;
4983
4984 ver.major = MD_MAJOR_VERSION;
4985 ver.minor = MD_MINOR_VERSION;
4986 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4987
4988 if (copy_to_user(arg, &ver, sizeof(ver)))
4989 return -EFAULT;
4990
4991 return 0;
4992}
4993
4994static int get_array_info(mddev_t * mddev, void __user * arg)
4995{
4996 mdu_array_info_t info;
a9f326eb 4997 int nr,working,insync,failed,spare;
1da177e4 4998 mdk_rdev_t *rdev;
1da177e4 4999
a9f326eb 5000 nr=working=insync=failed=spare=0;
159ec1fc 5001 list_for_each_entry(rdev, &mddev->disks, same_set) {
1da177e4 5002 nr++;
b2d444d7 5003 if (test_bit(Faulty, &rdev->flags))
1da177e4
LT
5004 failed++;
5005 else {
5006 working++;
b2d444d7 5007 if (test_bit(In_sync, &rdev->flags))
a9f326eb 5008 insync++;
1da177e4
LT
5009 else
5010 spare++;
5011 }
5012 }
5013
5014 info.major_version = mddev->major_version;
5015 info.minor_version = mddev->minor_version;
5016 info.patch_version = MD_PATCHLEVEL_VERSION;
5017 info.ctime = mddev->ctime;
5018 info.level = mddev->level;
58c0fed4
AN
5019 info.size = mddev->dev_sectors / 2;
5020 if (info.size != mddev->dev_sectors / 2) /* overflow */
284ae7ca 5021 info.size = -1;
1da177e4
LT
5022 info.nr_disks = nr;
5023 info.raid_disks = mddev->raid_disks;
5024 info.md_minor = mddev->md_minor;
5025 info.not_persistent= !mddev->persistent;
5026
5027 info.utime = mddev->utime;
5028 info.state = 0;
5029 if (mddev->in_sync)
5030 info.state = (1<<MD_SB_CLEAN);
c3d9714e 5031 if (mddev->bitmap && mddev->bitmap_info.offset)
36fa3063 5032 info.state = (1<<MD_SB_BITMAP_PRESENT);
a9f326eb 5033 info.active_disks = insync;
1da177e4
LT
5034 info.working_disks = working;
5035 info.failed_disks = failed;
5036 info.spare_disks = spare;
5037
5038 info.layout = mddev->layout;
9d8f0363 5039 info.chunk_size = mddev->chunk_sectors << 9;
1da177e4
LT
5040
5041 if (copy_to_user(arg, &info, sizeof(info)))
5042 return -EFAULT;
5043
5044 return 0;
5045}
5046
87162a28 5047static int get_bitmap_file(mddev_t * mddev, void __user * arg)
32a7627c
N
5048{
5049 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5050 char *ptr, *buf = NULL;
5051 int err = -ENOMEM;
5052
b5470dc5
DW
5053 if (md_allow_write(mddev))
5054 file = kmalloc(sizeof(*file), GFP_NOIO);
5055 else
5056 file = kmalloc(sizeof(*file), GFP_KERNEL);
2a2275d6 5057
32a7627c
N
5058 if (!file)
5059 goto out;
5060
5061 /* bitmap disabled, zero the first byte and copy out */
5062 if (!mddev->bitmap || !mddev->bitmap->file) {
5063 file->pathname[0] = '\0';
5064 goto copy_out;
5065 }
5066
5067 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5068 if (!buf)
5069 goto out;
5070
6bcfd601
CH
5071 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5072 if (IS_ERR(ptr))
32a7627c
N
5073 goto out;
5074
5075 strcpy(file->pathname, ptr);
5076
5077copy_out:
5078 err = 0;
5079 if (copy_to_user(arg, file, sizeof(*file)))
5080 err = -EFAULT;
5081out:
5082 kfree(buf);
5083 kfree(file);
5084 return err;
5085}
5086
1da177e4
LT
5087static int get_disk_info(mddev_t * mddev, void __user * arg)
5088{
5089 mdu_disk_info_t info;
1da177e4
LT
5090 mdk_rdev_t *rdev;
5091
5092 if (copy_from_user(&info, arg, sizeof(info)))
5093 return -EFAULT;
5094
26ef379f 5095 rdev = find_rdev_nr(mddev, info.number);
1da177e4
LT
5096 if (rdev) {
5097 info.major = MAJOR(rdev->bdev->bd_dev);
5098 info.minor = MINOR(rdev->bdev->bd_dev);
5099 info.raid_disk = rdev->raid_disk;
5100 info.state = 0;
b2d444d7 5101 if (test_bit(Faulty, &rdev->flags))
1da177e4 5102 info.state |= (1<<MD_DISK_FAULTY);
b2d444d7 5103 else if (test_bit(In_sync, &rdev->flags)) {
1da177e4
LT
5104 info.state |= (1<<MD_DISK_ACTIVE);
5105 info.state |= (1<<MD_DISK_SYNC);
5106 }
8ddf9efe
N
5107 if (test_bit(WriteMostly, &rdev->flags))
5108 info.state |= (1<<MD_DISK_WRITEMOSTLY);
1da177e4
LT
5109 } else {
5110 info.major = info.minor = 0;
5111 info.raid_disk = -1;
5112 info.state = (1<<MD_DISK_REMOVED);
5113 }
5114
5115 if (copy_to_user(arg, &info, sizeof(info)))
5116 return -EFAULT;
5117
5118 return 0;
5119}
5120
5121static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5122{
5123 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5124 mdk_rdev_t *rdev;
5125 dev_t dev = MKDEV(info->major,info->minor);
5126
5127 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5128 return -EOVERFLOW;
5129
5130 if (!mddev->raid_disks) {
5131 int err;
5132 /* expecting a device which has a superblock */
5133 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5134 if (IS_ERR(rdev)) {
5135 printk(KERN_WARNING
5136 "md: md_import_device returned %ld\n",
5137 PTR_ERR(rdev));
5138 return PTR_ERR(rdev);
5139 }
5140 if (!list_empty(&mddev->disks)) {
5141 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5142 mdk_rdev_t, same_set);
a9f326eb 5143 err = super_types[mddev->major_version]
1da177e4
LT
5144 .load_super(rdev, rdev0, mddev->minor_version);
5145 if (err < 0) {
5146 printk(KERN_WARNING
5147 "md: %s has different UUID to %s\n",
5148 bdevname(rdev->bdev,b),
5149 bdevname(rdev0->bdev,b2));
5150 export_rdev(rdev);
5151 return -EINVAL;
5152 }
5153 }
5154 err = bind_rdev_to_array(rdev, mddev);
5155 if (err)
5156 export_rdev(rdev);
5157 return err;
5158 }
5159
5160 /*
5161 * add_new_disk can be used once the array is assembled
5162 * to add "hot spares". They must already have a superblock
5163 * written
5164 */
5165 if (mddev->pers) {
5166 int err;
5167 if (!mddev->pers->hot_add_disk) {
5168 printk(KERN_WARNING
5169 "%s: personality does not support diskops!\n",
5170 mdname(mddev));
5171 return -EINVAL;
5172 }
7b1e35f6
N
5173 if (mddev->persistent)
5174 rdev = md_import_device(dev, mddev->major_version,
5175 mddev->minor_version);
5176 else
5177 rdev = md_import_device(dev, -1, -1);
1da177e4
LT
5178 if (IS_ERR(rdev)) {
5179 printk(KERN_WARNING
5180 "md: md_import_device returned %ld\n",
5181 PTR_ERR(rdev));
5182 return PTR_ERR(rdev);
5183 }
1a855a06 5184 /* set saved_raid_disk if appropriate */
41158c7e
N
5185 if (!mddev->persistent) {
5186 if (info->state & (1<<MD_DISK_SYNC) &&
bf572541 5187 info->raid_disk < mddev->raid_disks) {
41158c7e 5188 rdev->raid_disk = info->raid_disk;
bf572541
N
5189 set_bit(In_sync, &rdev->flags);
5190 } else
41158c7e
N
5191 rdev->raid_disk = -1;
5192 } else
5193 super_types[mddev->major_version].
5194 validate_super(mddev, rdev);
1a855a06
N
5195 if (test_bit(In_sync, &rdev->flags))
5196 rdev->saved_raid_disk = rdev->raid_disk;
5197 else
5198 rdev->saved_raid_disk = -1;
41158c7e 5199
b2d444d7 5200 clear_bit(In_sync, &rdev->flags); /* just to be sure */
8ddf9efe
N
5201 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5202 set_bit(WriteMostly, &rdev->flags);
575a80fa
N
5203 else
5204 clear_bit(WriteMostly, &rdev->flags);
8ddf9efe 5205
1da177e4
LT
5206 rdev->raid_disk = -1;
5207 err = bind_rdev_to_array(rdev, mddev);
7c7546cc
N
5208 if (!err && !mddev->pers->hot_remove_disk) {
5209 /* If there is hot_add_disk but no hot_remove_disk
5210 * then added disks for geometry changes,
5211 * and should be added immediately.
5212 */
5213 super_types[mddev->major_version].
5214 validate_super(mddev, rdev);
5215 err = mddev->pers->hot_add_disk(mddev, rdev);
5216 if (err)
5217 unbind_rdev_from_array(rdev);
5218 }
1da177e4
LT
5219 if (err)
5220 export_rdev(rdev);
52664732 5221 else
00bcb4ac 5222 sysfs_notify_dirent_safe(rdev->sysfs_state);
c361777f 5223
17571284 5224 md_update_sb(mddev, 1);
72a23c21
NB
5225 if (mddev->degraded)
5226 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
c361777f 5227 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
005eca5e 5228 md_wakeup_thread(mddev->thread);
1da177e4
LT
5229 return err;
5230 }
5231
5232 /* otherwise, add_new_disk is only allowed
5233 * for major_version==0 superblocks
5234 */
5235 if (mddev->major_version != 0) {
5236 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5237 mdname(mddev));
5238 return -EINVAL;
5239 }
5240
5241 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5242 int err;
d710e138 5243 rdev = md_import_device(dev, -1, 0);
1da177e4
LT
5244 if (IS_ERR(rdev)) {
5245 printk(KERN_WARNING
5246 "md: error, md_import_device() returned %ld\n",
5247 PTR_ERR(rdev));
5248 return PTR_ERR(rdev);
5249 }
5250 rdev->desc_nr = info->number;
5251 if (info->raid_disk < mddev->raid_disks)
5252 rdev->raid_disk = info->raid_disk;
5253 else
5254 rdev->raid_disk = -1;
5255
1da177e4 5256 if (rdev->raid_disk < mddev->raid_disks)
b2d444d7
N
5257 if (info->state & (1<<MD_DISK_SYNC))
5258 set_bit(In_sync, &rdev->flags);
1da177e4 5259
8ddf9efe
N
5260 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5261 set_bit(WriteMostly, &rdev->flags);
5262
1da177e4
LT
5263 if (!mddev->persistent) {
5264 printk(KERN_INFO "md: nonpersistent superblock ...\n");
77304d2a
MS
5265 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5266 } else
57b2caa3 5267 rdev->sb_start = calc_dev_sboffset(rdev);
8190e754 5268 rdev->sectors = rdev->sb_start;
1da177e4 5269
2bf071bf
N
5270 err = bind_rdev_to_array(rdev, mddev);
5271 if (err) {
5272 export_rdev(rdev);
5273 return err;
5274 }
1da177e4
LT
5275 }
5276
5277 return 0;
5278}
5279
5280static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5281{
5282 char b[BDEVNAME_SIZE];
5283 mdk_rdev_t *rdev;
5284
1da177e4
LT
5285 rdev = find_rdev(mddev, dev);
5286 if (!rdev)
5287 return -ENXIO;
5288
5289 if (rdev->raid_disk >= 0)
5290 goto busy;
5291
5292 kick_rdev_from_array(rdev);
850b2b42 5293 md_update_sb(mddev, 1);
d7603b7e 5294 md_new_event(mddev);
1da177e4
LT
5295
5296 return 0;
5297busy:
fdefa4d8 5298 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
1da177e4
LT
5299 bdevname(rdev->bdev,b), mdname(mddev));
5300 return -EBUSY;
5301}
5302
5303static int hot_add_disk(mddev_t * mddev, dev_t dev)
5304{
5305 char b[BDEVNAME_SIZE];
5306 int err;
1da177e4
LT
5307 mdk_rdev_t *rdev;
5308
5309 if (!mddev->pers)
5310 return -ENODEV;
5311
5312 if (mddev->major_version != 0) {
5313 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5314 " version-0 superblocks.\n",
5315 mdname(mddev));
5316 return -EINVAL;
5317 }
5318 if (!mddev->pers->hot_add_disk) {
5319 printk(KERN_WARNING
5320 "%s: personality does not support diskops!\n",
5321 mdname(mddev));
5322 return -EINVAL;
5323 }
5324
d710e138 5325 rdev = md_import_device(dev, -1, 0);
1da177e4
LT
5326 if (IS_ERR(rdev)) {
5327 printk(KERN_WARNING
5328 "md: error, md_import_device() returned %ld\n",
5329 PTR_ERR(rdev));
5330 return -EINVAL;
5331 }
5332
5333 if (mddev->persistent)
57b2caa3 5334 rdev->sb_start = calc_dev_sboffset(rdev);
1da177e4 5335 else
77304d2a 5336 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
1da177e4 5337
8190e754 5338 rdev->sectors = rdev->sb_start;
1da177e4 5339
b2d444d7 5340 if (test_bit(Faulty, &rdev->flags)) {
1da177e4
LT
5341 printk(KERN_WARNING
5342 "md: can not hot-add faulty %s disk to %s!\n",
5343 bdevname(rdev->bdev,b), mdname(mddev));
5344 err = -EINVAL;
5345 goto abort_export;
5346 }
b2d444d7 5347 clear_bit(In_sync, &rdev->flags);
1da177e4 5348 rdev->desc_nr = -1;
5842730d 5349 rdev->saved_raid_disk = -1;
2bf071bf
N
5350 err = bind_rdev_to_array(rdev, mddev);
5351 if (err)
5352 goto abort_export;
1da177e4
LT
5353
5354 /*
5355 * The rest should better be atomic, we can have disk failures
5356 * noticed in interrupt contexts ...
5357 */
5358
1da177e4
LT
5359 rdev->raid_disk = -1;
5360
850b2b42 5361 md_update_sb(mddev, 1);
1da177e4
LT
5362
5363 /*
5364 * Kick recovery, maybe this spare has to be added to the
5365 * array immediately.
5366 */
5367 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5368 md_wakeup_thread(mddev->thread);
d7603b7e 5369 md_new_event(mddev);
1da177e4
LT
5370 return 0;
5371
1da177e4
LT
5372abort_export:
5373 export_rdev(rdev);
5374 return err;
5375}
5376
32a7627c
N
5377static int set_bitmap_file(mddev_t *mddev, int fd)
5378{
5379 int err;
5380
36fa3063
N
5381 if (mddev->pers) {
5382 if (!mddev->pers->quiesce)
5383 return -EBUSY;
5384 if (mddev->recovery || mddev->sync_thread)
5385 return -EBUSY;
5386 /* we should be able to change the bitmap.. */
5387 }
32a7627c 5388
32a7627c 5389
36fa3063
N
5390 if (fd >= 0) {
5391 if (mddev->bitmap)
5392 return -EEXIST; /* cannot add when bitmap is present */
c3d9714e 5393 mddev->bitmap_info.file = fget(fd);
32a7627c 5394
c3d9714e 5395 if (mddev->bitmap_info.file == NULL) {
36fa3063
N
5396 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5397 mdname(mddev));
5398 return -EBADF;
5399 }
5400
c3d9714e 5401 err = deny_bitmap_write_access(mddev->bitmap_info.file);
36fa3063
N
5402 if (err) {
5403 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5404 mdname(mddev));
c3d9714e
N
5405 fput(mddev->bitmap_info.file);
5406 mddev->bitmap_info.file = NULL;
36fa3063
N
5407 return err;
5408 }
c3d9714e 5409 mddev->bitmap_info.offset = 0; /* file overrides offset */
36fa3063
N
5410 } else if (mddev->bitmap == NULL)
5411 return -ENOENT; /* cannot remove what isn't there */
5412 err = 0;
5413 if (mddev->pers) {
5414 mddev->pers->quiesce(mddev, 1);
69e51b44 5415 if (fd >= 0) {
36fa3063 5416 err = bitmap_create(mddev);
69e51b44
N
5417 if (!err)
5418 err = bitmap_load(mddev);
5419 }
d7375ab3 5420 if (fd < 0 || err) {
36fa3063 5421 bitmap_destroy(mddev);
d7375ab3
N
5422 fd = -1; /* make sure to put the file */
5423 }
36fa3063 5424 mddev->pers->quiesce(mddev, 0);
d7375ab3
N
5425 }
5426 if (fd < 0) {
c3d9714e
N
5427 if (mddev->bitmap_info.file) {
5428 restore_bitmap_write_access(mddev->bitmap_info.file);
5429 fput(mddev->bitmap_info.file);
acc55e22 5430 }
c3d9714e 5431 mddev->bitmap_info.file = NULL;
36fa3063
N
5432 }
5433
32a7627c
N
5434 return err;
5435}
5436
1da177e4
LT
5437/*
5438 * set_array_info is used two different ways
5439 * The original usage is when creating a new array.
5440 * In this usage, raid_disks is > 0 and it together with
5441 * level, size, not_persistent,layout,chunksize determine the
5442 * shape of the array.
5443 * This will always create an array with a type-0.90.0 superblock.
5444 * The newer usage is when assembling an array.
5445 * In this case raid_disks will be 0, and the major_version field is
5446 * use to determine which style super-blocks are to be found on the devices.
5447 * The minor and patch _version numbers are also kept incase the
5448 * super_block handler wishes to interpret them.
5449 */
5450static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5451{
5452
5453 if (info->raid_disks == 0) {
5454 /* just setting version number for superblock loading */
5455 if (info->major_version < 0 ||
50511da3 5456 info->major_version >= ARRAY_SIZE(super_types) ||
1da177e4
LT
5457 super_types[info->major_version].name == NULL) {
5458 /* maybe try to auto-load a module? */
5459 printk(KERN_INFO
5460 "md: superblock version %d not known\n",
5461 info->major_version);
5462 return -EINVAL;
5463 }
5464 mddev->major_version = info->major_version;
5465 mddev->minor_version = info->minor_version;
5466 mddev->patch_version = info->patch_version;
3f9d7b0d 5467 mddev->persistent = !info->not_persistent;
cbd19983
N
5468 /* ensure mddev_put doesn't delete this now that there
5469 * is some minimal configuration.
5470 */
5471 mddev->ctime = get_seconds();
1da177e4
LT
5472 return 0;
5473 }
5474 mddev->major_version = MD_MAJOR_VERSION;
5475 mddev->minor_version = MD_MINOR_VERSION;
5476 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5477 mddev->ctime = get_seconds();
5478
5479 mddev->level = info->level;
17115e03 5480 mddev->clevel[0] = 0;
58c0fed4 5481 mddev->dev_sectors = 2 * (sector_t)info->size;
1da177e4
LT
5482 mddev->raid_disks = info->raid_disks;
5483 /* don't set md_minor, it is determined by which /dev/md* was
5484 * openned
5485 */
5486 if (info->state & (1<<MD_SB_CLEAN))
5487 mddev->recovery_cp = MaxSector;
5488 else
5489 mddev->recovery_cp = 0;
5490 mddev->persistent = ! info->not_persistent;
e691063a 5491 mddev->external = 0;
1da177e4
LT
5492
5493 mddev->layout = info->layout;
9d8f0363 5494 mddev->chunk_sectors = info->chunk_size >> 9;
1da177e4
LT
5495
5496 mddev->max_disks = MD_SB_DISKS;
5497
e691063a
N
5498 if (mddev->persistent)
5499 mddev->flags = 0;
850b2b42 5500 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1da177e4 5501
c3d9714e
N
5502 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5503 mddev->bitmap_info.offset = 0;
b2a2703c 5504
f6705578
N
5505 mddev->reshape_position = MaxSector;
5506
1da177e4
LT
5507 /*
5508 * Generate a 128 bit UUID
5509 */
5510 get_random_bytes(mddev->uuid, 16);
5511
f6705578 5512 mddev->new_level = mddev->level;
664e7c41 5513 mddev->new_chunk_sectors = mddev->chunk_sectors;
f6705578
N
5514 mddev->new_layout = mddev->layout;
5515 mddev->delta_disks = 0;
5516
1da177e4
LT
5517 return 0;
5518}
5519
1f403624
DW
5520void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5521{
b522adcd
DW
5522 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5523
5524 if (mddev->external_size)
5525 return;
5526
1f403624
DW
5527 mddev->array_sectors = array_sectors;
5528}
5529EXPORT_SYMBOL(md_set_array_sectors);
5530
d71f9f88 5531static int update_size(mddev_t *mddev, sector_t num_sectors)
a35b0d69 5532{
159ec1fc 5533 mdk_rdev_t *rdev;
a35b0d69 5534 int rv;
d71f9f88 5535 int fit = (num_sectors == 0);
a35b0d69
N
5536
5537 if (mddev->pers->resize == NULL)
5538 return -EINVAL;
d71f9f88
AN
5539 /* The "num_sectors" is the number of sectors of each device that
5540 * is used. This can only make sense for arrays with redundancy.
5541 * linear and raid0 always use whatever space is available. We can only
5542 * consider changing this number if no resync or reconstruction is
5543 * happening, and if the new size is acceptable. It must fit before the
0f420358 5544 * sb_start or, if that is <data_offset, it must fit before the size
d71f9f88
AN
5545 * of each device. If num_sectors is zero, we find the largest size
5546 * that fits.
a35b0d69
N
5547 */
5548 if (mddev->sync_thread)
5549 return -EBUSY;
dba034ee
N
5550 if (mddev->bitmap)
5551 /* Sorry, cannot grow a bitmap yet, just remove it,
5552 * grow, and re-add.
5553 */
5554 return -EBUSY;
159ec1fc 5555 list_for_each_entry(rdev, &mddev->disks, same_set) {
dd8ac336 5556 sector_t avail = rdev->sectors;
01ab5662 5557
d71f9f88
AN
5558 if (fit && (num_sectors == 0 || num_sectors > avail))
5559 num_sectors = avail;
5560 if (avail < num_sectors)
a35b0d69
N
5561 return -ENOSPC;
5562 }
d71f9f88 5563 rv = mddev->pers->resize(mddev, num_sectors);
449aad3e
N
5564 if (!rv)
5565 revalidate_disk(mddev->gendisk);
a35b0d69
N
5566 return rv;
5567}
5568
da943b99
N
5569static int update_raid_disks(mddev_t *mddev, int raid_disks)
5570{
5571 int rv;
5572 /* change the number of raid disks */
63c70c4f 5573 if (mddev->pers->check_reshape == NULL)
da943b99
N
5574 return -EINVAL;
5575 if (raid_disks <= 0 ||
233fca36 5576 (mddev->max_disks && raid_disks >= mddev->max_disks))
da943b99 5577 return -EINVAL;
63c70c4f 5578 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
da943b99 5579 return -EBUSY;
63c70c4f
N
5580 mddev->delta_disks = raid_disks - mddev->raid_disks;
5581
5582 rv = mddev->pers->check_reshape(mddev);
da943b99
N
5583 return rv;
5584}
5585
5586
1da177e4
LT
5587/*
5588 * update_array_info is used to change the configuration of an
5589 * on-line array.
5590 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5591 * fields in the info are checked against the array.
5592 * Any differences that cannot be handled will cause an error.
5593 * Normally, only one change can be managed at a time.
5594 */
5595static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5596{
5597 int rv = 0;
5598 int cnt = 0;
36fa3063
N
5599 int state = 0;
5600
5601 /* calculate expected state,ignoring low bits */
c3d9714e 5602 if (mddev->bitmap && mddev->bitmap_info.offset)
36fa3063 5603 state |= (1 << MD_SB_BITMAP_PRESENT);
1da177e4
LT
5604
5605 if (mddev->major_version != info->major_version ||
5606 mddev->minor_version != info->minor_version ||
5607/* mddev->patch_version != info->patch_version || */
5608 mddev->ctime != info->ctime ||
5609 mddev->level != info->level ||
5610/* mddev->layout != info->layout || */
5611 !mddev->persistent != info->not_persistent||
9d8f0363 5612 mddev->chunk_sectors != info->chunk_size >> 9 ||
36fa3063
N
5613 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5614 ((state^info->state) & 0xfffffe00)
5615 )
1da177e4
LT
5616 return -EINVAL;
5617 /* Check there is only one change */
58c0fed4
AN
5618 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5619 cnt++;
5620 if (mddev->raid_disks != info->raid_disks)
5621 cnt++;
5622 if (mddev->layout != info->layout)
5623 cnt++;
5624 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5625 cnt++;
5626 if (cnt == 0)
5627 return 0;
5628 if (cnt > 1)
5629 return -EINVAL;
1da177e4
LT
5630
5631 if (mddev->layout != info->layout) {
5632 /* Change layout
5633 * we don't need to do anything at the md level, the
5634 * personality will take care of it all.
5635 */
50ac168a 5636 if (mddev->pers->check_reshape == NULL)
1da177e4 5637 return -EINVAL;
597a711b
N
5638 else {
5639 mddev->new_layout = info->layout;
50ac168a 5640 rv = mddev->pers->check_reshape(mddev);
597a711b
N
5641 if (rv)
5642 mddev->new_layout = mddev->layout;
5643 return rv;
5644 }
1da177e4 5645 }
58c0fed4 5646 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
d71f9f88 5647 rv = update_size(mddev, (sector_t)info->size * 2);
a35b0d69 5648
da943b99
N
5649 if (mddev->raid_disks != info->raid_disks)
5650 rv = update_raid_disks(mddev, info->raid_disks);
5651
36fa3063
N
5652 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5653 if (mddev->pers->quiesce == NULL)
5654 return -EINVAL;
5655 if (mddev->recovery || mddev->sync_thread)
5656 return -EBUSY;
5657 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5658 /* add the bitmap */
5659 if (mddev->bitmap)
5660 return -EEXIST;
c3d9714e 5661 if (mddev->bitmap_info.default_offset == 0)
36fa3063 5662 return -EINVAL;
c3d9714e
N
5663 mddev->bitmap_info.offset =
5664 mddev->bitmap_info.default_offset;
36fa3063
N
5665 mddev->pers->quiesce(mddev, 1);
5666 rv = bitmap_create(mddev);
69e51b44
N
5667 if (!rv)
5668 rv = bitmap_load(mddev);
36fa3063
N
5669 if (rv)
5670 bitmap_destroy(mddev);
5671 mddev->pers->quiesce(mddev, 0);
5672 } else {
5673 /* remove the bitmap */
5674 if (!mddev->bitmap)
5675 return -ENOENT;
5676 if (mddev->bitmap->file)
5677 return -EINVAL;
5678 mddev->pers->quiesce(mddev, 1);
5679 bitmap_destroy(mddev);
5680 mddev->pers->quiesce(mddev, 0);
c3d9714e 5681 mddev->bitmap_info.offset = 0;
36fa3063
N
5682 }
5683 }
850b2b42 5684 md_update_sb(mddev, 1);
1da177e4
LT
5685 return rv;
5686}
5687
5688static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5689{
5690 mdk_rdev_t *rdev;
5691
5692 if (mddev->pers == NULL)
5693 return -ENODEV;
5694
5695 rdev = find_rdev(mddev, dev);
5696 if (!rdev)
5697 return -ENODEV;
5698
5699 md_error(mddev, rdev);
5700 return 0;
5701}
5702
2f9618ce
AN
5703/*
5704 * We have a problem here : there is no easy way to give a CHS
5705 * virtual geometry. We currently pretend that we have a 2 heads
5706 * 4 sectors (with a BIG number of cylinders...). This drives
5707 * dosfs just mad... ;-)
5708 */
a885c8c4
CH
5709static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5710{
5711 mddev_t *mddev = bdev->bd_disk->private_data;
5712
5713 geo->heads = 2;
5714 geo->sectors = 4;
49ce6cea 5715 geo->cylinders = mddev->array_sectors / 8;
a885c8c4
CH
5716 return 0;
5717}
5718
a39907fa 5719static int md_ioctl(struct block_device *bdev, fmode_t mode,
1da177e4
LT
5720 unsigned int cmd, unsigned long arg)
5721{
5722 int err = 0;
5723 void __user *argp = (void __user *)arg;
1da177e4 5724 mddev_t *mddev = NULL;
e2218350 5725 int ro;
1da177e4
LT
5726
5727 if (!capable(CAP_SYS_ADMIN))
5728 return -EACCES;
5729
5730 /*
5731 * Commands dealing with the RAID driver but not any
5732 * particular array:
5733 */
5734 switch (cmd)
5735 {
5736 case RAID_VERSION:
5737 err = get_version(argp);
5738 goto done;
5739
5740 case PRINT_RAID_DEBUG:
5741 err = 0;
5742 md_print_devices();
5743 goto done;
5744
5745#ifndef MODULE
5746 case RAID_AUTORUN:
5747 err = 0;
5748 autostart_arrays(arg);
5749 goto done;
5750#endif
5751 default:;
5752 }
5753
5754 /*
5755 * Commands creating/starting a new array:
5756 */
5757
a39907fa 5758 mddev = bdev->bd_disk->private_data;
1da177e4
LT
5759
5760 if (!mddev) {
5761 BUG();
5762 goto abort;
5763 }
5764
1da177e4
LT
5765 err = mddev_lock(mddev);
5766 if (err) {
5767 printk(KERN_INFO
5768 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5769 err, cmd);
5770 goto abort;
5771 }
5772
5773 switch (cmd)
5774 {
5775 case SET_ARRAY_INFO:
5776 {
5777 mdu_array_info_t info;
5778 if (!arg)
5779 memset(&info, 0, sizeof(info));
5780 else if (copy_from_user(&info, argp, sizeof(info))) {
5781 err = -EFAULT;
5782 goto abort_unlock;
5783 }
5784 if (mddev->pers) {
5785 err = update_array_info(mddev, &info);
5786 if (err) {
5787 printk(KERN_WARNING "md: couldn't update"
5788 " array info. %d\n", err);
5789 goto abort_unlock;
5790 }
5791 goto done_unlock;
5792 }
5793 if (!list_empty(&mddev->disks)) {
5794 printk(KERN_WARNING
5795 "md: array %s already has disks!\n",
5796 mdname(mddev));
5797 err = -EBUSY;
5798 goto abort_unlock;
5799 }
5800 if (mddev->raid_disks) {
5801 printk(KERN_WARNING
5802 "md: array %s already initialised!\n",
5803 mdname(mddev));
5804 err = -EBUSY;
5805 goto abort_unlock;
5806 }
5807 err = set_array_info(mddev, &info);
5808 if (err) {
5809 printk(KERN_WARNING "md: couldn't set"
5810 " array info. %d\n", err);
5811 goto abort_unlock;
5812 }
5813 }
5814 goto done_unlock;
5815
5816 default:;
5817 }
5818
5819 /*
5820 * Commands querying/configuring an existing array:
5821 */
32a7627c 5822 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3f9d7b0d 5823 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
a17184a9
N
5824 if ((!mddev->raid_disks && !mddev->external)
5825 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5826 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5827 && cmd != GET_BITMAP_FILE) {
1da177e4
LT
5828 err = -ENODEV;
5829 goto abort_unlock;
5830 }
5831
5832 /*
5833 * Commands even a read-only array can execute:
5834 */
5835 switch (cmd)
5836 {
5837 case GET_ARRAY_INFO:
5838 err = get_array_info(mddev, argp);
5839 goto done_unlock;
5840
32a7627c 5841 case GET_BITMAP_FILE:
87162a28 5842 err = get_bitmap_file(mddev, argp);
32a7627c
N
5843 goto done_unlock;
5844
1da177e4
LT
5845 case GET_DISK_INFO:
5846 err = get_disk_info(mddev, argp);
5847 goto done_unlock;
5848
5849 case RESTART_ARRAY_RW:
5850 err = restart_array(mddev);
5851 goto done_unlock;
5852
5853 case STOP_ARRAY:
d710e138 5854 err = do_md_stop(mddev, 0, 1);
1da177e4
LT
5855 goto done_unlock;
5856
5857 case STOP_ARRAY_RO:
a4bd82d0 5858 err = md_set_readonly(mddev, 1);
1da177e4
LT
5859 goto done_unlock;
5860
e2218350
DW
5861 case BLKROSET:
5862 if (get_user(ro, (int __user *)(arg))) {
5863 err = -EFAULT;
5864 goto done_unlock;
5865 }
5866 err = -EINVAL;
5867
5868 /* if the bdev is going readonly the value of mddev->ro
5869 * does not matter, no writes are coming
5870 */
5871 if (ro)
5872 goto done_unlock;
5873
5874 /* are we are already prepared for writes? */
5875 if (mddev->ro != 1)
5876 goto done_unlock;
5877
5878 /* transitioning to readauto need only happen for
5879 * arrays that call md_write_start
5880 */
5881 if (mddev->pers) {
5882 err = restart_array(mddev);
5883 if (err == 0) {
5884 mddev->ro = 2;
5885 set_disk_ro(mddev->gendisk, 0);
5886 }
5887 }
5888 goto done_unlock;
1da177e4
LT
5889 }
5890
5891 /*
5892 * The remaining ioctls are changing the state of the
f91de92e
N
5893 * superblock, so we do not allow them on read-only arrays.
5894 * However non-MD ioctls (e.g. get-size) will still come through
5895 * here and hit the 'default' below, so only disallow
5896 * 'md' ioctls, and switch to rw mode if started auto-readonly.
1da177e4 5897 */
bb57fc64 5898 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
f91de92e
N
5899 if (mddev->ro == 2) {
5900 mddev->ro = 0;
00bcb4ac 5901 sysfs_notify_dirent_safe(mddev->sysfs_state);
0fd62b86
NB
5902 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5903 md_wakeup_thread(mddev->thread);
f91de92e
N
5904 } else {
5905 err = -EROFS;
5906 goto abort_unlock;
5907 }
1da177e4
LT
5908 }
5909
5910 switch (cmd)
5911 {
5912 case ADD_NEW_DISK:
5913 {
5914 mdu_disk_info_t info;
5915 if (copy_from_user(&info, argp, sizeof(info)))
5916 err = -EFAULT;
5917 else
5918 err = add_new_disk(mddev, &info);
5919 goto done_unlock;
5920 }
5921
5922 case HOT_REMOVE_DISK:
5923 err = hot_remove_disk(mddev, new_decode_dev(arg));
5924 goto done_unlock;
5925
5926 case HOT_ADD_DISK:
5927 err = hot_add_disk(mddev, new_decode_dev(arg));
5928 goto done_unlock;
5929
5930 case SET_DISK_FAULTY:
5931 err = set_disk_faulty(mddev, new_decode_dev(arg));
5932 goto done_unlock;
5933
5934 case RUN_ARRAY:
d710e138 5935 err = do_md_run(mddev);
1da177e4
LT
5936 goto done_unlock;
5937
32a7627c
N
5938 case SET_BITMAP_FILE:
5939 err = set_bitmap_file(mddev, (int)arg);
5940 goto done_unlock;
5941
1da177e4 5942 default:
1da177e4
LT
5943 err = -EINVAL;
5944 goto abort_unlock;
5945 }
5946
5947done_unlock:
5948abort_unlock:
d3374825
N
5949 if (mddev->hold_active == UNTIL_IOCTL &&
5950 err != -EINVAL)
5951 mddev->hold_active = 0;
1da177e4
LT
5952 mddev_unlock(mddev);
5953
5954 return err;
5955done:
5956 if (err)
5957 MD_BUG();
5958abort:
5959 return err;
5960}
aa98aa31
AB
5961#ifdef CONFIG_COMPAT
5962static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5963 unsigned int cmd, unsigned long arg)
5964{
5965 switch (cmd) {
5966 case HOT_REMOVE_DISK:
5967 case HOT_ADD_DISK:
5968 case SET_DISK_FAULTY:
5969 case SET_BITMAP_FILE:
5970 /* These take in integer arg, do not convert */
5971 break;
5972 default:
5973 arg = (unsigned long)compat_ptr(arg);
5974 break;
5975 }
5976
5977 return md_ioctl(bdev, mode, cmd, arg);
5978}
5979#endif /* CONFIG_COMPAT */
1da177e4 5980
a39907fa 5981static int md_open(struct block_device *bdev, fmode_t mode)
1da177e4
LT
5982{
5983 /*
5984 * Succeed if we can lock the mddev, which confirms that
5985 * it isn't being stopped right now.
5986 */
d3374825 5987 mddev_t *mddev = mddev_find(bdev->bd_dev);
1da177e4
LT
5988 int err;
5989
d3374825
N
5990 if (mddev->gendisk != bdev->bd_disk) {
5991 /* we are racing with mddev_put which is discarding this
5992 * bd_disk.
5993 */
5994 mddev_put(mddev);
5995 /* Wait until bdev->bd_disk is definitely gone */
e804ac78 5996 flush_workqueue(md_misc_wq);
d3374825
N
5997 /* Then retry the open from the top */
5998 return -ERESTARTSYS;
5999 }
6000 BUG_ON(mddev != bdev->bd_disk->private_data);
6001
c8c00a69 6002 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
1da177e4
LT
6003 goto out;
6004
6005 err = 0;
f2ea68cf 6006 atomic_inc(&mddev->openers);
c8c00a69 6007 mutex_unlock(&mddev->open_mutex);
1da177e4 6008
f3b99be1 6009 check_disk_size_change(mddev->gendisk, bdev);
1da177e4
LT
6010 out:
6011 return err;
6012}
6013
a39907fa 6014static int md_release(struct gendisk *disk, fmode_t mode)
1da177e4 6015{
a39907fa 6016 mddev_t *mddev = disk->private_data;
1da177e4 6017
52e5f9d1 6018 BUG_ON(!mddev);
f2ea68cf 6019 atomic_dec(&mddev->openers);
1da177e4
LT
6020 mddev_put(mddev);
6021
6022 return 0;
6023}
83d5cde4 6024static const struct block_device_operations md_fops =
1da177e4
LT
6025{
6026 .owner = THIS_MODULE,
a39907fa
AV
6027 .open = md_open,
6028 .release = md_release,
b492b852 6029 .ioctl = md_ioctl,
aa98aa31
AB
6030#ifdef CONFIG_COMPAT
6031 .compat_ioctl = md_compat_ioctl,
6032#endif
a885c8c4 6033 .getgeo = md_getgeo,
1da177e4
LT
6034};
6035
75c96f85 6036static int md_thread(void * arg)
1da177e4
LT
6037{
6038 mdk_thread_t *thread = arg;
6039
1da177e4
LT
6040 /*
6041 * md_thread is a 'system-thread', it's priority should be very
6042 * high. We avoid resource deadlocks individually in each
6043 * raid personality. (RAID5 does preallocation) We also use RR and
6044 * the very same RT priority as kswapd, thus we will never get
6045 * into a priority inversion deadlock.
6046 *
6047 * we definitely have to have equal or higher priority than
6048 * bdflush, otherwise bdflush will deadlock if there are too
6049 * many dirty RAID5 blocks.
6050 */
1da177e4 6051
6985c43f 6052 allow_signal(SIGKILL);
a6fb0934 6053 while (!kthread_should_stop()) {
1da177e4 6054
93588e22
N
6055 /* We need to wait INTERRUPTIBLE so that
6056 * we don't add to the load-average.
6057 * That means we need to be sure no signals are
6058 * pending
6059 */
6060 if (signal_pending(current))
6061 flush_signals(current);
6062
6063 wait_event_interruptible_timeout
6064 (thread->wqueue,
6065 test_bit(THREAD_WAKEUP, &thread->flags)
6066 || kthread_should_stop(),
6067 thread->timeout);
1da177e4 6068
6c987910
N
6069 clear_bit(THREAD_WAKEUP, &thread->flags);
6070 if (!kthread_should_stop())
589a594b 6071 thread->run(thread->mddev);
1da177e4 6072 }
a6fb0934 6073
1da177e4
LT
6074 return 0;
6075}
6076
6077void md_wakeup_thread(mdk_thread_t *thread)
6078{
6079 if (thread) {
6080 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6081 set_bit(THREAD_WAKEUP, &thread->flags);
6082 wake_up(&thread->wqueue);
6083 }
6084}
6085
6086mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6087 const char *name)
6088{
6089 mdk_thread_t *thread;
1da177e4 6090
9ffae0cf 6091 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
1da177e4
LT
6092 if (!thread)
6093 return NULL;
6094
1da177e4
LT
6095 init_waitqueue_head(&thread->wqueue);
6096
1da177e4
LT
6097 thread->run = run;
6098 thread->mddev = mddev;
32a7627c 6099 thread->timeout = MAX_SCHEDULE_TIMEOUT;
0da3c619
N
6100 thread->tsk = kthread_run(md_thread, thread,
6101 "%s_%s",
6102 mdname(thread->mddev),
6103 name ?: mddev->pers->name);
a6fb0934 6104 if (IS_ERR(thread->tsk)) {
1da177e4
LT
6105 kfree(thread);
6106 return NULL;
6107 }
1da177e4
LT
6108 return thread;
6109}
6110
1da177e4
LT
6111void md_unregister_thread(mdk_thread_t *thread)
6112{
e0cf8f04
N
6113 if (!thread)
6114 return;
ba25f9dc 6115 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
a6fb0934
N
6116
6117 kthread_stop(thread->tsk);
1da177e4
LT
6118 kfree(thread);
6119}
6120
6121void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6122{
6123 if (!mddev) {
6124 MD_BUG();
6125 return;
6126 }
6127
b2d444d7 6128 if (!rdev || test_bit(Faulty, &rdev->flags))
1da177e4 6129 return;
6bfe0b49
DW
6130
6131 if (mddev->external)
6132 set_bit(Blocked, &rdev->flags);
32a7627c 6133/*
1da177e4
LT
6134 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6135 mdname(mddev),
6136 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6137 __builtin_return_address(0),__builtin_return_address(1),
6138 __builtin_return_address(2),__builtin_return_address(3));
32a7627c 6139*/
d0a0a5ee
AM
6140 if (!mddev->pers)
6141 return;
1da177e4
LT
6142 if (!mddev->pers->error_handler)
6143 return;
6144 mddev->pers->error_handler(mddev,rdev);
72a23c21
NB
6145 if (mddev->degraded)
6146 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
00bcb4ac 6147 sysfs_notify_dirent_safe(rdev->sysfs_state);
1da177e4
LT
6148 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6149 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6150 md_wakeup_thread(mddev->thread);
768a418d 6151 if (mddev->event_work.func)
e804ac78 6152 queue_work(md_misc_wq, &mddev->event_work);
c331eb04 6153 md_new_event_inintr(mddev);
1da177e4
LT
6154}
6155
6156/* seq_file implementation /proc/mdstat */
6157
6158static void status_unused(struct seq_file *seq)
6159{
6160 int i = 0;
6161 mdk_rdev_t *rdev;
1da177e4
LT
6162
6163 seq_printf(seq, "unused devices: ");
6164
159ec1fc 6165 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
1da177e4
LT
6166 char b[BDEVNAME_SIZE];
6167 i++;
6168 seq_printf(seq, "%s ",
6169 bdevname(rdev->bdev,b));
6170 }
6171 if (!i)
6172 seq_printf(seq, "<none>");
6173
6174 seq_printf(seq, "\n");
6175}
6176
6177
6178static void status_resync(struct seq_file *seq, mddev_t * mddev)
6179{
dd71cf6b
N
6180 sector_t max_sectors, resync, res;
6181 unsigned long dt, db;
6182 sector_t rt;
4588b42e
N
6183 int scale;
6184 unsigned int per_milli;
1da177e4 6185
dd71cf6b 6186 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
1da177e4
LT
6187
6188 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
dd71cf6b 6189 max_sectors = mddev->resync_max_sectors;
1da177e4 6190 else
dd71cf6b 6191 max_sectors = mddev->dev_sectors;
1da177e4
LT
6192
6193 /*
6194 * Should not happen.
6195 */
dd71cf6b 6196 if (!max_sectors) {
1da177e4
LT
6197 MD_BUG();
6198 return;
6199 }
4588b42e 6200 /* Pick 'scale' such that (resync>>scale)*1000 will fit
dd71cf6b 6201 * in a sector_t, and (max_sectors>>scale) will fit in a
4588b42e
N
6202 * u32, as those are the requirements for sector_div.
6203 * Thus 'scale' must be at least 10
6204 */
6205 scale = 10;
6206 if (sizeof(sector_t) > sizeof(unsigned long)) {
dd71cf6b 6207 while ( max_sectors/2 > (1ULL<<(scale+32)))
4588b42e
N
6208 scale++;
6209 }
6210 res = (resync>>scale)*1000;
dd71cf6b 6211 sector_div(res, (u32)((max_sectors>>scale)+1));
4588b42e
N
6212
6213 per_milli = res;
1da177e4 6214 {
4588b42e 6215 int i, x = per_milli/50, y = 20-x;
1da177e4
LT
6216 seq_printf(seq, "[");
6217 for (i = 0; i < x; i++)
6218 seq_printf(seq, "=");
6219 seq_printf(seq, ">");
6220 for (i = 0; i < y; i++)
6221 seq_printf(seq, ".");
6222 seq_printf(seq, "] ");
6223 }
4588b42e 6224 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
ccfcc3c1
N
6225 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6226 "reshape" :
61df9d91
N
6227 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6228 "check" :
6229 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6230 "resync" : "recovery"))),
6231 per_milli/10, per_milli % 10,
dd71cf6b
N
6232 (unsigned long long) resync/2,
6233 (unsigned long long) max_sectors/2);
1da177e4
LT
6234
6235 /*
1da177e4
LT
6236 * dt: time from mark until now
6237 * db: blocks written from mark until now
6238 * rt: remaining time
dd71cf6b
N
6239 *
6240 * rt is a sector_t, so could be 32bit or 64bit.
6241 * So we divide before multiply in case it is 32bit and close
6242 * to the limit.
6243 * We scale the divisor (db) by 32 to avoid loosing precision
6244 * near the end of resync when the number of remaining sectors
6245 * is close to 'db'.
6246 * We then divide rt by 32 after multiplying by db to compensate.
6247 * The '+1' avoids division by zero if db is very small.
1da177e4
LT
6248 */
6249 dt = ((jiffies - mddev->resync_mark) / HZ);
6250 if (!dt) dt++;
ff4e8d9a
N
6251 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6252 - mddev->resync_mark_cnt;
1da177e4 6253
dd71cf6b
N
6254 rt = max_sectors - resync; /* number of remaining sectors */
6255 sector_div(rt, db/32+1);
6256 rt *= dt;
6257 rt >>= 5;
6258
6259 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6260 ((unsigned long)rt % 60)/6);
1da177e4 6261
ff4e8d9a 6262 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
1da177e4
LT
6263}
6264
6265static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6266{
6267 struct list_head *tmp;
6268 loff_t l = *pos;
6269 mddev_t *mddev;
6270
6271 if (l >= 0x10000)
6272 return NULL;
6273 if (!l--)
6274 /* header */
6275 return (void*)1;
6276
6277 spin_lock(&all_mddevs_lock);
6278 list_for_each(tmp,&all_mddevs)
6279 if (!l--) {
6280 mddev = list_entry(tmp, mddev_t, all_mddevs);
6281 mddev_get(mddev);
6282 spin_unlock(&all_mddevs_lock);
6283 return mddev;
6284 }
6285 spin_unlock(&all_mddevs_lock);
6286 if (!l--)
6287 return (void*)2;/* tail */
6288 return NULL;
6289}
6290
6291static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6292{
6293 struct list_head *tmp;
6294 mddev_t *next_mddev, *mddev = v;
6295
6296 ++*pos;
6297 if (v == (void*)2)
6298 return NULL;
6299
6300 spin_lock(&all_mddevs_lock);
6301 if (v == (void*)1)
6302 tmp = all_mddevs.next;
6303 else
6304 tmp = mddev->all_mddevs.next;
6305 if (tmp != &all_mddevs)
6306 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6307 else {
6308 next_mddev = (void*)2;
6309 *pos = 0x10000;
6310 }
6311 spin_unlock(&all_mddevs_lock);
6312
6313 if (v != (void*)1)
6314 mddev_put(mddev);
6315 return next_mddev;
6316
6317}
6318
6319static void md_seq_stop(struct seq_file *seq, void *v)
6320{
6321 mddev_t *mddev = v;
6322
6323 if (mddev && v != (void*)1 && v != (void*)2)
6324 mddev_put(mddev);
6325}
6326
d7603b7e
N
6327struct mdstat_info {
6328 int event;
6329};
6330
1da177e4
LT
6331static int md_seq_show(struct seq_file *seq, void *v)
6332{
6333 mddev_t *mddev = v;
dd8ac336 6334 sector_t sectors;
1da177e4 6335 mdk_rdev_t *rdev;
d7603b7e 6336 struct mdstat_info *mi = seq->private;
32a7627c 6337 struct bitmap *bitmap;
1da177e4
LT
6338
6339 if (v == (void*)1) {
2604b703 6340 struct mdk_personality *pers;
1da177e4
LT
6341 seq_printf(seq, "Personalities : ");
6342 spin_lock(&pers_lock);
2604b703
N
6343 list_for_each_entry(pers, &pers_list, list)
6344 seq_printf(seq, "[%s] ", pers->name);
1da177e4
LT
6345
6346 spin_unlock(&pers_lock);
6347 seq_printf(seq, "\n");
d7603b7e 6348 mi->event = atomic_read(&md_event_count);
1da177e4
LT
6349 return 0;
6350 }
6351 if (v == (void*)2) {
6352 status_unused(seq);
6353 return 0;
6354 }
6355
5dc5cf7d 6356 if (mddev_lock(mddev) < 0)
1da177e4 6357 return -EINTR;
5dc5cf7d 6358
1da177e4
LT
6359 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6360 seq_printf(seq, "%s : %sactive", mdname(mddev),
6361 mddev->pers ? "" : "in");
6362 if (mddev->pers) {
f91de92e 6363 if (mddev->ro==1)
1da177e4 6364 seq_printf(seq, " (read-only)");
f91de92e 6365 if (mddev->ro==2)
52720ae7 6366 seq_printf(seq, " (auto-read-only)");
1da177e4
LT
6367 seq_printf(seq, " %s", mddev->pers->name);
6368 }
6369
dd8ac336 6370 sectors = 0;
159ec1fc 6371 list_for_each_entry(rdev, &mddev->disks, same_set) {
1da177e4
LT
6372 char b[BDEVNAME_SIZE];
6373 seq_printf(seq, " %s[%d]",
6374 bdevname(rdev->bdev,b), rdev->desc_nr);
8ddf9efe
N
6375 if (test_bit(WriteMostly, &rdev->flags))
6376 seq_printf(seq, "(W)");
b2d444d7 6377 if (test_bit(Faulty, &rdev->flags)) {
1da177e4
LT
6378 seq_printf(seq, "(F)");
6379 continue;
b325a32e
N
6380 } else if (rdev->raid_disk < 0)
6381 seq_printf(seq, "(S)"); /* spare */
dd8ac336 6382 sectors += rdev->sectors;
1da177e4
LT
6383 }
6384
6385 if (!list_empty(&mddev->disks)) {
6386 if (mddev->pers)
6387 seq_printf(seq, "\n %llu blocks",
f233ea5c
AN
6388 (unsigned long long)
6389 mddev->array_sectors / 2);
1da177e4
LT
6390 else
6391 seq_printf(seq, "\n %llu blocks",
dd8ac336 6392 (unsigned long long)sectors / 2);
1da177e4 6393 }
1cd6bf19
N
6394 if (mddev->persistent) {
6395 if (mddev->major_version != 0 ||
6396 mddev->minor_version != 90) {
6397 seq_printf(seq," super %d.%d",
6398 mddev->major_version,
6399 mddev->minor_version);
6400 }
e691063a
N
6401 } else if (mddev->external)
6402 seq_printf(seq, " super external:%s",
6403 mddev->metadata_type);
6404 else
1cd6bf19 6405 seq_printf(seq, " super non-persistent");
1da177e4
LT
6406
6407 if (mddev->pers) {
d710e138 6408 mddev->pers->status(seq, mddev);
1da177e4 6409 seq_printf(seq, "\n ");
8e1b39d6
N
6410 if (mddev->pers->sync_request) {
6411 if (mddev->curr_resync > 2) {
d710e138 6412 status_resync(seq, mddev);
8e1b39d6
N
6413 seq_printf(seq, "\n ");
6414 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6415 seq_printf(seq, "\tresync=DELAYED\n ");
6416 else if (mddev->recovery_cp < MaxSector)
6417 seq_printf(seq, "\tresync=PENDING\n ");
6418 }
32a7627c
N
6419 } else
6420 seq_printf(seq, "\n ");
6421
6422 if ((bitmap = mddev->bitmap)) {
32a7627c
N
6423 unsigned long chunk_kb;
6424 unsigned long flags;
32a7627c 6425 spin_lock_irqsave(&bitmap->lock, flags);
42a04b50 6426 chunk_kb = mddev->bitmap_info.chunksize >> 10;
32a7627c
N
6427 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6428 "%lu%s chunk",
6429 bitmap->pages - bitmap->missing_pages,
6430 bitmap->pages,
6431 (bitmap->pages - bitmap->missing_pages)
6432 << (PAGE_SHIFT - 10),
42a04b50 6433 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
32a7627c 6434 chunk_kb ? "KB" : "B");
78d742d8
N
6435 if (bitmap->file) {
6436 seq_printf(seq, ", file: ");
c32c2f63 6437 seq_path(seq, &bitmap->file->f_path, " \t\n");
32a7627c 6438 }
78d742d8 6439
32a7627c
N
6440 seq_printf(seq, "\n");
6441 spin_unlock_irqrestore(&bitmap->lock, flags);
1da177e4
LT
6442 }
6443
6444 seq_printf(seq, "\n");
6445 }
6446 mddev_unlock(mddev);
6447
6448 return 0;
6449}
6450
110518bc 6451static const struct seq_operations md_seq_ops = {
1da177e4
LT
6452 .start = md_seq_start,
6453 .next = md_seq_next,
6454 .stop = md_seq_stop,
6455 .show = md_seq_show,
6456};
6457
6458static int md_seq_open(struct inode *inode, struct file *file)
6459{
6460 int error;
d7603b7e
N
6461 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6462 if (mi == NULL)
6463 return -ENOMEM;
1da177e4
LT
6464
6465 error = seq_open(file, &md_seq_ops);
d7603b7e
N
6466 if (error)
6467 kfree(mi);
6468 else {
6469 struct seq_file *p = file->private_data;
6470 p->private = mi;
6471 mi->event = atomic_read(&md_event_count);
6472 }
1da177e4
LT
6473 return error;
6474}
6475
d7603b7e
N
6476static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6477{
6478 struct seq_file *m = filp->private_data;
6479 struct mdstat_info *mi = m->private;
6480 int mask;
6481
6482 poll_wait(filp, &md_event_waiters, wait);
6483
6484 /* always allow read */
6485 mask = POLLIN | POLLRDNORM;
6486
6487 if (mi->event != atomic_read(&md_event_count))
6488 mask |= POLLERR | POLLPRI;
6489 return mask;
6490}
6491
fa027c2a 6492static const struct file_operations md_seq_fops = {
e24650c2 6493 .owner = THIS_MODULE,
1da177e4
LT
6494 .open = md_seq_open,
6495 .read = seq_read,
6496 .llseek = seq_lseek,
c3f94b40 6497 .release = seq_release_private,
d7603b7e 6498 .poll = mdstat_poll,
1da177e4
LT
6499};
6500
2604b703 6501int register_md_personality(struct mdk_personality *p)
1da177e4 6502{
1da177e4 6503 spin_lock(&pers_lock);
2604b703
N
6504 list_add_tail(&p->list, &pers_list);
6505 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
1da177e4
LT
6506 spin_unlock(&pers_lock);
6507 return 0;
6508}
6509
2604b703 6510int unregister_md_personality(struct mdk_personality *p)
1da177e4 6511{
2604b703 6512 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
1da177e4 6513 spin_lock(&pers_lock);
2604b703 6514 list_del_init(&p->list);
1da177e4
LT
6515 spin_unlock(&pers_lock);
6516 return 0;
6517}
6518
eea1bf38 6519static int is_mddev_idle(mddev_t *mddev, int init)
1da177e4
LT
6520{
6521 mdk_rdev_t * rdev;
1da177e4 6522 int idle;
eea1bf38 6523 int curr_events;
1da177e4
LT
6524
6525 idle = 1;
4b80991c
N
6526 rcu_read_lock();
6527 rdev_for_each_rcu(rdev, mddev) {
1da177e4 6528 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
eea1bf38
N
6529 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6530 (int)part_stat_read(&disk->part0, sectors[1]) -
6531 atomic_read(&disk->sync_io);
713f6ab1
N
6532 /* sync IO will cause sync_io to increase before the disk_stats
6533 * as sync_io is counted when a request starts, and
6534 * disk_stats is counted when it completes.
6535 * So resync activity will cause curr_events to be smaller than
6536 * when there was no such activity.
6537 * non-sync IO will cause disk_stat to increase without
6538 * increasing sync_io so curr_events will (eventually)
6539 * be larger than it was before. Once it becomes
6540 * substantially larger, the test below will cause
6541 * the array to appear non-idle, and resync will slow
6542 * down.
6543 * If there is a lot of outstanding resync activity when
6544 * we set last_event to curr_events, then all that activity
6545 * completing might cause the array to appear non-idle
6546 * and resync will be slowed down even though there might
6547 * not have been non-resync activity. This will only
6548 * happen once though. 'last_events' will soon reflect
6549 * the state where there is little or no outstanding
6550 * resync requests, and further resync activity will
6551 * always make curr_events less than last_events.
c0e48521 6552 *
1da177e4 6553 */
eea1bf38 6554 if (init || curr_events - rdev->last_events > 64) {
1da177e4
LT
6555 rdev->last_events = curr_events;
6556 idle = 0;
6557 }
6558 }
4b80991c 6559 rcu_read_unlock();
1da177e4
LT
6560 return idle;
6561}
6562
6563void md_done_sync(mddev_t *mddev, int blocks, int ok)
6564{
6565 /* another "blocks" (512byte) blocks have been synced */
6566 atomic_sub(blocks, &mddev->recovery_active);
6567 wake_up(&mddev->recovery_wait);
6568 if (!ok) {
dfc70645 6569 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1da177e4
LT
6570 md_wakeup_thread(mddev->thread);
6571 // stop recovery, signal do_sync ....
6572 }
6573}
6574
6575
06d91a5f
N
6576/* md_write_start(mddev, bi)
6577 * If we need to update some array metadata (e.g. 'active' flag
3d310eb7
N
6578 * in superblock) before writing, schedule a superblock update
6579 * and wait for it to complete.
06d91a5f 6580 */
3d310eb7 6581void md_write_start(mddev_t *mddev, struct bio *bi)
1da177e4 6582{
0fd62b86 6583 int did_change = 0;
06d91a5f 6584 if (bio_data_dir(bi) != WRITE)
3d310eb7 6585 return;
06d91a5f 6586
f91de92e
N
6587 BUG_ON(mddev->ro == 1);
6588 if (mddev->ro == 2) {
6589 /* need to switch to read/write */
6590 mddev->ro = 0;
6591 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6592 md_wakeup_thread(mddev->thread);
25156198 6593 md_wakeup_thread(mddev->sync_thread);
0fd62b86 6594 did_change = 1;
f91de92e 6595 }
06d91a5f 6596 atomic_inc(&mddev->writes_pending);
31a59e34
N
6597 if (mddev->safemode == 1)
6598 mddev->safemode = 0;
06d91a5f 6599 if (mddev->in_sync) {
a9701a30 6600 spin_lock_irq(&mddev->write_lock);
3d310eb7
N
6601 if (mddev->in_sync) {
6602 mddev->in_sync = 0;
850b2b42 6603 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
070dc6dd 6604 set_bit(MD_CHANGE_PENDING, &mddev->flags);
3d310eb7 6605 md_wakeup_thread(mddev->thread);
0fd62b86 6606 did_change = 1;
3d310eb7 6607 }
a9701a30 6608 spin_unlock_irq(&mddev->write_lock);
06d91a5f 6609 }
0fd62b86 6610 if (did_change)
00bcb4ac 6611 sysfs_notify_dirent_safe(mddev->sysfs_state);
09a44cc1 6612 wait_event(mddev->sb_wait,
09a44cc1 6613 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
1da177e4
LT
6614}
6615
6616void md_write_end(mddev_t *mddev)
6617{
6618 if (atomic_dec_and_test(&mddev->writes_pending)) {
6619 if (mddev->safemode == 2)
6620 md_wakeup_thread(mddev->thread);
16f17b39 6621 else if (mddev->safemode_delay)
1da177e4
LT
6622 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6623 }
6624}
6625
2a2275d6
N
6626/* md_allow_write(mddev)
6627 * Calling this ensures that the array is marked 'active' so that writes
6628 * may proceed without blocking. It is important to call this before
6629 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6630 * Must be called with mddev_lock held.
b5470dc5
DW
6631 *
6632 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6633 * is dropped, so return -EAGAIN after notifying userspace.
2a2275d6 6634 */
b5470dc5 6635int md_allow_write(mddev_t *mddev)
2a2275d6
N
6636{
6637 if (!mddev->pers)
b5470dc5 6638 return 0;
2a2275d6 6639 if (mddev->ro)
b5470dc5 6640 return 0;
1a0fd497 6641 if (!mddev->pers->sync_request)
b5470dc5 6642 return 0;
2a2275d6
N
6643
6644 spin_lock_irq(&mddev->write_lock);
6645 if (mddev->in_sync) {
6646 mddev->in_sync = 0;
6647 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
070dc6dd 6648 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2a2275d6
N
6649 if (mddev->safemode_delay &&
6650 mddev->safemode == 0)
6651 mddev->safemode = 1;
6652 spin_unlock_irq(&mddev->write_lock);
6653 md_update_sb(mddev, 0);
00bcb4ac 6654 sysfs_notify_dirent_safe(mddev->sysfs_state);
2a2275d6
N
6655 } else
6656 spin_unlock_irq(&mddev->write_lock);
b5470dc5 6657
070dc6dd 6658 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
b5470dc5
DW
6659 return -EAGAIN;
6660 else
6661 return 0;
2a2275d6
N
6662}
6663EXPORT_SYMBOL_GPL(md_allow_write);
6664
b63d7c2e 6665void md_unplug(mddev_t *mddev)
252ac522
N
6666{
6667 if (mddev->queue)
6668 blk_unplug(mddev->queue);
6669 if (mddev->plug)
6670 mddev->plug->unplug_fn(mddev->plug);
6671}
6672
1da177e4
LT
6673#define SYNC_MARKS 10
6674#define SYNC_MARK_STEP (3*HZ)
29269553 6675void md_do_sync(mddev_t *mddev)
1da177e4
LT
6676{
6677 mddev_t *mddev2;
6678 unsigned int currspeed = 0,
6679 window;
57afd89f 6680 sector_t max_sectors,j, io_sectors;
1da177e4
LT
6681 unsigned long mark[SYNC_MARKS];
6682 sector_t mark_cnt[SYNC_MARKS];
6683 int last_mark,m;
6684 struct list_head *tmp;
6685 sector_t last_check;
57afd89f 6686 int skipped = 0;
5fd6c1dc 6687 mdk_rdev_t *rdev;
61df9d91 6688 char *desc;
1da177e4
LT
6689
6690 /* just incase thread restarts... */
6691 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6692 return;
5fd6c1dc
N
6693 if (mddev->ro) /* never try to sync a read-only array */
6694 return;
1da177e4 6695
61df9d91
N
6696 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6697 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6698 desc = "data-check";
6699 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6700 desc = "requested-resync";
6701 else
6702 desc = "resync";
6703 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6704 desc = "reshape";
6705 else
6706 desc = "recovery";
6707
1da177e4
LT
6708 /* we overload curr_resync somewhat here.
6709 * 0 == not engaged in resync at all
6710 * 2 == checking that there is no conflict with another sync
6711 * 1 == like 2, but have yielded to allow conflicting resync to
6712 * commense
6713 * other == active in resync - this many blocks
6714 *
6715 * Before starting a resync we must have set curr_resync to
6716 * 2, and then checked that every "conflicting" array has curr_resync
6717 * less than ours. When we find one that is the same or higher
6718 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6719 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6720 * This will mean we have to start checking from the beginning again.
6721 *
6722 */
6723
6724 do {
6725 mddev->curr_resync = 2;
6726
6727 try_again:
404e4b43 6728 if (kthread_should_stop())
6985c43f 6729 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
404e4b43
N
6730
6731 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
1da177e4 6732 goto skip;
29ac4aa3 6733 for_each_mddev(mddev2, tmp) {
1da177e4
LT
6734 if (mddev2 == mddev)
6735 continue;
90b08710
BS
6736 if (!mddev->parallel_resync
6737 && mddev2->curr_resync
6738 && match_mddev_units(mddev, mddev2)) {
1da177e4
LT
6739 DEFINE_WAIT(wq);
6740 if (mddev < mddev2 && mddev->curr_resync == 2) {
6741 /* arbitrarily yield */
6742 mddev->curr_resync = 1;
6743 wake_up(&resync_wait);
6744 }
6745 if (mddev > mddev2 && mddev->curr_resync == 1)
6746 /* no need to wait here, we can wait the next
6747 * time 'round when curr_resync == 2
6748 */
6749 continue;
9744197c
N
6750 /* We need to wait 'interruptible' so as not to
6751 * contribute to the load average, and not to
6752 * be caught by 'softlockup'
6753 */
6754 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
787453c2 6755 if (!kthread_should_stop() &&
8712e553 6756 mddev2->curr_resync >= mddev->curr_resync) {
61df9d91
N
6757 printk(KERN_INFO "md: delaying %s of %s"
6758 " until %s has finished (they"
1da177e4 6759 " share one or more physical units)\n",
61df9d91 6760 desc, mdname(mddev), mdname(mddev2));
1da177e4 6761 mddev_put(mddev2);
9744197c
N
6762 if (signal_pending(current))
6763 flush_signals(current);
1da177e4
LT
6764 schedule();
6765 finish_wait(&resync_wait, &wq);
6766 goto try_again;
6767 }
6768 finish_wait(&resync_wait, &wq);
6769 }
6770 }
6771 } while (mddev->curr_resync < 2);
6772
5fd6c1dc 6773 j = 0;
9d88883e 6774 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1da177e4 6775 /* resync follows the size requested by the personality,
57afd89f 6776 * which defaults to physical size, but can be virtual size
1da177e4
LT
6777 */
6778 max_sectors = mddev->resync_max_sectors;
9d88883e 6779 mddev->resync_mismatches = 0;
5fd6c1dc 6780 /* we don't use the checkpoint if there's a bitmap */
5e96ee65
NB
6781 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6782 j = mddev->resync_min;
6783 else if (!mddev->bitmap)
5fd6c1dc 6784 j = mddev->recovery_cp;
5e96ee65 6785
ccfcc3c1 6786 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
58c0fed4 6787 max_sectors = mddev->dev_sectors;
5fd6c1dc 6788 else {
1da177e4 6789 /* recovery follows the physical size of devices */
58c0fed4 6790 max_sectors = mddev->dev_sectors;
5fd6c1dc 6791 j = MaxSector;
4e59ca7d
DW
6792 rcu_read_lock();
6793 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
5fd6c1dc
N
6794 if (rdev->raid_disk >= 0 &&
6795 !test_bit(Faulty, &rdev->flags) &&
6796 !test_bit(In_sync, &rdev->flags) &&
6797 rdev->recovery_offset < j)
6798 j = rdev->recovery_offset;
4e59ca7d 6799 rcu_read_unlock();
5fd6c1dc 6800 }
1da177e4 6801
61df9d91
N
6802 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6803 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6804 " %d KB/sec/disk.\n", speed_min(mddev));
338cec32 6805 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
61df9d91
N
6806 "(but not more than %d KB/sec) for %s.\n",
6807 speed_max(mddev), desc);
1da177e4 6808
eea1bf38 6809 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
5fd6c1dc 6810
57afd89f 6811 io_sectors = 0;
1da177e4
LT
6812 for (m = 0; m < SYNC_MARKS; m++) {
6813 mark[m] = jiffies;
57afd89f 6814 mark_cnt[m] = io_sectors;
1da177e4
LT
6815 }
6816 last_mark = 0;
6817 mddev->resync_mark = mark[last_mark];
6818 mddev->resync_mark_cnt = mark_cnt[last_mark];
6819
6820 /*
6821 * Tune reconstruction:
6822 */
6823 window = 32*(PAGE_SIZE/512);
6824 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6825 window/2,(unsigned long long) max_sectors/2);
6826
6827 atomic_set(&mddev->recovery_active, 0);
1da177e4
LT
6828 last_check = 0;
6829
6830 if (j>2) {
6831 printk(KERN_INFO
61df9d91
N
6832 "md: resuming %s of %s from checkpoint.\n",
6833 desc, mdname(mddev));
1da177e4
LT
6834 mddev->curr_resync = j;
6835 }
75d3da43 6836 mddev->curr_resync_completed = j;
1da177e4
LT
6837
6838 while (j < max_sectors) {
57afd89f 6839 sector_t sectors;
1da177e4 6840
57afd89f 6841 skipped = 0;
97e4f42d 6842
7a91ee1f
N
6843 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6844 ((mddev->curr_resync > mddev->curr_resync_completed &&
6845 (mddev->curr_resync - mddev->curr_resync_completed)
6846 > (max_sectors >> 4)) ||
6847 (j - mddev->curr_resync_completed)*2
6848 >= mddev->resync_max - mddev->curr_resync_completed
6849 )) {
97e4f42d 6850 /* time to update curr_resync_completed */
252ac522 6851 md_unplug(mddev);
97e4f42d
N
6852 wait_event(mddev->recovery_wait,
6853 atomic_read(&mddev->recovery_active) == 0);
75d3da43 6854 mddev->curr_resync_completed = j;
070dc6dd 6855 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
acb180b0 6856 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
97e4f42d 6857 }
acb180b0 6858
e62e58a5
N
6859 while (j >= mddev->resync_max && !kthread_should_stop()) {
6860 /* As this condition is controlled by user-space,
6861 * we can block indefinitely, so use '_interruptible'
6862 * to avoid triggering warnings.
6863 */
6864 flush_signals(current); /* just in case */
6865 wait_event_interruptible(mddev->recovery_wait,
6866 mddev->resync_max > j
6867 || kthread_should_stop());
6868 }
acb180b0
N
6869
6870 if (kthread_should_stop())
6871 goto interrupted;
6872
57afd89f 6873 sectors = mddev->pers->sync_request(mddev, j, &skipped,
c6207277 6874 currspeed < speed_min(mddev));
57afd89f 6875 if (sectors == 0) {
dfc70645 6876 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1da177e4
LT
6877 goto out;
6878 }
57afd89f
N
6879
6880 if (!skipped) { /* actual IO requested */
6881 io_sectors += sectors;
6882 atomic_add(sectors, &mddev->recovery_active);
6883 }
6884
1da177e4
LT
6885 j += sectors;
6886 if (j>1) mddev->curr_resync = j;
ff4e8d9a 6887 mddev->curr_mark_cnt = io_sectors;
d7603b7e
N
6888 if (last_check == 0)
6889 /* this is the earliers that rebuilt will be
6890 * visible in /proc/mdstat
6891 */
6892 md_new_event(mddev);
57afd89f
N
6893
6894 if (last_check + window > io_sectors || j == max_sectors)
1da177e4
LT
6895 continue;
6896
57afd89f 6897 last_check = io_sectors;
1da177e4 6898
dfc70645 6899 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
1da177e4
LT
6900 break;
6901
6902 repeat:
6903 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6904 /* step marks */
6905 int next = (last_mark+1) % SYNC_MARKS;
6906
6907 mddev->resync_mark = mark[next];
6908 mddev->resync_mark_cnt = mark_cnt[next];
6909 mark[next] = jiffies;
57afd89f 6910 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
1da177e4
LT
6911 last_mark = next;
6912 }
6913
6914
c6207277
N
6915 if (kthread_should_stop())
6916 goto interrupted;
6917
1da177e4
LT
6918
6919 /*
6920 * this loop exits only if either when we are slower than
6921 * the 'hard' speed limit, or the system was IO-idle for
6922 * a jiffy.
6923 * the system might be non-idle CPU-wise, but we only care
6924 * about not overloading the IO subsystem. (things like an
6925 * e2fsck being done on the RAID array should execute fast)
6926 */
252ac522 6927 md_unplug(mddev);
1da177e4
LT
6928 cond_resched();
6929
57afd89f
N
6930 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6931 /((jiffies-mddev->resync_mark)/HZ +1) +1;
1da177e4 6932
88202a0c
N
6933 if (currspeed > speed_min(mddev)) {
6934 if ((currspeed > speed_max(mddev)) ||
eea1bf38 6935 !is_mddev_idle(mddev, 0)) {
c0e48521 6936 msleep(500);
1da177e4
LT
6937 goto repeat;
6938 }
6939 }
6940 }
61df9d91 6941 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
1da177e4
LT
6942 /*
6943 * this also signals 'finished resyncing' to md_stop
6944 */
6945 out:
252ac522 6946 md_unplug(mddev);
1da177e4
LT
6947
6948 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6949
6950 /* tell personality that we are finished */
57afd89f 6951 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
1da177e4 6952
dfc70645 6953 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
5fd6c1dc
N
6954 mddev->curr_resync > 2) {
6955 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6956 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6957 if (mddev->curr_resync >= mddev->recovery_cp) {
6958 printk(KERN_INFO
61df9d91
N
6959 "md: checkpointing %s of %s.\n",
6960 desc, mdname(mddev));
5fd6c1dc
N
6961 mddev->recovery_cp = mddev->curr_resync;
6962 }
6963 } else
6964 mddev->recovery_cp = MaxSector;
6965 } else {
6966 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6967 mddev->curr_resync = MaxSector;
4e59ca7d
DW
6968 rcu_read_lock();
6969 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
5fd6c1dc 6970 if (rdev->raid_disk >= 0 &&
70fffd0b 6971 mddev->delta_disks >= 0 &&
5fd6c1dc
N
6972 !test_bit(Faulty, &rdev->flags) &&
6973 !test_bit(In_sync, &rdev->flags) &&
6974 rdev->recovery_offset < mddev->curr_resync)
6975 rdev->recovery_offset = mddev->curr_resync;
4e59ca7d 6976 rcu_read_unlock();
5fd6c1dc 6977 }
1da177e4 6978 }
17571284 6979 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1da177e4 6980
1da177e4 6981 skip:
c07b70ad
N
6982 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6983 /* We completed so min/max setting can be forgotten if used. */
6984 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6985 mddev->resync_min = 0;
6986 mddev->resync_max = MaxSector;
6987 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6988 mddev->resync_min = mddev->curr_resync_completed;
1da177e4 6989 mddev->curr_resync = 0;
efa59339
N
6990 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6991 mddev->curr_resync_completed = 0;
c6207277 6992 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
1da177e4
LT
6993 wake_up(&resync_wait);
6994 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6995 md_wakeup_thread(mddev->thread);
c6207277
N
6996 return;
6997
6998 interrupted:
6999 /*
7000 * got a signal, exit.
7001 */
7002 printk(KERN_INFO
7003 "md: md_do_sync() got signal ... exiting\n");
7004 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7005 goto out;
7006
1da177e4 7007}
29269553 7008EXPORT_SYMBOL_GPL(md_do_sync);
1da177e4
LT
7009
7010
b4c4c7b8
N
7011static int remove_and_add_spares(mddev_t *mddev)
7012{
7013 mdk_rdev_t *rdev;
b4c4c7b8
N
7014 int spares = 0;
7015
97e4f42d
N
7016 mddev->curr_resync_completed = 0;
7017
159ec1fc 7018 list_for_each_entry(rdev, &mddev->disks, same_set)
b4c4c7b8 7019 if (rdev->raid_disk >= 0 &&
6bfe0b49 7020 !test_bit(Blocked, &rdev->flags) &&
b4c4c7b8
N
7021 (test_bit(Faulty, &rdev->flags) ||
7022 ! test_bit(In_sync, &rdev->flags)) &&
7023 atomic_read(&rdev->nr_pending)==0) {
7024 if (mddev->pers->hot_remove_disk(
7025 mddev, rdev->raid_disk)==0) {
7026 char nm[20];
7027 sprintf(nm,"rd%d", rdev->raid_disk);
7028 sysfs_remove_link(&mddev->kobj, nm);
7029 rdev->raid_disk = -1;
7030 }
7031 }
7032
4044ba58 7033 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
159ec1fc 7034 list_for_each_entry(rdev, &mddev->disks, same_set) {
dfc70645 7035 if (rdev->raid_disk >= 0 &&
e5427135
DW
7036 !test_bit(In_sync, &rdev->flags) &&
7037 !test_bit(Blocked, &rdev->flags))
dfc70645 7038 spares++;
b4c4c7b8
N
7039 if (rdev->raid_disk < 0
7040 && !test_bit(Faulty, &rdev->flags)) {
7041 rdev->recovery_offset = 0;
199050ea
NB
7042 if (mddev->pers->
7043 hot_add_disk(mddev, rdev) == 0) {
b4c4c7b8
N
7044 char nm[20];
7045 sprintf(nm, "rd%d", rdev->raid_disk);
5e55e2f5
N
7046 if (sysfs_create_link(&mddev->kobj,
7047 &rdev->kobj, nm))
00bcb4ac 7048 /* failure here is OK */;
b4c4c7b8
N
7049 spares++;
7050 md_new_event(mddev);
93be75ff 7051 set_bit(MD_CHANGE_DEVS, &mddev->flags);
b4c4c7b8
N
7052 } else
7053 break;
7054 }
dfc70645 7055 }
b4c4c7b8
N
7056 }
7057 return spares;
7058}
7ebc0be7
N
7059
7060static void reap_sync_thread(mddev_t *mddev)
7061{
7062 mdk_rdev_t *rdev;
7063
7064 /* resync has finished, collect result */
7065 md_unregister_thread(mddev->sync_thread);
7066 mddev->sync_thread = NULL;
7067 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7068 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7069 /* success...*/
7070 /* activate any spares */
7071 if (mddev->pers->spare_active(mddev))
7072 sysfs_notify(&mddev->kobj, NULL,
7073 "degraded");
7074 }
7075 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7076 mddev->pers->finish_reshape)
7077 mddev->pers->finish_reshape(mddev);
7078 md_update_sb(mddev, 1);
7079
7080 /* if array is no-longer degraded, then any saved_raid_disk
7081 * information must be scrapped
7082 */
7083 if (!mddev->degraded)
7084 list_for_each_entry(rdev, &mddev->disks, same_set)
7085 rdev->saved_raid_disk = -1;
7086
7087 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7088 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7089 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7090 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7091 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7092 /* flag recovery needed just to double check */
7093 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7094 sysfs_notify_dirent_safe(mddev->sysfs_action);
7095 md_new_event(mddev);
7096}
7097
1da177e4
LT
7098/*
7099 * This routine is regularly called by all per-raid-array threads to
7100 * deal with generic issues like resync and super-block update.
7101 * Raid personalities that don't have a thread (linear/raid0) do not
7102 * need this as they never do any recovery or update the superblock.
7103 *
7104 * It does not do any resync itself, but rather "forks" off other threads
7105 * to do that as needed.
7106 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7107 * "->recovery" and create a thread at ->sync_thread.
dfc70645 7108 * When the thread finishes it sets MD_RECOVERY_DONE
1da177e4
LT
7109 * and wakeups up this thread which will reap the thread and finish up.
7110 * This thread also removes any faulty devices (with nr_pending == 0).
7111 *
7112 * The overall approach is:
7113 * 1/ if the superblock needs updating, update it.
7114 * 2/ If a recovery thread is running, don't do anything else.
7115 * 3/ If recovery has finished, clean up, possibly marking spares active.
7116 * 4/ If there are any faulty devices, remove them.
7117 * 5/ If array is degraded, try to add spares devices
7118 * 6/ If array has spares or is not in-sync, start a resync thread.
7119 */
7120void md_check_recovery(mddev_t *mddev)
7121{
5f40402d 7122 if (mddev->bitmap)
aa5cbd10 7123 bitmap_daemon_work(mddev);
1da177e4
LT
7124
7125 if (mddev->ro)
7126 return;
fca4d848
N
7127
7128 if (signal_pending(current)) {
31a59e34 7129 if (mddev->pers->sync_request && !mddev->external) {
fca4d848
N
7130 printk(KERN_INFO "md: %s in immediate safe mode\n",
7131 mdname(mddev));
7132 mddev->safemode = 2;
7133 }
7134 flush_signals(current);
7135 }
7136
c89a8eee
N
7137 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7138 return;
1da177e4 7139 if ( ! (
126925c0 7140 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
1da177e4 7141 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
fca4d848 7142 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
31a59e34 7143 (mddev->external == 0 && mddev->safemode == 1) ||
fca4d848
N
7144 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7145 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
1da177e4
LT
7146 ))
7147 return;
fca4d848 7148
df5b89b3 7149 if (mddev_trylock(mddev)) {
b4c4c7b8 7150 int spares = 0;
fca4d848 7151
c89a8eee
N
7152 if (mddev->ro) {
7153 /* Only thing we do on a ro array is remove
7154 * failed devices.
7155 */
7156 remove_and_add_spares(mddev);
7157 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7158 goto unlock;
7159 }
7160
31a59e34 7161 if (!mddev->external) {
0fd62b86 7162 int did_change = 0;
31a59e34
N
7163 spin_lock_irq(&mddev->write_lock);
7164 if (mddev->safemode &&
7165 !atomic_read(&mddev->writes_pending) &&
7166 !mddev->in_sync &&
7167 mddev->recovery_cp == MaxSector) {
7168 mddev->in_sync = 1;
0fd62b86 7169 did_change = 1;
070dc6dd 7170 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
31a59e34
N
7171 }
7172 if (mddev->safemode == 1)
7173 mddev->safemode = 0;
7174 spin_unlock_irq(&mddev->write_lock);
0fd62b86 7175 if (did_change)
00bcb4ac 7176 sysfs_notify_dirent_safe(mddev->sysfs_state);
fca4d848 7177 }
fca4d848 7178
850b2b42
N
7179 if (mddev->flags)
7180 md_update_sb(mddev, 0);
06d91a5f 7181
1da177e4
LT
7182 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7183 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7184 /* resync/recovery still happening */
7185 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7186 goto unlock;
7187 }
7188 if (mddev->sync_thread) {
7ebc0be7 7189 reap_sync_thread(mddev);
1da177e4
LT
7190 goto unlock;
7191 }
72a23c21
NB
7192 /* Set RUNNING before clearing NEEDED to avoid
7193 * any transients in the value of "sync_action".
7194 */
7195 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7196 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
24dd469d
N
7197 /* Clear some bits that don't mean anything, but
7198 * might be left set
7199 */
24dd469d
N
7200 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7201 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
1da177e4 7202
5fd6c1dc
N
7203 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7204 goto unlock;
1da177e4
LT
7205 /* no recovery is running.
7206 * remove any failed drives, then
7207 * add spares if possible.
7208 * Spare are also removed and re-added, to allow
7209 * the personality to fail the re-add.
7210 */
1da177e4 7211
b4c4c7b8 7212 if (mddev->reshape_position != MaxSector) {
50ac168a
N
7213 if (mddev->pers->check_reshape == NULL ||
7214 mddev->pers->check_reshape(mddev) != 0)
b4c4c7b8
N
7215 /* Cannot proceed */
7216 goto unlock;
7217 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
72a23c21 7218 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
b4c4c7b8 7219 } else if ((spares = remove_and_add_spares(mddev))) {
24dd469d
N
7220 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7221 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
56ac36d7 7222 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
72a23c21 7223 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
24dd469d
N
7224 } else if (mddev->recovery_cp < MaxSector) {
7225 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
72a23c21 7226 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
24dd469d
N
7227 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7228 /* nothing to be done ... */
1da177e4 7229 goto unlock;
24dd469d 7230
1da177e4 7231 if (mddev->pers->sync_request) {
a654b9d8
N
7232 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7233 /* We are adding a device or devices to an array
7234 * which has the bitmap stored on all devices.
7235 * So make sure all bitmap pages get written
7236 */
7237 bitmap_write_all(mddev->bitmap);
7238 }
1da177e4
LT
7239 mddev->sync_thread = md_register_thread(md_do_sync,
7240 mddev,
0da3c619 7241 "resync");
1da177e4
LT
7242 if (!mddev->sync_thread) {
7243 printk(KERN_ERR "%s: could not start resync"
7244 " thread...\n",
7245 mdname(mddev));
7246 /* leave the spares where they are, it shouldn't hurt */
7ebc0be7
N
7247 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7248 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7249 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7250 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7251 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
d7603b7e 7252 } else
1da177e4 7253 md_wakeup_thread(mddev->sync_thread);
00bcb4ac 7254 sysfs_notify_dirent_safe(mddev->sysfs_action);
d7603b7e 7255 md_new_event(mddev);
1da177e4
LT
7256 }
7257 unlock:
72a23c21
NB
7258 if (!mddev->sync_thread) {
7259 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7260 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7261 &mddev->recovery))
0c3573f1 7262 if (mddev->sysfs_action)
00bcb4ac 7263 sysfs_notify_dirent_safe(mddev->sysfs_action);
72a23c21 7264 }
1da177e4
LT
7265 mddev_unlock(mddev);
7266 }
7267}
7268
6bfe0b49
DW
7269void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7270{
00bcb4ac 7271 sysfs_notify_dirent_safe(rdev->sysfs_state);
6bfe0b49
DW
7272 wait_event_timeout(rdev->blocked_wait,
7273 !test_bit(Blocked, &rdev->flags),
7274 msecs_to_jiffies(5000));
7275 rdev_dec_pending(rdev, mddev);
7276}
7277EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7278
75c96f85
AB
7279static int md_notify_reboot(struct notifier_block *this,
7280 unsigned long code, void *x)
1da177e4
LT
7281{
7282 struct list_head *tmp;
7283 mddev_t *mddev;
7284
7285 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7286
7287 printk(KERN_INFO "md: stopping all md devices.\n");
7288
29ac4aa3 7289 for_each_mddev(mddev, tmp)
c71d4887 7290 if (mddev_trylock(mddev)) {
2b25000b
N
7291 /* Force a switch to readonly even array
7292 * appears to still be in use. Hence
7293 * the '100'.
7294 */
a4bd82d0 7295 md_set_readonly(mddev, 100);
c71d4887
NB
7296 mddev_unlock(mddev);
7297 }
1da177e4
LT
7298 /*
7299 * certain more exotic SCSI devices are known to be
7300 * volatile wrt too early system reboots. While the
7301 * right place to handle this issue is the given
7302 * driver, we do want to have a safe RAID driver ...
7303 */
7304 mdelay(1000*1);
7305 }
7306 return NOTIFY_DONE;
7307}
7308
75c96f85 7309static struct notifier_block md_notifier = {
1da177e4
LT
7310 .notifier_call = md_notify_reboot,
7311 .next = NULL,
7312 .priority = INT_MAX, /* before any real devices */
7313};
7314
7315static void md_geninit(void)
7316{
1da177e4
LT
7317 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7318
c7705f34 7319 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
1da177e4
LT
7320}
7321
75c96f85 7322static int __init md_init(void)
1da177e4 7323{
e804ac78
TH
7324 int ret = -ENOMEM;
7325
7326 md_wq = alloc_workqueue("md", WQ_RESCUER, 0);
7327 if (!md_wq)
7328 goto err_wq;
7329
7330 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7331 if (!md_misc_wq)
7332 goto err_misc_wq;
7333
7334 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7335 goto err_md;
7336
7337 if ((ret = register_blkdev(0, "mdp")) < 0)
7338 goto err_mdp;
7339 mdp_major = ret;
7340
3dbd8c2e 7341 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
e8703fe1
N
7342 md_probe, NULL, NULL);
7343 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
1da177e4
LT
7344 md_probe, NULL, NULL);
7345
1da177e4 7346 register_reboot_notifier(&md_notifier);
0b4d4147 7347 raid_table_header = register_sysctl_table(raid_root_table);
1da177e4
LT
7348
7349 md_geninit();
d710e138 7350 return 0;
1da177e4 7351
e804ac78
TH
7352err_mdp:
7353 unregister_blkdev(MD_MAJOR, "md");
7354err_md:
7355 destroy_workqueue(md_misc_wq);
7356err_misc_wq:
7357 destroy_workqueue(md_wq);
7358err_wq:
7359 return ret;
7360}
1da177e4
LT
7361
7362#ifndef MODULE
7363
7364/*
7365 * Searches all registered partitions for autorun RAID arrays
7366 * at boot time.
7367 */
4d936ec1
ME
7368
7369static LIST_HEAD(all_detected_devices);
7370struct detected_devices_node {
7371 struct list_head list;
7372 dev_t dev;
7373};
1da177e4
LT
7374
7375void md_autodetect_dev(dev_t dev)
7376{
4d936ec1
ME
7377 struct detected_devices_node *node_detected_dev;
7378
7379 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7380 if (node_detected_dev) {
7381 node_detected_dev->dev = dev;
7382 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7383 } else {
7384 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7385 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7386 }
1da177e4
LT
7387}
7388
7389
7390static void autostart_arrays(int part)
7391{
7392 mdk_rdev_t *rdev;
4d936ec1
ME
7393 struct detected_devices_node *node_detected_dev;
7394 dev_t dev;
7395 int i_scanned, i_passed;
1da177e4 7396
4d936ec1
ME
7397 i_scanned = 0;
7398 i_passed = 0;
1da177e4 7399
4d936ec1 7400 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
1da177e4 7401
4d936ec1
ME
7402 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7403 i_scanned++;
7404 node_detected_dev = list_entry(all_detected_devices.next,
7405 struct detected_devices_node, list);
7406 list_del(&node_detected_dev->list);
7407 dev = node_detected_dev->dev;
7408 kfree(node_detected_dev);
df968c4e 7409 rdev = md_import_device(dev,0, 90);
1da177e4
LT
7410 if (IS_ERR(rdev))
7411 continue;
7412
b2d444d7 7413 if (test_bit(Faulty, &rdev->flags)) {
1da177e4
LT
7414 MD_BUG();
7415 continue;
7416 }
d0fae18f 7417 set_bit(AutoDetected, &rdev->flags);
1da177e4 7418 list_add(&rdev->same_set, &pending_raid_disks);
4d936ec1 7419 i_passed++;
1da177e4 7420 }
4d936ec1
ME
7421
7422 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7423 i_scanned, i_passed);
1da177e4
LT
7424
7425 autorun_devices(part);
7426}
7427
fdee8ae4 7428#endif /* !MODULE */
1da177e4
LT
7429
7430static __exit void md_exit(void)
7431{
7432 mddev_t *mddev;
7433 struct list_head *tmp;
8ab5e4c1 7434
3dbd8c2e 7435 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
e8703fe1 7436 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
1da177e4 7437
3dbd8c2e 7438 unregister_blkdev(MD_MAJOR,"md");
1da177e4
LT
7439 unregister_blkdev(mdp_major, "mdp");
7440 unregister_reboot_notifier(&md_notifier);
7441 unregister_sysctl_table(raid_table_header);
7442 remove_proc_entry("mdstat", NULL);
29ac4aa3 7443 for_each_mddev(mddev, tmp) {
1da177e4 7444 export_array(mddev);
d3374825 7445 mddev->hold_active = 0;
1da177e4 7446 }
e804ac78
TH
7447 destroy_workqueue(md_misc_wq);
7448 destroy_workqueue(md_wq);
1da177e4
LT
7449}
7450
685784aa 7451subsys_initcall(md_init);
1da177e4
LT
7452module_exit(md_exit)
7453
f91de92e
N
7454static int get_ro(char *buffer, struct kernel_param *kp)
7455{
7456 return sprintf(buffer, "%d", start_readonly);
7457}
7458static int set_ro(const char *val, struct kernel_param *kp)
7459{
7460 char *e;
7461 int num = simple_strtoul(val, &e, 10);
7462 if (*val && (*e == '\0' || *e == '\n')) {
7463 start_readonly = num;
4dbcdc75 7464 return 0;
f91de92e
N
7465 }
7466 return -EINVAL;
7467}
7468
80ca3a44
N
7469module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7470module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
6ff8d8ec 7471
efeb53c0 7472module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
f91de92e 7473
1da177e4
LT
7474EXPORT_SYMBOL(register_md_personality);
7475EXPORT_SYMBOL(unregister_md_personality);
7476EXPORT_SYMBOL(md_error);
7477EXPORT_SYMBOL(md_done_sync);
7478EXPORT_SYMBOL(md_write_start);
7479EXPORT_SYMBOL(md_write_end);
1da177e4
LT
7480EXPORT_SYMBOL(md_register_thread);
7481EXPORT_SYMBOL(md_unregister_thread);
7482EXPORT_SYMBOL(md_wakeup_thread);
1da177e4
LT
7483EXPORT_SYMBOL(md_check_recovery);
7484MODULE_LICENSE("GPL");
0efb9e61 7485MODULE_DESCRIPTION("MD RAID framework");
aa1595e9 7486MODULE_ALIAS("md");
72008652 7487MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);