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