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