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