]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/md/md.c
md/raid5: move comment of fetch_block to right location
[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
36d091f4 5294 err = pers->run(mddev);
13e53df3 5295 if (err)
9d48739e 5296 pr_warn("md: pers->run() failed ...\n");
36d091f4 5297 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
9d48739e
N
5298 WARN_ONCE(!mddev->external_size,
5299 "%s: default size too small, but 'external_size' not in effect?\n",
5300 __func__);
5301 pr_warn("md: invalid array_size %llu > default size %llu\n",
5302 (unsigned long long)mddev->array_sectors / 2,
5303 (unsigned long long)pers->size(mddev, 0, 0) / 2);
b522adcd 5304 err = -EINVAL;
b522adcd 5305 }
36d091f4 5306 if (err == 0 && pers->sync_request &&
ef99bf48 5307 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
f9209a32
GR
5308 struct bitmap *bitmap;
5309
5310 bitmap = bitmap_create(mddev, -1);
5311 if (IS_ERR(bitmap)) {
5312 err = PTR_ERR(bitmap);
9d48739e
N
5313 pr_warn("%s: failed to create bitmap (%d)\n",
5314 mdname(mddev), err);
f9209a32
GR
5315 } else
5316 mddev->bitmap = bitmap;
5317
b15c2e57 5318 }
1da177e4 5319 if (err) {
5aa61f42 5320 mddev_detach(mddev);
0c35bd47
N
5321 if (mddev->private)
5322 pers->free(mddev, mddev->private);
bd691922 5323 mddev->private = NULL;
36d091f4 5324 module_put(pers->owner);
32a7627c
N
5325 bitmap_destroy(mddev);
5326 return err;
1da177e4 5327 }
5c675f83 5328 if (mddev->queue) {
bb086a89
SL
5329 bool nonrot = true;
5330
5331 rdev_for_each(rdev, mddev) {
5332 if (rdev->raid_disk >= 0 &&
5333 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5334 nonrot = false;
5335 break;
5336 }
5337 }
5338 if (mddev->degraded)
5339 nonrot = false;
5340 if (nonrot)
5341 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5342 else
5343 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5c675f83
N
5344 mddev->queue->backing_dev_info.congested_data = mddev;
5345 mddev->queue->backing_dev_info.congested_fn = md_congested;
5346 }
36d091f4 5347 if (pers->sync_request) {
00bcb4ac
N
5348 if (mddev->kobj.sd &&
5349 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
9d48739e
N
5350 pr_warn("md: cannot register extra attributes for %s\n",
5351 mdname(mddev));
00bcb4ac 5352 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5e55e2f5 5353 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
fd9d49ca
N
5354 mddev->ro = 0;
5355
f72ffdd6 5356 atomic_set(&mddev->writes_pending,0);
1e50915f
RB
5357 atomic_set(&mddev->max_corr_read_errors,
5358 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
1da177e4 5359 mddev->safemode = 0;
28c1b9fd
GR
5360 if (mddev_is_clustered(mddev))
5361 mddev->safemode_delay = 0;
5362 else
5363 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
1da177e4 5364 mddev->in_sync = 1;
0ca69886 5365 smp_wmb();
36d091f4
N
5366 spin_lock(&mddev->lock);
5367 mddev->pers = pers;
36d091f4 5368 spin_unlock(&mddev->lock);
dafb20fa 5369 rdev_for_each(rdev, mddev)
36fad858
NK
5370 if (rdev->raid_disk >= 0)
5371 if (sysfs_link_rdev(mddev, rdev))
00bcb4ac 5372 /* failure here is OK */;
f72ffdd6 5373
a4a3d26d
N
5374 if (mddev->degraded && !mddev->ro)
5375 /* This ensures that recovering status is reported immediately
5376 * via sysfs - until a lack of spares is confirmed.
5377 */
5378 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
1da177e4 5379 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
f72ffdd6 5380
2953079c 5381 if (mddev->sb_flags)
850b2b42 5382 md_update_sb(mddev, 0);
1da177e4 5383
d7603b7e 5384 md_new_event(mddev);
00bcb4ac
N
5385 sysfs_notify_dirent_safe(mddev->sysfs_state);
5386 sysfs_notify_dirent_safe(mddev->sysfs_action);
a99ac971 5387 sysfs_notify(&mddev->kobj, NULL, "degraded");
1da177e4
LT
5388 return 0;
5389}
390ee602 5390EXPORT_SYMBOL_GPL(md_run);
1da177e4 5391
fd01b88c 5392static int do_md_run(struct mddev *mddev)
fe60b014
N
5393{
5394 int err;
5395
5396 err = md_run(mddev);
5397 if (err)
5398 goto out;
69e51b44
N
5399 err = bitmap_load(mddev);
5400 if (err) {
5401 bitmap_destroy(mddev);
5402 goto out;
5403 }
0fd018af 5404
28c1b9fd
GR
5405 if (mddev_is_clustered(mddev))
5406 md_allow_write(mddev);
5407
0fd018af
JB
5408 md_wakeup_thread(mddev->thread);
5409 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5410
fe60b014
N
5411 set_capacity(mddev->gendisk, mddev->array_sectors);
5412 revalidate_disk(mddev->gendisk);
f0b4f7e2 5413 mddev->changed = 1;
fe60b014
N
5414 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5415out:
5416 return err;
5417}
5418
fd01b88c 5419static int restart_array(struct mddev *mddev)
1da177e4
LT
5420{
5421 struct gendisk *disk = mddev->gendisk;
1da177e4 5422
80fab1d7 5423 /* Complain if it has no devices */
1da177e4 5424 if (list_empty(&mddev->disks))
80fab1d7
AN
5425 return -ENXIO;
5426 if (!mddev->pers)
5427 return -EINVAL;
5428 if (!mddev->ro)
5429 return -EBUSY;
339421de
SL
5430 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5431 struct md_rdev *rdev;
5432 bool has_journal = false;
5433
5434 rcu_read_lock();
5435 rdev_for_each_rcu(rdev, mddev) {
5436 if (test_bit(Journal, &rdev->flags) &&
5437 !test_bit(Faulty, &rdev->flags)) {
5438 has_journal = true;
5439 break;
5440 }
5441 }
5442 rcu_read_unlock();
5443
5444 /* Don't restart rw with journal missing/faulty */
5445 if (!has_journal)
5446 return -EINVAL;
5447 }
5448
80fab1d7
AN
5449 mddev->safemode = 0;
5450 mddev->ro = 0;
5451 set_disk_ro(disk, 0);
9d48739e 5452 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
80fab1d7
AN
5453 /* Kick recovery or resync if necessary */
5454 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5455 md_wakeup_thread(mddev->thread);
5456 md_wakeup_thread(mddev->sync_thread);
00bcb4ac 5457 sysfs_notify_dirent_safe(mddev->sysfs_state);
80fab1d7 5458 return 0;
1da177e4
LT
5459}
5460
fd01b88c 5461static void md_clean(struct mddev *mddev)
6177b472
N
5462{
5463 mddev->array_sectors = 0;
5464 mddev->external_size = 0;
5465 mddev->dev_sectors = 0;
5466 mddev->raid_disks = 0;
5467 mddev->recovery_cp = 0;
5468 mddev->resync_min = 0;
5469 mddev->resync_max = MaxSector;
5470 mddev->reshape_position = MaxSector;
5471 mddev->external = 0;
5472 mddev->persistent = 0;
5473 mddev->level = LEVEL_NONE;
5474 mddev->clevel[0] = 0;
5475 mddev->flags = 0;
2953079c 5476 mddev->sb_flags = 0;
6177b472
N
5477 mddev->ro = 0;
5478 mddev->metadata_type[0] = 0;
5479 mddev->chunk_sectors = 0;
5480 mddev->ctime = mddev->utime = 0;
5481 mddev->layout = 0;
5482 mddev->max_disks = 0;
5483 mddev->events = 0;
a8707c08 5484 mddev->can_decrease_events = 0;
6177b472 5485 mddev->delta_disks = 0;
2c810cdd 5486 mddev->reshape_backwards = 0;
6177b472
N
5487 mddev->new_level = LEVEL_NONE;
5488 mddev->new_layout = 0;
5489 mddev->new_chunk_sectors = 0;
5490 mddev->curr_resync = 0;
7f7583d4 5491 atomic64_set(&mddev->resync_mismatches, 0);
6177b472
N
5492 mddev->suspend_lo = mddev->suspend_hi = 0;
5493 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5494 mddev->recovery = 0;
5495 mddev->in_sync = 0;
f0b4f7e2 5496 mddev->changed = 0;
6177b472 5497 mddev->degraded = 0;
6177b472 5498 mddev->safemode = 0;
bd691922 5499 mddev->private = NULL;
c20c33f0 5500 mddev->cluster_info = NULL;
6177b472
N
5501 mddev->bitmap_info.offset = 0;
5502 mddev->bitmap_info.default_offset = 0;
6409bb05 5503 mddev->bitmap_info.default_space = 0;
6177b472
N
5504 mddev->bitmap_info.chunksize = 0;
5505 mddev->bitmap_info.daemon_sleep = 0;
5506 mddev->bitmap_info.max_write_behind = 0;
c20c33f0 5507 mddev->bitmap_info.nodes = 0;
6177b472
N
5508}
5509
fd01b88c 5510static void __md_stop_writes(struct mddev *mddev)
a047e125 5511{
6b6204ee 5512 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
f851b60d 5513 flush_workqueue(md_misc_wq);
a047e125 5514 if (mddev->sync_thread) {
a047e125 5515 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
a91d5ac0 5516 md_reap_sync_thread(mddev);
a047e125
N
5517 }
5518
5519 del_timer_sync(&mddev->safemode_timer);
5520
034e33f5
SL
5521 if (mddev->pers && mddev->pers->quiesce) {
5522 mddev->pers->quiesce(mddev, 1);
5523 mddev->pers->quiesce(mddev, 0);
5524 }
a047e125 5525 bitmap_flush(mddev);
a047e125 5526
b6d428c6 5527 if (mddev->ro == 0 &&
28c1b9fd 5528 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
2953079c 5529 mddev->sb_flags)) {
a047e125 5530 /* mark array as shutdown cleanly */
28c1b9fd
GR
5531 if (!mddev_is_clustered(mddev))
5532 mddev->in_sync = 1;
a047e125
N
5533 md_update_sb(mddev, 1);
5534 }
5535}
defad61a 5536
fd01b88c 5537void md_stop_writes(struct mddev *mddev)
defad61a 5538{
29f097c4 5539 mddev_lock_nointr(mddev);
defad61a
N
5540 __md_stop_writes(mddev);
5541 mddev_unlock(mddev);
5542}
390ee602 5543EXPORT_SYMBOL_GPL(md_stop_writes);
a047e125 5544
5aa61f42
N
5545static void mddev_detach(struct mddev *mddev)
5546{
5547 struct bitmap *bitmap = mddev->bitmap;
5548 /* wait for behind writes to complete */
5549 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
9d48739e
N
5550 pr_debug("md:%s: behind writes in progress - waiting to stop.\n",
5551 mdname(mddev));
5aa61f42
N
5552 /* need to kick something here to make sure I/O goes? */
5553 wait_event(bitmap->behind_wait,
5554 atomic_read(&bitmap->behind_writes) == 0);
5555 }
36d091f4 5556 if (mddev->pers && mddev->pers->quiesce) {
5aa61f42
N
5557 mddev->pers->quiesce(mddev, 1);
5558 mddev->pers->quiesce(mddev, 0);
5559 }
5560 md_unregister_thread(&mddev->thread);
5561 if (mddev->queue)
5562 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5563}
5564
5eff3c43 5565static void __md_stop(struct mddev *mddev)
6177b472 5566{
36d091f4 5567 struct md_personality *pers = mddev->pers;
5aa61f42 5568 mddev_detach(mddev);
ee5d004f
N
5569 /* Ensure ->event_work is done */
5570 flush_workqueue(md_misc_wq);
36d091f4 5571 spin_lock(&mddev->lock);
6177b472 5572 mddev->pers = NULL;
36d091f4
N
5573 spin_unlock(&mddev->lock);
5574 pers->free(mddev, mddev->private);
bd691922 5575 mddev->private = NULL;
36d091f4
N
5576 if (pers->sync_request && mddev->to_remove == NULL)
5577 mddev->to_remove = &md_redundancy_group;
5578 module_put(pers->owner);
cca9cf90 5579 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6177b472 5580}
5eff3c43
N
5581
5582void md_stop(struct mddev *mddev)
5583{
5584 /* stop the array and free an attached data structures.
5585 * This is called from dm-raid
5586 */
5587 __md_stop(mddev);
5588 bitmap_destroy(mddev);
5589 if (mddev->bio_set)
5590 bioset_free(mddev->bio_set);
5591}
5592
390ee602 5593EXPORT_SYMBOL_GPL(md_stop);
6177b472 5594
a05b7ea0 5595static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
a4bd82d0
N
5596{
5597 int err = 0;
30b8feb7
N
5598 int did_freeze = 0;
5599
5600 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5601 did_freeze = 1;
5602 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5603 md_wakeup_thread(mddev->thread);
5604 }
f851b60d 5605 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
30b8feb7 5606 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
f851b60d 5607 if (mddev->sync_thread)
30b8feb7
N
5608 /* Thread might be blocked waiting for metadata update
5609 * which will now never happen */
5610 wake_up_process(mddev->sync_thread->tsk);
f851b60d 5611
2953079c 5612 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
88724bfa 5613 return -EBUSY;
30b8feb7 5614 mddev_unlock(mddev);
f851b60d
N
5615 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5616 &mddev->recovery));
88724bfa 5617 wait_event(mddev->sb_wait,
2953079c 5618 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
30b8feb7
N
5619 mddev_lock_nointr(mddev);
5620
a4bd82d0 5621 mutex_lock(&mddev->open_mutex);
9ba3b7f5 5622 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
30b8feb7 5623 mddev->sync_thread ||
af8d8e6f 5624 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
9d48739e 5625 pr_warn("md: %s still in use.\n",mdname(mddev));
30b8feb7
N
5626 if (did_freeze) {
5627 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
45eaf45d 5628 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
30b8feb7
N
5629 md_wakeup_thread(mddev->thread);
5630 }
a4bd82d0
N
5631 err = -EBUSY;
5632 goto out;
5633 }
5634 if (mddev->pers) {
defad61a 5635 __md_stop_writes(mddev);
a4bd82d0
N
5636
5637 err = -ENXIO;
5638 if (mddev->ro==1)
5639 goto out;
5640 mddev->ro = 1;
5641 set_disk_ro(mddev->gendisk, 1);
5642 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
45eaf45d
N
5643 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5644 md_wakeup_thread(mddev->thread);
00bcb4ac 5645 sysfs_notify_dirent_safe(mddev->sysfs_state);
30b8feb7 5646 err = 0;
a4bd82d0
N
5647 }
5648out:
5649 mutex_unlock(&mddev->open_mutex);
5650 return err;
5651}
5652
9e653b63
N
5653/* mode:
5654 * 0 - completely stop and dis-assemble array
9e653b63
N
5655 * 2 - stop but do not disassemble array
5656 */
f72ffdd6 5657static int do_md_stop(struct mddev *mddev, int mode,
a05b7ea0 5658 struct block_device *bdev)
1da177e4 5659{
1da177e4 5660 struct gendisk *disk = mddev->gendisk;
3cb03002 5661 struct md_rdev *rdev;
30b8feb7
N
5662 int did_freeze = 0;
5663
5664 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5665 did_freeze = 1;
5666 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5667 md_wakeup_thread(mddev->thread);
5668 }
f851b60d 5669 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
30b8feb7 5670 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
f851b60d 5671 if (mddev->sync_thread)
30b8feb7
N
5672 /* Thread might be blocked waiting for metadata update
5673 * which will now never happen */
5674 wake_up_process(mddev->sync_thread->tsk);
f851b60d 5675
30b8feb7 5676 mddev_unlock(mddev);
f851b60d
N
5677 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5678 !test_bit(MD_RECOVERY_RUNNING,
5679 &mddev->recovery)));
30b8feb7 5680 mddev_lock_nointr(mddev);
1da177e4 5681
c8c00a69 5682 mutex_lock(&mddev->open_mutex);
9ba3b7f5 5683 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
30b8feb7
N
5684 mddev->sysfs_active ||
5685 mddev->sync_thread ||
af8d8e6f 5686 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
9d48739e 5687 pr_warn("md: %s still in use.\n",mdname(mddev));
6e17b027 5688 mutex_unlock(&mddev->open_mutex);
30b8feb7
N
5689 if (did_freeze) {
5690 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
45eaf45d 5691 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
30b8feb7
N
5692 md_wakeup_thread(mddev->thread);
5693 }
260fa034
N
5694 return -EBUSY;
5695 }
6e17b027 5696 if (mddev->pers) {
a4bd82d0
N
5697 if (mddev->ro)
5698 set_disk_ro(disk, 0);
409c57f3 5699
defad61a 5700 __md_stop_writes(mddev);
5eff3c43 5701 __md_stop(mddev);
a4bd82d0 5702 mddev->queue->backing_dev_info.congested_fn = NULL;
6177b472 5703
a4bd82d0 5704 /* tell userspace to handle 'inactive' */
00bcb4ac 5705 sysfs_notify_dirent_safe(mddev->sysfs_state);
0d4ca600 5706
dafb20fa 5707 rdev_for_each(rdev, mddev)
36fad858
NK
5708 if (rdev->raid_disk >= 0)
5709 sysfs_unlink_rdev(mddev, rdev);
c4647292 5710
a4bd82d0 5711 set_capacity(disk, 0);
6e17b027 5712 mutex_unlock(&mddev->open_mutex);
f0b4f7e2 5713 mddev->changed = 1;
a4bd82d0 5714 revalidate_disk(disk);
0d4ca600 5715
a4bd82d0
N
5716 if (mddev->ro)
5717 mddev->ro = 0;
6e17b027
N
5718 } else
5719 mutex_unlock(&mddev->open_mutex);
1da177e4
LT
5720 /*
5721 * Free resources if final stop
5722 */
9e653b63 5723 if (mode == 0) {
9d48739e 5724 pr_info("md: %s stopped.\n", mdname(mddev));
1da177e4 5725
978f946b 5726 bitmap_destroy(mddev);
c3d9714e 5727 if (mddev->bitmap_info.file) {
4af1a041
N
5728 struct file *f = mddev->bitmap_info.file;
5729 spin_lock(&mddev->lock);
c3d9714e 5730 mddev->bitmap_info.file = NULL;
4af1a041
N
5731 spin_unlock(&mddev->lock);
5732 fput(f);
978f946b 5733 }
c3d9714e 5734 mddev->bitmap_info.offset = 0;
978f946b 5735
1da177e4
LT
5736 export_array(mddev);
5737
6177b472 5738 md_clean(mddev);
efeb53c0
N
5739 if (mddev->hold_active == UNTIL_STOP)
5740 mddev->hold_active = 0;
a4bd82d0 5741 }
d7603b7e 5742 md_new_event(mddev);
00bcb4ac 5743 sysfs_notify_dirent_safe(mddev->sysfs_state);
6e17b027 5744 return 0;
1da177e4
LT
5745}
5746
fdee8ae4 5747#ifndef MODULE
fd01b88c 5748static void autorun_array(struct mddev *mddev)
1da177e4 5749{
3cb03002 5750 struct md_rdev *rdev;
1da177e4
LT
5751 int err;
5752
a757e64c 5753 if (list_empty(&mddev->disks))
1da177e4 5754 return;
1da177e4 5755
9d48739e 5756 pr_info("md: running: ");
1da177e4 5757
dafb20fa 5758 rdev_for_each(rdev, mddev) {
1da177e4 5759 char b[BDEVNAME_SIZE];
9d48739e 5760 pr_cont("<%s>", bdevname(rdev->bdev,b));
1da177e4 5761 }
9d48739e 5762 pr_cont("\n");
1da177e4 5763
d710e138 5764 err = do_md_run(mddev);
1da177e4 5765 if (err) {
9d48739e 5766 pr_warn("md: do_md_run() returned %d\n", err);
a05b7ea0 5767 do_md_stop(mddev, 0, NULL);
1da177e4
LT
5768 }
5769}
5770
5771/*
5772 * lets try to run arrays based on all disks that have arrived
5773 * until now. (those are in pending_raid_disks)
5774 *
5775 * the method: pick the first pending disk, collect all disks with
5776 * the same UUID, remove all from the pending list and put them into
5777 * the 'same_array' list. Then order this list based on superblock
5778 * update time (freshest comes first), kick out 'old' disks and
5779 * compare superblocks. If everything's fine then run it.
5780 *
5781 * If "unit" is allocated, then bump its reference count
5782 */
5783static void autorun_devices(int part)
5784{
3cb03002 5785 struct md_rdev *rdev0, *rdev, *tmp;
fd01b88c 5786 struct mddev *mddev;
1da177e4
LT
5787 char b[BDEVNAME_SIZE];
5788
9d48739e 5789 pr_info("md: autorun ...\n");
1da177e4 5790 while (!list_empty(&pending_raid_disks)) {
e8703fe1 5791 int unit;
1da177e4 5792 dev_t dev;
ad01c9e3 5793 LIST_HEAD(candidates);
1da177e4 5794 rdev0 = list_entry(pending_raid_disks.next,
3cb03002 5795 struct md_rdev, same_set);
1da177e4 5796
9d48739e 5797 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
1da177e4 5798 INIT_LIST_HEAD(&candidates);
159ec1fc 5799 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
1da177e4 5800 if (super_90_load(rdev, rdev0, 0) >= 0) {
9d48739e
N
5801 pr_debug("md: adding %s ...\n",
5802 bdevname(rdev->bdev,b));
1da177e4
LT
5803 list_move(&rdev->same_set, &candidates);
5804 }
5805 /*
5806 * now we have a set of devices, with all of them having
5807 * mostly sane superblocks. It's time to allocate the
5808 * mddev.
5809 */
e8703fe1
N
5810 if (part) {
5811 dev = MKDEV(mdp_major,
5812 rdev0->preferred_minor << MdpMinorShift);
5813 unit = MINOR(dev) >> MdpMinorShift;
5814 } else {
5815 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5816 unit = MINOR(dev);
5817 }
5818 if (rdev0->preferred_minor != unit) {
9d48739e
N
5819 pr_warn("md: unit number in %s is bad: %d\n",
5820 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
1da177e4
LT
5821 break;
5822 }
1da177e4
LT
5823
5824 md_probe(dev, NULL, NULL);
5825 mddev = mddev_find(dev);
9bbbca3a
NB
5826 if (!mddev || !mddev->gendisk) {
5827 if (mddev)
5828 mddev_put(mddev);
1da177e4
LT
5829 break;
5830 }
f72ffdd6 5831 if (mddev_lock(mddev))
9d48739e 5832 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
1da177e4
LT
5833 else if (mddev->raid_disks || mddev->major_version
5834 || !list_empty(&mddev->disks)) {
9d48739e 5835 pr_warn("md: %s already running, cannot run %s\n",
1da177e4
LT
5836 mdname(mddev), bdevname(rdev0->bdev,b));
5837 mddev_unlock(mddev);
5838 } else {
9d48739e 5839 pr_debug("md: created %s\n", mdname(mddev));
1ec4a939 5840 mddev->persistent = 1;
159ec1fc 5841 rdev_for_each_list(rdev, tmp, &candidates) {
1da177e4
LT
5842 list_del_init(&rdev->same_set);
5843 if (bind_rdev_to_array(rdev, mddev))
5844 export_rdev(rdev);
5845 }
5846 autorun_array(mddev);
5847 mddev_unlock(mddev);
5848 }
5849 /* on success, candidates will be empty, on error
5850 * it won't...
5851 */
159ec1fc 5852 rdev_for_each_list(rdev, tmp, &candidates) {
4b80991c 5853 list_del_init(&rdev->same_set);
1da177e4 5854 export_rdev(rdev);
4b80991c 5855 }
1da177e4
LT
5856 mddev_put(mddev);
5857 }
9d48739e 5858 pr_info("md: ... autorun DONE.\n");
1da177e4 5859}
fdee8ae4 5860#endif /* !MODULE */
1da177e4 5861
f72ffdd6 5862static int get_version(void __user *arg)
1da177e4
LT
5863{
5864 mdu_version_t ver;
5865
5866 ver.major = MD_MAJOR_VERSION;
5867 ver.minor = MD_MINOR_VERSION;
5868 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5869
5870 if (copy_to_user(arg, &ver, sizeof(ver)))
5871 return -EFAULT;
5872
5873 return 0;
5874}
5875
f72ffdd6 5876static int get_array_info(struct mddev *mddev, void __user *arg)
1da177e4
LT
5877{
5878 mdu_array_info_t info;
a9f326eb 5879 int nr,working,insync,failed,spare;
3cb03002 5880 struct md_rdev *rdev;
1da177e4 5881
1ca69c4b
N
5882 nr = working = insync = failed = spare = 0;
5883 rcu_read_lock();
5884 rdev_for_each_rcu(rdev, mddev) {
1da177e4 5885 nr++;
b2d444d7 5886 if (test_bit(Faulty, &rdev->flags))
1da177e4
LT
5887 failed++;
5888 else {
5889 working++;
b2d444d7 5890 if (test_bit(In_sync, &rdev->flags))
f72ffdd6 5891 insync++;
b347af81
SL
5892 else if (test_bit(Journal, &rdev->flags))
5893 /* TODO: add journal count to md_u.h */
5894 ;
1da177e4
LT
5895 else
5896 spare++;
5897 }
5898 }
1ca69c4b 5899 rcu_read_unlock();
1da177e4
LT
5900
5901 info.major_version = mddev->major_version;
5902 info.minor_version = mddev->minor_version;
5903 info.patch_version = MD_PATCHLEVEL_VERSION;
9ebc6ef1 5904 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
1da177e4 5905 info.level = mddev->level;
58c0fed4
AN
5906 info.size = mddev->dev_sectors / 2;
5907 if (info.size != mddev->dev_sectors / 2) /* overflow */
284ae7ca 5908 info.size = -1;
1da177e4
LT
5909 info.nr_disks = nr;
5910 info.raid_disks = mddev->raid_disks;
5911 info.md_minor = mddev->md_minor;
5912 info.not_persistent= !mddev->persistent;
5913
9ebc6ef1 5914 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
1da177e4
LT
5915 info.state = 0;
5916 if (mddev->in_sync)
5917 info.state = (1<<MD_SB_CLEAN);
c3d9714e 5918 if (mddev->bitmap && mddev->bitmap_info.offset)
9bd35920 5919 info.state |= (1<<MD_SB_BITMAP_PRESENT);
ca8895d9
GR
5920 if (mddev_is_clustered(mddev))
5921 info.state |= (1<<MD_SB_CLUSTERED);
a9f326eb 5922 info.active_disks = insync;
1da177e4
LT
5923 info.working_disks = working;
5924 info.failed_disks = failed;
5925 info.spare_disks = spare;
5926
5927 info.layout = mddev->layout;
9d8f0363 5928 info.chunk_size = mddev->chunk_sectors << 9;
1da177e4
LT
5929
5930 if (copy_to_user(arg, &info, sizeof(info)))
5931 return -EFAULT;
5932
5933 return 0;
5934}
5935
f72ffdd6 5936static int get_bitmap_file(struct mddev *mddev, void __user * arg)
32a7627c
N
5937{
5938 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
f4ad3d38 5939 char *ptr;
4af1a041 5940 int err;
32a7627c 5941
b6878d9e 5942 file = kzalloc(sizeof(*file), GFP_NOIO);
32a7627c 5943 if (!file)
4af1a041 5944 return -ENOMEM;
32a7627c 5945
4af1a041
N
5946 err = 0;
5947 spin_lock(&mddev->lock);
25eafe1a
BR
5948 /* bitmap enabled */
5949 if (mddev->bitmap_info.file) {
5950 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5951 sizeof(file->pathname));
5952 if (IS_ERR(ptr))
5953 err = PTR_ERR(ptr);
5954 else
5955 memmove(file->pathname, ptr,
5956 sizeof(file->pathname)-(ptr-file->pathname));
5957 }
4af1a041 5958 spin_unlock(&mddev->lock);
32a7627c 5959
4af1a041
N
5960 if (err == 0 &&
5961 copy_to_user(arg, file, sizeof(*file)))
32a7627c 5962 err = -EFAULT;
4af1a041 5963
32a7627c
N
5964 kfree(file);
5965 return err;
5966}
5967
f72ffdd6 5968static int get_disk_info(struct mddev *mddev, void __user * arg)
1da177e4
LT
5969{
5970 mdu_disk_info_t info;
3cb03002 5971 struct md_rdev *rdev;
1da177e4
LT
5972
5973 if (copy_from_user(&info, arg, sizeof(info)))
5974 return -EFAULT;
5975
1ca69c4b 5976 rcu_read_lock();
57d051dc 5977 rdev = md_find_rdev_nr_rcu(mddev, info.number);
1da177e4
LT
5978 if (rdev) {
5979 info.major = MAJOR(rdev->bdev->bd_dev);
5980 info.minor = MINOR(rdev->bdev->bd_dev);
5981 info.raid_disk = rdev->raid_disk;
5982 info.state = 0;
b2d444d7 5983 if (test_bit(Faulty, &rdev->flags))
1da177e4 5984 info.state |= (1<<MD_DISK_FAULTY);
b2d444d7 5985 else if (test_bit(In_sync, &rdev->flags)) {
1da177e4
LT
5986 info.state |= (1<<MD_DISK_ACTIVE);
5987 info.state |= (1<<MD_DISK_SYNC);
5988 }
9efdca16 5989 if (test_bit(Journal, &rdev->flags))
bac624f3 5990 info.state |= (1<<MD_DISK_JOURNAL);
8ddf9efe
N
5991 if (test_bit(WriteMostly, &rdev->flags))
5992 info.state |= (1<<MD_DISK_WRITEMOSTLY);
688834e6
N
5993 if (test_bit(FailFast, &rdev->flags))
5994 info.state |= (1<<MD_DISK_FAILFAST);
1da177e4
LT
5995 } else {
5996 info.major = info.minor = 0;
5997 info.raid_disk = -1;
5998 info.state = (1<<MD_DISK_REMOVED);
5999 }
1ca69c4b 6000 rcu_read_unlock();
1da177e4
LT
6001
6002 if (copy_to_user(arg, &info, sizeof(info)))
6003 return -EFAULT;
6004
6005 return 0;
6006}
6007
f72ffdd6 6008static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
1da177e4
LT
6009{
6010 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3cb03002 6011 struct md_rdev *rdev;
1da177e4
LT
6012 dev_t dev = MKDEV(info->major,info->minor);
6013
1aee41f6
GR
6014 if (mddev_is_clustered(mddev) &&
6015 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
9d48739e
N
6016 pr_warn("%s: Cannot add to clustered mddev.\n",
6017 mdname(mddev));
1aee41f6
GR
6018 return -EINVAL;
6019 }
6020
1da177e4
LT
6021 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6022 return -EOVERFLOW;
6023
6024 if (!mddev->raid_disks) {
6025 int err;
6026 /* expecting a device which has a superblock */
6027 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6028 if (IS_ERR(rdev)) {
9d48739e 6029 pr_warn("md: md_import_device returned %ld\n",
1da177e4
LT
6030 PTR_ERR(rdev));
6031 return PTR_ERR(rdev);
6032 }
6033 if (!list_empty(&mddev->disks)) {
3cb03002
N
6034 struct md_rdev *rdev0
6035 = list_entry(mddev->disks.next,
6036 struct md_rdev, same_set);
a9f326eb 6037 err = super_types[mddev->major_version]
1da177e4
LT
6038 .load_super(rdev, rdev0, mddev->minor_version);
6039 if (err < 0) {
9d48739e 6040 pr_warn("md: %s has different UUID to %s\n",
f72ffdd6 6041 bdevname(rdev->bdev,b),
1da177e4
LT
6042 bdevname(rdev0->bdev,b2));
6043 export_rdev(rdev);
6044 return -EINVAL;
6045 }
6046 }
6047 err = bind_rdev_to_array(rdev, mddev);
6048 if (err)
6049 export_rdev(rdev);
6050 return err;
6051 }
6052
6053 /*
6054 * add_new_disk can be used once the array is assembled
6055 * to add "hot spares". They must already have a superblock
6056 * written
6057 */
6058 if (mddev->pers) {
6059 int err;
6060 if (!mddev->pers->hot_add_disk) {
9d48739e
N
6061 pr_warn("%s: personality does not support diskops!\n",
6062 mdname(mddev));
1da177e4
LT
6063 return -EINVAL;
6064 }
7b1e35f6
N
6065 if (mddev->persistent)
6066 rdev = md_import_device(dev, mddev->major_version,
6067 mddev->minor_version);
6068 else
6069 rdev = md_import_device(dev, -1, -1);
1da177e4 6070 if (IS_ERR(rdev)) {
9d48739e 6071 pr_warn("md: md_import_device returned %ld\n",
1da177e4
LT
6072 PTR_ERR(rdev));
6073 return PTR_ERR(rdev);
6074 }
1a855a06 6075 /* set saved_raid_disk if appropriate */
41158c7e
N
6076 if (!mddev->persistent) {
6077 if (info->state & (1<<MD_DISK_SYNC) &&
bf572541 6078 info->raid_disk < mddev->raid_disks) {
41158c7e 6079 rdev->raid_disk = info->raid_disk;
bf572541 6080 set_bit(In_sync, &rdev->flags);
8313b8e5 6081 clear_bit(Bitmap_sync, &rdev->flags);
bf572541 6082 } else
41158c7e 6083 rdev->raid_disk = -1;
f466722c 6084 rdev->saved_raid_disk = rdev->raid_disk;
41158c7e
N
6085 } else
6086 super_types[mddev->major_version].
6087 validate_super(mddev, rdev);
bedd86b7 6088 if ((info->state & (1<<MD_DISK_SYNC)) &&
f4563091 6089 rdev->raid_disk != info->raid_disk) {
bedd86b7
N
6090 /* This was a hot-add request, but events doesn't
6091 * match, so reject it.
6092 */
6093 export_rdev(rdev);
6094 return -EINVAL;
6095 }
6096
b2d444d7 6097 clear_bit(In_sync, &rdev->flags); /* just to be sure */
8ddf9efe
N
6098 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6099 set_bit(WriteMostly, &rdev->flags);
575a80fa
N
6100 else
6101 clear_bit(WriteMostly, &rdev->flags);
688834e6
N
6102 if (info->state & (1<<MD_DISK_FAILFAST))
6103 set_bit(FailFast, &rdev->flags);
6104 else
6105 clear_bit(FailFast, &rdev->flags);
8ddf9efe 6106
f6b6ec5c
SL
6107 if (info->state & (1<<MD_DISK_JOURNAL)) {
6108 struct md_rdev *rdev2;
6109 bool has_journal = false;
6110
6111 /* make sure no existing journal disk */
6112 rdev_for_each(rdev2, mddev) {
6113 if (test_bit(Journal, &rdev2->flags)) {
6114 has_journal = true;
6115 break;
6116 }
6117 }
6118 if (has_journal) {
6119 export_rdev(rdev);
6120 return -EBUSY;
6121 }
bac624f3 6122 set_bit(Journal, &rdev->flags);
f6b6ec5c 6123 }
1aee41f6
GR
6124 /*
6125 * check whether the device shows up in other nodes
6126 */
6127 if (mddev_is_clustered(mddev)) {
dbb64f86 6128 if (info->state & (1 << MD_DISK_CANDIDATE))
1aee41f6 6129 set_bit(Candidate, &rdev->flags);
dbb64f86 6130 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
1aee41f6 6131 /* --add initiated by this node */
dbb64f86 6132 err = md_cluster_ops->add_new_disk(mddev, rdev);
1aee41f6 6133 if (err) {
1aee41f6
GR
6134 export_rdev(rdev);
6135 return err;
6136 }
6137 }
6138 }
6139
1da177e4
LT
6140 rdev->raid_disk = -1;
6141 err = bind_rdev_to_array(rdev, mddev);
dbb64f86 6142
1da177e4
LT
6143 if (err)
6144 export_rdev(rdev);
dbb64f86
GR
6145
6146 if (mddev_is_clustered(mddev)) {
e566aef1
GJ
6147 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6148 if (!err) {
6149 err = md_cluster_ops->new_disk_ack(mddev,
6150 err == 0);
6151 if (err)
6152 md_kick_rdev_from_array(rdev);
6153 }
6154 } else {
dbb64f86
GR
6155 if (err)
6156 md_cluster_ops->add_new_disk_cancel(mddev);
6157 else
6158 err = add_bound_rdev(rdev);
6159 }
6160
6161 } else if (!err)
a6da4ef8 6162 err = add_bound_rdev(rdev);
dbb64f86 6163
1da177e4
LT
6164 return err;
6165 }
6166
6167 /* otherwise, add_new_disk is only allowed
6168 * for major_version==0 superblocks
6169 */
6170 if (mddev->major_version != 0) {
9d48739e 6171 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
1da177e4
LT
6172 return -EINVAL;
6173 }
6174
6175 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6176 int err;
d710e138 6177 rdev = md_import_device(dev, -1, 0);
1da177e4 6178 if (IS_ERR(rdev)) {
9d48739e 6179 pr_warn("md: error, md_import_device() returned %ld\n",
1da177e4
LT
6180 PTR_ERR(rdev));
6181 return PTR_ERR(rdev);
6182 }
6183 rdev->desc_nr = info->number;
6184 if (info->raid_disk < mddev->raid_disks)
6185 rdev->raid_disk = info->raid_disk;
6186 else
6187 rdev->raid_disk = -1;
6188
1da177e4 6189 if (rdev->raid_disk < mddev->raid_disks)
b2d444d7
N
6190 if (info->state & (1<<MD_DISK_SYNC))
6191 set_bit(In_sync, &rdev->flags);
1da177e4 6192
8ddf9efe
N
6193 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6194 set_bit(WriteMostly, &rdev->flags);
688834e6
N
6195 if (info->state & (1<<MD_DISK_FAILFAST))
6196 set_bit(FailFast, &rdev->flags);
8ddf9efe 6197
1da177e4 6198 if (!mddev->persistent) {
9d48739e 6199 pr_debug("md: nonpersistent superblock ...\n");
77304d2a
MS
6200 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6201 } else
57b2caa3 6202 rdev->sb_start = calc_dev_sboffset(rdev);
8190e754 6203 rdev->sectors = rdev->sb_start;
1da177e4 6204
2bf071bf
N
6205 err = bind_rdev_to_array(rdev, mddev);
6206 if (err) {
6207 export_rdev(rdev);
6208 return err;
6209 }
1da177e4
LT
6210 }
6211
6212 return 0;
6213}
6214
f72ffdd6 6215static int hot_remove_disk(struct mddev *mddev, dev_t dev)
1da177e4
LT
6216{
6217 char b[BDEVNAME_SIZE];
3cb03002 6218 struct md_rdev *rdev;
1da177e4 6219
1da177e4
LT
6220 rdev = find_rdev(mddev, dev);
6221 if (!rdev)
6222 return -ENXIO;
6223
2910ff17
GR
6224 if (rdev->raid_disk < 0)
6225 goto kick_rdev;
293467aa 6226
3ea8929d
N
6227 clear_bit(Blocked, &rdev->flags);
6228 remove_and_add_spares(mddev, rdev);
6229
1da177e4
LT
6230 if (rdev->raid_disk >= 0)
6231 goto busy;
6232
2910ff17 6233kick_rdev:
54a88392 6234 if (mddev_is_clustered(mddev))
88bcfef7
GR
6235 md_cluster_ops->remove_disk(mddev, rdev);
6236
fb56dfef 6237 md_kick_rdev_from_array(rdev);
2953079c 6238 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
060b0689
N
6239 if (mddev->thread)
6240 md_wakeup_thread(mddev->thread);
6241 else
6242 md_update_sb(mddev, 1);
d7603b7e 6243 md_new_event(mddev);
1da177e4
LT
6244
6245 return 0;
6246busy:
9d48739e
N
6247 pr_debug("md: cannot remove active disk %s from %s ...\n",
6248 bdevname(rdev->bdev,b), mdname(mddev));
1da177e4
LT
6249 return -EBUSY;
6250}
6251
f72ffdd6 6252static int hot_add_disk(struct mddev *mddev, dev_t dev)
1da177e4
LT
6253{
6254 char b[BDEVNAME_SIZE];
6255 int err;
3cb03002 6256 struct md_rdev *rdev;
1da177e4
LT
6257
6258 if (!mddev->pers)
6259 return -ENODEV;
6260
6261 if (mddev->major_version != 0) {
9d48739e 6262 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
1da177e4
LT
6263 mdname(mddev));
6264 return -EINVAL;
6265 }
6266 if (!mddev->pers->hot_add_disk) {
9d48739e 6267 pr_warn("%s: personality does not support diskops!\n",
1da177e4
LT
6268 mdname(mddev));
6269 return -EINVAL;
6270 }
6271
d710e138 6272 rdev = md_import_device(dev, -1, 0);
1da177e4 6273 if (IS_ERR(rdev)) {
9d48739e 6274 pr_warn("md: error, md_import_device() returned %ld\n",
1da177e4
LT
6275 PTR_ERR(rdev));
6276 return -EINVAL;
6277 }
6278
6279 if (mddev->persistent)
57b2caa3 6280 rdev->sb_start = calc_dev_sboffset(rdev);
1da177e4 6281 else
77304d2a 6282 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
1da177e4 6283
8190e754 6284 rdev->sectors = rdev->sb_start;
1da177e4 6285
b2d444d7 6286 if (test_bit(Faulty, &rdev->flags)) {
9d48739e 6287 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
1da177e4
LT
6288 bdevname(rdev->bdev,b), mdname(mddev));
6289 err = -EINVAL;
6290 goto abort_export;
6291 }
293467aa 6292
b2d444d7 6293 clear_bit(In_sync, &rdev->flags);
1da177e4 6294 rdev->desc_nr = -1;
5842730d 6295 rdev->saved_raid_disk = -1;
2bf071bf
N
6296 err = bind_rdev_to_array(rdev, mddev);
6297 if (err)
2aa82191 6298 goto abort_export;
1da177e4
LT
6299
6300 /*
6301 * The rest should better be atomic, we can have disk failures
6302 * noticed in interrupt contexts ...
6303 */
6304
1da177e4
LT
6305 rdev->raid_disk = -1;
6306
2953079c 6307 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
060b0689
N
6308 if (!mddev->thread)
6309 md_update_sb(mddev, 1);
1da177e4
LT
6310 /*
6311 * Kick recovery, maybe this spare has to be added to the
6312 * array immediately.
6313 */
6314 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6315 md_wakeup_thread(mddev->thread);
d7603b7e 6316 md_new_event(mddev);
1da177e4
LT
6317 return 0;
6318
1da177e4
LT
6319abort_export:
6320 export_rdev(rdev);
6321 return err;
6322}
6323
fd01b88c 6324static int set_bitmap_file(struct mddev *mddev, int fd)
32a7627c 6325{
035328c2 6326 int err = 0;
32a7627c 6327
36fa3063 6328 if (mddev->pers) {
d66b1b39 6329 if (!mddev->pers->quiesce || !mddev->thread)
36fa3063
N
6330 return -EBUSY;
6331 if (mddev->recovery || mddev->sync_thread)
6332 return -EBUSY;
6333 /* we should be able to change the bitmap.. */
6334 }
32a7627c 6335
36fa3063 6336 if (fd >= 0) {
035328c2 6337 struct inode *inode;
1e594bb2
N
6338 struct file *f;
6339
6340 if (mddev->bitmap || mddev->bitmap_info.file)
36fa3063 6341 return -EEXIST; /* cannot add when bitmap is present */
1e594bb2 6342 f = fget(fd);
32a7627c 6343
1e594bb2 6344 if (f == NULL) {
9d48739e
N
6345 pr_warn("%s: error: failed to get bitmap file\n",
6346 mdname(mddev));
36fa3063
N
6347 return -EBADF;
6348 }
6349
1e594bb2 6350 inode = f->f_mapping->host;
035328c2 6351 if (!S_ISREG(inode->i_mode)) {
9d48739e
N
6352 pr_warn("%s: error: bitmap file must be a regular file\n",
6353 mdname(mddev));
035328c2 6354 err = -EBADF;
1e594bb2 6355 } else if (!(f->f_mode & FMODE_WRITE)) {
9d48739e
N
6356 pr_warn("%s: error: bitmap file must open for write\n",
6357 mdname(mddev));
035328c2
N
6358 err = -EBADF;
6359 } else if (atomic_read(&inode->i_writecount) != 1) {
9d48739e
N
6360 pr_warn("%s: error: bitmap file is already in use\n",
6361 mdname(mddev));
035328c2
N
6362 err = -EBUSY;
6363 }
6364 if (err) {
1e594bb2 6365 fput(f);
36fa3063
N
6366 return err;
6367 }
1e594bb2 6368 mddev->bitmap_info.file = f;
c3d9714e 6369 mddev->bitmap_info.offset = 0; /* file overrides offset */
36fa3063
N
6370 } else if (mddev->bitmap == NULL)
6371 return -ENOENT; /* cannot remove what isn't there */
6372 err = 0;
6373 if (mddev->pers) {
6374 mddev->pers->quiesce(mddev, 1);
69e51b44 6375 if (fd >= 0) {
f9209a32
GR
6376 struct bitmap *bitmap;
6377
6378 bitmap = bitmap_create(mddev, -1);
6379 if (!IS_ERR(bitmap)) {
6380 mddev->bitmap = bitmap;
69e51b44 6381 err = bitmap_load(mddev);
ba599aca
N
6382 } else
6383 err = PTR_ERR(bitmap);
69e51b44 6384 }
d7375ab3 6385 if (fd < 0 || err) {
36fa3063 6386 bitmap_destroy(mddev);
d7375ab3
N
6387 fd = -1; /* make sure to put the file */
6388 }
36fa3063 6389 mddev->pers->quiesce(mddev, 0);
d7375ab3
N
6390 }
6391 if (fd < 0) {
4af1a041
N
6392 struct file *f = mddev->bitmap_info.file;
6393 if (f) {
6394 spin_lock(&mddev->lock);
6395 mddev->bitmap_info.file = NULL;
6396 spin_unlock(&mddev->lock);
6397 fput(f);
6398 }
36fa3063
N
6399 }
6400
32a7627c
N
6401 return err;
6402}
6403
1da177e4
LT
6404/*
6405 * set_array_info is used two different ways
6406 * The original usage is when creating a new array.
6407 * In this usage, raid_disks is > 0 and it together with
6408 * level, size, not_persistent,layout,chunksize determine the
6409 * shape of the array.
6410 * This will always create an array with a type-0.90.0 superblock.
6411 * The newer usage is when assembling an array.
6412 * In this case raid_disks will be 0, and the major_version field is
6413 * use to determine which style super-blocks are to be found on the devices.
6414 * The minor and patch _version numbers are also kept incase the
6415 * super_block handler wishes to interpret them.
6416 */
f72ffdd6 6417static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
1da177e4
LT
6418{
6419
6420 if (info->raid_disks == 0) {
6421 /* just setting version number for superblock loading */
6422 if (info->major_version < 0 ||
50511da3 6423 info->major_version >= ARRAY_SIZE(super_types) ||
1da177e4
LT
6424 super_types[info->major_version].name == NULL) {
6425 /* maybe try to auto-load a module? */
9d48739e 6426 pr_warn("md: superblock version %d not known\n",
1da177e4
LT
6427 info->major_version);
6428 return -EINVAL;
6429 }
6430 mddev->major_version = info->major_version;
6431 mddev->minor_version = info->minor_version;
6432 mddev->patch_version = info->patch_version;
3f9d7b0d 6433 mddev->persistent = !info->not_persistent;
cbd19983
N
6434 /* ensure mddev_put doesn't delete this now that there
6435 * is some minimal configuration.
6436 */
9ebc6ef1 6437 mddev->ctime = ktime_get_real_seconds();
1da177e4
LT
6438 return 0;
6439 }
6440 mddev->major_version = MD_MAJOR_VERSION;
6441 mddev->minor_version = MD_MINOR_VERSION;
6442 mddev->patch_version = MD_PATCHLEVEL_VERSION;
9ebc6ef1 6443 mddev->ctime = ktime_get_real_seconds();
1da177e4
LT
6444
6445 mddev->level = info->level;
17115e03 6446 mddev->clevel[0] = 0;
58c0fed4 6447 mddev->dev_sectors = 2 * (sector_t)info->size;
1da177e4
LT
6448 mddev->raid_disks = info->raid_disks;
6449 /* don't set md_minor, it is determined by which /dev/md* was
6450 * openned
6451 */
6452 if (info->state & (1<<MD_SB_CLEAN))
6453 mddev->recovery_cp = MaxSector;
6454 else
6455 mddev->recovery_cp = 0;
6456 mddev->persistent = ! info->not_persistent;
e691063a 6457 mddev->external = 0;
1da177e4
LT
6458
6459 mddev->layout = info->layout;
9d8f0363 6460 mddev->chunk_sectors = info->chunk_size >> 9;
1da177e4
LT
6461
6462 mddev->max_disks = MD_SB_DISKS;
6463
2953079c 6464 if (mddev->persistent) {
e691063a 6465 mddev->flags = 0;
2953079c
SL
6466 mddev->sb_flags = 0;
6467 }
6468 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
1da177e4 6469
c3d9714e 6470 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6409bb05 6471 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
c3d9714e 6472 mddev->bitmap_info.offset = 0;
b2a2703c 6473
f6705578
N
6474 mddev->reshape_position = MaxSector;
6475
1da177e4
LT
6476 /*
6477 * Generate a 128 bit UUID
6478 */
6479 get_random_bytes(mddev->uuid, 16);
6480
f6705578 6481 mddev->new_level = mddev->level;
664e7c41 6482 mddev->new_chunk_sectors = mddev->chunk_sectors;
f6705578
N
6483 mddev->new_layout = mddev->layout;
6484 mddev->delta_disks = 0;
2c810cdd 6485 mddev->reshape_backwards = 0;
f6705578 6486
1da177e4
LT
6487 return 0;
6488}
6489
fd01b88c 6490void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
1f403624 6491{
b522adcd
DW
6492 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6493
6494 if (mddev->external_size)
6495 return;
6496
1f403624
DW
6497 mddev->array_sectors = array_sectors;
6498}
6499EXPORT_SYMBOL(md_set_array_sectors);
6500
fd01b88c 6501static int update_size(struct mddev *mddev, sector_t num_sectors)
a35b0d69 6502{
3cb03002 6503 struct md_rdev *rdev;
a35b0d69 6504 int rv;
d71f9f88 6505 int fit = (num_sectors == 0);
a35b0d69 6506
ab5a98b1
GJ
6507 /* cluster raid doesn't support update size */
6508 if (mddev_is_clustered(mddev))
6509 return -EINVAL;
6510
a35b0d69
N
6511 if (mddev->pers->resize == NULL)
6512 return -EINVAL;
d71f9f88
AN
6513 /* The "num_sectors" is the number of sectors of each device that
6514 * is used. This can only make sense for arrays with redundancy.
6515 * linear and raid0 always use whatever space is available. We can only
6516 * consider changing this number if no resync or reconstruction is
6517 * happening, and if the new size is acceptable. It must fit before the
0f420358 6518 * sb_start or, if that is <data_offset, it must fit before the size
d71f9f88
AN
6519 * of each device. If num_sectors is zero, we find the largest size
6520 * that fits.
a35b0d69 6521 */
f851b60d
N
6522 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6523 mddev->sync_thread)
a35b0d69 6524 return -EBUSY;
bd8839e0
N
6525 if (mddev->ro)
6526 return -EROFS;
a4a6125a 6527
dafb20fa 6528 rdev_for_each(rdev, mddev) {
dd8ac336 6529 sector_t avail = rdev->sectors;
01ab5662 6530
d71f9f88
AN
6531 if (fit && (num_sectors == 0 || num_sectors > avail))
6532 num_sectors = avail;
6533 if (avail < num_sectors)
a35b0d69
N
6534 return -ENOSPC;
6535 }
d71f9f88 6536 rv = mddev->pers->resize(mddev, num_sectors);
449aad3e
N
6537 if (!rv)
6538 revalidate_disk(mddev->gendisk);
a35b0d69
N
6539 return rv;
6540}
6541
fd01b88c 6542static int update_raid_disks(struct mddev *mddev, int raid_disks)
da943b99
N
6543{
6544 int rv;
c6563a8c 6545 struct md_rdev *rdev;
da943b99 6546 /* change the number of raid disks */
63c70c4f 6547 if (mddev->pers->check_reshape == NULL)
da943b99 6548 return -EINVAL;
bd8839e0
N
6549 if (mddev->ro)
6550 return -EROFS;
da943b99 6551 if (raid_disks <= 0 ||
233fca36 6552 (mddev->max_disks && raid_disks >= mddev->max_disks))
da943b99 6553 return -EINVAL;
f851b60d
N
6554 if (mddev->sync_thread ||
6555 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6556 mddev->reshape_position != MaxSector)
da943b99 6557 return -EBUSY;
c6563a8c
N
6558
6559 rdev_for_each(rdev, mddev) {
6560 if (mddev->raid_disks < raid_disks &&
6561 rdev->data_offset < rdev->new_data_offset)
6562 return -EINVAL;
6563 if (mddev->raid_disks > raid_disks &&
6564 rdev->data_offset > rdev->new_data_offset)
6565 return -EINVAL;
6566 }
6567
63c70c4f 6568 mddev->delta_disks = raid_disks - mddev->raid_disks;
2c810cdd
N
6569 if (mddev->delta_disks < 0)
6570 mddev->reshape_backwards = 1;
6571 else if (mddev->delta_disks > 0)
6572 mddev->reshape_backwards = 0;
63c70c4f
N
6573
6574 rv = mddev->pers->check_reshape(mddev);
2c810cdd 6575 if (rv < 0) {
de171cb9 6576 mddev->delta_disks = 0;
2c810cdd
N
6577 mddev->reshape_backwards = 0;
6578 }
da943b99
N
6579 return rv;
6580}
6581
1da177e4
LT
6582/*
6583 * update_array_info is used to change the configuration of an
6584 * on-line array.
6585 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6586 * fields in the info are checked against the array.
6587 * Any differences that cannot be handled will cause an error.
6588 * Normally, only one change can be managed at a time.
6589 */
fd01b88c 6590static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
1da177e4
LT
6591{
6592 int rv = 0;
6593 int cnt = 0;
36fa3063
N
6594 int state = 0;
6595
6596 /* calculate expected state,ignoring low bits */
c3d9714e 6597 if (mddev->bitmap && mddev->bitmap_info.offset)
36fa3063 6598 state |= (1 << MD_SB_BITMAP_PRESENT);
1da177e4
LT
6599
6600 if (mddev->major_version != info->major_version ||
6601 mddev->minor_version != info->minor_version ||
6602/* mddev->patch_version != info->patch_version || */
6603 mddev->ctime != info->ctime ||
6604 mddev->level != info->level ||
6605/* mddev->layout != info->layout || */
4e023612 6606 mddev->persistent != !info->not_persistent ||
9d8f0363 6607 mddev->chunk_sectors != info->chunk_size >> 9 ||
36fa3063
N
6608 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6609 ((state^info->state) & 0xfffffe00)
6610 )
1da177e4
LT
6611 return -EINVAL;
6612 /* Check there is only one change */
58c0fed4
AN
6613 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6614 cnt++;
6615 if (mddev->raid_disks != info->raid_disks)
6616 cnt++;
6617 if (mddev->layout != info->layout)
6618 cnt++;
6619 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6620 cnt++;
6621 if (cnt == 0)
6622 return 0;
6623 if (cnt > 1)
6624 return -EINVAL;
1da177e4
LT
6625
6626 if (mddev->layout != info->layout) {
6627 /* Change layout
6628 * we don't need to do anything at the md level, the
6629 * personality will take care of it all.
6630 */
50ac168a 6631 if (mddev->pers->check_reshape == NULL)
1da177e4 6632 return -EINVAL;
597a711b
N
6633 else {
6634 mddev->new_layout = info->layout;
50ac168a 6635 rv = mddev->pers->check_reshape(mddev);
597a711b
N
6636 if (rv)
6637 mddev->new_layout = mddev->layout;
6638 return rv;
6639 }
1da177e4 6640 }
58c0fed4 6641 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
d71f9f88 6642 rv = update_size(mddev, (sector_t)info->size * 2);
a35b0d69 6643
da943b99
N
6644 if (mddev->raid_disks != info->raid_disks)
6645 rv = update_raid_disks(mddev, info->raid_disks);
6646
36fa3063 6647 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
293467aa
GR
6648 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6649 rv = -EINVAL;
6650 goto err;
6651 }
6652 if (mddev->recovery || mddev->sync_thread) {
6653 rv = -EBUSY;
6654 goto err;
6655 }
36fa3063 6656 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
f9209a32 6657 struct bitmap *bitmap;
36fa3063 6658 /* add the bitmap */
293467aa
GR
6659 if (mddev->bitmap) {
6660 rv = -EEXIST;
6661 goto err;
6662 }
6663 if (mddev->bitmap_info.default_offset == 0) {
6664 rv = -EINVAL;
6665 goto err;
6666 }
c3d9714e
N
6667 mddev->bitmap_info.offset =
6668 mddev->bitmap_info.default_offset;
6409bb05
N
6669 mddev->bitmap_info.space =
6670 mddev->bitmap_info.default_space;
36fa3063 6671 mddev->pers->quiesce(mddev, 1);
f9209a32
GR
6672 bitmap = bitmap_create(mddev, -1);
6673 if (!IS_ERR(bitmap)) {
6674 mddev->bitmap = bitmap;
69e51b44 6675 rv = bitmap_load(mddev);
ba599aca
N
6676 } else
6677 rv = PTR_ERR(bitmap);
36fa3063
N
6678 if (rv)
6679 bitmap_destroy(mddev);
6680 mddev->pers->quiesce(mddev, 0);
6681 } else {
6682 /* remove the bitmap */
293467aa
GR
6683 if (!mddev->bitmap) {
6684 rv = -ENOENT;
6685 goto err;
6686 }
6687 if (mddev->bitmap->storage.file) {
6688 rv = -EINVAL;
6689 goto err;
6690 }
f6a2dc64
GJ
6691 if (mddev->bitmap_info.nodes) {
6692 /* hold PW on all the bitmap lock */
6693 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
9d48739e 6694 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
f6a2dc64
GJ
6695 rv = -EPERM;
6696 md_cluster_ops->unlock_all_bitmaps(mddev);
6697 goto err;
6698 }
6699
6700 mddev->bitmap_info.nodes = 0;
6701 md_cluster_ops->leave(mddev);
6702 }
36fa3063
N
6703 mddev->pers->quiesce(mddev, 1);
6704 bitmap_destroy(mddev);
6705 mddev->pers->quiesce(mddev, 0);
c3d9714e 6706 mddev->bitmap_info.offset = 0;
36fa3063
N
6707 }
6708 }
850b2b42 6709 md_update_sb(mddev, 1);
293467aa
GR
6710 return rv;
6711err:
1da177e4
LT
6712 return rv;
6713}
6714
fd01b88c 6715static int set_disk_faulty(struct mddev *mddev, dev_t dev)
1da177e4 6716{
3cb03002 6717 struct md_rdev *rdev;
1ca69c4b 6718 int err = 0;
1da177e4
LT
6719
6720 if (mddev->pers == NULL)
6721 return -ENODEV;
6722
1ca69c4b
N
6723 rcu_read_lock();
6724 rdev = find_rdev_rcu(mddev, dev);
1da177e4 6725 if (!rdev)
1ca69c4b
N
6726 err = -ENODEV;
6727 else {
6728 md_error(mddev, rdev);
6729 if (!test_bit(Faulty, &rdev->flags))
6730 err = -EBUSY;
6731 }
6732 rcu_read_unlock();
6733 return err;
1da177e4
LT
6734}
6735
2f9618ce
AN
6736/*
6737 * We have a problem here : there is no easy way to give a CHS
6738 * virtual geometry. We currently pretend that we have a 2 heads
6739 * 4 sectors (with a BIG number of cylinders...). This drives
6740 * dosfs just mad... ;-)
6741 */
a885c8c4
CH
6742static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6743{
fd01b88c 6744 struct mddev *mddev = bdev->bd_disk->private_data;
a885c8c4
CH
6745
6746 geo->heads = 2;
6747 geo->sectors = 4;
49ce6cea 6748 geo->cylinders = mddev->array_sectors / 8;
a885c8c4
CH
6749 return 0;
6750}
6751
cb335f88
NS
6752static inline bool md_ioctl_valid(unsigned int cmd)
6753{
6754 switch (cmd) {
6755 case ADD_NEW_DISK:
6756 case BLKROSET:
6757 case GET_ARRAY_INFO:
6758 case GET_BITMAP_FILE:
6759 case GET_DISK_INFO:
6760 case HOT_ADD_DISK:
6761 case HOT_REMOVE_DISK:
cb335f88
NS
6762 case RAID_AUTORUN:
6763 case RAID_VERSION:
6764 case RESTART_ARRAY_RW:
6765 case RUN_ARRAY:
6766 case SET_ARRAY_INFO:
6767 case SET_BITMAP_FILE:
6768 case SET_DISK_FAULTY:
6769 case STOP_ARRAY:
6770 case STOP_ARRAY_RO:
1aee41f6 6771 case CLUSTERED_DISK_NACK:
cb335f88
NS
6772 return true;
6773 default:
6774 return false;
6775 }
6776}
6777
a39907fa 6778static int md_ioctl(struct block_device *bdev, fmode_t mode,
1da177e4
LT
6779 unsigned int cmd, unsigned long arg)
6780{
6781 int err = 0;
6782 void __user *argp = (void __user *)arg;
fd01b88c 6783 struct mddev *mddev = NULL;
e2218350 6784 int ro;
1da177e4 6785
cb335f88
NS
6786 if (!md_ioctl_valid(cmd))
6787 return -ENOTTY;
6788
506c9e44
N
6789 switch (cmd) {
6790 case RAID_VERSION:
6791 case GET_ARRAY_INFO:
6792 case GET_DISK_INFO:
6793 break;
6794 default:
6795 if (!capable(CAP_SYS_ADMIN))
6796 return -EACCES;
6797 }
1da177e4
LT
6798
6799 /*
6800 * Commands dealing with the RAID driver but not any
6801 * particular array:
6802 */
c02c0aeb
N
6803 switch (cmd) {
6804 case RAID_VERSION:
6805 err = get_version(argp);
3adc28d8 6806 goto out;
1da177e4 6807
1da177e4 6808#ifndef MODULE
c02c0aeb
N
6809 case RAID_AUTORUN:
6810 err = 0;
6811 autostart_arrays(arg);
3adc28d8 6812 goto out;
1da177e4 6813#endif
c02c0aeb 6814 default:;
1da177e4
LT
6815 }
6816
6817 /*
6818 * Commands creating/starting a new array:
6819 */
6820
a39907fa 6821 mddev = bdev->bd_disk->private_data;
1da177e4
LT
6822
6823 if (!mddev) {
6824 BUG();
3adc28d8 6825 goto out;
1da177e4
LT
6826 }
6827
1ca69c4b
N
6828 /* Some actions do not requires the mutex */
6829 switch (cmd) {
6830 case GET_ARRAY_INFO:
6831 if (!mddev->raid_disks && !mddev->external)
6832 err = -ENODEV;
6833 else
6834 err = get_array_info(mddev, argp);
3adc28d8 6835 goto out;
1ca69c4b
N
6836
6837 case GET_DISK_INFO:
6838 if (!mddev->raid_disks && !mddev->external)
6839 err = -ENODEV;
6840 else
6841 err = get_disk_info(mddev, argp);
3adc28d8 6842 goto out;
1ca69c4b
N
6843
6844 case SET_DISK_FAULTY:
6845 err = set_disk_faulty(mddev, new_decode_dev(arg));
3adc28d8 6846 goto out;
4af1a041
N
6847
6848 case GET_BITMAP_FILE:
6849 err = get_bitmap_file(mddev, argp);
6850 goto out;
6851
1ca69c4b
N
6852 }
6853
a7a3f08d
N
6854 if (cmd == ADD_NEW_DISK)
6855 /* need to ensure md_delayed_delete() has completed */
6856 flush_workqueue(md_misc_wq);
6857
90f5f7ad
HR
6858 if (cmd == HOT_REMOVE_DISK)
6859 /* need to ensure recovery thread has run */
6860 wait_event_interruptible_timeout(mddev->sb_wait,
6861 !test_bit(MD_RECOVERY_NEEDED,
82a301cb 6862 &mddev->recovery),
90f5f7ad 6863 msecs_to_jiffies(5000));
260fa034
N
6864 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6865 /* Need to flush page cache, and ensure no-one else opens
6866 * and writes
6867 */
6868 mutex_lock(&mddev->open_mutex);
9ba3b7f5 6869 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
260fa034
N
6870 mutex_unlock(&mddev->open_mutex);
6871 err = -EBUSY;
3adc28d8 6872 goto out;
260fa034 6873 }
af8d8e6f 6874 set_bit(MD_CLOSING, &mddev->flags);
260fa034
N
6875 mutex_unlock(&mddev->open_mutex);
6876 sync_blockdev(bdev);
6877 }
1da177e4
LT
6878 err = mddev_lock(mddev);
6879 if (err) {
9d48739e
N
6880 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
6881 err, cmd);
3adc28d8 6882 goto out;
1da177e4
LT
6883 }
6884
c02c0aeb
N
6885 if (cmd == SET_ARRAY_INFO) {
6886 mdu_array_info_t info;
6887 if (!arg)
6888 memset(&info, 0, sizeof(info));
6889 else if (copy_from_user(&info, argp, sizeof(info))) {
6890 err = -EFAULT;
3adc28d8 6891 goto unlock;
c02c0aeb
N
6892 }
6893 if (mddev->pers) {
6894 err = update_array_info(mddev, &info);
6895 if (err) {
9d48739e 6896 pr_warn("md: couldn't update array info. %d\n", err);
3adc28d8 6897 goto unlock;
1da177e4 6898 }
3adc28d8 6899 goto unlock;
c02c0aeb
N
6900 }
6901 if (!list_empty(&mddev->disks)) {
9d48739e 6902 pr_warn("md: array %s already has disks!\n", mdname(mddev));
c02c0aeb 6903 err = -EBUSY;
3adc28d8 6904 goto unlock;
c02c0aeb
N
6905 }
6906 if (mddev->raid_disks) {
9d48739e 6907 pr_warn("md: array %s already initialised!\n", mdname(mddev));
c02c0aeb 6908 err = -EBUSY;
3adc28d8 6909 goto unlock;
c02c0aeb
N
6910 }
6911 err = set_array_info(mddev, &info);
6912 if (err) {
9d48739e 6913 pr_warn("md: couldn't set array info. %d\n", err);
3adc28d8 6914 goto unlock;
c02c0aeb 6915 }
3adc28d8 6916 goto unlock;
1da177e4
LT
6917 }
6918
6919 /*
6920 * Commands querying/configuring an existing array:
6921 */
32a7627c 6922 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3f9d7b0d 6923 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
a17184a9
N
6924 if ((!mddev->raid_disks && !mddev->external)
6925 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6926 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6927 && cmd != GET_BITMAP_FILE) {
1da177e4 6928 err = -ENODEV;
3adc28d8 6929 goto unlock;
1da177e4
LT
6930 }
6931
6932 /*
6933 * Commands even a read-only array can execute:
6934 */
c02c0aeb 6935 switch (cmd) {
c02c0aeb
N
6936 case RESTART_ARRAY_RW:
6937 err = restart_array(mddev);
3adc28d8 6938 goto unlock;
1da177e4 6939
c02c0aeb
N
6940 case STOP_ARRAY:
6941 err = do_md_stop(mddev, 0, bdev);
3adc28d8 6942 goto unlock;
1da177e4 6943
c02c0aeb
N
6944 case STOP_ARRAY_RO:
6945 err = md_set_readonly(mddev, bdev);
3adc28d8 6946 goto unlock;
1da177e4 6947
3ea8929d
N
6948 case HOT_REMOVE_DISK:
6949 err = hot_remove_disk(mddev, new_decode_dev(arg));
3adc28d8 6950 goto unlock;
3ea8929d 6951
7ceb17e8
N
6952 case ADD_NEW_DISK:
6953 /* We can support ADD_NEW_DISK on read-only arrays
466ad292 6954 * only if we are re-adding a preexisting device.
7ceb17e8
N
6955 * So require mddev->pers and MD_DISK_SYNC.
6956 */
6957 if (mddev->pers) {
6958 mdu_disk_info_t info;
6959 if (copy_from_user(&info, argp, sizeof(info)))
6960 err = -EFAULT;
6961 else if (!(info.state & (1<<MD_DISK_SYNC)))
6962 /* Need to clear read-only for this */
6963 break;
6964 else
6965 err = add_new_disk(mddev, &info);
3adc28d8 6966 goto unlock;
7ceb17e8
N
6967 }
6968 break;
6969
c02c0aeb
N
6970 case BLKROSET:
6971 if (get_user(ro, (int __user *)(arg))) {
6972 err = -EFAULT;
3adc28d8 6973 goto unlock;
c02c0aeb
N
6974 }
6975 err = -EINVAL;
e2218350 6976
c02c0aeb
N
6977 /* if the bdev is going readonly the value of mddev->ro
6978 * does not matter, no writes are coming
6979 */
6980 if (ro)
3adc28d8 6981 goto unlock;
e2218350 6982
c02c0aeb
N
6983 /* are we are already prepared for writes? */
6984 if (mddev->ro != 1)
3adc28d8 6985 goto unlock;
e2218350 6986
c02c0aeb
N
6987 /* transitioning to readauto need only happen for
6988 * arrays that call md_write_start
6989 */
6990 if (mddev->pers) {
6991 err = restart_array(mddev);
6992 if (err == 0) {
6993 mddev->ro = 2;
6994 set_disk_ro(mddev->gendisk, 0);
e2218350 6995 }
c02c0aeb 6996 }
3adc28d8 6997 goto unlock;
1da177e4
LT
6998 }
6999
7000 /*
7001 * The remaining ioctls are changing the state of the
f91de92e 7002 * superblock, so we do not allow them on read-only arrays.
1da177e4 7003 */
326eb17d 7004 if (mddev->ro && mddev->pers) {
f91de92e
N
7005 if (mddev->ro == 2) {
7006 mddev->ro = 0;
00bcb4ac 7007 sysfs_notify_dirent_safe(mddev->sysfs_state);
0fd62b86 7008 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
f3378b48
N
7009 /* mddev_unlock will wake thread */
7010 /* If a device failed while we were read-only, we
7011 * need to make sure the metadata is updated now.
7012 */
2953079c 7013 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
f3378b48
N
7014 mddev_unlock(mddev);
7015 wait_event(mddev->sb_wait,
2953079c
SL
7016 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7017 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
29f097c4 7018 mddev_lock_nointr(mddev);
f3378b48 7019 }
f91de92e
N
7020 } else {
7021 err = -EROFS;
3adc28d8 7022 goto unlock;
f91de92e 7023 }
1da177e4
LT
7024 }
7025
c02c0aeb
N
7026 switch (cmd) {
7027 case ADD_NEW_DISK:
1da177e4 7028 {
c02c0aeb
N
7029 mdu_disk_info_t info;
7030 if (copy_from_user(&info, argp, sizeof(info)))
7031 err = -EFAULT;
7032 else
7033 err = add_new_disk(mddev, &info);
3adc28d8 7034 goto unlock;
c02c0aeb 7035 }
1da177e4 7036
1aee41f6
GR
7037 case CLUSTERED_DISK_NACK:
7038 if (mddev_is_clustered(mddev))
7039 md_cluster_ops->new_disk_ack(mddev, false);
7040 else
7041 err = -EINVAL;
7042 goto unlock;
7043
c02c0aeb
N
7044 case HOT_ADD_DISK:
7045 err = hot_add_disk(mddev, new_decode_dev(arg));
3adc28d8 7046 goto unlock;
1da177e4 7047
c02c0aeb
N
7048 case RUN_ARRAY:
7049 err = do_md_run(mddev);
3adc28d8 7050 goto unlock;
1da177e4 7051
c02c0aeb
N
7052 case SET_BITMAP_FILE:
7053 err = set_bitmap_file(mddev, (int)arg);
3adc28d8 7054 goto unlock;
32a7627c 7055
c02c0aeb
N
7056 default:
7057 err = -EINVAL;
3adc28d8 7058 goto unlock;
1da177e4
LT
7059 }
7060
3adc28d8 7061unlock:
d3374825
N
7062 if (mddev->hold_active == UNTIL_IOCTL &&
7063 err != -EINVAL)
7064 mddev->hold_active = 0;
1da177e4 7065 mddev_unlock(mddev);
3adc28d8 7066out:
1da177e4
LT
7067 return err;
7068}
aa98aa31
AB
7069#ifdef CONFIG_COMPAT
7070static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7071 unsigned int cmd, unsigned long arg)
7072{
7073 switch (cmd) {
7074 case HOT_REMOVE_DISK:
7075 case HOT_ADD_DISK:
7076 case SET_DISK_FAULTY:
7077 case SET_BITMAP_FILE:
7078 /* These take in integer arg, do not convert */
7079 break;
7080 default:
7081 arg = (unsigned long)compat_ptr(arg);
7082 break;
7083 }
7084
7085 return md_ioctl(bdev, mode, cmd, arg);
7086}
7087#endif /* CONFIG_COMPAT */
1da177e4 7088
a39907fa 7089static int md_open(struct block_device *bdev, fmode_t mode)
1da177e4
LT
7090{
7091 /*
7092 * Succeed if we can lock the mddev, which confirms that
7093 * it isn't being stopped right now.
7094 */
fd01b88c 7095 struct mddev *mddev = mddev_find(bdev->bd_dev);
1da177e4
LT
7096 int err;
7097
0c098220
YL
7098 if (!mddev)
7099 return -ENODEV;
7100
d3374825
N
7101 if (mddev->gendisk != bdev->bd_disk) {
7102 /* we are racing with mddev_put which is discarding this
7103 * bd_disk.
7104 */
7105 mddev_put(mddev);
7106 /* Wait until bdev->bd_disk is definitely gone */
e804ac78 7107 flush_workqueue(md_misc_wq);
d3374825
N
7108 /* Then retry the open from the top */
7109 return -ERESTARTSYS;
7110 }
7111 BUG_ON(mddev != bdev->bd_disk->private_data);
7112
c8c00a69 7113 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
1da177e4
LT
7114 goto out;
7115
af8d8e6f
GJ
7116 if (test_bit(MD_CLOSING, &mddev->flags)) {
7117 mutex_unlock(&mddev->open_mutex);
e2342ca8
N
7118 err = -ENODEV;
7119 goto out;
af8d8e6f
GJ
7120 }
7121
1da177e4 7122 err = 0;
f2ea68cf 7123 atomic_inc(&mddev->openers);
c8c00a69 7124 mutex_unlock(&mddev->open_mutex);
1da177e4 7125
f0b4f7e2 7126 check_disk_change(bdev);
1da177e4 7127 out:
e2342ca8
N
7128 if (err)
7129 mddev_put(mddev);
1da177e4
LT
7130 return err;
7131}
7132
db2a144b 7133static void md_release(struct gendisk *disk, fmode_t mode)
1da177e4 7134{
f72ffdd6 7135 struct mddev *mddev = disk->private_data;
1da177e4 7136
52e5f9d1 7137 BUG_ON(!mddev);
f2ea68cf 7138 atomic_dec(&mddev->openers);
1da177e4 7139 mddev_put(mddev);
1da177e4 7140}
f0b4f7e2
N
7141
7142static int md_media_changed(struct gendisk *disk)
7143{
fd01b88c 7144 struct mddev *mddev = disk->private_data;
f0b4f7e2
N
7145
7146 return mddev->changed;
7147}
7148
7149static int md_revalidate(struct gendisk *disk)
7150{
fd01b88c 7151 struct mddev *mddev = disk->private_data;
f0b4f7e2
N
7152
7153 mddev->changed = 0;
7154 return 0;
7155}
83d5cde4 7156static const struct block_device_operations md_fops =
1da177e4
LT
7157{
7158 .owner = THIS_MODULE,
a39907fa
AV
7159 .open = md_open,
7160 .release = md_release,
b492b852 7161 .ioctl = md_ioctl,
aa98aa31
AB
7162#ifdef CONFIG_COMPAT
7163 .compat_ioctl = md_compat_ioctl,
7164#endif
a885c8c4 7165 .getgeo = md_getgeo,
f0b4f7e2
N
7166 .media_changed = md_media_changed,
7167 .revalidate_disk= md_revalidate,
1da177e4
LT
7168};
7169
f72ffdd6 7170static int md_thread(void *arg)
1da177e4 7171{
2b8bf345 7172 struct md_thread *thread = arg;
1da177e4 7173
1da177e4
LT
7174 /*
7175 * md_thread is a 'system-thread', it's priority should be very
7176 * high. We avoid resource deadlocks individually in each
7177 * raid personality. (RAID5 does preallocation) We also use RR and
7178 * the very same RT priority as kswapd, thus we will never get
7179 * into a priority inversion deadlock.
7180 *
7181 * we definitely have to have equal or higher priority than
7182 * bdflush, otherwise bdflush will deadlock if there are too
7183 * many dirty RAID5 blocks.
7184 */
1da177e4 7185
6985c43f 7186 allow_signal(SIGKILL);
a6fb0934 7187 while (!kthread_should_stop()) {
1da177e4 7188
93588e22
N
7189 /* We need to wait INTERRUPTIBLE so that
7190 * we don't add to the load-average.
7191 * That means we need to be sure no signals are
7192 * pending
7193 */
7194 if (signal_pending(current))
7195 flush_signals(current);
7196
7197 wait_event_interruptible_timeout
7198 (thread->wqueue,
7199 test_bit(THREAD_WAKEUP, &thread->flags)
ce1ccd07 7200 || kthread_should_stop() || kthread_should_park(),
93588e22 7201 thread->timeout);
1da177e4 7202
6c987910 7203 clear_bit(THREAD_WAKEUP, &thread->flags);
ce1ccd07
SL
7204 if (kthread_should_park())
7205 kthread_parkme();
6c987910 7206 if (!kthread_should_stop())
4ed8731d 7207 thread->run(thread);
1da177e4 7208 }
a6fb0934 7209
1da177e4
LT
7210 return 0;
7211}
7212
2b8bf345 7213void md_wakeup_thread(struct md_thread *thread)
1da177e4
LT
7214{
7215 if (thread) {
36a4e1fe 7216 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
1da177e4
LT
7217 set_bit(THREAD_WAKEUP, &thread->flags);
7218 wake_up(&thread->wqueue);
7219 }
7220}
6c144d31 7221EXPORT_SYMBOL(md_wakeup_thread);
1da177e4 7222
4ed8731d
SL
7223struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7224 struct mddev *mddev, const char *name)
1da177e4 7225{
2b8bf345 7226 struct md_thread *thread;
1da177e4 7227
2b8bf345 7228 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
1da177e4
LT
7229 if (!thread)
7230 return NULL;
7231
1da177e4
LT
7232 init_waitqueue_head(&thread->wqueue);
7233
1da177e4
LT
7234 thread->run = run;
7235 thread->mddev = mddev;
32a7627c 7236 thread->timeout = MAX_SCHEDULE_TIMEOUT;
0da3c619
N
7237 thread->tsk = kthread_run(md_thread, thread,
7238 "%s_%s",
7239 mdname(thread->mddev),
0232605d 7240 name);
a6fb0934 7241 if (IS_ERR(thread->tsk)) {
1da177e4
LT
7242 kfree(thread);
7243 return NULL;
7244 }
1da177e4
LT
7245 return thread;
7246}
6c144d31 7247EXPORT_SYMBOL(md_register_thread);
1da177e4 7248
2b8bf345 7249void md_unregister_thread(struct md_thread **threadp)
1da177e4 7250{
2b8bf345 7251 struct md_thread *thread = *threadp;
e0cf8f04
N
7252 if (!thread)
7253 return;
36a4e1fe 7254 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
01f96c0a
N
7255 /* Locking ensures that mddev_unlock does not wake_up a
7256 * non-existent thread
7257 */
7258 spin_lock(&pers_lock);
7259 *threadp = NULL;
7260 spin_unlock(&pers_lock);
a6fb0934
N
7261
7262 kthread_stop(thread->tsk);
1da177e4
LT
7263 kfree(thread);
7264}
6c144d31 7265EXPORT_SYMBOL(md_unregister_thread);
1da177e4 7266
fd01b88c 7267void md_error(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 7268{
b2d444d7 7269 if (!rdev || test_bit(Faulty, &rdev->flags))
1da177e4 7270 return;
6bfe0b49 7271
de393cde 7272 if (!mddev->pers || !mddev->pers->error_handler)
1da177e4
LT
7273 return;
7274 mddev->pers->error_handler(mddev,rdev);
72a23c21
NB
7275 if (mddev->degraded)
7276 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
00bcb4ac 7277 sysfs_notify_dirent_safe(rdev->sysfs_state);
1da177e4
LT
7278 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7279 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7280 md_wakeup_thread(mddev->thread);
768a418d 7281 if (mddev->event_work.func)
e804ac78 7282 queue_work(md_misc_wq, &mddev->event_work);
bb9ef716 7283 md_new_event(mddev);
1da177e4 7284}
6c144d31 7285EXPORT_SYMBOL(md_error);
1da177e4
LT
7286
7287/* seq_file implementation /proc/mdstat */
7288
7289static void status_unused(struct seq_file *seq)
7290{
7291 int i = 0;
3cb03002 7292 struct md_rdev *rdev;
1da177e4
LT
7293
7294 seq_printf(seq, "unused devices: ");
7295
159ec1fc 7296 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
1da177e4
LT
7297 char b[BDEVNAME_SIZE];
7298 i++;
7299 seq_printf(seq, "%s ",
7300 bdevname(rdev->bdev,b));
7301 }
7302 if (!i)
7303 seq_printf(seq, "<none>");
7304
7305 seq_printf(seq, "\n");
7306}
7307
f7851be7 7308static int status_resync(struct seq_file *seq, struct mddev *mddev)
1da177e4 7309{
dd71cf6b
N
7310 sector_t max_sectors, resync, res;
7311 unsigned long dt, db;
7312 sector_t rt;
4588b42e
N
7313 int scale;
7314 unsigned int per_milli;
1da177e4 7315
c804cdec
N
7316 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7317 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
dd71cf6b 7318 max_sectors = mddev->resync_max_sectors;
1da177e4 7319 else
dd71cf6b 7320 max_sectors = mddev->dev_sectors;
1da177e4 7321
f7851be7
N
7322 resync = mddev->curr_resync;
7323 if (resync <= 3) {
7324 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7325 /* Still cleaning up */
7326 resync = max_sectors;
7327 } else
7328 resync -= atomic_read(&mddev->recovery_active);
7329
7330 if (resync == 0) {
7331 if (mddev->recovery_cp < MaxSector) {
7332 seq_printf(seq, "\tresync=PENDING");
7333 return 1;
7334 }
7335 return 0;
7336 }
7337 if (resync < 3) {
7338 seq_printf(seq, "\tresync=DELAYED");
7339 return 1;
7340 }
7341
403df478 7342 WARN_ON(max_sectors == 0);
4588b42e 7343 /* Pick 'scale' such that (resync>>scale)*1000 will fit
dd71cf6b 7344 * in a sector_t, and (max_sectors>>scale) will fit in a
4588b42e
N
7345 * u32, as those are the requirements for sector_div.
7346 * Thus 'scale' must be at least 10
7347 */
7348 scale = 10;
7349 if (sizeof(sector_t) > sizeof(unsigned long)) {
dd71cf6b 7350 while ( max_sectors/2 > (1ULL<<(scale+32)))
4588b42e
N
7351 scale++;
7352 }
7353 res = (resync>>scale)*1000;
dd71cf6b 7354 sector_div(res, (u32)((max_sectors>>scale)+1));
4588b42e
N
7355
7356 per_milli = res;
1da177e4 7357 {
4588b42e 7358 int i, x = per_milli/50, y = 20-x;
1da177e4
LT
7359 seq_printf(seq, "[");
7360 for (i = 0; i < x; i++)
7361 seq_printf(seq, "=");
7362 seq_printf(seq, ">");
7363 for (i = 0; i < y; i++)
7364 seq_printf(seq, ".");
7365 seq_printf(seq, "] ");
7366 }
4588b42e 7367 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
ccfcc3c1
N
7368 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7369 "reshape" :
61df9d91
N
7370 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7371 "check" :
7372 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7373 "resync" : "recovery"))),
7374 per_milli/10, per_milli % 10,
dd71cf6b
N
7375 (unsigned long long) resync/2,
7376 (unsigned long long) max_sectors/2);
1da177e4
LT
7377
7378 /*
1da177e4
LT
7379 * dt: time from mark until now
7380 * db: blocks written from mark until now
7381 * rt: remaining time
dd71cf6b
N
7382 *
7383 * rt is a sector_t, so could be 32bit or 64bit.
7384 * So we divide before multiply in case it is 32bit and close
7385 * to the limit.
25985edc 7386 * We scale the divisor (db) by 32 to avoid losing precision
dd71cf6b
N
7387 * near the end of resync when the number of remaining sectors
7388 * is close to 'db'.
7389 * We then divide rt by 32 after multiplying by db to compensate.
7390 * The '+1' avoids division by zero if db is very small.
1da177e4
LT
7391 */
7392 dt = ((jiffies - mddev->resync_mark) / HZ);
7393 if (!dt) dt++;
ff4e8d9a
N
7394 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7395 - mddev->resync_mark_cnt;
1da177e4 7396
dd71cf6b
N
7397 rt = max_sectors - resync; /* number of remaining sectors */
7398 sector_div(rt, db/32+1);
7399 rt *= dt;
7400 rt >>= 5;
7401
7402 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7403 ((unsigned long)rt % 60)/6);
1da177e4 7404
ff4e8d9a 7405 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
f7851be7 7406 return 1;
1da177e4
LT
7407}
7408
7409static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7410{
7411 struct list_head *tmp;
7412 loff_t l = *pos;
fd01b88c 7413 struct mddev *mddev;
1da177e4
LT
7414
7415 if (l >= 0x10000)
7416 return NULL;
7417 if (!l--)
7418 /* header */
7419 return (void*)1;
7420
7421 spin_lock(&all_mddevs_lock);
7422 list_for_each(tmp,&all_mddevs)
7423 if (!l--) {
fd01b88c 7424 mddev = list_entry(tmp, struct mddev, all_mddevs);
1da177e4
LT
7425 mddev_get(mddev);
7426 spin_unlock(&all_mddevs_lock);
7427 return mddev;
7428 }
7429 spin_unlock(&all_mddevs_lock);
7430 if (!l--)
7431 return (void*)2;/* tail */
7432 return NULL;
7433}
7434
7435static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7436{
7437 struct list_head *tmp;
fd01b88c 7438 struct mddev *next_mddev, *mddev = v;
f72ffdd6 7439
1da177e4
LT
7440 ++*pos;
7441 if (v == (void*)2)
7442 return NULL;
7443
7444 spin_lock(&all_mddevs_lock);
7445 if (v == (void*)1)
7446 tmp = all_mddevs.next;
7447 else
7448 tmp = mddev->all_mddevs.next;
7449 if (tmp != &all_mddevs)
fd01b88c 7450 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
1da177e4
LT
7451 else {
7452 next_mddev = (void*)2;
7453 *pos = 0x10000;
f72ffdd6 7454 }
1da177e4
LT
7455 spin_unlock(&all_mddevs_lock);
7456
7457 if (v != (void*)1)
7458 mddev_put(mddev);
7459 return next_mddev;
7460
7461}
7462
7463static void md_seq_stop(struct seq_file *seq, void *v)
7464{
fd01b88c 7465 struct mddev *mddev = v;
1da177e4
LT
7466
7467 if (mddev && v != (void*)1 && v != (void*)2)
7468 mddev_put(mddev);
7469}
7470
7471static int md_seq_show(struct seq_file *seq, void *v)
7472{
fd01b88c 7473 struct mddev *mddev = v;
dd8ac336 7474 sector_t sectors;
3cb03002 7475 struct md_rdev *rdev;
1da177e4
LT
7476
7477 if (v == (void*)1) {
84fc4b56 7478 struct md_personality *pers;
1da177e4
LT
7479 seq_printf(seq, "Personalities : ");
7480 spin_lock(&pers_lock);
2604b703
N
7481 list_for_each_entry(pers, &pers_list, list)
7482 seq_printf(seq, "[%s] ", pers->name);
1da177e4
LT
7483
7484 spin_unlock(&pers_lock);
7485 seq_printf(seq, "\n");
f1514638 7486 seq->poll_event = atomic_read(&md_event_count);
1da177e4
LT
7487 return 0;
7488 }
7489 if (v == (void*)2) {
7490 status_unused(seq);
7491 return 0;
7492 }
7493
36d091f4 7494 spin_lock(&mddev->lock);
1da177e4
LT
7495 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7496 seq_printf(seq, "%s : %sactive", mdname(mddev),
7497 mddev->pers ? "" : "in");
7498 if (mddev->pers) {
f91de92e 7499 if (mddev->ro==1)
1da177e4 7500 seq_printf(seq, " (read-only)");
f91de92e 7501 if (mddev->ro==2)
52720ae7 7502 seq_printf(seq, " (auto-read-only)");
1da177e4
LT
7503 seq_printf(seq, " %s", mddev->pers->name);
7504 }
7505
dd8ac336 7506 sectors = 0;
f97fcad3
N
7507 rcu_read_lock();
7508 rdev_for_each_rcu(rdev, mddev) {
1da177e4
LT
7509 char b[BDEVNAME_SIZE];
7510 seq_printf(seq, " %s[%d]",
7511 bdevname(rdev->bdev,b), rdev->desc_nr);
8ddf9efe
N
7512 if (test_bit(WriteMostly, &rdev->flags))
7513 seq_printf(seq, "(W)");
9efdca16
SL
7514 if (test_bit(Journal, &rdev->flags))
7515 seq_printf(seq, "(J)");
b2d444d7 7516 if (test_bit(Faulty, &rdev->flags)) {
1da177e4
LT
7517 seq_printf(seq, "(F)");
7518 continue;
2d78f8c4
N
7519 }
7520 if (rdev->raid_disk < 0)
b325a32e 7521 seq_printf(seq, "(S)"); /* spare */
2d78f8c4
N
7522 if (test_bit(Replacement, &rdev->flags))
7523 seq_printf(seq, "(R)");
dd8ac336 7524 sectors += rdev->sectors;
1da177e4 7525 }
f97fcad3 7526 rcu_read_unlock();
1da177e4
LT
7527
7528 if (!list_empty(&mddev->disks)) {
7529 if (mddev->pers)
7530 seq_printf(seq, "\n %llu blocks",
f233ea5c
AN
7531 (unsigned long long)
7532 mddev->array_sectors / 2);
1da177e4
LT
7533 else
7534 seq_printf(seq, "\n %llu blocks",
dd8ac336 7535 (unsigned long long)sectors / 2);
1da177e4 7536 }
1cd6bf19
N
7537 if (mddev->persistent) {
7538 if (mddev->major_version != 0 ||
7539 mddev->minor_version != 90) {
7540 seq_printf(seq," super %d.%d",
7541 mddev->major_version,
7542 mddev->minor_version);
7543 }
e691063a
N
7544 } else if (mddev->external)
7545 seq_printf(seq, " super external:%s",
7546 mddev->metadata_type);
7547 else
1cd6bf19 7548 seq_printf(seq, " super non-persistent");
1da177e4
LT
7549
7550 if (mddev->pers) {
d710e138 7551 mddev->pers->status(seq, mddev);
f72ffdd6 7552 seq_printf(seq, "\n ");
8e1b39d6 7553 if (mddev->pers->sync_request) {
f7851be7 7554 if (status_resync(seq, mddev))
8e1b39d6 7555 seq_printf(seq, "\n ");
8e1b39d6 7556 }
32a7627c
N
7557 } else
7558 seq_printf(seq, "\n ");
7559
57148964 7560 bitmap_status(seq, mddev->bitmap);
1da177e4
LT
7561
7562 seq_printf(seq, "\n");
7563 }
36d091f4 7564 spin_unlock(&mddev->lock);
f72ffdd6 7565
1da177e4
LT
7566 return 0;
7567}
7568
110518bc 7569static const struct seq_operations md_seq_ops = {
1da177e4
LT
7570 .start = md_seq_start,
7571 .next = md_seq_next,
7572 .stop = md_seq_stop,
7573 .show = md_seq_show,
7574};
7575
7576static int md_seq_open(struct inode *inode, struct file *file)
7577{
f1514638 7578 struct seq_file *seq;
1da177e4
LT
7579 int error;
7580
7581 error = seq_open(file, &md_seq_ops);
d7603b7e 7582 if (error)
f1514638
KS
7583 return error;
7584
7585 seq = file->private_data;
7586 seq->poll_event = atomic_read(&md_event_count);
1da177e4
LT
7587 return error;
7588}
7589
e2f23b60 7590static int md_unloading;
d7603b7e
N
7591static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7592{
f1514638 7593 struct seq_file *seq = filp->private_data;
d7603b7e
N
7594 int mask;
7595
e2f23b60 7596 if (md_unloading)
7d7e64f2 7597 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
d7603b7e
N
7598 poll_wait(filp, &md_event_waiters, wait);
7599
7600 /* always allow read */
7601 mask = POLLIN | POLLRDNORM;
7602
f1514638 7603 if (seq->poll_event != atomic_read(&md_event_count))
d7603b7e
N
7604 mask |= POLLERR | POLLPRI;
7605 return mask;
7606}
7607
fa027c2a 7608static const struct file_operations md_seq_fops = {
e24650c2 7609 .owner = THIS_MODULE,
1da177e4
LT
7610 .open = md_seq_open,
7611 .read = seq_read,
7612 .llseek = seq_lseek,
c3f94b40 7613 .release = seq_release_private,
d7603b7e 7614 .poll = mdstat_poll,
1da177e4
LT
7615};
7616
84fc4b56 7617int register_md_personality(struct md_personality *p)
1da177e4 7618{
9d48739e
N
7619 pr_debug("md: %s personality registered for level %d\n",
7620 p->name, p->level);
1da177e4 7621 spin_lock(&pers_lock);
2604b703 7622 list_add_tail(&p->list, &pers_list);
1da177e4
LT
7623 spin_unlock(&pers_lock);
7624 return 0;
7625}
6c144d31 7626EXPORT_SYMBOL(register_md_personality);
1da177e4 7627
84fc4b56 7628int unregister_md_personality(struct md_personality *p)
1da177e4 7629{
9d48739e 7630 pr_debug("md: %s personality unregistered\n", p->name);
1da177e4 7631 spin_lock(&pers_lock);
2604b703 7632 list_del_init(&p->list);
1da177e4
LT
7633 spin_unlock(&pers_lock);
7634 return 0;
7635}
6c144d31 7636EXPORT_SYMBOL(unregister_md_personality);
1da177e4 7637
6022e75b
N
7638int register_md_cluster_operations(struct md_cluster_operations *ops,
7639 struct module *module)
edb39c9d 7640{
6022e75b 7641 int ret = 0;
edb39c9d 7642 spin_lock(&pers_lock);
6022e75b
N
7643 if (md_cluster_ops != NULL)
7644 ret = -EALREADY;
7645 else {
7646 md_cluster_ops = ops;
7647 md_cluster_mod = module;
7648 }
edb39c9d 7649 spin_unlock(&pers_lock);
6022e75b 7650 return ret;
edb39c9d
GR
7651}
7652EXPORT_SYMBOL(register_md_cluster_operations);
7653
7654int unregister_md_cluster_operations(void)
7655{
7656 spin_lock(&pers_lock);
7657 md_cluster_ops = NULL;
7658 spin_unlock(&pers_lock);
7659 return 0;
7660}
7661EXPORT_SYMBOL(unregister_md_cluster_operations);
7662
7663int md_setup_cluster(struct mddev *mddev, int nodes)
7664{
47a7b0d8
GJ
7665 if (!md_cluster_ops)
7666 request_module("md-cluster");
edb39c9d 7667 spin_lock(&pers_lock);
47a7b0d8 7668 /* ensure module won't be unloaded */
edb39c9d 7669 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
9d48739e 7670 pr_warn("can't find md-cluster module or get it's reference.\n");
edb39c9d
GR
7671 spin_unlock(&pers_lock);
7672 return -ENOENT;
7673 }
7674 spin_unlock(&pers_lock);
7675
cf921cc1 7676 return md_cluster_ops->join(mddev, nodes);
edb39c9d
GR
7677}
7678
7679void md_cluster_stop(struct mddev *mddev)
7680{
c4ce867f
GR
7681 if (!md_cluster_ops)
7682 return;
edb39c9d
GR
7683 md_cluster_ops->leave(mddev);
7684 module_put(md_cluster_mod);
7685}
7686
fd01b88c 7687static int is_mddev_idle(struct mddev *mddev, int init)
1da177e4 7688{
f72ffdd6 7689 struct md_rdev *rdev;
1da177e4 7690 int idle;
eea1bf38 7691 int curr_events;
1da177e4
LT
7692
7693 idle = 1;
4b80991c
N
7694 rcu_read_lock();
7695 rdev_for_each_rcu(rdev, mddev) {
1da177e4 7696 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
eea1bf38
N
7697 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7698 (int)part_stat_read(&disk->part0, sectors[1]) -
7699 atomic_read(&disk->sync_io);
713f6ab1
N
7700 /* sync IO will cause sync_io to increase before the disk_stats
7701 * as sync_io is counted when a request starts, and
7702 * disk_stats is counted when it completes.
7703 * So resync activity will cause curr_events to be smaller than
7704 * when there was no such activity.
7705 * non-sync IO will cause disk_stat to increase without
7706 * increasing sync_io so curr_events will (eventually)
7707 * be larger than it was before. Once it becomes
7708 * substantially larger, the test below will cause
7709 * the array to appear non-idle, and resync will slow
7710 * down.
7711 * If there is a lot of outstanding resync activity when
7712 * we set last_event to curr_events, then all that activity
7713 * completing might cause the array to appear non-idle
7714 * and resync will be slowed down even though there might
7715 * not have been non-resync activity. This will only
7716 * happen once though. 'last_events' will soon reflect
7717 * the state where there is little or no outstanding
7718 * resync requests, and further resync activity will
7719 * always make curr_events less than last_events.
c0e48521 7720 *
1da177e4 7721 */
eea1bf38 7722 if (init || curr_events - rdev->last_events > 64) {
1da177e4
LT
7723 rdev->last_events = curr_events;
7724 idle = 0;
7725 }
7726 }
4b80991c 7727 rcu_read_unlock();
1da177e4
LT
7728 return idle;
7729}
7730
fd01b88c 7731void md_done_sync(struct mddev *mddev, int blocks, int ok)
1da177e4
LT
7732{
7733 /* another "blocks" (512byte) blocks have been synced */
7734 atomic_sub(blocks, &mddev->recovery_active);
7735 wake_up(&mddev->recovery_wait);
7736 if (!ok) {
dfc70645 7737 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
0a19caab 7738 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
1da177e4
LT
7739 md_wakeup_thread(mddev->thread);
7740 // stop recovery, signal do_sync ....
7741 }
7742}
6c144d31 7743EXPORT_SYMBOL(md_done_sync);
1da177e4 7744
06d91a5f
N
7745/* md_write_start(mddev, bi)
7746 * If we need to update some array metadata (e.g. 'active' flag
3d310eb7
N
7747 * in superblock) before writing, schedule a superblock update
7748 * and wait for it to complete.
06d91a5f 7749 */
fd01b88c 7750void md_write_start(struct mddev *mddev, struct bio *bi)
1da177e4 7751{
0fd62b86 7752 int did_change = 0;
06d91a5f 7753 if (bio_data_dir(bi) != WRITE)
3d310eb7 7754 return;
06d91a5f 7755
f91de92e
N
7756 BUG_ON(mddev->ro == 1);
7757 if (mddev->ro == 2) {
7758 /* need to switch to read/write */
7759 mddev->ro = 0;
7760 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7761 md_wakeup_thread(mddev->thread);
25156198 7762 md_wakeup_thread(mddev->sync_thread);
0fd62b86 7763 did_change = 1;
f91de92e 7764 }
06d91a5f 7765 atomic_inc(&mddev->writes_pending);
31a59e34
N
7766 if (mddev->safemode == 1)
7767 mddev->safemode = 0;
06d91a5f 7768 if (mddev->in_sync) {
85572d7c 7769 spin_lock(&mddev->lock);
3d310eb7
N
7770 if (mddev->in_sync) {
7771 mddev->in_sync = 0;
2953079c
SL
7772 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7773 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
3d310eb7 7774 md_wakeup_thread(mddev->thread);
0fd62b86 7775 did_change = 1;
3d310eb7 7776 }
85572d7c 7777 spin_unlock(&mddev->lock);
06d91a5f 7778 }
0fd62b86 7779 if (did_change)
00bcb4ac 7780 sysfs_notify_dirent_safe(mddev->sysfs_state);
09a44cc1 7781 wait_event(mddev->sb_wait,
2953079c 7782 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
1da177e4 7783}
6c144d31 7784EXPORT_SYMBOL(md_write_start);
1da177e4 7785
fd01b88c 7786void md_write_end(struct mddev *mddev)
1da177e4
LT
7787{
7788 if (atomic_dec_and_test(&mddev->writes_pending)) {
7789 if (mddev->safemode == 2)
7790 md_wakeup_thread(mddev->thread);
16f17b39 7791 else if (mddev->safemode_delay)
1da177e4
LT
7792 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7793 }
7794}
6c144d31 7795EXPORT_SYMBOL(md_write_end);
1da177e4 7796
2a2275d6
N
7797/* md_allow_write(mddev)
7798 * Calling this ensures that the array is marked 'active' so that writes
7799 * may proceed without blocking. It is important to call this before
7800 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7801 * Must be called with mddev_lock held.
b5470dc5 7802 *
2953079c 7803 * In the ->external case MD_SB_CHANGE_PENDING can not be cleared until mddev->lock
b5470dc5 7804 * is dropped, so return -EAGAIN after notifying userspace.
2a2275d6 7805 */
fd01b88c 7806int md_allow_write(struct mddev *mddev)
2a2275d6
N
7807{
7808 if (!mddev->pers)
b5470dc5 7809 return 0;
2a2275d6 7810 if (mddev->ro)
b5470dc5 7811 return 0;
1a0fd497 7812 if (!mddev->pers->sync_request)
b5470dc5 7813 return 0;
2a2275d6 7814
85572d7c 7815 spin_lock(&mddev->lock);
2a2275d6
N
7816 if (mddev->in_sync) {
7817 mddev->in_sync = 0;
2953079c
SL
7818 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7819 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
2a2275d6
N
7820 if (mddev->safemode_delay &&
7821 mddev->safemode == 0)
7822 mddev->safemode = 1;
85572d7c 7823 spin_unlock(&mddev->lock);
2a2275d6 7824 md_update_sb(mddev, 0);
00bcb4ac 7825 sysfs_notify_dirent_safe(mddev->sysfs_state);
2a2275d6 7826 } else
85572d7c 7827 spin_unlock(&mddev->lock);
b5470dc5 7828
2953079c 7829 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
b5470dc5
DW
7830 return -EAGAIN;
7831 else
7832 return 0;
2a2275d6
N
7833}
7834EXPORT_SYMBOL_GPL(md_allow_write);
7835
1da177e4
LT
7836#define SYNC_MARKS 10
7837#define SYNC_MARK_STEP (3*HZ)
54f89341 7838#define UPDATE_FREQUENCY (5*60*HZ)
4ed8731d 7839void md_do_sync(struct md_thread *thread)
1da177e4 7840{
4ed8731d 7841 struct mddev *mddev = thread->mddev;
fd01b88c 7842 struct mddev *mddev2;
1da177e4
LT
7843 unsigned int currspeed = 0,
7844 window;
ac7e50a3 7845 sector_t max_sectors,j, io_sectors, recovery_done;
1da177e4 7846 unsigned long mark[SYNC_MARKS];
54f89341 7847 unsigned long update_time;
1da177e4
LT
7848 sector_t mark_cnt[SYNC_MARKS];
7849 int last_mark,m;
7850 struct list_head *tmp;
7851 sector_t last_check;
57afd89f 7852 int skipped = 0;
3cb03002 7853 struct md_rdev *rdev;
c4a39551 7854 char *desc, *action = NULL;
7c2c57c9 7855 struct blk_plug plug;
41a9a0dc 7856 int ret;
1da177e4
LT
7857
7858 /* just incase thread restarts... */
7859 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7860 return;
3991b31e
N
7861 if (mddev->ro) {/* never try to sync a read-only array */
7862 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5fd6c1dc 7863 return;
3991b31e 7864 }
1da177e4 7865
41a9a0dc
GJ
7866 if (mddev_is_clustered(mddev)) {
7867 ret = md_cluster_ops->resync_start(mddev);
7868 if (ret)
7869 goto skip;
7870
bb8bf15b 7871 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
41a9a0dc
GJ
7872 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7873 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
7874 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
7875 && ((unsigned long long)mddev->curr_resync_completed
7876 < (unsigned long long)mddev->resync_max_sectors))
7877 goto skip;
7878 }
7879
61df9d91 7880 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
c4a39551 7881 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
61df9d91 7882 desc = "data-check";
c4a39551
JB
7883 action = "check";
7884 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
61df9d91 7885 desc = "requested-resync";
c4a39551
JB
7886 action = "repair";
7887 } else
61df9d91
N
7888 desc = "resync";
7889 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7890 desc = "reshape";
7891 else
7892 desc = "recovery";
7893
c4a39551
JB
7894 mddev->last_sync_action = action ?: desc;
7895
1da177e4
LT
7896 /* we overload curr_resync somewhat here.
7897 * 0 == not engaged in resync at all
7898 * 2 == checking that there is no conflict with another sync
7899 * 1 == like 2, but have yielded to allow conflicting resync to
7900 * commense
7901 * other == active in resync - this many blocks
7902 *
7903 * Before starting a resync we must have set curr_resync to
7904 * 2, and then checked that every "conflicting" array has curr_resync
7905 * less than ours. When we find one that is the same or higher
7906 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7907 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7908 * This will mean we have to start checking from the beginning again.
7909 *
7910 */
7911
7912 do {
c622ca54 7913 int mddev2_minor = -1;
1da177e4
LT
7914 mddev->curr_resync = 2;
7915
7916 try_again:
404e4b43 7917 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
1da177e4 7918 goto skip;
29ac4aa3 7919 for_each_mddev(mddev2, tmp) {
1da177e4
LT
7920 if (mddev2 == mddev)
7921 continue;
90b08710
BS
7922 if (!mddev->parallel_resync
7923 && mddev2->curr_resync
7924 && match_mddev_units(mddev, mddev2)) {
1da177e4
LT
7925 DEFINE_WAIT(wq);
7926 if (mddev < mddev2 && mddev->curr_resync == 2) {
7927 /* arbitrarily yield */
7928 mddev->curr_resync = 1;
7929 wake_up(&resync_wait);
7930 }
7931 if (mddev > mddev2 && mddev->curr_resync == 1)
7932 /* no need to wait here, we can wait the next
7933 * time 'round when curr_resync == 2
7934 */
7935 continue;
9744197c
N
7936 /* We need to wait 'interruptible' so as not to
7937 * contribute to the load average, and not to
7938 * be caught by 'softlockup'
7939 */
7940 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
c91abf5a 7941 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8712e553 7942 mddev2->curr_resync >= mddev->curr_resync) {
c622ca54
AP
7943 if (mddev2_minor != mddev2->md_minor) {
7944 mddev2_minor = mddev2->md_minor;
9d48739e
N
7945 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
7946 desc, mdname(mddev),
7947 mdname(mddev2));
c622ca54 7948 }
1da177e4 7949 mddev_put(mddev2);
9744197c
N
7950 if (signal_pending(current))
7951 flush_signals(current);
1da177e4
LT
7952 schedule();
7953 finish_wait(&resync_wait, &wq);
7954 goto try_again;
7955 }
7956 finish_wait(&resync_wait, &wq);
7957 }
7958 }
7959 } while (mddev->curr_resync < 2);
7960
5fd6c1dc 7961 j = 0;
9d88883e 7962 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1da177e4 7963 /* resync follows the size requested by the personality,
57afd89f 7964 * which defaults to physical size, but can be virtual size
1da177e4
LT
7965 */
7966 max_sectors = mddev->resync_max_sectors;
7f7583d4 7967 atomic64_set(&mddev->resync_mismatches, 0);
5fd6c1dc 7968 /* we don't use the checkpoint if there's a bitmap */
5e96ee65
NB
7969 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7970 j = mddev->resync_min;
7971 else if (!mddev->bitmap)
5fd6c1dc 7972 j = mddev->recovery_cp;
5e96ee65 7973
ccfcc3c1 7974 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
c804cdec 7975 max_sectors = mddev->resync_max_sectors;
5fd6c1dc 7976 else {
1da177e4 7977 /* recovery follows the physical size of devices */
58c0fed4 7978 max_sectors = mddev->dev_sectors;
5fd6c1dc 7979 j = MaxSector;
4e59ca7d 7980 rcu_read_lock();
dafb20fa 7981 rdev_for_each_rcu(rdev, mddev)
5fd6c1dc 7982 if (rdev->raid_disk >= 0 &&
f2076e7d 7983 !test_bit(Journal, &rdev->flags) &&
5fd6c1dc
N
7984 !test_bit(Faulty, &rdev->flags) &&
7985 !test_bit(In_sync, &rdev->flags) &&
7986 rdev->recovery_offset < j)
7987 j = rdev->recovery_offset;
4e59ca7d 7988 rcu_read_unlock();
133d4527
N
7989
7990 /* If there is a bitmap, we need to make sure all
7991 * writes that started before we added a spare
7992 * complete before we start doing a recovery.
7993 * Otherwise the write might complete and (via
7994 * bitmap_endwrite) set a bit in the bitmap after the
7995 * recovery has checked that bit and skipped that
7996 * region.
7997 */
7998 if (mddev->bitmap) {
7999 mddev->pers->quiesce(mddev, 1);
8000 mddev->pers->quiesce(mddev, 0);
8001 }
5fd6c1dc 8002 }
1da177e4 8003
9d48739e
N
8004 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8005 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8006 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8007 speed_max(mddev), desc);
1da177e4 8008
eea1bf38 8009 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
5fd6c1dc 8010
57afd89f 8011 io_sectors = 0;
1da177e4
LT
8012 for (m = 0; m < SYNC_MARKS; m++) {
8013 mark[m] = jiffies;
57afd89f 8014 mark_cnt[m] = io_sectors;
1da177e4
LT
8015 }
8016 last_mark = 0;
8017 mddev->resync_mark = mark[last_mark];
8018 mddev->resync_mark_cnt = mark_cnt[last_mark];
8019
8020 /*
8021 * Tune reconstruction:
8022 */
8023 window = 32*(PAGE_SIZE/512);
9d48739e
N
8024 pr_debug("md: using %dk window, over a total of %lluk.\n",
8025 window/2, (unsigned long long)max_sectors/2);
1da177e4
LT
8026
8027 atomic_set(&mddev->recovery_active, 0);
1da177e4
LT
8028 last_check = 0;
8029
8030 if (j>2) {
9d48739e
N
8031 pr_debug("md: resuming %s of %s from checkpoint.\n",
8032 desc, mdname(mddev));
1da177e4 8033 mddev->curr_resync = j;
72f36d59
N
8034 } else
8035 mddev->curr_resync = 3; /* no longer delayed */
75d3da43 8036 mddev->curr_resync_completed = j;
72f36d59
N
8037 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8038 md_new_event(mddev);
54f89341 8039 update_time = jiffies;
1da177e4 8040
7c2c57c9 8041 blk_start_plug(&plug);
1da177e4 8042 while (j < max_sectors) {
57afd89f 8043 sector_t sectors;
1da177e4 8044
57afd89f 8045 skipped = 0;
97e4f42d 8046
7a91ee1f
N
8047 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8048 ((mddev->curr_resync > mddev->curr_resync_completed &&
8049 (mddev->curr_resync - mddev->curr_resync_completed)
8050 > (max_sectors >> 4)) ||
54f89341 8051 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
7a91ee1f 8052 (j - mddev->curr_resync_completed)*2
c5e19d90
N
8053 >= mddev->resync_max - mddev->curr_resync_completed ||
8054 mddev->curr_resync_completed > mddev->resync_max
7a91ee1f 8055 )) {
97e4f42d 8056 /* time to update curr_resync_completed */
97e4f42d
N
8057 wait_event(mddev->recovery_wait,
8058 atomic_read(&mddev->recovery_active) == 0);
75d3da43 8059 mddev->curr_resync_completed = j;
35d78c66 8060 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8061 j > mddev->recovery_cp)
8062 mddev->recovery_cp = j;
54f89341 8063 update_time = jiffies;
2953079c 8064 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
acb180b0 8065 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
97e4f42d 8066 }
acb180b0 8067
c91abf5a
N
8068 while (j >= mddev->resync_max &&
8069 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
e62e58a5
N
8070 /* As this condition is controlled by user-space,
8071 * we can block indefinitely, so use '_interruptible'
8072 * to avoid triggering warnings.
8073 */
8074 flush_signals(current); /* just in case */
8075 wait_event_interruptible(mddev->recovery_wait,
8076 mddev->resync_max > j
c91abf5a
N
8077 || test_bit(MD_RECOVERY_INTR,
8078 &mddev->recovery));
e62e58a5 8079 }
acb180b0 8080
c91abf5a
N
8081 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8082 break;
acb180b0 8083
09314799 8084 sectors = mddev->pers->sync_request(mddev, j, &skipped);
57afd89f 8085 if (sectors == 0) {
dfc70645 8086 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
c91abf5a 8087 break;
1da177e4 8088 }
57afd89f
N
8089
8090 if (!skipped) { /* actual IO requested */
8091 io_sectors += sectors;
8092 atomic_add(sectors, &mddev->recovery_active);
8093 }
8094
e875ecea
N
8095 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8096 break;
8097
1da177e4 8098 j += sectors;
5ed1df2e
N
8099 if (j > max_sectors)
8100 /* when skipping, extra large numbers can be returned. */
8101 j = max_sectors;
72f36d59
N
8102 if (j > 2)
8103 mddev->curr_resync = j;
ff4e8d9a 8104 mddev->curr_mark_cnt = io_sectors;
d7603b7e 8105 if (last_check == 0)
e875ecea 8106 /* this is the earliest that rebuild will be
d7603b7e
N
8107 * visible in /proc/mdstat
8108 */
8109 md_new_event(mddev);
57afd89f
N
8110
8111 if (last_check + window > io_sectors || j == max_sectors)
1da177e4
LT
8112 continue;
8113
57afd89f 8114 last_check = io_sectors;
1da177e4
LT
8115 repeat:
8116 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8117 /* step marks */
8118 int next = (last_mark+1) % SYNC_MARKS;
8119
8120 mddev->resync_mark = mark[next];
8121 mddev->resync_mark_cnt = mark_cnt[next];
8122 mark[next] = jiffies;
57afd89f 8123 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
1da177e4
LT
8124 last_mark = next;
8125 }
8126
c91abf5a
N
8127 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8128 break;
1da177e4
LT
8129
8130 /*
8131 * this loop exits only if either when we are slower than
8132 * the 'hard' speed limit, or the system was IO-idle for
8133 * a jiffy.
8134 * the system might be non-idle CPU-wise, but we only care
8135 * about not overloading the IO subsystem. (things like an
8136 * e2fsck being done on the RAID array should execute fast)
8137 */
1da177e4
LT
8138 cond_resched();
8139
ac7e50a3
XN
8140 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8141 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
57afd89f 8142 /((jiffies-mddev->resync_mark)/HZ +1) +1;
1da177e4 8143
88202a0c 8144 if (currspeed > speed_min(mddev)) {
ac8fa419 8145 if (currspeed > speed_max(mddev)) {
c0e48521 8146 msleep(500);
1da177e4
LT
8147 goto repeat;
8148 }
ac8fa419
N
8149 if (!is_mddev_idle(mddev, 0)) {
8150 /*
8151 * Give other IO more of a chance.
8152 * The faster the devices, the less we wait.
8153 */
8154 wait_event(mddev->recovery_wait,
8155 !atomic_read(&mddev->recovery_active));
8156 }
1da177e4
LT
8157 }
8158 }
9d48739e
N
8159 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8160 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8161 ? "interrupted" : "done");
1da177e4
LT
8162 /*
8163 * this also signals 'finished resyncing' to md_stop
8164 */
7c2c57c9 8165 blk_finish_plug(&plug);
1da177e4
LT
8166 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8167
5ed1df2e
N
8168 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8169 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
1217e1d1 8170 mddev->curr_resync > 3) {
5ed1df2e
N
8171 mddev->curr_resync_completed = mddev->curr_resync;
8172 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8173 }
09314799 8174 mddev->pers->sync_request(mddev, max_sectors, &skipped);
1da177e4 8175
dfc70645 8176 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
1217e1d1 8177 mddev->curr_resync > 3) {
5fd6c1dc
N
8178 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8179 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8180 if (mddev->curr_resync >= mddev->recovery_cp) {
9d48739e
N
8181 pr_debug("md: checkpointing %s of %s.\n",
8182 desc, mdname(mddev));
0a19caab 8183 if (test_bit(MD_RECOVERY_ERROR,
8184 &mddev->recovery))
8185 mddev->recovery_cp =
8186 mddev->curr_resync_completed;
8187 else
8188 mddev->recovery_cp =
8189 mddev->curr_resync;
5fd6c1dc
N
8190 }
8191 } else
8192 mddev->recovery_cp = MaxSector;
8193 } else {
8194 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8195 mddev->curr_resync = MaxSector;
4e59ca7d 8196 rcu_read_lock();
dafb20fa 8197 rdev_for_each_rcu(rdev, mddev)
5fd6c1dc 8198 if (rdev->raid_disk >= 0 &&
70fffd0b 8199 mddev->delta_disks >= 0 &&
f2076e7d 8200 !test_bit(Journal, &rdev->flags) &&
5fd6c1dc
N
8201 !test_bit(Faulty, &rdev->flags) &&
8202 !test_bit(In_sync, &rdev->flags) &&
8203 rdev->recovery_offset < mddev->curr_resync)
8204 rdev->recovery_offset = mddev->curr_resync;
4e59ca7d 8205 rcu_read_unlock();
5fd6c1dc 8206 }
1da177e4 8207 }
db91ff55 8208 skip:
bb8bf15b
GJ
8209 /* set CHANGE_PENDING here since maybe another update is needed,
8210 * so other nodes are informed. It should be harmless for normal
8211 * raid */
2953079c
SL
8212 set_mask_bits(&mddev->sb_flags, 0,
8213 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
c186b128 8214
23da422b 8215 spin_lock(&mddev->lock);
c07b70ad
N
8216 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8217 /* We completed so min/max setting can be forgotten if used. */
8218 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8219 mddev->resync_min = 0;
8220 mddev->resync_max = MaxSector;
8221 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8222 mddev->resync_min = mddev->curr_resync_completed;
f7851be7 8223 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
1da177e4 8224 mddev->curr_resync = 0;
23da422b
N
8225 spin_unlock(&mddev->lock);
8226
1da177e4 8227 wake_up(&resync_wait);
1da177e4 8228 md_wakeup_thread(mddev->thread);
c6207277 8229 return;
1da177e4 8230}
29269553 8231EXPORT_SYMBOL_GPL(md_do_sync);
1da177e4 8232
746d3207
N
8233static int remove_and_add_spares(struct mddev *mddev,
8234 struct md_rdev *this)
b4c4c7b8 8235{
3cb03002 8236 struct md_rdev *rdev;
b4c4c7b8 8237 int spares = 0;
f2a371c5 8238 int removed = 0;
d787be40 8239 bool remove_some = false;
b4c4c7b8 8240
d787be40
N
8241 rdev_for_each(rdev, mddev) {
8242 if ((this == NULL || rdev == this) &&
8243 rdev->raid_disk >= 0 &&
8244 !test_bit(Blocked, &rdev->flags) &&
8245 test_bit(Faulty, &rdev->flags) &&
8246 atomic_read(&rdev->nr_pending)==0) {
8247 /* Faulty non-Blocked devices with nr_pending == 0
8248 * never get nr_pending incremented,
8249 * never get Faulty cleared, and never get Blocked set.
8250 * So we can synchronize_rcu now rather than once per device
8251 */
8252 remove_some = true;
8253 set_bit(RemoveSynchronized, &rdev->flags);
8254 }
8255 }
8256
8257 if (remove_some)
8258 synchronize_rcu();
8259 rdev_for_each(rdev, mddev) {
746d3207
N
8260 if ((this == NULL || rdev == this) &&
8261 rdev->raid_disk >= 0 &&
6bfe0b49 8262 !test_bit(Blocked, &rdev->flags) &&
d787be40 8263 ((test_bit(RemoveSynchronized, &rdev->flags) ||
f2076e7d
SL
8264 (!test_bit(In_sync, &rdev->flags) &&
8265 !test_bit(Journal, &rdev->flags))) &&
d787be40 8266 atomic_read(&rdev->nr_pending)==0)) {
b4c4c7b8 8267 if (mddev->pers->hot_remove_disk(
b8321b68 8268 mddev, rdev) == 0) {
36fad858 8269 sysfs_unlink_rdev(mddev, rdev);
b4c4c7b8 8270 rdev->raid_disk = -1;
f2a371c5 8271 removed++;
b4c4c7b8
N
8272 }
8273 }
d787be40
N
8274 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8275 clear_bit(RemoveSynchronized, &rdev->flags);
8276 }
8277
90584fc9
JB
8278 if (removed && mddev->kobj.sd)
8279 sysfs_notify(&mddev->kobj, NULL, "degraded");
b4c4c7b8 8280
2910ff17 8281 if (this && removed)
746d3207
N
8282 goto no_add;
8283
dafb20fa 8284 rdev_for_each(rdev, mddev) {
2910ff17
GR
8285 if (this && this != rdev)
8286 continue;
dbb64f86
GR
8287 if (test_bit(Candidate, &rdev->flags))
8288 continue;
7bfec5f3
N
8289 if (rdev->raid_disk >= 0 &&
8290 !test_bit(In_sync, &rdev->flags) &&
f2076e7d 8291 !test_bit(Journal, &rdev->flags) &&
7bfec5f3
N
8292 !test_bit(Faulty, &rdev->flags))
8293 spares++;
7ceb17e8
N
8294 if (rdev->raid_disk >= 0)
8295 continue;
8296 if (test_bit(Faulty, &rdev->flags))
8297 continue;
f6b6ec5c
SL
8298 if (!test_bit(Journal, &rdev->flags)) {
8299 if (mddev->ro &&
8300 ! (rdev->saved_raid_disk >= 0 &&
8301 !test_bit(Bitmap_sync, &rdev->flags)))
8302 continue;
7ceb17e8 8303
f6b6ec5c
SL
8304 rdev->recovery_offset = 0;
8305 }
7ceb17e8
N
8306 if (mddev->pers->
8307 hot_add_disk(mddev, rdev) == 0) {
8308 if (sysfs_link_rdev(mddev, rdev))
8309 /* failure here is OK */;
f6b6ec5c
SL
8310 if (!test_bit(Journal, &rdev->flags))
8311 spares++;
7ceb17e8 8312 md_new_event(mddev);
2953079c 8313 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
dfc70645 8314 }
b4c4c7b8 8315 }
746d3207 8316no_add:
6dafab6b 8317 if (removed)
2953079c 8318 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
b4c4c7b8
N
8319 return spares;
8320}
7ebc0be7 8321
ac05f256
N
8322static void md_start_sync(struct work_struct *ws)
8323{
8324 struct mddev *mddev = container_of(ws, struct mddev, del_work);
c186b128 8325
ac05f256
N
8326 mddev->sync_thread = md_register_thread(md_do_sync,
8327 mddev,
8328 "resync");
8329 if (!mddev->sync_thread) {
9d48739e
N
8330 pr_warn("%s: could not start resync thread...\n",
8331 mdname(mddev));
ac05f256
N
8332 /* leave the spares where they are, it shouldn't hurt */
8333 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8334 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8335 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8336 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8337 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
f851b60d 8338 wake_up(&resync_wait);
ac05f256
N
8339 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8340 &mddev->recovery))
8341 if (mddev->sysfs_action)
8342 sysfs_notify_dirent_safe(mddev->sysfs_action);
8343 } else
8344 md_wakeup_thread(mddev->sync_thread);
8345 sysfs_notify_dirent_safe(mddev->sysfs_action);
8346 md_new_event(mddev);
8347}
8348
1da177e4
LT
8349/*
8350 * This routine is regularly called by all per-raid-array threads to
8351 * deal with generic issues like resync and super-block update.
8352 * Raid personalities that don't have a thread (linear/raid0) do not
8353 * need this as they never do any recovery or update the superblock.
8354 *
8355 * It does not do any resync itself, but rather "forks" off other threads
8356 * to do that as needed.
8357 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8358 * "->recovery" and create a thread at ->sync_thread.
dfc70645 8359 * When the thread finishes it sets MD_RECOVERY_DONE
1da177e4
LT
8360 * and wakeups up this thread which will reap the thread and finish up.
8361 * This thread also removes any faulty devices (with nr_pending == 0).
8362 *
8363 * The overall approach is:
8364 * 1/ if the superblock needs updating, update it.
8365 * 2/ If a recovery thread is running, don't do anything else.
8366 * 3/ If recovery has finished, clean up, possibly marking spares active.
8367 * 4/ If there are any faulty devices, remove them.
8368 * 5/ If array is degraded, try to add spares devices
8369 * 6/ If array has spares or is not in-sync, start a resync thread.
8370 */
fd01b88c 8371void md_check_recovery(struct mddev *mddev)
1da177e4 8372{
68866e42
JB
8373 if (mddev->suspended)
8374 return;
8375
5f40402d 8376 if (mddev->bitmap)
aa5cbd10 8377 bitmap_daemon_work(mddev);
1da177e4 8378
fca4d848 8379 if (signal_pending(current)) {
31a59e34 8380 if (mddev->pers->sync_request && !mddev->external) {
9d48739e
N
8381 pr_debug("md: %s in immediate safe mode\n",
8382 mdname(mddev));
fca4d848
N
8383 mddev->safemode = 2;
8384 }
8385 flush_signals(current);
8386 }
8387
c89a8eee
N
8388 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8389 return;
1da177e4 8390 if ( ! (
2953079c 8391 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
1da177e4 8392 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
fca4d848 8393 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
15858fa5 8394 test_bit(MD_RELOAD_SB, &mddev->flags) ||
31a59e34 8395 (mddev->external == 0 && mddev->safemode == 1) ||
fca4d848
N
8396 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8397 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
1da177e4
LT
8398 ))
8399 return;
fca4d848 8400
df5b89b3 8401 if (mddev_trylock(mddev)) {
b4c4c7b8 8402 int spares = 0;
fca4d848 8403
c89a8eee 8404 if (mddev->ro) {
ab16bfc7
NB
8405 struct md_rdev *rdev;
8406 if (!mddev->external && mddev->in_sync)
8407 /* 'Blocked' flag not needed as failed devices
8408 * will be recorded if array switched to read/write.
8409 * Leaving it set will prevent the device
8410 * from being removed.
8411 */
8412 rdev_for_each(rdev, mddev)
8413 clear_bit(Blocked, &rdev->flags);
7ceb17e8
N
8414 /* On a read-only array we can:
8415 * - remove failed devices
8416 * - add already-in_sync devices if the array itself
8417 * is in-sync.
8418 * As we only add devices that are already in-sync,
8419 * we can activate the spares immediately.
c89a8eee 8420 */
7ceb17e8 8421 remove_and_add_spares(mddev, NULL);
8313b8e5
N
8422 /* There is no thread, but we need to call
8423 * ->spare_active and clear saved_raid_disk
8424 */
2ac295a5 8425 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8313b8e5 8426 md_reap_sync_thread(mddev);
a4a3d26d 8427 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8313b8e5 8428 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2953079c 8429 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
c89a8eee
N
8430 goto unlock;
8431 }
8432
659b254f
GJ
8433 if (mddev_is_clustered(mddev)) {
8434 struct md_rdev *rdev;
8435 /* kick the device if another node issued a
8436 * remove disk.
8437 */
8438 rdev_for_each(rdev, mddev) {
8439 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8440 rdev->raid_disk < 0)
8441 md_kick_rdev_from_array(rdev);
8442 }
15858fa5
GJ
8443
8444 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8445 md_reload_sb(mddev, mddev->good_device_nr);
659b254f
GJ
8446 }
8447
31a59e34 8448 if (!mddev->external) {
0fd62b86 8449 int did_change = 0;
85572d7c 8450 spin_lock(&mddev->lock);
31a59e34
N
8451 if (mddev->safemode &&
8452 !atomic_read(&mddev->writes_pending) &&
8453 !mddev->in_sync &&
8454 mddev->recovery_cp == MaxSector) {
8455 mddev->in_sync = 1;
0fd62b86 8456 did_change = 1;
2953079c 8457 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
31a59e34
N
8458 }
8459 if (mddev->safemode == 1)
8460 mddev->safemode = 0;
85572d7c 8461 spin_unlock(&mddev->lock);
0fd62b86 8462 if (did_change)
00bcb4ac 8463 sysfs_notify_dirent_safe(mddev->sysfs_state);
fca4d848 8464 }
fca4d848 8465
2953079c 8466 if (mddev->sb_flags)
850b2b42 8467 md_update_sb(mddev, 0);
06d91a5f 8468
1da177e4
LT
8469 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8470 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8471 /* resync/recovery still happening */
8472 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8473 goto unlock;
8474 }
8475 if (mddev->sync_thread) {
a91d5ac0 8476 md_reap_sync_thread(mddev);
1da177e4
LT
8477 goto unlock;
8478 }
72a23c21
NB
8479 /* Set RUNNING before clearing NEEDED to avoid
8480 * any transients in the value of "sync_action".
8481 */
72f36d59 8482 mddev->curr_resync_completed = 0;
23da422b 8483 spin_lock(&mddev->lock);
72a23c21 8484 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
23da422b 8485 spin_unlock(&mddev->lock);
24dd469d
N
8486 /* Clear some bits that don't mean anything, but
8487 * might be left set
8488 */
24dd469d
N
8489 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8490 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
1da177e4 8491
ed209584
N
8492 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8493 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
ac05f256 8494 goto not_running;
1da177e4
LT
8495 /* no recovery is running.
8496 * remove any failed drives, then
8497 * add spares if possible.
72f36d59 8498 * Spares are also removed and re-added, to allow
1da177e4
LT
8499 * the personality to fail the re-add.
8500 */
1da177e4 8501
b4c4c7b8 8502 if (mddev->reshape_position != MaxSector) {
50ac168a
N
8503 if (mddev->pers->check_reshape == NULL ||
8504 mddev->pers->check_reshape(mddev) != 0)
b4c4c7b8 8505 /* Cannot proceed */
ac05f256 8506 goto not_running;
b4c4c7b8 8507 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
72a23c21 8508 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
746d3207 8509 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
24dd469d
N
8510 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8511 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
56ac36d7 8512 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
72a23c21 8513 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
24dd469d
N
8514 } else if (mddev->recovery_cp < MaxSector) {
8515 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
72a23c21 8516 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
24dd469d
N
8517 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8518 /* nothing to be done ... */
ac05f256 8519 goto not_running;
24dd469d 8520
1da177e4 8521 if (mddev->pers->sync_request) {
ef99bf48 8522 if (spares) {
a654b9d8
N
8523 /* We are adding a device or devices to an array
8524 * which has the bitmap stored on all devices.
8525 * So make sure all bitmap pages get written
8526 */
8527 bitmap_write_all(mddev->bitmap);
8528 }
ac05f256
N
8529 INIT_WORK(&mddev->del_work, md_start_sync);
8530 queue_work(md_misc_wq, &mddev->del_work);
8531 goto unlock;
1da177e4 8532 }
ac05f256 8533 not_running:
72a23c21
NB
8534 if (!mddev->sync_thread) {
8535 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
f851b60d 8536 wake_up(&resync_wait);
72a23c21
NB
8537 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8538 &mddev->recovery))
0c3573f1 8539 if (mddev->sysfs_action)
00bcb4ac 8540 sysfs_notify_dirent_safe(mddev->sysfs_action);
72a23c21 8541 }
ac05f256
N
8542 unlock:
8543 wake_up(&mddev->sb_wait);
1da177e4
LT
8544 mddev_unlock(mddev);
8545 }
8546}
6c144d31 8547EXPORT_SYMBOL(md_check_recovery);
1da177e4 8548
a91d5ac0
JB
8549void md_reap_sync_thread(struct mddev *mddev)
8550{
8551 struct md_rdev *rdev;
8552
8553 /* resync has finished, collect result */
8554 md_unregister_thread(&mddev->sync_thread);
8555 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8556 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8557 /* success...*/
8558 /* activate any spares */
8559 if (mddev->pers->spare_active(mddev)) {
8560 sysfs_notify(&mddev->kobj, NULL,
8561 "degraded");
2953079c 8562 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
a91d5ac0
JB
8563 }
8564 }
8565 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8566 mddev->pers->finish_reshape)
8567 mddev->pers->finish_reshape(mddev);
8568
8569 /* If array is no-longer degraded, then any saved_raid_disk
f466722c 8570 * information must be scrapped.
a91d5ac0 8571 */
f466722c
N
8572 if (!mddev->degraded)
8573 rdev_for_each(rdev, mddev)
a91d5ac0
JB
8574 rdev->saved_raid_disk = -1;
8575
8576 md_update_sb(mddev, 1);
2953079c 8577 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
bb8bf15b
GJ
8578 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8579 * clustered raid */
8580 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8581 md_cluster_ops->resync_finish(mddev);
a91d5ac0 8582 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
ea358cd0 8583 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
a91d5ac0
JB
8584 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8585 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8586 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8587 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
f851b60d 8588 wake_up(&resync_wait);
a91d5ac0
JB
8589 /* flag recovery needed just to double check */
8590 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8591 sysfs_notify_dirent_safe(mddev->sysfs_action);
8592 md_new_event(mddev);
8593 if (mddev->event_work.func)
8594 queue_work(md_misc_wq, &mddev->event_work);
8595}
6c144d31 8596EXPORT_SYMBOL(md_reap_sync_thread);
a91d5ac0 8597
fd01b88c 8598void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
6bfe0b49 8599{
00bcb4ac 8600 sysfs_notify_dirent_safe(rdev->sysfs_state);
6bfe0b49 8601 wait_event_timeout(rdev->blocked_wait,
de393cde
N
8602 !test_bit(Blocked, &rdev->flags) &&
8603 !test_bit(BlockedBadBlocks, &rdev->flags),
6bfe0b49
DW
8604 msecs_to_jiffies(5000));
8605 rdev_dec_pending(rdev, mddev);
8606}
8607EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8608
c6563a8c
N
8609void md_finish_reshape(struct mddev *mddev)
8610{
8611 /* called be personality module when reshape completes. */
8612 struct md_rdev *rdev;
8613
8614 rdev_for_each(rdev, mddev) {
8615 if (rdev->data_offset > rdev->new_data_offset)
8616 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8617 else
8618 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8619 rdev->data_offset = rdev->new_data_offset;
8620 }
8621}
8622EXPORT_SYMBOL(md_finish_reshape);
2230dfe4 8623
fc974ee2 8624/* Bad block management */
2230dfe4 8625
fc974ee2 8626/* Returns 1 on success, 0 on failure */
3cb03002 8627int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
c6563a8c 8628 int is_new)
2230dfe4 8629{
85ad1d13 8630 struct mddev *mddev = rdev->mddev;
c6563a8c
N
8631 int rv;
8632 if (is_new)
8633 s += rdev->new_data_offset;
8634 else
8635 s += rdev->data_offset;
fc974ee2
VV
8636 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8637 if (rv == 0) {
2230dfe4 8638 /* Make sure they get written out promptly */
35b785f7
TM
8639 if (test_bit(ExternalBbl, &rdev->flags))
8640 sysfs_notify(&rdev->kobj, NULL,
8641 "unacknowledged_bad_blocks");
8bd2f0a0 8642 sysfs_notify_dirent_safe(rdev->sysfs_state);
2953079c
SL
8643 set_mask_bits(&mddev->sb_flags, 0,
8644 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
2230dfe4 8645 md_wakeup_thread(rdev->mddev->thread);
fc974ee2
VV
8646 return 1;
8647 } else
8648 return 0;
2230dfe4
N
8649}
8650EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8651
c6563a8c
N
8652int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8653 int is_new)
2230dfe4 8654{
35b785f7 8655 int rv;
c6563a8c
N
8656 if (is_new)
8657 s += rdev->new_data_offset;
8658 else
8659 s += rdev->data_offset;
35b785f7
TM
8660 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8661 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8662 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8663 return rv;
2230dfe4
N
8664}
8665EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8666
75c96f85
AB
8667static int md_notify_reboot(struct notifier_block *this,
8668 unsigned long code, void *x)
1da177e4
LT
8669{
8670 struct list_head *tmp;
fd01b88c 8671 struct mddev *mddev;
2dba6a91 8672 int need_delay = 0;
1da177e4 8673
c744a65c
N
8674 for_each_mddev(mddev, tmp) {
8675 if (mddev_trylock(mddev)) {
30b8aa91
N
8676 if (mddev->pers)
8677 __md_stop_writes(mddev);
0f62fb22
N
8678 if (mddev->persistent)
8679 mddev->safemode = 2;
c744a65c 8680 mddev_unlock(mddev);
2dba6a91 8681 }
c744a65c 8682 need_delay = 1;
1da177e4 8683 }
c744a65c
N
8684 /*
8685 * certain more exotic SCSI devices are known to be
8686 * volatile wrt too early system reboots. While the
8687 * right place to handle this issue is the given
8688 * driver, we do want to have a safe RAID driver ...
8689 */
8690 if (need_delay)
8691 mdelay(1000*1);
8692
1da177e4
LT
8693 return NOTIFY_DONE;
8694}
8695
75c96f85 8696static struct notifier_block md_notifier = {
1da177e4
LT
8697 .notifier_call = md_notify_reboot,
8698 .next = NULL,
8699 .priority = INT_MAX, /* before any real devices */
8700};
8701
8702static void md_geninit(void)
8703{
36a4e1fe 8704 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
1da177e4 8705
c7705f34 8706 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
1da177e4
LT
8707}
8708
75c96f85 8709static int __init md_init(void)
1da177e4 8710{
e804ac78
TH
8711 int ret = -ENOMEM;
8712
ada609ee 8713 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
e804ac78
TH
8714 if (!md_wq)
8715 goto err_wq;
8716
8717 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8718 if (!md_misc_wq)
8719 goto err_misc_wq;
8720
8721 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8722 goto err_md;
8723
8724 if ((ret = register_blkdev(0, "mdp")) < 0)
8725 goto err_mdp;
8726 mdp_major = ret;
8727
af5628f0 8728 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
e8703fe1
N
8729 md_probe, NULL, NULL);
8730 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
1da177e4
LT
8731 md_probe, NULL, NULL);
8732
1da177e4 8733 register_reboot_notifier(&md_notifier);
0b4d4147 8734 raid_table_header = register_sysctl_table(raid_root_table);
1da177e4
LT
8735
8736 md_geninit();
d710e138 8737 return 0;
1da177e4 8738
e804ac78
TH
8739err_mdp:
8740 unregister_blkdev(MD_MAJOR, "md");
8741err_md:
8742 destroy_workqueue(md_misc_wq);
8743err_misc_wq:
8744 destroy_workqueue(md_wq);
8745err_wq:
8746 return ret;
8747}
1da177e4 8748
70bcecdb 8749static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
1d7e3e96 8750{
70bcecdb
GR
8751 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8752 struct md_rdev *rdev2;
8753 int role, ret;
8754 char b[BDEVNAME_SIZE];
1d7e3e96 8755
70bcecdb
GR
8756 /* Check for change of roles in the active devices */
8757 rdev_for_each(rdev2, mddev) {
8758 if (test_bit(Faulty, &rdev2->flags))
8759 continue;
8760
8761 /* Check if the roles changed */
8762 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
dbb64f86
GR
8763
8764 if (test_bit(Candidate, &rdev2->flags)) {
8765 if (role == 0xfffe) {
8766 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8767 md_kick_rdev_from_array(rdev2);
8768 continue;
8769 }
8770 else
8771 clear_bit(Candidate, &rdev2->flags);
8772 }
8773
70bcecdb
GR
8774 if (role != rdev2->raid_disk) {
8775 /* got activated */
8776 if (rdev2->raid_disk == -1 && role != 0xffff) {
8777 rdev2->saved_raid_disk = role;
8778 ret = remove_and_add_spares(mddev, rdev2);
8779 pr_info("Activated spare: %s\n",
9d48739e 8780 bdevname(rdev2->bdev,b));
a578183e
GJ
8781 /* wakeup mddev->thread here, so array could
8782 * perform resync with the new activated disk */
8783 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8784 md_wakeup_thread(mddev->thread);
8785
70bcecdb
GR
8786 }
8787 /* device faulty
8788 * We just want to do the minimum to mark the disk
8789 * as faulty. The recovery is performed by the
8790 * one who initiated the error.
8791 */
8792 if ((role == 0xfffe) || (role == 0xfffd)) {
8793 md_error(mddev, rdev2);
8794 clear_bit(Blocked, &rdev2->flags);
8795 }
8796 }
1d7e3e96 8797 }
70bcecdb 8798
28c1b9fd
GR
8799 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
8800 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
70bcecdb
GR
8801
8802 /* Finally set the event to be up to date */
8803 mddev->events = le64_to_cpu(sb->events);
8804}
8805
8806static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
8807{
8808 int err;
8809 struct page *swapout = rdev->sb_page;
8810 struct mdp_superblock_1 *sb;
8811
8812 /* Store the sb page of the rdev in the swapout temporary
8813 * variable in case we err in the future
8814 */
8815 rdev->sb_page = NULL;
7f0f0d87
N
8816 err = alloc_disk_sb(rdev);
8817 if (err == 0) {
8818 ClearPageUptodate(rdev->sb_page);
8819 rdev->sb_loaded = 0;
8820 err = super_types[mddev->major_version].
8821 load_super(rdev, NULL, mddev->minor_version);
8822 }
70bcecdb
GR
8823 if (err < 0) {
8824 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8825 __func__, __LINE__, rdev->desc_nr, err);
7f0f0d87
N
8826 if (rdev->sb_page)
8827 put_page(rdev->sb_page);
70bcecdb
GR
8828 rdev->sb_page = swapout;
8829 rdev->sb_loaded = 1;
8830 return err;
1d7e3e96
GR
8831 }
8832
70bcecdb
GR
8833 sb = page_address(rdev->sb_page);
8834 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8835 * is not set
8836 */
8837
8838 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
8839 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
8840
8841 /* The other node finished recovery, call spare_active to set
8842 * device In_sync and mddev->degraded
8843 */
8844 if (rdev->recovery_offset == MaxSector &&
8845 !test_bit(In_sync, &rdev->flags) &&
8846 mddev->pers->spare_active(mddev))
8847 sysfs_notify(&mddev->kobj, NULL, "degraded");
8848
8849 put_page(swapout);
8850 return 0;
8851}
8852
8853void md_reload_sb(struct mddev *mddev, int nr)
8854{
8855 struct md_rdev *rdev;
8856 int err;
8857
8858 /* Find the rdev */
8859 rdev_for_each_rcu(rdev, mddev) {
8860 if (rdev->desc_nr == nr)
8861 break;
8862 }
8863
8864 if (!rdev || rdev->desc_nr != nr) {
8865 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
8866 return;
8867 }
8868
8869 err = read_rdev(mddev, rdev);
8870 if (err < 0)
8871 return;
8872
8873 check_sb_changes(mddev, rdev);
8874
8875 /* Read all rdev's to update recovery_offset */
8876 rdev_for_each_rcu(rdev, mddev)
8877 read_rdev(mddev, rdev);
1d7e3e96
GR
8878}
8879EXPORT_SYMBOL(md_reload_sb);
8880
1da177e4
LT
8881#ifndef MODULE
8882
8883/*
8884 * Searches all registered partitions for autorun RAID arrays
8885 * at boot time.
8886 */
4d936ec1 8887
5b1f5bc3 8888static DEFINE_MUTEX(detected_devices_mutex);
4d936ec1
ME
8889static LIST_HEAD(all_detected_devices);
8890struct detected_devices_node {
8891 struct list_head list;
8892 dev_t dev;
8893};
1da177e4
LT
8894
8895void md_autodetect_dev(dev_t dev)
8896{
4d936ec1
ME
8897 struct detected_devices_node *node_detected_dev;
8898
8899 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8900 if (node_detected_dev) {
8901 node_detected_dev->dev = dev;
5b1f5bc3 8902 mutex_lock(&detected_devices_mutex);
4d936ec1 8903 list_add_tail(&node_detected_dev->list, &all_detected_devices);
5b1f5bc3 8904 mutex_unlock(&detected_devices_mutex);
4d936ec1 8905 }
1da177e4
LT
8906}
8907
1da177e4
LT
8908static void autostart_arrays(int part)
8909{
3cb03002 8910 struct md_rdev *rdev;
4d936ec1
ME
8911 struct detected_devices_node *node_detected_dev;
8912 dev_t dev;
8913 int i_scanned, i_passed;
1da177e4 8914
4d936ec1
ME
8915 i_scanned = 0;
8916 i_passed = 0;
1da177e4 8917
9d48739e 8918 pr_info("md: Autodetecting RAID arrays.\n");
1da177e4 8919
5b1f5bc3 8920 mutex_lock(&detected_devices_mutex);
4d936ec1
ME
8921 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8922 i_scanned++;
8923 node_detected_dev = list_entry(all_detected_devices.next,
8924 struct detected_devices_node, list);
8925 list_del(&node_detected_dev->list);
8926 dev = node_detected_dev->dev;
8927 kfree(node_detected_dev);
90bcf133 8928 mutex_unlock(&detected_devices_mutex);
df968c4e 8929 rdev = md_import_device(dev,0, 90);
90bcf133 8930 mutex_lock(&detected_devices_mutex);
1da177e4
LT
8931 if (IS_ERR(rdev))
8932 continue;
8933
403df478 8934 if (test_bit(Faulty, &rdev->flags))
1da177e4 8935 continue;
403df478 8936
d0fae18f 8937 set_bit(AutoDetected, &rdev->flags);
1da177e4 8938 list_add(&rdev->same_set, &pending_raid_disks);
4d936ec1 8939 i_passed++;
1da177e4 8940 }
5b1f5bc3 8941 mutex_unlock(&detected_devices_mutex);
4d936ec1 8942
9d48739e 8943 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
1da177e4
LT
8944
8945 autorun_devices(part);
8946}
8947
fdee8ae4 8948#endif /* !MODULE */
1da177e4
LT
8949
8950static __exit void md_exit(void)
8951{
fd01b88c 8952 struct mddev *mddev;
1da177e4 8953 struct list_head *tmp;
e2f23b60 8954 int delay = 1;
8ab5e4c1 8955
af5628f0 8956 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
e8703fe1 8957 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
1da177e4 8958
3dbd8c2e 8959 unregister_blkdev(MD_MAJOR,"md");
1da177e4
LT
8960 unregister_blkdev(mdp_major, "mdp");
8961 unregister_reboot_notifier(&md_notifier);
8962 unregister_sysctl_table(raid_table_header);
e2f23b60
N
8963
8964 /* We cannot unload the modules while some process is
8965 * waiting for us in select() or poll() - wake them up
8966 */
8967 md_unloading = 1;
8968 while (waitqueue_active(&md_event_waiters)) {
8969 /* not safe to leave yet */
8970 wake_up(&md_event_waiters);
8971 msleep(delay);
8972 delay += delay;
8973 }
1da177e4 8974 remove_proc_entry("mdstat", NULL);
e2f23b60 8975
29ac4aa3 8976 for_each_mddev(mddev, tmp) {
1da177e4 8977 export_array(mddev);
d3374825 8978 mddev->hold_active = 0;
1da177e4 8979 }
e804ac78
TH
8980 destroy_workqueue(md_misc_wq);
8981 destroy_workqueue(md_wq);
1da177e4
LT
8982}
8983
685784aa 8984subsys_initcall(md_init);
1da177e4
LT
8985module_exit(md_exit)
8986
f91de92e
N
8987static int get_ro(char *buffer, struct kernel_param *kp)
8988{
8989 return sprintf(buffer, "%d", start_readonly);
8990}
8991static int set_ro(const char *val, struct kernel_param *kp)
8992{
4c9309c0 8993 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
f91de92e
N
8994}
8995
80ca3a44
N
8996module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8997module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
efeb53c0 8998module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
f91de92e 8999
1da177e4 9000MODULE_LICENSE("GPL");
0efb9e61 9001MODULE_DESCRIPTION("MD RAID framework");
aa1595e9 9002MODULE_ALIAS("md");
72008652 9003MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);