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