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