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