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