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