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