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