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