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