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