]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/md/md.c
md: tidy up set_bitmap_file
[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 */
f4ad3d38 5505 char *ptr;
32a7627c
N
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
1ec885cd 5519 ptr = d_path(&mddev->bitmap->storage.file->f_path,
f4ad3d38 5520 file->pathname, sizeof(file->pathname));
6bcfd601 5521 if (IS_ERR(ptr))
32a7627c
N
5522 goto out;
5523
f4ad3d38
N
5524 memmove(file->pathname, ptr,
5525 sizeof(file->pathname)-(ptr-file->pathname));
32a7627c
N
5526
5527copy_out:
5528 err = 0;
5529 if (copy_to_user(arg, file, sizeof(*file)))
5530 err = -EFAULT;
5531out:
32a7627c
N
5532 kfree(file);
5533 return err;
5534}
5535
f72ffdd6 5536static int get_disk_info(struct mddev *mddev, void __user * arg)
1da177e4
LT
5537{
5538 mdu_disk_info_t info;
3cb03002 5539 struct md_rdev *rdev;
1da177e4
LT
5540
5541 if (copy_from_user(&info, arg, sizeof(info)))
5542 return -EFAULT;
5543
1ca69c4b
N
5544 rcu_read_lock();
5545 rdev = find_rdev_nr_rcu(mddev, info.number);
1da177e4
LT
5546 if (rdev) {
5547 info.major = MAJOR(rdev->bdev->bd_dev);
5548 info.minor = MINOR(rdev->bdev->bd_dev);
5549 info.raid_disk = rdev->raid_disk;
5550 info.state = 0;
b2d444d7 5551 if (test_bit(Faulty, &rdev->flags))
1da177e4 5552 info.state |= (1<<MD_DISK_FAULTY);
b2d444d7 5553 else if (test_bit(In_sync, &rdev->flags)) {
1da177e4
LT
5554 info.state |= (1<<MD_DISK_ACTIVE);
5555 info.state |= (1<<MD_DISK_SYNC);
5556 }
8ddf9efe
N
5557 if (test_bit(WriteMostly, &rdev->flags))
5558 info.state |= (1<<MD_DISK_WRITEMOSTLY);
1da177e4
LT
5559 } else {
5560 info.major = info.minor = 0;
5561 info.raid_disk = -1;
5562 info.state = (1<<MD_DISK_REMOVED);
5563 }
1ca69c4b 5564 rcu_read_unlock();
1da177e4
LT
5565
5566 if (copy_to_user(arg, &info, sizeof(info)))
5567 return -EFAULT;
5568
5569 return 0;
5570}
5571
f72ffdd6 5572static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
1da177e4
LT
5573{
5574 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3cb03002 5575 struct md_rdev *rdev;
1da177e4
LT
5576 dev_t dev = MKDEV(info->major,info->minor);
5577
5578 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5579 return -EOVERFLOW;
5580
5581 if (!mddev->raid_disks) {
5582 int err;
5583 /* expecting a device which has a superblock */
5584 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5585 if (IS_ERR(rdev)) {
f72ffdd6 5586 printk(KERN_WARNING
1da177e4
LT
5587 "md: md_import_device returned %ld\n",
5588 PTR_ERR(rdev));
5589 return PTR_ERR(rdev);
5590 }
5591 if (!list_empty(&mddev->disks)) {
3cb03002
N
5592 struct md_rdev *rdev0
5593 = list_entry(mddev->disks.next,
5594 struct md_rdev, same_set);
a9f326eb 5595 err = super_types[mddev->major_version]
1da177e4
LT
5596 .load_super(rdev, rdev0, mddev->minor_version);
5597 if (err < 0) {
f72ffdd6 5598 printk(KERN_WARNING
1da177e4 5599 "md: %s has different UUID to %s\n",
f72ffdd6 5600 bdevname(rdev->bdev,b),
1da177e4
LT
5601 bdevname(rdev0->bdev,b2));
5602 export_rdev(rdev);
5603 return -EINVAL;
5604 }
5605 }
5606 err = bind_rdev_to_array(rdev, mddev);
5607 if (err)
5608 export_rdev(rdev);
5609 return err;
5610 }
5611
5612 /*
5613 * add_new_disk can be used once the array is assembled
5614 * to add "hot spares". They must already have a superblock
5615 * written
5616 */
5617 if (mddev->pers) {
5618 int err;
5619 if (!mddev->pers->hot_add_disk) {
f72ffdd6 5620 printk(KERN_WARNING
1da177e4
LT
5621 "%s: personality does not support diskops!\n",
5622 mdname(mddev));
5623 return -EINVAL;
5624 }
7b1e35f6
N
5625 if (mddev->persistent)
5626 rdev = md_import_device(dev, mddev->major_version,
5627 mddev->minor_version);
5628 else
5629 rdev = md_import_device(dev, -1, -1);
1da177e4 5630 if (IS_ERR(rdev)) {
f72ffdd6 5631 printk(KERN_WARNING
1da177e4
LT
5632 "md: md_import_device returned %ld\n",
5633 PTR_ERR(rdev));
5634 return PTR_ERR(rdev);
5635 }
1a855a06 5636 /* set saved_raid_disk if appropriate */
41158c7e
N
5637 if (!mddev->persistent) {
5638 if (info->state & (1<<MD_DISK_SYNC) &&
bf572541 5639 info->raid_disk < mddev->raid_disks) {
41158c7e 5640 rdev->raid_disk = info->raid_disk;
bf572541 5641 set_bit(In_sync, &rdev->flags);
8313b8e5 5642 clear_bit(Bitmap_sync, &rdev->flags);
bf572541 5643 } else
41158c7e 5644 rdev->raid_disk = -1;
f466722c 5645 rdev->saved_raid_disk = rdev->raid_disk;
41158c7e
N
5646 } else
5647 super_types[mddev->major_version].
5648 validate_super(mddev, rdev);
bedd86b7 5649 if ((info->state & (1<<MD_DISK_SYNC)) &&
f4563091 5650 rdev->raid_disk != info->raid_disk) {
bedd86b7
N
5651 /* This was a hot-add request, but events doesn't
5652 * match, so reject it.
5653 */
5654 export_rdev(rdev);
5655 return -EINVAL;
5656 }
5657
b2d444d7 5658 clear_bit(In_sync, &rdev->flags); /* just to be sure */
8ddf9efe
N
5659 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5660 set_bit(WriteMostly, &rdev->flags);
575a80fa
N
5661 else
5662 clear_bit(WriteMostly, &rdev->flags);
8ddf9efe 5663
1da177e4
LT
5664 rdev->raid_disk = -1;
5665 err = bind_rdev_to_array(rdev, mddev);
7c7546cc
N
5666 if (!err && !mddev->pers->hot_remove_disk) {
5667 /* If there is hot_add_disk but no hot_remove_disk
5668 * then added disks for geometry changes,
5669 * and should be added immediately.
5670 */
5671 super_types[mddev->major_version].
5672 validate_super(mddev, rdev);
5673 err = mddev->pers->hot_add_disk(mddev, rdev);
5674 if (err)
5675 unbind_rdev_from_array(rdev);
5676 }
1da177e4
LT
5677 if (err)
5678 export_rdev(rdev);
52664732 5679 else
00bcb4ac 5680 sysfs_notify_dirent_safe(rdev->sysfs_state);
c361777f 5681
7ceb17e8 5682 set_bit(MD_CHANGE_DEVS, &mddev->flags);
72a23c21
NB
5683 if (mddev->degraded)
5684 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
c361777f 5685 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9864c005 5686 if (!err)
5687 md_new_event(mddev);
005eca5e 5688 md_wakeup_thread(mddev->thread);
1da177e4
LT
5689 return err;
5690 }
5691
5692 /* otherwise, add_new_disk is only allowed
5693 * for major_version==0 superblocks
5694 */
5695 if (mddev->major_version != 0) {
5696 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5697 mdname(mddev));
5698 return -EINVAL;
5699 }
5700
5701 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5702 int err;
d710e138 5703 rdev = md_import_device(dev, -1, 0);
1da177e4 5704 if (IS_ERR(rdev)) {
f72ffdd6 5705 printk(KERN_WARNING
1da177e4
LT
5706 "md: error, md_import_device() returned %ld\n",
5707 PTR_ERR(rdev));
5708 return PTR_ERR(rdev);
5709 }
5710 rdev->desc_nr = info->number;
5711 if (info->raid_disk < mddev->raid_disks)
5712 rdev->raid_disk = info->raid_disk;
5713 else
5714 rdev->raid_disk = -1;
5715
1da177e4 5716 if (rdev->raid_disk < mddev->raid_disks)
b2d444d7
N
5717 if (info->state & (1<<MD_DISK_SYNC))
5718 set_bit(In_sync, &rdev->flags);
1da177e4 5719
8ddf9efe
N
5720 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5721 set_bit(WriteMostly, &rdev->flags);
5722
1da177e4
LT
5723 if (!mddev->persistent) {
5724 printk(KERN_INFO "md: nonpersistent superblock ...\n");
77304d2a
MS
5725 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5726 } else
57b2caa3 5727 rdev->sb_start = calc_dev_sboffset(rdev);
8190e754 5728 rdev->sectors = rdev->sb_start;
1da177e4 5729
2bf071bf
N
5730 err = bind_rdev_to_array(rdev, mddev);
5731 if (err) {
5732 export_rdev(rdev);
5733 return err;
5734 }
1da177e4
LT
5735 }
5736
5737 return 0;
5738}
5739
f72ffdd6 5740static int hot_remove_disk(struct mddev *mddev, dev_t dev)
1da177e4
LT
5741{
5742 char b[BDEVNAME_SIZE];
3cb03002 5743 struct md_rdev *rdev;
1da177e4 5744
1da177e4
LT
5745 rdev = find_rdev(mddev, dev);
5746 if (!rdev)
5747 return -ENXIO;
5748
3ea8929d
N
5749 clear_bit(Blocked, &rdev->flags);
5750 remove_and_add_spares(mddev, rdev);
5751
1da177e4
LT
5752 if (rdev->raid_disk >= 0)
5753 goto busy;
5754
5755 kick_rdev_from_array(rdev);
850b2b42 5756 md_update_sb(mddev, 1);
d7603b7e 5757 md_new_event(mddev);
1da177e4
LT
5758
5759 return 0;
5760busy:
fdefa4d8 5761 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
1da177e4
LT
5762 bdevname(rdev->bdev,b), mdname(mddev));
5763 return -EBUSY;
5764}
5765
f72ffdd6 5766static int hot_add_disk(struct mddev *mddev, dev_t dev)
1da177e4
LT
5767{
5768 char b[BDEVNAME_SIZE];
5769 int err;
3cb03002 5770 struct md_rdev *rdev;
1da177e4
LT
5771
5772 if (!mddev->pers)
5773 return -ENODEV;
5774
5775 if (mddev->major_version != 0) {
5776 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5777 " version-0 superblocks.\n",
5778 mdname(mddev));
5779 return -EINVAL;
5780 }
5781 if (!mddev->pers->hot_add_disk) {
f72ffdd6 5782 printk(KERN_WARNING
1da177e4
LT
5783 "%s: personality does not support diskops!\n",
5784 mdname(mddev));
5785 return -EINVAL;
5786 }
5787
d710e138 5788 rdev = md_import_device(dev, -1, 0);
1da177e4 5789 if (IS_ERR(rdev)) {
f72ffdd6 5790 printk(KERN_WARNING
1da177e4
LT
5791 "md: error, md_import_device() returned %ld\n",
5792 PTR_ERR(rdev));
5793 return -EINVAL;
5794 }
5795
5796 if (mddev->persistent)
57b2caa3 5797 rdev->sb_start = calc_dev_sboffset(rdev);
1da177e4 5798 else
77304d2a 5799 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
1da177e4 5800
8190e754 5801 rdev->sectors = rdev->sb_start;
1da177e4 5802
b2d444d7 5803 if (test_bit(Faulty, &rdev->flags)) {
f72ffdd6 5804 printk(KERN_WARNING
1da177e4
LT
5805 "md: can not hot-add faulty %s disk to %s!\n",
5806 bdevname(rdev->bdev,b), mdname(mddev));
5807 err = -EINVAL;
5808 goto abort_export;
5809 }
b2d444d7 5810 clear_bit(In_sync, &rdev->flags);
1da177e4 5811 rdev->desc_nr = -1;
5842730d 5812 rdev->saved_raid_disk = -1;
2bf071bf
N
5813 err = bind_rdev_to_array(rdev, mddev);
5814 if (err)
5815 goto abort_export;
1da177e4
LT
5816
5817 /*
5818 * The rest should better be atomic, we can have disk failures
5819 * noticed in interrupt contexts ...
5820 */
5821
1da177e4
LT
5822 rdev->raid_disk = -1;
5823
850b2b42 5824 md_update_sb(mddev, 1);
1da177e4
LT
5825
5826 /*
5827 * Kick recovery, maybe this spare has to be added to the
5828 * array immediately.
5829 */
5830 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5831 md_wakeup_thread(mddev->thread);
d7603b7e 5832 md_new_event(mddev);
1da177e4
LT
5833 return 0;
5834
1da177e4
LT
5835abort_export:
5836 export_rdev(rdev);
5837 return err;
5838}
5839
fd01b88c 5840static int set_bitmap_file(struct mddev *mddev, int fd)
32a7627c 5841{
035328c2 5842 int err = 0;
32a7627c 5843
36fa3063 5844 if (mddev->pers) {
d66b1b39 5845 if (!mddev->pers->quiesce || !mddev->thread)
36fa3063
N
5846 return -EBUSY;
5847 if (mddev->recovery || mddev->sync_thread)
5848 return -EBUSY;
5849 /* we should be able to change the bitmap.. */
5850 }
32a7627c 5851
36fa3063 5852 if (fd >= 0) {
035328c2 5853 struct inode *inode;
1e594bb2
N
5854 struct file *f;
5855
5856 if (mddev->bitmap || mddev->bitmap_info.file)
36fa3063 5857 return -EEXIST; /* cannot add when bitmap is present */
1e594bb2 5858 f = fget(fd);
32a7627c 5859
1e594bb2 5860 if (f == NULL) {
36fa3063
N
5861 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5862 mdname(mddev));
5863 return -EBADF;
5864 }
5865
1e594bb2 5866 inode = f->f_mapping->host;
035328c2
N
5867 if (!S_ISREG(inode->i_mode)) {
5868 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
5869 mdname(mddev));
5870 err = -EBADF;
1e594bb2 5871 } else if (!(f->f_mode & FMODE_WRITE)) {
035328c2
N
5872 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
5873 mdname(mddev));
5874 err = -EBADF;
5875 } else if (atomic_read(&inode->i_writecount) != 1) {
36fa3063
N
5876 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5877 mdname(mddev));
035328c2
N
5878 err = -EBUSY;
5879 }
5880 if (err) {
1e594bb2 5881 fput(f);
36fa3063
N
5882 return err;
5883 }
1e594bb2 5884 mddev->bitmap_info.file = f;
c3d9714e 5885 mddev->bitmap_info.offset = 0; /* file overrides offset */
36fa3063
N
5886 } else if (mddev->bitmap == NULL)
5887 return -ENOENT; /* cannot remove what isn't there */
5888 err = 0;
5889 if (mddev->pers) {
5890 mddev->pers->quiesce(mddev, 1);
69e51b44 5891 if (fd >= 0) {
36fa3063 5892 err = bitmap_create(mddev);
69e51b44
N
5893 if (!err)
5894 err = bitmap_load(mddev);
5895 }
d7375ab3 5896 if (fd < 0 || err) {
36fa3063 5897 bitmap_destroy(mddev);
d7375ab3
N
5898 fd = -1; /* make sure to put the file */
5899 }
36fa3063 5900 mddev->pers->quiesce(mddev, 0);
d7375ab3
N
5901 }
5902 if (fd < 0) {
035328c2 5903 if (mddev->bitmap_info.file)
c3d9714e 5904 fput(mddev->bitmap_info.file);
c3d9714e 5905 mddev->bitmap_info.file = NULL;
36fa3063
N
5906 }
5907
32a7627c
N
5908 return err;
5909}
5910
1da177e4
LT
5911/*
5912 * set_array_info is used two different ways
5913 * The original usage is when creating a new array.
5914 * In this usage, raid_disks is > 0 and it together with
5915 * level, size, not_persistent,layout,chunksize determine the
5916 * shape of the array.
5917 * This will always create an array with a type-0.90.0 superblock.
5918 * The newer usage is when assembling an array.
5919 * In this case raid_disks will be 0, and the major_version field is
5920 * use to determine which style super-blocks are to be found on the devices.
5921 * The minor and patch _version numbers are also kept incase the
5922 * super_block handler wishes to interpret them.
5923 */
f72ffdd6 5924static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
1da177e4
LT
5925{
5926
5927 if (info->raid_disks == 0) {
5928 /* just setting version number for superblock loading */
5929 if (info->major_version < 0 ||
50511da3 5930 info->major_version >= ARRAY_SIZE(super_types) ||
1da177e4
LT
5931 super_types[info->major_version].name == NULL) {
5932 /* maybe try to auto-load a module? */
f72ffdd6 5933 printk(KERN_INFO
1da177e4
LT
5934 "md: superblock version %d not known\n",
5935 info->major_version);
5936 return -EINVAL;
5937 }
5938 mddev->major_version = info->major_version;
5939 mddev->minor_version = info->minor_version;
5940 mddev->patch_version = info->patch_version;
3f9d7b0d 5941 mddev->persistent = !info->not_persistent;
cbd19983
N
5942 /* ensure mddev_put doesn't delete this now that there
5943 * is some minimal configuration.
5944 */
5945 mddev->ctime = get_seconds();
1da177e4
LT
5946 return 0;
5947 }
5948 mddev->major_version = MD_MAJOR_VERSION;
5949 mddev->minor_version = MD_MINOR_VERSION;
5950 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5951 mddev->ctime = get_seconds();
5952
5953 mddev->level = info->level;
17115e03 5954 mddev->clevel[0] = 0;
58c0fed4 5955 mddev->dev_sectors = 2 * (sector_t)info->size;
1da177e4
LT
5956 mddev->raid_disks = info->raid_disks;
5957 /* don't set md_minor, it is determined by which /dev/md* was
5958 * openned
5959 */
5960 if (info->state & (1<<MD_SB_CLEAN))
5961 mddev->recovery_cp = MaxSector;
5962 else
5963 mddev->recovery_cp = 0;
5964 mddev->persistent = ! info->not_persistent;
e691063a 5965 mddev->external = 0;
1da177e4
LT
5966
5967 mddev->layout = info->layout;
9d8f0363 5968 mddev->chunk_sectors = info->chunk_size >> 9;
1da177e4
LT
5969
5970 mddev->max_disks = MD_SB_DISKS;
5971
e691063a
N
5972 if (mddev->persistent)
5973 mddev->flags = 0;
850b2b42 5974 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1da177e4 5975
c3d9714e 5976 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6409bb05 5977 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
c3d9714e 5978 mddev->bitmap_info.offset = 0;
b2a2703c 5979
f6705578
N
5980 mddev->reshape_position = MaxSector;
5981
1da177e4
LT
5982 /*
5983 * Generate a 128 bit UUID
5984 */
5985 get_random_bytes(mddev->uuid, 16);
5986
f6705578 5987 mddev->new_level = mddev->level;
664e7c41 5988 mddev->new_chunk_sectors = mddev->chunk_sectors;
f6705578
N
5989 mddev->new_layout = mddev->layout;
5990 mddev->delta_disks = 0;
2c810cdd 5991 mddev->reshape_backwards = 0;
f6705578 5992
1da177e4
LT
5993 return 0;
5994}
5995
fd01b88c 5996void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
1f403624 5997{
b522adcd
DW
5998 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5999
6000 if (mddev->external_size)
6001 return;
6002
1f403624
DW
6003 mddev->array_sectors = array_sectors;
6004}
6005EXPORT_SYMBOL(md_set_array_sectors);
6006
fd01b88c 6007static int update_size(struct mddev *mddev, sector_t num_sectors)
a35b0d69 6008{
3cb03002 6009 struct md_rdev *rdev;
a35b0d69 6010 int rv;
d71f9f88 6011 int fit = (num_sectors == 0);
a35b0d69
N
6012
6013 if (mddev->pers->resize == NULL)
6014 return -EINVAL;
d71f9f88
AN
6015 /* The "num_sectors" is the number of sectors of each device that
6016 * is used. This can only make sense for arrays with redundancy.
6017 * linear and raid0 always use whatever space is available. We can only
6018 * consider changing this number if no resync or reconstruction is
6019 * happening, and if the new size is acceptable. It must fit before the
0f420358 6020 * sb_start or, if that is <data_offset, it must fit before the size
d71f9f88
AN
6021 * of each device. If num_sectors is zero, we find the largest size
6022 * that fits.
a35b0d69 6023 */
f851b60d
N
6024 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6025 mddev->sync_thread)
a35b0d69 6026 return -EBUSY;
bd8839e0
N
6027 if (mddev->ro)
6028 return -EROFS;
a4a6125a 6029
dafb20fa 6030 rdev_for_each(rdev, mddev) {
dd8ac336 6031 sector_t avail = rdev->sectors;
01ab5662 6032
d71f9f88
AN
6033 if (fit && (num_sectors == 0 || num_sectors > avail))
6034 num_sectors = avail;
6035 if (avail < num_sectors)
a35b0d69
N
6036 return -ENOSPC;
6037 }
d71f9f88 6038 rv = mddev->pers->resize(mddev, num_sectors);
449aad3e
N
6039 if (!rv)
6040 revalidate_disk(mddev->gendisk);
a35b0d69
N
6041 return rv;
6042}
6043
fd01b88c 6044static int update_raid_disks(struct mddev *mddev, int raid_disks)
da943b99
N
6045{
6046 int rv;
c6563a8c 6047 struct md_rdev *rdev;
da943b99 6048 /* change the number of raid disks */
63c70c4f 6049 if (mddev->pers->check_reshape == NULL)
da943b99 6050 return -EINVAL;
bd8839e0
N
6051 if (mddev->ro)
6052 return -EROFS;
da943b99 6053 if (raid_disks <= 0 ||
233fca36 6054 (mddev->max_disks && raid_disks >= mddev->max_disks))
da943b99 6055 return -EINVAL;
f851b60d
N
6056 if (mddev->sync_thread ||
6057 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6058 mddev->reshape_position != MaxSector)
da943b99 6059 return -EBUSY;
c6563a8c
N
6060
6061 rdev_for_each(rdev, mddev) {
6062 if (mddev->raid_disks < raid_disks &&
6063 rdev->data_offset < rdev->new_data_offset)
6064 return -EINVAL;
6065 if (mddev->raid_disks > raid_disks &&
6066 rdev->data_offset > rdev->new_data_offset)
6067 return -EINVAL;
6068 }
6069
63c70c4f 6070 mddev->delta_disks = raid_disks - mddev->raid_disks;
2c810cdd
N
6071 if (mddev->delta_disks < 0)
6072 mddev->reshape_backwards = 1;
6073 else if (mddev->delta_disks > 0)
6074 mddev->reshape_backwards = 0;
63c70c4f
N
6075
6076 rv = mddev->pers->check_reshape(mddev);
2c810cdd 6077 if (rv < 0) {
de171cb9 6078 mddev->delta_disks = 0;
2c810cdd
N
6079 mddev->reshape_backwards = 0;
6080 }
da943b99
N
6081 return rv;
6082}
6083
1da177e4
LT
6084/*
6085 * update_array_info is used to change the configuration of an
6086 * on-line array.
6087 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6088 * fields in the info are checked against the array.
6089 * Any differences that cannot be handled will cause an error.
6090 * Normally, only one change can be managed at a time.
6091 */
fd01b88c 6092static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
1da177e4
LT
6093{
6094 int rv = 0;
6095 int cnt = 0;
36fa3063
N
6096 int state = 0;
6097
6098 /* calculate expected state,ignoring low bits */
c3d9714e 6099 if (mddev->bitmap && mddev->bitmap_info.offset)
36fa3063 6100 state |= (1 << MD_SB_BITMAP_PRESENT);
1da177e4
LT
6101
6102 if (mddev->major_version != info->major_version ||
6103 mddev->minor_version != info->minor_version ||
6104/* mddev->patch_version != info->patch_version || */
6105 mddev->ctime != info->ctime ||
6106 mddev->level != info->level ||
6107/* mddev->layout != info->layout || */
6108 !mddev->persistent != info->not_persistent||
9d8f0363 6109 mddev->chunk_sectors != info->chunk_size >> 9 ||
36fa3063
N
6110 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6111 ((state^info->state) & 0xfffffe00)
6112 )
1da177e4
LT
6113 return -EINVAL;
6114 /* Check there is only one change */
58c0fed4
AN
6115 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6116 cnt++;
6117 if (mddev->raid_disks != info->raid_disks)
6118 cnt++;
6119 if (mddev->layout != info->layout)
6120 cnt++;
6121 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6122 cnt++;
6123 if (cnt == 0)
6124 return 0;
6125 if (cnt > 1)
6126 return -EINVAL;
1da177e4
LT
6127
6128 if (mddev->layout != info->layout) {
6129 /* Change layout
6130 * we don't need to do anything at the md level, the
6131 * personality will take care of it all.
6132 */
50ac168a 6133 if (mddev->pers->check_reshape == NULL)
1da177e4 6134 return -EINVAL;
597a711b
N
6135 else {
6136 mddev->new_layout = info->layout;
50ac168a 6137 rv = mddev->pers->check_reshape(mddev);
597a711b
N
6138 if (rv)
6139 mddev->new_layout = mddev->layout;
6140 return rv;
6141 }
1da177e4 6142 }
58c0fed4 6143 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
d71f9f88 6144 rv = update_size(mddev, (sector_t)info->size * 2);
a35b0d69 6145
da943b99
N
6146 if (mddev->raid_disks != info->raid_disks)
6147 rv = update_raid_disks(mddev, info->raid_disks);
6148
36fa3063 6149 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
d66b1b39 6150 if (mddev->pers->quiesce == NULL || mddev->thread == NULL)
36fa3063
N
6151 return -EINVAL;
6152 if (mddev->recovery || mddev->sync_thread)
6153 return -EBUSY;
6154 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6155 /* add the bitmap */
6156 if (mddev->bitmap)
6157 return -EEXIST;
c3d9714e 6158 if (mddev->bitmap_info.default_offset == 0)
36fa3063 6159 return -EINVAL;
c3d9714e
N
6160 mddev->bitmap_info.offset =
6161 mddev->bitmap_info.default_offset;
6409bb05
N
6162 mddev->bitmap_info.space =
6163 mddev->bitmap_info.default_space;
36fa3063
N
6164 mddev->pers->quiesce(mddev, 1);
6165 rv = bitmap_create(mddev);
69e51b44
N
6166 if (!rv)
6167 rv = bitmap_load(mddev);
36fa3063
N
6168 if (rv)
6169 bitmap_destroy(mddev);
6170 mddev->pers->quiesce(mddev, 0);
6171 } else {
6172 /* remove the bitmap */
6173 if (!mddev->bitmap)
6174 return -ENOENT;
1ec885cd 6175 if (mddev->bitmap->storage.file)
36fa3063
N
6176 return -EINVAL;
6177 mddev->pers->quiesce(mddev, 1);
6178 bitmap_destroy(mddev);
6179 mddev->pers->quiesce(mddev, 0);
c3d9714e 6180 mddev->bitmap_info.offset = 0;
36fa3063
N
6181 }
6182 }
850b2b42 6183 md_update_sb(mddev, 1);
1da177e4
LT
6184 return rv;
6185}
6186
fd01b88c 6187static int set_disk_faulty(struct mddev *mddev, dev_t dev)
1da177e4 6188{
3cb03002 6189 struct md_rdev *rdev;
1ca69c4b 6190 int err = 0;
1da177e4
LT
6191
6192 if (mddev->pers == NULL)
6193 return -ENODEV;
6194
1ca69c4b
N
6195 rcu_read_lock();
6196 rdev = find_rdev_rcu(mddev, dev);
1da177e4 6197 if (!rdev)
1ca69c4b
N
6198 err = -ENODEV;
6199 else {
6200 md_error(mddev, rdev);
6201 if (!test_bit(Faulty, &rdev->flags))
6202 err = -EBUSY;
6203 }
6204 rcu_read_unlock();
6205 return err;
1da177e4
LT
6206}
6207
2f9618ce
AN
6208/*
6209 * We have a problem here : there is no easy way to give a CHS
6210 * virtual geometry. We currently pretend that we have a 2 heads
6211 * 4 sectors (with a BIG number of cylinders...). This drives
6212 * dosfs just mad... ;-)
6213 */
a885c8c4
CH
6214static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6215{
fd01b88c 6216 struct mddev *mddev = bdev->bd_disk->private_data;
a885c8c4
CH
6217
6218 geo->heads = 2;
6219 geo->sectors = 4;
49ce6cea 6220 geo->cylinders = mddev->array_sectors / 8;
a885c8c4
CH
6221 return 0;
6222}
6223
cb335f88
NS
6224static inline bool md_ioctl_valid(unsigned int cmd)
6225{
6226 switch (cmd) {
6227 case ADD_NEW_DISK:
6228 case BLKROSET:
6229 case GET_ARRAY_INFO:
6230 case GET_BITMAP_FILE:
6231 case GET_DISK_INFO:
6232 case HOT_ADD_DISK:
6233 case HOT_REMOVE_DISK:
cb335f88
NS
6234 case RAID_AUTORUN:
6235 case RAID_VERSION:
6236 case RESTART_ARRAY_RW:
6237 case RUN_ARRAY:
6238 case SET_ARRAY_INFO:
6239 case SET_BITMAP_FILE:
6240 case SET_DISK_FAULTY:
6241 case STOP_ARRAY:
6242 case STOP_ARRAY_RO:
6243 return true;
6244 default:
6245 return false;
6246 }
6247}
6248
a39907fa 6249static int md_ioctl(struct block_device *bdev, fmode_t mode,
1da177e4
LT
6250 unsigned int cmd, unsigned long arg)
6251{
6252 int err = 0;
6253 void __user *argp = (void __user *)arg;
fd01b88c 6254 struct mddev *mddev = NULL;
e2218350 6255 int ro;
1da177e4 6256
cb335f88
NS
6257 if (!md_ioctl_valid(cmd))
6258 return -ENOTTY;
6259
506c9e44
N
6260 switch (cmd) {
6261 case RAID_VERSION:
6262 case GET_ARRAY_INFO:
6263 case GET_DISK_INFO:
6264 break;
6265 default:
6266 if (!capable(CAP_SYS_ADMIN))
6267 return -EACCES;
6268 }
1da177e4
LT
6269
6270 /*
6271 * Commands dealing with the RAID driver but not any
6272 * particular array:
6273 */
c02c0aeb
N
6274 switch (cmd) {
6275 case RAID_VERSION:
6276 err = get_version(argp);
3adc28d8 6277 goto out;
1da177e4 6278
1da177e4 6279#ifndef MODULE
c02c0aeb
N
6280 case RAID_AUTORUN:
6281 err = 0;
6282 autostart_arrays(arg);
3adc28d8 6283 goto out;
1da177e4 6284#endif
c02c0aeb 6285 default:;
1da177e4
LT
6286 }
6287
6288 /*
6289 * Commands creating/starting a new array:
6290 */
6291
a39907fa 6292 mddev = bdev->bd_disk->private_data;
1da177e4
LT
6293
6294 if (!mddev) {
6295 BUG();
3adc28d8 6296 goto out;
1da177e4
LT
6297 }
6298
1ca69c4b
N
6299 /* Some actions do not requires the mutex */
6300 switch (cmd) {
6301 case GET_ARRAY_INFO:
6302 if (!mddev->raid_disks && !mddev->external)
6303 err = -ENODEV;
6304 else
6305 err = get_array_info(mddev, argp);
3adc28d8 6306 goto out;
1ca69c4b
N
6307
6308 case GET_DISK_INFO:
6309 if (!mddev->raid_disks && !mddev->external)
6310 err = -ENODEV;
6311 else
6312 err = get_disk_info(mddev, argp);
3adc28d8 6313 goto out;
1ca69c4b
N
6314
6315 case SET_DISK_FAULTY:
6316 err = set_disk_faulty(mddev, new_decode_dev(arg));
3adc28d8 6317 goto out;
1ca69c4b
N
6318 }
6319
a7a3f08d
N
6320 if (cmd == ADD_NEW_DISK)
6321 /* need to ensure md_delayed_delete() has completed */
6322 flush_workqueue(md_misc_wq);
6323
90f5f7ad
HR
6324 if (cmd == HOT_REMOVE_DISK)
6325 /* need to ensure recovery thread has run */
6326 wait_event_interruptible_timeout(mddev->sb_wait,
6327 !test_bit(MD_RECOVERY_NEEDED,
6328 &mddev->flags),
6329 msecs_to_jiffies(5000));
260fa034
N
6330 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6331 /* Need to flush page cache, and ensure no-one else opens
6332 * and writes
6333 */
6334 mutex_lock(&mddev->open_mutex);
9ba3b7f5 6335 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
260fa034
N
6336 mutex_unlock(&mddev->open_mutex);
6337 err = -EBUSY;
3adc28d8 6338 goto out;
260fa034
N
6339 }
6340 set_bit(MD_STILL_CLOSED, &mddev->flags);
6341 mutex_unlock(&mddev->open_mutex);
6342 sync_blockdev(bdev);
6343 }
1da177e4
LT
6344 err = mddev_lock(mddev);
6345 if (err) {
f72ffdd6 6346 printk(KERN_INFO
1da177e4
LT
6347 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6348 err, cmd);
3adc28d8 6349 goto out;
1da177e4
LT
6350 }
6351
c02c0aeb
N
6352 if (cmd == SET_ARRAY_INFO) {
6353 mdu_array_info_t info;
6354 if (!arg)
6355 memset(&info, 0, sizeof(info));
6356 else if (copy_from_user(&info, argp, sizeof(info))) {
6357 err = -EFAULT;
3adc28d8 6358 goto unlock;
c02c0aeb
N
6359 }
6360 if (mddev->pers) {
6361 err = update_array_info(mddev, &info);
6362 if (err) {
6363 printk(KERN_WARNING "md: couldn't update"
6364 " array info. %d\n", err);
3adc28d8 6365 goto unlock;
1da177e4 6366 }
3adc28d8 6367 goto unlock;
c02c0aeb
N
6368 }
6369 if (!list_empty(&mddev->disks)) {
6370 printk(KERN_WARNING
6371 "md: array %s already has disks!\n",
6372 mdname(mddev));
6373 err = -EBUSY;
3adc28d8 6374 goto unlock;
c02c0aeb
N
6375 }
6376 if (mddev->raid_disks) {
6377 printk(KERN_WARNING
6378 "md: array %s already initialised!\n",
6379 mdname(mddev));
6380 err = -EBUSY;
3adc28d8 6381 goto unlock;
c02c0aeb
N
6382 }
6383 err = set_array_info(mddev, &info);
6384 if (err) {
6385 printk(KERN_WARNING "md: couldn't set"
6386 " array info. %d\n", err);
3adc28d8 6387 goto unlock;
c02c0aeb 6388 }
3adc28d8 6389 goto unlock;
1da177e4
LT
6390 }
6391
6392 /*
6393 * Commands querying/configuring an existing array:
6394 */
32a7627c 6395 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3f9d7b0d 6396 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
a17184a9
N
6397 if ((!mddev->raid_disks && !mddev->external)
6398 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6399 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6400 && cmd != GET_BITMAP_FILE) {
1da177e4 6401 err = -ENODEV;
3adc28d8 6402 goto unlock;
1da177e4
LT
6403 }
6404
6405 /*
6406 * Commands even a read-only array can execute:
6407 */
c02c0aeb
N
6408 switch (cmd) {
6409 case GET_BITMAP_FILE:
6410 err = get_bitmap_file(mddev, argp);
3adc28d8 6411 goto unlock;
32a7627c 6412
c02c0aeb
N
6413 case RESTART_ARRAY_RW:
6414 err = restart_array(mddev);
3adc28d8 6415 goto unlock;
1da177e4 6416
c02c0aeb
N
6417 case STOP_ARRAY:
6418 err = do_md_stop(mddev, 0, bdev);
3adc28d8 6419 goto unlock;
1da177e4 6420
c02c0aeb
N
6421 case STOP_ARRAY_RO:
6422 err = md_set_readonly(mddev, bdev);
3adc28d8 6423 goto unlock;
1da177e4 6424
3ea8929d
N
6425 case HOT_REMOVE_DISK:
6426 err = hot_remove_disk(mddev, new_decode_dev(arg));
3adc28d8 6427 goto unlock;
3ea8929d 6428
7ceb17e8
N
6429 case ADD_NEW_DISK:
6430 /* We can support ADD_NEW_DISK on read-only arrays
6431 * on if we are re-adding a preexisting device.
6432 * So require mddev->pers and MD_DISK_SYNC.
6433 */
6434 if (mddev->pers) {
6435 mdu_disk_info_t info;
6436 if (copy_from_user(&info, argp, sizeof(info)))
6437 err = -EFAULT;
6438 else if (!(info.state & (1<<MD_DISK_SYNC)))
6439 /* Need to clear read-only for this */
6440 break;
6441 else
6442 err = add_new_disk(mddev, &info);
3adc28d8 6443 goto unlock;
7ceb17e8
N
6444 }
6445 break;
6446
c02c0aeb
N
6447 case BLKROSET:
6448 if (get_user(ro, (int __user *)(arg))) {
6449 err = -EFAULT;
3adc28d8 6450 goto unlock;
c02c0aeb
N
6451 }
6452 err = -EINVAL;
e2218350 6453
c02c0aeb
N
6454 /* if the bdev is going readonly the value of mddev->ro
6455 * does not matter, no writes are coming
6456 */
6457 if (ro)
3adc28d8 6458 goto unlock;
e2218350 6459
c02c0aeb
N
6460 /* are we are already prepared for writes? */
6461 if (mddev->ro != 1)
3adc28d8 6462 goto unlock;
e2218350 6463
c02c0aeb
N
6464 /* transitioning to readauto need only happen for
6465 * arrays that call md_write_start
6466 */
6467 if (mddev->pers) {
6468 err = restart_array(mddev);
6469 if (err == 0) {
6470 mddev->ro = 2;
6471 set_disk_ro(mddev->gendisk, 0);
e2218350 6472 }
c02c0aeb 6473 }
3adc28d8 6474 goto unlock;
1da177e4
LT
6475 }
6476
6477 /*
6478 * The remaining ioctls are changing the state of the
f91de92e 6479 * superblock, so we do not allow them on read-only arrays.
1da177e4 6480 */
326eb17d 6481 if (mddev->ro && mddev->pers) {
f91de92e
N
6482 if (mddev->ro == 2) {
6483 mddev->ro = 0;
00bcb4ac 6484 sysfs_notify_dirent_safe(mddev->sysfs_state);
0fd62b86 6485 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
f3378b48
N
6486 /* mddev_unlock will wake thread */
6487 /* If a device failed while we were read-only, we
6488 * need to make sure the metadata is updated now.
6489 */
6490 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6491 mddev_unlock(mddev);
6492 wait_event(mddev->sb_wait,
6493 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6494 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
29f097c4 6495 mddev_lock_nointr(mddev);
f3378b48 6496 }
f91de92e
N
6497 } else {
6498 err = -EROFS;
3adc28d8 6499 goto unlock;
f91de92e 6500 }
1da177e4
LT
6501 }
6502
c02c0aeb
N
6503 switch (cmd) {
6504 case ADD_NEW_DISK:
1da177e4 6505 {
c02c0aeb
N
6506 mdu_disk_info_t info;
6507 if (copy_from_user(&info, argp, sizeof(info)))
6508 err = -EFAULT;
6509 else
6510 err = add_new_disk(mddev, &info);
3adc28d8 6511 goto unlock;
c02c0aeb 6512 }
1da177e4 6513
c02c0aeb
N
6514 case HOT_ADD_DISK:
6515 err = hot_add_disk(mddev, new_decode_dev(arg));
3adc28d8 6516 goto unlock;
1da177e4 6517
c02c0aeb
N
6518 case RUN_ARRAY:
6519 err = do_md_run(mddev);
3adc28d8 6520 goto unlock;
1da177e4 6521
c02c0aeb
N
6522 case SET_BITMAP_FILE:
6523 err = set_bitmap_file(mddev, (int)arg);
3adc28d8 6524 goto unlock;
32a7627c 6525
c02c0aeb
N
6526 default:
6527 err = -EINVAL;
3adc28d8 6528 goto unlock;
1da177e4
LT
6529 }
6530
3adc28d8 6531unlock:
d3374825
N
6532 if (mddev->hold_active == UNTIL_IOCTL &&
6533 err != -EINVAL)
6534 mddev->hold_active = 0;
1da177e4 6535 mddev_unlock(mddev);
3adc28d8 6536out:
1da177e4
LT
6537 return err;
6538}
aa98aa31
AB
6539#ifdef CONFIG_COMPAT
6540static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6541 unsigned int cmd, unsigned long arg)
6542{
6543 switch (cmd) {
6544 case HOT_REMOVE_DISK:
6545 case HOT_ADD_DISK:
6546 case SET_DISK_FAULTY:
6547 case SET_BITMAP_FILE:
6548 /* These take in integer arg, do not convert */
6549 break;
6550 default:
6551 arg = (unsigned long)compat_ptr(arg);
6552 break;
6553 }
6554
6555 return md_ioctl(bdev, mode, cmd, arg);
6556}
6557#endif /* CONFIG_COMPAT */
1da177e4 6558
a39907fa 6559static int md_open(struct block_device *bdev, fmode_t mode)
1da177e4
LT
6560{
6561 /*
6562 * Succeed if we can lock the mddev, which confirms that
6563 * it isn't being stopped right now.
6564 */
fd01b88c 6565 struct mddev *mddev = mddev_find(bdev->bd_dev);
1da177e4
LT
6566 int err;
6567
0c098220
YL
6568 if (!mddev)
6569 return -ENODEV;
6570
d3374825
N
6571 if (mddev->gendisk != bdev->bd_disk) {
6572 /* we are racing with mddev_put which is discarding this
6573 * bd_disk.
6574 */
6575 mddev_put(mddev);
6576 /* Wait until bdev->bd_disk is definitely gone */
e804ac78 6577 flush_workqueue(md_misc_wq);
d3374825
N
6578 /* Then retry the open from the top */
6579 return -ERESTARTSYS;
6580 }
6581 BUG_ON(mddev != bdev->bd_disk->private_data);
6582
c8c00a69 6583 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
1da177e4
LT
6584 goto out;
6585
6586 err = 0;
f2ea68cf 6587 atomic_inc(&mddev->openers);
260fa034 6588 clear_bit(MD_STILL_CLOSED, &mddev->flags);
c8c00a69 6589 mutex_unlock(&mddev->open_mutex);
1da177e4 6590
f0b4f7e2 6591 check_disk_change(bdev);
1da177e4
LT
6592 out:
6593 return err;
6594}
6595
db2a144b 6596static void md_release(struct gendisk *disk, fmode_t mode)
1da177e4 6597{
f72ffdd6 6598 struct mddev *mddev = disk->private_data;
1da177e4 6599
52e5f9d1 6600 BUG_ON(!mddev);
f2ea68cf 6601 atomic_dec(&mddev->openers);
1da177e4 6602 mddev_put(mddev);
1da177e4 6603}
f0b4f7e2
N
6604
6605static int md_media_changed(struct gendisk *disk)
6606{
fd01b88c 6607 struct mddev *mddev = disk->private_data;
f0b4f7e2
N
6608
6609 return mddev->changed;
6610}
6611
6612static int md_revalidate(struct gendisk *disk)
6613{
fd01b88c 6614 struct mddev *mddev = disk->private_data;
f0b4f7e2
N
6615
6616 mddev->changed = 0;
6617 return 0;
6618}
83d5cde4 6619static const struct block_device_operations md_fops =
1da177e4
LT
6620{
6621 .owner = THIS_MODULE,
a39907fa
AV
6622 .open = md_open,
6623 .release = md_release,
b492b852 6624 .ioctl = md_ioctl,
aa98aa31
AB
6625#ifdef CONFIG_COMPAT
6626 .compat_ioctl = md_compat_ioctl,
6627#endif
a885c8c4 6628 .getgeo = md_getgeo,
f0b4f7e2
N
6629 .media_changed = md_media_changed,
6630 .revalidate_disk= md_revalidate,
1da177e4
LT
6631};
6632
f72ffdd6 6633static int md_thread(void *arg)
1da177e4 6634{
2b8bf345 6635 struct md_thread *thread = arg;
1da177e4 6636
1da177e4
LT
6637 /*
6638 * md_thread is a 'system-thread', it's priority should be very
6639 * high. We avoid resource deadlocks individually in each
6640 * raid personality. (RAID5 does preallocation) We also use RR and
6641 * the very same RT priority as kswapd, thus we will never get
6642 * into a priority inversion deadlock.
6643 *
6644 * we definitely have to have equal or higher priority than
6645 * bdflush, otherwise bdflush will deadlock if there are too
6646 * many dirty RAID5 blocks.
6647 */
1da177e4 6648
6985c43f 6649 allow_signal(SIGKILL);
a6fb0934 6650 while (!kthread_should_stop()) {
1da177e4 6651
93588e22
N
6652 /* We need to wait INTERRUPTIBLE so that
6653 * we don't add to the load-average.
6654 * That means we need to be sure no signals are
6655 * pending
6656 */
6657 if (signal_pending(current))
6658 flush_signals(current);
6659
6660 wait_event_interruptible_timeout
6661 (thread->wqueue,
6662 test_bit(THREAD_WAKEUP, &thread->flags)
6663 || kthread_should_stop(),
6664 thread->timeout);
1da177e4 6665
6c987910
N
6666 clear_bit(THREAD_WAKEUP, &thread->flags);
6667 if (!kthread_should_stop())
4ed8731d 6668 thread->run(thread);
1da177e4 6669 }
a6fb0934 6670
1da177e4
LT
6671 return 0;
6672}
6673
2b8bf345 6674void md_wakeup_thread(struct md_thread *thread)
1da177e4
LT
6675{
6676 if (thread) {
36a4e1fe 6677 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
1da177e4
LT
6678 set_bit(THREAD_WAKEUP, &thread->flags);
6679 wake_up(&thread->wqueue);
6680 }
6681}
6c144d31 6682EXPORT_SYMBOL(md_wakeup_thread);
1da177e4 6683
4ed8731d
SL
6684struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6685 struct mddev *mddev, const char *name)
1da177e4 6686{
2b8bf345 6687 struct md_thread *thread;
1da177e4 6688
2b8bf345 6689 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
1da177e4
LT
6690 if (!thread)
6691 return NULL;
6692
1da177e4
LT
6693 init_waitqueue_head(&thread->wqueue);
6694
1da177e4
LT
6695 thread->run = run;
6696 thread->mddev = mddev;
32a7627c 6697 thread->timeout = MAX_SCHEDULE_TIMEOUT;
0da3c619
N
6698 thread->tsk = kthread_run(md_thread, thread,
6699 "%s_%s",
6700 mdname(thread->mddev),
0232605d 6701 name);
a6fb0934 6702 if (IS_ERR(thread->tsk)) {
1da177e4
LT
6703 kfree(thread);
6704 return NULL;
6705 }
1da177e4
LT
6706 return thread;
6707}
6c144d31 6708EXPORT_SYMBOL(md_register_thread);
1da177e4 6709
2b8bf345 6710void md_unregister_thread(struct md_thread **threadp)
1da177e4 6711{
2b8bf345 6712 struct md_thread *thread = *threadp;
e0cf8f04
N
6713 if (!thread)
6714 return;
36a4e1fe 6715 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
01f96c0a
N
6716 /* Locking ensures that mddev_unlock does not wake_up a
6717 * non-existent thread
6718 */
6719 spin_lock(&pers_lock);
6720 *threadp = NULL;
6721 spin_unlock(&pers_lock);
a6fb0934
N
6722
6723 kthread_stop(thread->tsk);
1da177e4
LT
6724 kfree(thread);
6725}
6c144d31 6726EXPORT_SYMBOL(md_unregister_thread);
1da177e4 6727
fd01b88c 6728void md_error(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 6729{
b2d444d7 6730 if (!rdev || test_bit(Faulty, &rdev->flags))
1da177e4 6731 return;
6bfe0b49 6732
de393cde 6733 if (!mddev->pers || !mddev->pers->error_handler)
1da177e4
LT
6734 return;
6735 mddev->pers->error_handler(mddev,rdev);
72a23c21
NB
6736 if (mddev->degraded)
6737 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
00bcb4ac 6738 sysfs_notify_dirent_safe(rdev->sysfs_state);
1da177e4
LT
6739 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6740 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6741 md_wakeup_thread(mddev->thread);
768a418d 6742 if (mddev->event_work.func)
e804ac78 6743 queue_work(md_misc_wq, &mddev->event_work);
c331eb04 6744 md_new_event_inintr(mddev);
1da177e4 6745}
6c144d31 6746EXPORT_SYMBOL(md_error);
1da177e4
LT
6747
6748/* seq_file implementation /proc/mdstat */
6749
6750static void status_unused(struct seq_file *seq)
6751{
6752 int i = 0;
3cb03002 6753 struct md_rdev *rdev;
1da177e4
LT
6754
6755 seq_printf(seq, "unused devices: ");
6756
159ec1fc 6757 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
1da177e4
LT
6758 char b[BDEVNAME_SIZE];
6759 i++;
6760 seq_printf(seq, "%s ",
6761 bdevname(rdev->bdev,b));
6762 }
6763 if (!i)
6764 seq_printf(seq, "<none>");
6765
6766 seq_printf(seq, "\n");
6767}
6768
f72ffdd6 6769static void status_resync(struct seq_file *seq, struct mddev *mddev)
1da177e4 6770{
dd71cf6b
N
6771 sector_t max_sectors, resync, res;
6772 unsigned long dt, db;
6773 sector_t rt;
4588b42e
N
6774 int scale;
6775 unsigned int per_milli;
1da177e4 6776
72f36d59
N
6777 if (mddev->curr_resync <= 3)
6778 resync = 0;
6779 else
6780 resync = mddev->curr_resync
6781 - atomic_read(&mddev->recovery_active);
1da177e4 6782
c804cdec
N
6783 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6784 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
dd71cf6b 6785 max_sectors = mddev->resync_max_sectors;
1da177e4 6786 else
dd71cf6b 6787 max_sectors = mddev->dev_sectors;
1da177e4 6788
403df478 6789 WARN_ON(max_sectors == 0);
4588b42e 6790 /* Pick 'scale' such that (resync>>scale)*1000 will fit
dd71cf6b 6791 * in a sector_t, and (max_sectors>>scale) will fit in a
4588b42e
N
6792 * u32, as those are the requirements for sector_div.
6793 * Thus 'scale' must be at least 10
6794 */
6795 scale = 10;
6796 if (sizeof(sector_t) > sizeof(unsigned long)) {
dd71cf6b 6797 while ( max_sectors/2 > (1ULL<<(scale+32)))
4588b42e
N
6798 scale++;
6799 }
6800 res = (resync>>scale)*1000;
dd71cf6b 6801 sector_div(res, (u32)((max_sectors>>scale)+1));
4588b42e
N
6802
6803 per_milli = res;
1da177e4 6804 {
4588b42e 6805 int i, x = per_milli/50, y = 20-x;
1da177e4
LT
6806 seq_printf(seq, "[");
6807 for (i = 0; i < x; i++)
6808 seq_printf(seq, "=");
6809 seq_printf(seq, ">");
6810 for (i = 0; i < y; i++)
6811 seq_printf(seq, ".");
6812 seq_printf(seq, "] ");
6813 }
4588b42e 6814 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
ccfcc3c1
N
6815 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6816 "reshape" :
61df9d91
N
6817 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6818 "check" :
6819 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6820 "resync" : "recovery"))),
6821 per_milli/10, per_milli % 10,
dd71cf6b
N
6822 (unsigned long long) resync/2,
6823 (unsigned long long) max_sectors/2);
1da177e4
LT
6824
6825 /*
1da177e4
LT
6826 * dt: time from mark until now
6827 * db: blocks written from mark until now
6828 * rt: remaining time
dd71cf6b
N
6829 *
6830 * rt is a sector_t, so could be 32bit or 64bit.
6831 * So we divide before multiply in case it is 32bit and close
6832 * to the limit.
25985edc 6833 * We scale the divisor (db) by 32 to avoid losing precision
dd71cf6b
N
6834 * near the end of resync when the number of remaining sectors
6835 * is close to 'db'.
6836 * We then divide rt by 32 after multiplying by db to compensate.
6837 * The '+1' avoids division by zero if db is very small.
1da177e4
LT
6838 */
6839 dt = ((jiffies - mddev->resync_mark) / HZ);
6840 if (!dt) dt++;
ff4e8d9a
N
6841 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6842 - mddev->resync_mark_cnt;
1da177e4 6843
dd71cf6b
N
6844 rt = max_sectors - resync; /* number of remaining sectors */
6845 sector_div(rt, db/32+1);
6846 rt *= dt;
6847 rt >>= 5;
6848
6849 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6850 ((unsigned long)rt % 60)/6);
1da177e4 6851
ff4e8d9a 6852 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
1da177e4
LT
6853}
6854
6855static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6856{
6857 struct list_head *tmp;
6858 loff_t l = *pos;
fd01b88c 6859 struct mddev *mddev;
1da177e4
LT
6860
6861 if (l >= 0x10000)
6862 return NULL;
6863 if (!l--)
6864 /* header */
6865 return (void*)1;
6866
6867 spin_lock(&all_mddevs_lock);
6868 list_for_each(tmp,&all_mddevs)
6869 if (!l--) {
fd01b88c 6870 mddev = list_entry(tmp, struct mddev, all_mddevs);
1da177e4
LT
6871 mddev_get(mddev);
6872 spin_unlock(&all_mddevs_lock);
6873 return mddev;
6874 }
6875 spin_unlock(&all_mddevs_lock);
6876 if (!l--)
6877 return (void*)2;/* tail */
6878 return NULL;
6879}
6880
6881static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6882{
6883 struct list_head *tmp;
fd01b88c 6884 struct mddev *next_mddev, *mddev = v;
f72ffdd6 6885
1da177e4
LT
6886 ++*pos;
6887 if (v == (void*)2)
6888 return NULL;
6889
6890 spin_lock(&all_mddevs_lock);
6891 if (v == (void*)1)
6892 tmp = all_mddevs.next;
6893 else
6894 tmp = mddev->all_mddevs.next;
6895 if (tmp != &all_mddevs)
fd01b88c 6896 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
1da177e4
LT
6897 else {
6898 next_mddev = (void*)2;
6899 *pos = 0x10000;
f72ffdd6 6900 }
1da177e4
LT
6901 spin_unlock(&all_mddevs_lock);
6902
6903 if (v != (void*)1)
6904 mddev_put(mddev);
6905 return next_mddev;
6906
6907}
6908
6909static void md_seq_stop(struct seq_file *seq, void *v)
6910{
fd01b88c 6911 struct mddev *mddev = v;
1da177e4
LT
6912
6913 if (mddev && v != (void*)1 && v != (void*)2)
6914 mddev_put(mddev);
6915}
6916
6917static int md_seq_show(struct seq_file *seq, void *v)
6918{
fd01b88c 6919 struct mddev *mddev = v;
dd8ac336 6920 sector_t sectors;
3cb03002 6921 struct md_rdev *rdev;
1da177e4
LT
6922
6923 if (v == (void*)1) {
84fc4b56 6924 struct md_personality *pers;
1da177e4
LT
6925 seq_printf(seq, "Personalities : ");
6926 spin_lock(&pers_lock);
2604b703
N
6927 list_for_each_entry(pers, &pers_list, list)
6928 seq_printf(seq, "[%s] ", pers->name);
1da177e4
LT
6929
6930 spin_unlock(&pers_lock);
6931 seq_printf(seq, "\n");
f1514638 6932 seq->poll_event = atomic_read(&md_event_count);
1da177e4
LT
6933 return 0;
6934 }
6935 if (v == (void*)2) {
6936 status_unused(seq);
6937 return 0;
6938 }
6939
36d091f4 6940 spin_lock(&mddev->lock);
1da177e4
LT
6941 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6942 seq_printf(seq, "%s : %sactive", mdname(mddev),
6943 mddev->pers ? "" : "in");
6944 if (mddev->pers) {
f91de92e 6945 if (mddev->ro==1)
1da177e4 6946 seq_printf(seq, " (read-only)");
f91de92e 6947 if (mddev->ro==2)
52720ae7 6948 seq_printf(seq, " (auto-read-only)");
1da177e4
LT
6949 seq_printf(seq, " %s", mddev->pers->name);
6950 }
6951
dd8ac336 6952 sectors = 0;
f97fcad3
N
6953 rcu_read_lock();
6954 rdev_for_each_rcu(rdev, mddev) {
1da177e4
LT
6955 char b[BDEVNAME_SIZE];
6956 seq_printf(seq, " %s[%d]",
6957 bdevname(rdev->bdev,b), rdev->desc_nr);
8ddf9efe
N
6958 if (test_bit(WriteMostly, &rdev->flags))
6959 seq_printf(seq, "(W)");
b2d444d7 6960 if (test_bit(Faulty, &rdev->flags)) {
1da177e4
LT
6961 seq_printf(seq, "(F)");
6962 continue;
2d78f8c4
N
6963 }
6964 if (rdev->raid_disk < 0)
b325a32e 6965 seq_printf(seq, "(S)"); /* spare */
2d78f8c4
N
6966 if (test_bit(Replacement, &rdev->flags))
6967 seq_printf(seq, "(R)");
dd8ac336 6968 sectors += rdev->sectors;
1da177e4 6969 }
f97fcad3 6970 rcu_read_unlock();
1da177e4
LT
6971
6972 if (!list_empty(&mddev->disks)) {
6973 if (mddev->pers)
6974 seq_printf(seq, "\n %llu blocks",
f233ea5c
AN
6975 (unsigned long long)
6976 mddev->array_sectors / 2);
1da177e4
LT
6977 else
6978 seq_printf(seq, "\n %llu blocks",
dd8ac336 6979 (unsigned long long)sectors / 2);
1da177e4 6980 }
1cd6bf19
N
6981 if (mddev->persistent) {
6982 if (mddev->major_version != 0 ||
6983 mddev->minor_version != 90) {
6984 seq_printf(seq," super %d.%d",
6985 mddev->major_version,
6986 mddev->minor_version);
6987 }
e691063a
N
6988 } else if (mddev->external)
6989 seq_printf(seq, " super external:%s",
6990 mddev->metadata_type);
6991 else
1cd6bf19 6992 seq_printf(seq, " super non-persistent");
1da177e4
LT
6993
6994 if (mddev->pers) {
d710e138 6995 mddev->pers->status(seq, mddev);
f72ffdd6 6996 seq_printf(seq, "\n ");
8e1b39d6
N
6997 if (mddev->pers->sync_request) {
6998 if (mddev->curr_resync > 2) {
d710e138 6999 status_resync(seq, mddev);
8e1b39d6 7000 seq_printf(seq, "\n ");
72f36d59 7001 } else if (mddev->curr_resync >= 1)
8e1b39d6
N
7002 seq_printf(seq, "\tresync=DELAYED\n ");
7003 else if (mddev->recovery_cp < MaxSector)
7004 seq_printf(seq, "\tresync=PENDING\n ");
7005 }
32a7627c
N
7006 } else
7007 seq_printf(seq, "\n ");
7008
57148964 7009 bitmap_status(seq, mddev->bitmap);
1da177e4
LT
7010
7011 seq_printf(seq, "\n");
7012 }
36d091f4 7013 spin_unlock(&mddev->lock);
f72ffdd6 7014
1da177e4
LT
7015 return 0;
7016}
7017
110518bc 7018static const struct seq_operations md_seq_ops = {
1da177e4
LT
7019 .start = md_seq_start,
7020 .next = md_seq_next,
7021 .stop = md_seq_stop,
7022 .show = md_seq_show,
7023};
7024
7025static int md_seq_open(struct inode *inode, struct file *file)
7026{
f1514638 7027 struct seq_file *seq;
1da177e4
LT
7028 int error;
7029
7030 error = seq_open(file, &md_seq_ops);
d7603b7e 7031 if (error)
f1514638
KS
7032 return error;
7033
7034 seq = file->private_data;
7035 seq->poll_event = atomic_read(&md_event_count);
1da177e4
LT
7036 return error;
7037}
7038
e2f23b60 7039static int md_unloading;
d7603b7e
N
7040static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7041{
f1514638 7042 struct seq_file *seq = filp->private_data;
d7603b7e
N
7043 int mask;
7044
e2f23b60 7045 if (md_unloading)
7d7e64f2 7046 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
d7603b7e
N
7047 poll_wait(filp, &md_event_waiters, wait);
7048
7049 /* always allow read */
7050 mask = POLLIN | POLLRDNORM;
7051
f1514638 7052 if (seq->poll_event != atomic_read(&md_event_count))
d7603b7e
N
7053 mask |= POLLERR | POLLPRI;
7054 return mask;
7055}
7056
fa027c2a 7057static const struct file_operations md_seq_fops = {
e24650c2 7058 .owner = THIS_MODULE,
1da177e4
LT
7059 .open = md_seq_open,
7060 .read = seq_read,
7061 .llseek = seq_lseek,
c3f94b40 7062 .release = seq_release_private,
d7603b7e 7063 .poll = mdstat_poll,
1da177e4
LT
7064};
7065
84fc4b56 7066int register_md_personality(struct md_personality *p)
1da177e4 7067{
50bd3774
CY
7068 printk(KERN_INFO "md: %s personality registered for level %d\n",
7069 p->name, p->level);
1da177e4 7070 spin_lock(&pers_lock);
2604b703 7071 list_add_tail(&p->list, &pers_list);
1da177e4
LT
7072 spin_unlock(&pers_lock);
7073 return 0;
7074}
6c144d31 7075EXPORT_SYMBOL(register_md_personality);
1da177e4 7076
84fc4b56 7077int unregister_md_personality(struct md_personality *p)
1da177e4 7078{
2604b703 7079 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
1da177e4 7080 spin_lock(&pers_lock);
2604b703 7081 list_del_init(&p->list);
1da177e4
LT
7082 spin_unlock(&pers_lock);
7083 return 0;
7084}
6c144d31 7085EXPORT_SYMBOL(unregister_md_personality);
1da177e4 7086
fd01b88c 7087static int is_mddev_idle(struct mddev *mddev, int init)
1da177e4 7088{
f72ffdd6 7089 struct md_rdev *rdev;
1da177e4 7090 int idle;
eea1bf38 7091 int curr_events;
1da177e4
LT
7092
7093 idle = 1;
4b80991c
N
7094 rcu_read_lock();
7095 rdev_for_each_rcu(rdev, mddev) {
1da177e4 7096 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
eea1bf38
N
7097 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7098 (int)part_stat_read(&disk->part0, sectors[1]) -
7099 atomic_read(&disk->sync_io);
713f6ab1
N
7100 /* sync IO will cause sync_io to increase before the disk_stats
7101 * as sync_io is counted when a request starts, and
7102 * disk_stats is counted when it completes.
7103 * So resync activity will cause curr_events to be smaller than
7104 * when there was no such activity.
7105 * non-sync IO will cause disk_stat to increase without
7106 * increasing sync_io so curr_events will (eventually)
7107 * be larger than it was before. Once it becomes
7108 * substantially larger, the test below will cause
7109 * the array to appear non-idle, and resync will slow
7110 * down.
7111 * If there is a lot of outstanding resync activity when
7112 * we set last_event to curr_events, then all that activity
7113 * completing might cause the array to appear non-idle
7114 * and resync will be slowed down even though there might
7115 * not have been non-resync activity. This will only
7116 * happen once though. 'last_events' will soon reflect
7117 * the state where there is little or no outstanding
7118 * resync requests, and further resync activity will
7119 * always make curr_events less than last_events.
c0e48521 7120 *
1da177e4 7121 */
eea1bf38 7122 if (init || curr_events - rdev->last_events > 64) {
1da177e4
LT
7123 rdev->last_events = curr_events;
7124 idle = 0;
7125 }
7126 }
4b80991c 7127 rcu_read_unlock();
1da177e4
LT
7128 return idle;
7129}
7130
fd01b88c 7131void md_done_sync(struct mddev *mddev, int blocks, int ok)
1da177e4
LT
7132{
7133 /* another "blocks" (512byte) blocks have been synced */
7134 atomic_sub(blocks, &mddev->recovery_active);
7135 wake_up(&mddev->recovery_wait);
7136 if (!ok) {
dfc70645 7137 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
0a19caab 7138 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
1da177e4
LT
7139 md_wakeup_thread(mddev->thread);
7140 // stop recovery, signal do_sync ....
7141 }
7142}
6c144d31 7143EXPORT_SYMBOL(md_done_sync);
1da177e4 7144
06d91a5f
N
7145/* md_write_start(mddev, bi)
7146 * If we need to update some array metadata (e.g. 'active' flag
3d310eb7
N
7147 * in superblock) before writing, schedule a superblock update
7148 * and wait for it to complete.
06d91a5f 7149 */
fd01b88c 7150void md_write_start(struct mddev *mddev, struct bio *bi)
1da177e4 7151{
0fd62b86 7152 int did_change = 0;
06d91a5f 7153 if (bio_data_dir(bi) != WRITE)
3d310eb7 7154 return;
06d91a5f 7155
f91de92e
N
7156 BUG_ON(mddev->ro == 1);
7157 if (mddev->ro == 2) {
7158 /* need to switch to read/write */
7159 mddev->ro = 0;
7160 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7161 md_wakeup_thread(mddev->thread);
25156198 7162 md_wakeup_thread(mddev->sync_thread);
0fd62b86 7163 did_change = 1;
f91de92e 7164 }
06d91a5f 7165 atomic_inc(&mddev->writes_pending);
31a59e34
N
7166 if (mddev->safemode == 1)
7167 mddev->safemode = 0;
06d91a5f 7168 if (mddev->in_sync) {
85572d7c 7169 spin_lock(&mddev->lock);
3d310eb7
N
7170 if (mddev->in_sync) {
7171 mddev->in_sync = 0;
850b2b42 7172 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
070dc6dd 7173 set_bit(MD_CHANGE_PENDING, &mddev->flags);
3d310eb7 7174 md_wakeup_thread(mddev->thread);
0fd62b86 7175 did_change = 1;
3d310eb7 7176 }
85572d7c 7177 spin_unlock(&mddev->lock);
06d91a5f 7178 }
0fd62b86 7179 if (did_change)
00bcb4ac 7180 sysfs_notify_dirent_safe(mddev->sysfs_state);
09a44cc1 7181 wait_event(mddev->sb_wait,
09a44cc1 7182 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
1da177e4 7183}
6c144d31 7184EXPORT_SYMBOL(md_write_start);
1da177e4 7185
fd01b88c 7186void md_write_end(struct mddev *mddev)
1da177e4
LT
7187{
7188 if (atomic_dec_and_test(&mddev->writes_pending)) {
7189 if (mddev->safemode == 2)
7190 md_wakeup_thread(mddev->thread);
16f17b39 7191 else if (mddev->safemode_delay)
1da177e4
LT
7192 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7193 }
7194}
6c144d31 7195EXPORT_SYMBOL(md_write_end);
1da177e4 7196
2a2275d6
N
7197/* md_allow_write(mddev)
7198 * Calling this ensures that the array is marked 'active' so that writes
7199 * may proceed without blocking. It is important to call this before
7200 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7201 * Must be called with mddev_lock held.
b5470dc5
DW
7202 *
7203 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7204 * is dropped, so return -EAGAIN after notifying userspace.
2a2275d6 7205 */
fd01b88c 7206int md_allow_write(struct mddev *mddev)
2a2275d6
N
7207{
7208 if (!mddev->pers)
b5470dc5 7209 return 0;
2a2275d6 7210 if (mddev->ro)
b5470dc5 7211 return 0;
1a0fd497 7212 if (!mddev->pers->sync_request)
b5470dc5 7213 return 0;
2a2275d6 7214
85572d7c 7215 spin_lock(&mddev->lock);
2a2275d6
N
7216 if (mddev->in_sync) {
7217 mddev->in_sync = 0;
7218 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
070dc6dd 7219 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2a2275d6
N
7220 if (mddev->safemode_delay &&
7221 mddev->safemode == 0)
7222 mddev->safemode = 1;
85572d7c 7223 spin_unlock(&mddev->lock);
2a2275d6 7224 md_update_sb(mddev, 0);
00bcb4ac 7225 sysfs_notify_dirent_safe(mddev->sysfs_state);
2a2275d6 7226 } else
85572d7c 7227 spin_unlock(&mddev->lock);
b5470dc5 7228
070dc6dd 7229 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
b5470dc5
DW
7230 return -EAGAIN;
7231 else
7232 return 0;
2a2275d6
N
7233}
7234EXPORT_SYMBOL_GPL(md_allow_write);
7235
1da177e4
LT
7236#define SYNC_MARKS 10
7237#define SYNC_MARK_STEP (3*HZ)
54f89341 7238#define UPDATE_FREQUENCY (5*60*HZ)
4ed8731d 7239void md_do_sync(struct md_thread *thread)
1da177e4 7240{
4ed8731d 7241 struct mddev *mddev = thread->mddev;
fd01b88c 7242 struct mddev *mddev2;
1da177e4
LT
7243 unsigned int currspeed = 0,
7244 window;
ac7e50a3 7245 sector_t max_sectors,j, io_sectors, recovery_done;
1da177e4 7246 unsigned long mark[SYNC_MARKS];
54f89341 7247 unsigned long update_time;
1da177e4
LT
7248 sector_t mark_cnt[SYNC_MARKS];
7249 int last_mark,m;
7250 struct list_head *tmp;
7251 sector_t last_check;
57afd89f 7252 int skipped = 0;
3cb03002 7253 struct md_rdev *rdev;
c4a39551 7254 char *desc, *action = NULL;
7c2c57c9 7255 struct blk_plug plug;
1da177e4
LT
7256
7257 /* just incase thread restarts... */
7258 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7259 return;
3991b31e
N
7260 if (mddev->ro) {/* never try to sync a read-only array */
7261 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5fd6c1dc 7262 return;
3991b31e 7263 }
1da177e4 7264
61df9d91 7265 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
c4a39551 7266 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
61df9d91 7267 desc = "data-check";
c4a39551
JB
7268 action = "check";
7269 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
61df9d91 7270 desc = "requested-resync";
c4a39551
JB
7271 action = "repair";
7272 } else
61df9d91
N
7273 desc = "resync";
7274 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7275 desc = "reshape";
7276 else
7277 desc = "recovery";
7278
c4a39551
JB
7279 mddev->last_sync_action = action ?: desc;
7280
1da177e4
LT
7281 /* we overload curr_resync somewhat here.
7282 * 0 == not engaged in resync at all
7283 * 2 == checking that there is no conflict with another sync
7284 * 1 == like 2, but have yielded to allow conflicting resync to
7285 * commense
7286 * other == active in resync - this many blocks
7287 *
7288 * Before starting a resync we must have set curr_resync to
7289 * 2, and then checked that every "conflicting" array has curr_resync
7290 * less than ours. When we find one that is the same or higher
7291 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7292 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7293 * This will mean we have to start checking from the beginning again.
7294 *
7295 */
7296
7297 do {
7298 mddev->curr_resync = 2;
7299
7300 try_again:
404e4b43 7301 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
1da177e4 7302 goto skip;
29ac4aa3 7303 for_each_mddev(mddev2, tmp) {
1da177e4
LT
7304 if (mddev2 == mddev)
7305 continue;
90b08710
BS
7306 if (!mddev->parallel_resync
7307 && mddev2->curr_resync
7308 && match_mddev_units(mddev, mddev2)) {
1da177e4
LT
7309 DEFINE_WAIT(wq);
7310 if (mddev < mddev2 && mddev->curr_resync == 2) {
7311 /* arbitrarily yield */
7312 mddev->curr_resync = 1;
7313 wake_up(&resync_wait);
7314 }
7315 if (mddev > mddev2 && mddev->curr_resync == 1)
7316 /* no need to wait here, we can wait the next
7317 * time 'round when curr_resync == 2
7318 */
7319 continue;
9744197c
N
7320 /* We need to wait 'interruptible' so as not to
7321 * contribute to the load average, and not to
7322 * be caught by 'softlockup'
7323 */
7324 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
c91abf5a 7325 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8712e553 7326 mddev2->curr_resync >= mddev->curr_resync) {
61df9d91
N
7327 printk(KERN_INFO "md: delaying %s of %s"
7328 " until %s has finished (they"
1da177e4 7329 " share one or more physical units)\n",
61df9d91 7330 desc, mdname(mddev), mdname(mddev2));
1da177e4 7331 mddev_put(mddev2);
9744197c
N
7332 if (signal_pending(current))
7333 flush_signals(current);
1da177e4
LT
7334 schedule();
7335 finish_wait(&resync_wait, &wq);
7336 goto try_again;
7337 }
7338 finish_wait(&resync_wait, &wq);
7339 }
7340 }
7341 } while (mddev->curr_resync < 2);
7342
5fd6c1dc 7343 j = 0;
9d88883e 7344 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1da177e4 7345 /* resync follows the size requested by the personality,
57afd89f 7346 * which defaults to physical size, but can be virtual size
1da177e4
LT
7347 */
7348 max_sectors = mddev->resync_max_sectors;
7f7583d4 7349 atomic64_set(&mddev->resync_mismatches, 0);
5fd6c1dc 7350 /* we don't use the checkpoint if there's a bitmap */
5e96ee65
NB
7351 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7352 j = mddev->resync_min;
7353 else if (!mddev->bitmap)
5fd6c1dc 7354 j = mddev->recovery_cp;
5e96ee65 7355
ccfcc3c1 7356 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
c804cdec 7357 max_sectors = mddev->resync_max_sectors;
5fd6c1dc 7358 else {
1da177e4 7359 /* recovery follows the physical size of devices */
58c0fed4 7360 max_sectors = mddev->dev_sectors;
5fd6c1dc 7361 j = MaxSector;
4e59ca7d 7362 rcu_read_lock();
dafb20fa 7363 rdev_for_each_rcu(rdev, mddev)
5fd6c1dc
N
7364 if (rdev->raid_disk >= 0 &&
7365 !test_bit(Faulty, &rdev->flags) &&
7366 !test_bit(In_sync, &rdev->flags) &&
7367 rdev->recovery_offset < j)
7368 j = rdev->recovery_offset;
4e59ca7d 7369 rcu_read_unlock();
133d4527
N
7370
7371 /* If there is a bitmap, we need to make sure all
7372 * writes that started before we added a spare
7373 * complete before we start doing a recovery.
7374 * Otherwise the write might complete and (via
7375 * bitmap_endwrite) set a bit in the bitmap after the
7376 * recovery has checked that bit and skipped that
7377 * region.
7378 */
7379 if (mddev->bitmap) {
7380 mddev->pers->quiesce(mddev, 1);
7381 mddev->pers->quiesce(mddev, 0);
7382 }
5fd6c1dc 7383 }
1da177e4 7384
61df9d91
N
7385 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7386 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7387 " %d KB/sec/disk.\n", speed_min(mddev));
338cec32 7388 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
61df9d91
N
7389 "(but not more than %d KB/sec) for %s.\n",
7390 speed_max(mddev), desc);
1da177e4 7391
eea1bf38 7392 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
5fd6c1dc 7393
57afd89f 7394 io_sectors = 0;
1da177e4
LT
7395 for (m = 0; m < SYNC_MARKS; m++) {
7396 mark[m] = jiffies;
57afd89f 7397 mark_cnt[m] = io_sectors;
1da177e4
LT
7398 }
7399 last_mark = 0;
7400 mddev->resync_mark = mark[last_mark];
7401 mddev->resync_mark_cnt = mark_cnt[last_mark];
7402
7403 /*
7404 * Tune reconstruction:
7405 */
7406 window = 32*(PAGE_SIZE/512);
ac42450c
JB
7407 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7408 window/2, (unsigned long long)max_sectors/2);
1da177e4
LT
7409
7410 atomic_set(&mddev->recovery_active, 0);
1da177e4
LT
7411 last_check = 0;
7412
7413 if (j>2) {
c91abf5a 7414 printk(KERN_INFO
61df9d91
N
7415 "md: resuming %s of %s from checkpoint.\n",
7416 desc, mdname(mddev));
1da177e4 7417 mddev->curr_resync = j;
72f36d59
N
7418 } else
7419 mddev->curr_resync = 3; /* no longer delayed */
75d3da43 7420 mddev->curr_resync_completed = j;
72f36d59
N
7421 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7422 md_new_event(mddev);
54f89341 7423 update_time = jiffies;
1da177e4 7424
7c2c57c9 7425 blk_start_plug(&plug);
1da177e4 7426 while (j < max_sectors) {
57afd89f 7427 sector_t sectors;
1da177e4 7428
57afd89f 7429 skipped = 0;
97e4f42d 7430
7a91ee1f
N
7431 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7432 ((mddev->curr_resync > mddev->curr_resync_completed &&
7433 (mddev->curr_resync - mddev->curr_resync_completed)
7434 > (max_sectors >> 4)) ||
54f89341 7435 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
7a91ee1f
N
7436 (j - mddev->curr_resync_completed)*2
7437 >= mddev->resync_max - mddev->curr_resync_completed
7438 )) {
97e4f42d 7439 /* time to update curr_resync_completed */
97e4f42d
N
7440 wait_event(mddev->recovery_wait,
7441 atomic_read(&mddev->recovery_active) == 0);
75d3da43 7442 mddev->curr_resync_completed = j;
35d78c66 7443 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7444 j > mddev->recovery_cp)
7445 mddev->recovery_cp = j;
54f89341 7446 update_time = jiffies;
070dc6dd 7447 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
acb180b0 7448 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
97e4f42d 7449 }
acb180b0 7450
c91abf5a
N
7451 while (j >= mddev->resync_max &&
7452 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
e62e58a5
N
7453 /* As this condition is controlled by user-space,
7454 * we can block indefinitely, so use '_interruptible'
7455 * to avoid triggering warnings.
7456 */
7457 flush_signals(current); /* just in case */
7458 wait_event_interruptible(mddev->recovery_wait,
7459 mddev->resync_max > j
c91abf5a
N
7460 || test_bit(MD_RECOVERY_INTR,
7461 &mddev->recovery));
e62e58a5 7462 }
acb180b0 7463
c91abf5a
N
7464 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7465 break;
acb180b0 7466
57afd89f 7467 sectors = mddev->pers->sync_request(mddev, j, &skipped,
c6207277 7468 currspeed < speed_min(mddev));
57afd89f 7469 if (sectors == 0) {
dfc70645 7470 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
c91abf5a 7471 break;
1da177e4 7472 }
57afd89f
N
7473
7474 if (!skipped) { /* actual IO requested */
7475 io_sectors += sectors;
7476 atomic_add(sectors, &mddev->recovery_active);
7477 }
7478
e875ecea
N
7479 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7480 break;
7481
1da177e4 7482 j += sectors;
72f36d59
N
7483 if (j > 2)
7484 mddev->curr_resync = j;
ff4e8d9a 7485 mddev->curr_mark_cnt = io_sectors;
d7603b7e 7486 if (last_check == 0)
e875ecea 7487 /* this is the earliest that rebuild will be
d7603b7e
N
7488 * visible in /proc/mdstat
7489 */
7490 md_new_event(mddev);
57afd89f
N
7491
7492 if (last_check + window > io_sectors || j == max_sectors)
1da177e4
LT
7493 continue;
7494
57afd89f 7495 last_check = io_sectors;
1da177e4
LT
7496 repeat:
7497 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7498 /* step marks */
7499 int next = (last_mark+1) % SYNC_MARKS;
7500
7501 mddev->resync_mark = mark[next];
7502 mddev->resync_mark_cnt = mark_cnt[next];
7503 mark[next] = jiffies;
57afd89f 7504 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
1da177e4
LT
7505 last_mark = next;
7506 }
7507
c91abf5a
N
7508 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7509 break;
1da177e4
LT
7510
7511 /*
7512 * this loop exits only if either when we are slower than
7513 * the 'hard' speed limit, or the system was IO-idle for
7514 * a jiffy.
7515 * the system might be non-idle CPU-wise, but we only care
7516 * about not overloading the IO subsystem. (things like an
7517 * e2fsck being done on the RAID array should execute fast)
7518 */
1da177e4
LT
7519 cond_resched();
7520
ac7e50a3
XN
7521 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7522 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
57afd89f 7523 /((jiffies-mddev->resync_mark)/HZ +1) +1;
1da177e4 7524
88202a0c
N
7525 if (currspeed > speed_min(mddev)) {
7526 if ((currspeed > speed_max(mddev)) ||
eea1bf38 7527 !is_mddev_idle(mddev, 0)) {
c0e48521 7528 msleep(500);
1da177e4
LT
7529 goto repeat;
7530 }
7531 }
7532 }
c91abf5a
N
7533 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7534 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7535 ? "interrupted" : "done");
1da177e4
LT
7536 /*
7537 * this also signals 'finished resyncing' to md_stop
7538 */
7c2c57c9 7539 blk_finish_plug(&plug);
1da177e4
LT
7540 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7541
7542 /* tell personality that we are finished */
57afd89f 7543 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
1da177e4 7544
dfc70645 7545 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
5fd6c1dc
N
7546 mddev->curr_resync > 2) {
7547 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7548 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7549 if (mddev->curr_resync >= mddev->recovery_cp) {
7550 printk(KERN_INFO
61df9d91
N
7551 "md: checkpointing %s of %s.\n",
7552 desc, mdname(mddev));
0a19caab 7553 if (test_bit(MD_RECOVERY_ERROR,
7554 &mddev->recovery))
7555 mddev->recovery_cp =
7556 mddev->curr_resync_completed;
7557 else
7558 mddev->recovery_cp =
7559 mddev->curr_resync;
5fd6c1dc
N
7560 }
7561 } else
7562 mddev->recovery_cp = MaxSector;
7563 } else {
7564 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7565 mddev->curr_resync = MaxSector;
4e59ca7d 7566 rcu_read_lock();
dafb20fa 7567 rdev_for_each_rcu(rdev, mddev)
5fd6c1dc 7568 if (rdev->raid_disk >= 0 &&
70fffd0b 7569 mddev->delta_disks >= 0 &&
5fd6c1dc
N
7570 !test_bit(Faulty, &rdev->flags) &&
7571 !test_bit(In_sync, &rdev->flags) &&
7572 rdev->recovery_offset < mddev->curr_resync)
7573 rdev->recovery_offset = mddev->curr_resync;
4e59ca7d 7574 rcu_read_unlock();
5fd6c1dc 7575 }
1da177e4 7576 }
db91ff55 7577 skip:
17571284 7578 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1da177e4 7579
c07b70ad
N
7580 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7581 /* We completed so min/max setting can be forgotten if used. */
7582 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7583 mddev->resync_min = 0;
7584 mddev->resync_max = MaxSector;
7585 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7586 mddev->resync_min = mddev->curr_resync_completed;
1da177e4
LT
7587 mddev->curr_resync = 0;
7588 wake_up(&resync_wait);
7589 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7590 md_wakeup_thread(mddev->thread);
c6207277 7591 return;
1da177e4 7592}
29269553 7593EXPORT_SYMBOL_GPL(md_do_sync);
1da177e4 7594
746d3207
N
7595static int remove_and_add_spares(struct mddev *mddev,
7596 struct md_rdev *this)
b4c4c7b8 7597{
3cb03002 7598 struct md_rdev *rdev;
b4c4c7b8 7599 int spares = 0;
f2a371c5 7600 int removed = 0;
b4c4c7b8 7601
dafb20fa 7602 rdev_for_each(rdev, mddev)
746d3207
N
7603 if ((this == NULL || rdev == this) &&
7604 rdev->raid_disk >= 0 &&
6bfe0b49 7605 !test_bit(Blocked, &rdev->flags) &&
b4c4c7b8
N
7606 (test_bit(Faulty, &rdev->flags) ||
7607 ! test_bit(In_sync, &rdev->flags)) &&
7608 atomic_read(&rdev->nr_pending)==0) {
7609 if (mddev->pers->hot_remove_disk(
b8321b68 7610 mddev, rdev) == 0) {
36fad858 7611 sysfs_unlink_rdev(mddev, rdev);
b4c4c7b8 7612 rdev->raid_disk = -1;
f2a371c5 7613 removed++;
b4c4c7b8
N
7614 }
7615 }
90584fc9
JB
7616 if (removed && mddev->kobj.sd)
7617 sysfs_notify(&mddev->kobj, NULL, "degraded");
b4c4c7b8 7618
746d3207
N
7619 if (this)
7620 goto no_add;
7621
dafb20fa 7622 rdev_for_each(rdev, mddev) {
7bfec5f3
N
7623 if (rdev->raid_disk >= 0 &&
7624 !test_bit(In_sync, &rdev->flags) &&
7625 !test_bit(Faulty, &rdev->flags))
7626 spares++;
7ceb17e8
N
7627 if (rdev->raid_disk >= 0)
7628 continue;
7629 if (test_bit(Faulty, &rdev->flags))
7630 continue;
7631 if (mddev->ro &&
8313b8e5
N
7632 ! (rdev->saved_raid_disk >= 0 &&
7633 !test_bit(Bitmap_sync, &rdev->flags)))
7ceb17e8
N
7634 continue;
7635
7eb41885
N
7636 if (rdev->saved_raid_disk < 0)
7637 rdev->recovery_offset = 0;
7ceb17e8
N
7638 if (mddev->pers->
7639 hot_add_disk(mddev, rdev) == 0) {
7640 if (sysfs_link_rdev(mddev, rdev))
7641 /* failure here is OK */;
7642 spares++;
7643 md_new_event(mddev);
7644 set_bit(MD_CHANGE_DEVS, &mddev->flags);
dfc70645 7645 }
b4c4c7b8 7646 }
746d3207 7647no_add:
6dafab6b
N
7648 if (removed)
7649 set_bit(MD_CHANGE_DEVS, &mddev->flags);
b4c4c7b8
N
7650 return spares;
7651}
7ebc0be7 7652
ac05f256
N
7653static void md_start_sync(struct work_struct *ws)
7654{
7655 struct mddev *mddev = container_of(ws, struct mddev, del_work);
7656
7657 mddev->sync_thread = md_register_thread(md_do_sync,
7658 mddev,
7659 "resync");
7660 if (!mddev->sync_thread) {
7661 printk(KERN_ERR "%s: could not start resync"
7662 " thread...\n",
7663 mdname(mddev));
7664 /* leave the spares where they are, it shouldn't hurt */
7665 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7666 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7667 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7668 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7669 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
f851b60d 7670 wake_up(&resync_wait);
ac05f256
N
7671 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7672 &mddev->recovery))
7673 if (mddev->sysfs_action)
7674 sysfs_notify_dirent_safe(mddev->sysfs_action);
7675 } else
7676 md_wakeup_thread(mddev->sync_thread);
7677 sysfs_notify_dirent_safe(mddev->sysfs_action);
7678 md_new_event(mddev);
7679}
7680
1da177e4
LT
7681/*
7682 * This routine is regularly called by all per-raid-array threads to
7683 * deal with generic issues like resync and super-block update.
7684 * Raid personalities that don't have a thread (linear/raid0) do not
7685 * need this as they never do any recovery or update the superblock.
7686 *
7687 * It does not do any resync itself, but rather "forks" off other threads
7688 * to do that as needed.
7689 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7690 * "->recovery" and create a thread at ->sync_thread.
dfc70645 7691 * When the thread finishes it sets MD_RECOVERY_DONE
1da177e4
LT
7692 * and wakeups up this thread which will reap the thread and finish up.
7693 * This thread also removes any faulty devices (with nr_pending == 0).
7694 *
7695 * The overall approach is:
7696 * 1/ if the superblock needs updating, update it.
7697 * 2/ If a recovery thread is running, don't do anything else.
7698 * 3/ If recovery has finished, clean up, possibly marking spares active.
7699 * 4/ If there are any faulty devices, remove them.
7700 * 5/ If array is degraded, try to add spares devices
7701 * 6/ If array has spares or is not in-sync, start a resync thread.
7702 */
fd01b88c 7703void md_check_recovery(struct mddev *mddev)
1da177e4 7704{
68866e42
JB
7705 if (mddev->suspended)
7706 return;
7707
5f40402d 7708 if (mddev->bitmap)
aa5cbd10 7709 bitmap_daemon_work(mddev);
1da177e4 7710
fca4d848 7711 if (signal_pending(current)) {
31a59e34 7712 if (mddev->pers->sync_request && !mddev->external) {
fca4d848
N
7713 printk(KERN_INFO "md: %s in immediate safe mode\n",
7714 mdname(mddev));
7715 mddev->safemode = 2;
7716 }
7717 flush_signals(current);
7718 }
7719
c89a8eee
N
7720 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7721 return;
1da177e4 7722 if ( ! (
142d44c3 7723 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
1da177e4 7724 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
fca4d848 7725 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
31a59e34 7726 (mddev->external == 0 && mddev->safemode == 1) ||
fca4d848
N
7727 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7728 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
1da177e4
LT
7729 ))
7730 return;
fca4d848 7731
df5b89b3 7732 if (mddev_trylock(mddev)) {
b4c4c7b8 7733 int spares = 0;
fca4d848 7734
c89a8eee 7735 if (mddev->ro) {
7ceb17e8
N
7736 /* On a read-only array we can:
7737 * - remove failed devices
7738 * - add already-in_sync devices if the array itself
7739 * is in-sync.
7740 * As we only add devices that are already in-sync,
7741 * we can activate the spares immediately.
c89a8eee 7742 */
7ceb17e8 7743 remove_and_add_spares(mddev, NULL);
8313b8e5
N
7744 /* There is no thread, but we need to call
7745 * ->spare_active and clear saved_raid_disk
7746 */
2ac295a5 7747 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8313b8e5
N
7748 md_reap_sync_thread(mddev);
7749 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
c89a8eee
N
7750 goto unlock;
7751 }
7752
31a59e34 7753 if (!mddev->external) {
0fd62b86 7754 int did_change = 0;
85572d7c 7755 spin_lock(&mddev->lock);
31a59e34
N
7756 if (mddev->safemode &&
7757 !atomic_read(&mddev->writes_pending) &&
7758 !mddev->in_sync &&
7759 mddev->recovery_cp == MaxSector) {
7760 mddev->in_sync = 1;
0fd62b86 7761 did_change = 1;
070dc6dd 7762 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
31a59e34
N
7763 }
7764 if (mddev->safemode == 1)
7765 mddev->safemode = 0;
85572d7c 7766 spin_unlock(&mddev->lock);
0fd62b86 7767 if (did_change)
00bcb4ac 7768 sysfs_notify_dirent_safe(mddev->sysfs_state);
fca4d848 7769 }
fca4d848 7770
7a0a5355 7771 if (mddev->flags & MD_UPDATE_SB_FLAGS)
850b2b42 7772 md_update_sb(mddev, 0);
06d91a5f 7773
1da177e4
LT
7774 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7775 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7776 /* resync/recovery still happening */
7777 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7778 goto unlock;
7779 }
7780 if (mddev->sync_thread) {
a91d5ac0 7781 md_reap_sync_thread(mddev);
1da177e4
LT
7782 goto unlock;
7783 }
72a23c21
NB
7784 /* Set RUNNING before clearing NEEDED to avoid
7785 * any transients in the value of "sync_action".
7786 */
72f36d59 7787 mddev->curr_resync_completed = 0;
72a23c21 7788 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
24dd469d
N
7789 /* Clear some bits that don't mean anything, but
7790 * might be left set
7791 */
24dd469d
N
7792 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7793 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
1da177e4 7794
ed209584
N
7795 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7796 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
ac05f256 7797 goto not_running;
1da177e4
LT
7798 /* no recovery is running.
7799 * remove any failed drives, then
7800 * add spares if possible.
72f36d59 7801 * Spares are also removed and re-added, to allow
1da177e4
LT
7802 * the personality to fail the re-add.
7803 */
1da177e4 7804
b4c4c7b8 7805 if (mddev->reshape_position != MaxSector) {
50ac168a
N
7806 if (mddev->pers->check_reshape == NULL ||
7807 mddev->pers->check_reshape(mddev) != 0)
b4c4c7b8 7808 /* Cannot proceed */
ac05f256 7809 goto not_running;
b4c4c7b8 7810 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
72a23c21 7811 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
746d3207 7812 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
24dd469d
N
7813 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7814 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
56ac36d7 7815 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
72a23c21 7816 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
24dd469d
N
7817 } else if (mddev->recovery_cp < MaxSector) {
7818 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
72a23c21 7819 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
24dd469d
N
7820 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7821 /* nothing to be done ... */
ac05f256 7822 goto not_running;
24dd469d 7823
1da177e4 7824 if (mddev->pers->sync_request) {
ef99bf48 7825 if (spares) {
a654b9d8
N
7826 /* We are adding a device or devices to an array
7827 * which has the bitmap stored on all devices.
7828 * So make sure all bitmap pages get written
7829 */
7830 bitmap_write_all(mddev->bitmap);
7831 }
ac05f256
N
7832 INIT_WORK(&mddev->del_work, md_start_sync);
7833 queue_work(md_misc_wq, &mddev->del_work);
7834 goto unlock;
1da177e4 7835 }
ac05f256 7836 not_running:
72a23c21
NB
7837 if (!mddev->sync_thread) {
7838 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
f851b60d 7839 wake_up(&resync_wait);
72a23c21
NB
7840 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7841 &mddev->recovery))
0c3573f1 7842 if (mddev->sysfs_action)
00bcb4ac 7843 sysfs_notify_dirent_safe(mddev->sysfs_action);
72a23c21 7844 }
ac05f256
N
7845 unlock:
7846 wake_up(&mddev->sb_wait);
1da177e4
LT
7847 mddev_unlock(mddev);
7848 }
7849}
6c144d31 7850EXPORT_SYMBOL(md_check_recovery);
1da177e4 7851
a91d5ac0
JB
7852void md_reap_sync_thread(struct mddev *mddev)
7853{
7854 struct md_rdev *rdev;
7855
7856 /* resync has finished, collect result */
7857 md_unregister_thread(&mddev->sync_thread);
7858 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7859 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7860 /* success...*/
7861 /* activate any spares */
7862 if (mddev->pers->spare_active(mddev)) {
7863 sysfs_notify(&mddev->kobj, NULL,
7864 "degraded");
7865 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7866 }
7867 }
7868 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7869 mddev->pers->finish_reshape)
7870 mddev->pers->finish_reshape(mddev);
7871
7872 /* If array is no-longer degraded, then any saved_raid_disk
f466722c 7873 * information must be scrapped.
a91d5ac0 7874 */
f466722c
N
7875 if (!mddev->degraded)
7876 rdev_for_each(rdev, mddev)
a91d5ac0
JB
7877 rdev->saved_raid_disk = -1;
7878
7879 md_update_sb(mddev, 1);
7880 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7881 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7882 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7883 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7884 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
f851b60d 7885 wake_up(&resync_wait);
a91d5ac0
JB
7886 /* flag recovery needed just to double check */
7887 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7888 sysfs_notify_dirent_safe(mddev->sysfs_action);
7889 md_new_event(mddev);
7890 if (mddev->event_work.func)
7891 queue_work(md_misc_wq, &mddev->event_work);
7892}
6c144d31 7893EXPORT_SYMBOL(md_reap_sync_thread);
a91d5ac0 7894
fd01b88c 7895void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
6bfe0b49 7896{
00bcb4ac 7897 sysfs_notify_dirent_safe(rdev->sysfs_state);
6bfe0b49 7898 wait_event_timeout(rdev->blocked_wait,
de393cde
N
7899 !test_bit(Blocked, &rdev->flags) &&
7900 !test_bit(BlockedBadBlocks, &rdev->flags),
6bfe0b49
DW
7901 msecs_to_jiffies(5000));
7902 rdev_dec_pending(rdev, mddev);
7903}
7904EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7905
c6563a8c
N
7906void md_finish_reshape(struct mddev *mddev)
7907{
7908 /* called be personality module when reshape completes. */
7909 struct md_rdev *rdev;
7910
7911 rdev_for_each(rdev, mddev) {
7912 if (rdev->data_offset > rdev->new_data_offset)
7913 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7914 else
7915 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7916 rdev->data_offset = rdev->new_data_offset;
7917 }
7918}
7919EXPORT_SYMBOL(md_finish_reshape);
2230dfe4
N
7920
7921/* Bad block management.
7922 * We can record which blocks on each device are 'bad' and so just
7923 * fail those blocks, or that stripe, rather than the whole device.
7924 * Entries in the bad-block table are 64bits wide. This comprises:
7925 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7926 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7927 * A 'shift' can be set so that larger blocks are tracked and
7928 * consequently larger devices can be covered.
7929 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7930 *
7931 * Locking of the bad-block table uses a seqlock so md_is_badblock
7932 * might need to retry if it is very unlucky.
7933 * We will sometimes want to check for bad blocks in a bi_end_io function,
7934 * so we use the write_seqlock_irq variant.
7935 *
7936 * When looking for a bad block we specify a range and want to
7937 * know if any block in the range is bad. So we binary-search
7938 * to the last range that starts at-or-before the given endpoint,
7939 * (or "before the sector after the target range")
7940 * then see if it ends after the given start.
7941 * We return
7942 * 0 if there are no known bad blocks in the range
7943 * 1 if there are known bad block which are all acknowledged
7944 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7945 * plus the start/length of the first bad section we overlap.
7946 */
7947int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7948 sector_t *first_bad, int *bad_sectors)
7949{
7950 int hi;
ab05613a 7951 int lo;
2230dfe4 7952 u64 *p = bb->page;
ab05613a 7953 int rv;
2230dfe4
N
7954 sector_t target = s + sectors;
7955 unsigned seq;
7956
7957 if (bb->shift > 0) {
7958 /* round the start down, and the end up */
7959 s >>= bb->shift;
7960 target += (1<<bb->shift) - 1;
7961 target >>= bb->shift;
7962 sectors = target - s;
7963 }
7964 /* 'target' is now the first block after the bad range */
7965
7966retry:
7967 seq = read_seqbegin(&bb->lock);
ab05613a 7968 lo = 0;
7969 rv = 0;
2230dfe4
N
7970 hi = bb->count;
7971
7972 /* Binary search between lo and hi for 'target'
7973 * i.e. for the last range that starts before 'target'
7974 */
7975 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7976 * are known not to be the last range before target.
7977 * VARIANT: hi-lo is the number of possible
7978 * ranges, and decreases until it reaches 1
7979 */
7980 while (hi - lo > 1) {
7981 int mid = (lo + hi) / 2;
7982 sector_t a = BB_OFFSET(p[mid]);
7983 if (a < target)
7984 /* This could still be the one, earlier ranges
7985 * could not. */
7986 lo = mid;
7987 else
7988 /* This and later ranges are definitely out. */
7989 hi = mid;
7990 }
7991 /* 'lo' might be the last that started before target, but 'hi' isn't */
7992 if (hi > lo) {
7993 /* need to check all range that end after 's' to see if
7994 * any are unacknowledged.
7995 */
7996 while (lo >= 0 &&
7997 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7998 if (BB_OFFSET(p[lo]) < target) {
7999 /* starts before the end, and finishes after
8000 * the start, so they must overlap
8001 */
8002 if (rv != -1 && BB_ACK(p[lo]))
8003 rv = 1;
8004 else
8005 rv = -1;
8006 *first_bad = BB_OFFSET(p[lo]);
8007 *bad_sectors = BB_LEN(p[lo]);
8008 }
8009 lo--;
8010 }
8011 }
8012
8013 if (read_seqretry(&bb->lock, seq))
8014 goto retry;
8015
8016 return rv;
8017}
8018EXPORT_SYMBOL_GPL(md_is_badblock);
8019
8020/*
8021 * Add a range of bad blocks to the table.
8022 * This might extend the table, or might contract it
8023 * if two adjacent ranges can be merged.
8024 * We binary-search to find the 'insertion' point, then
8025 * decide how best to handle it.
8026 */
8027static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8028 int acknowledged)
8029{
8030 u64 *p;
8031 int lo, hi;
8032 int rv = 1;
905b0297 8033 unsigned long flags;
2230dfe4
N
8034
8035 if (bb->shift < 0)
8036 /* badblocks are disabled */
8037 return 0;
8038
8039 if (bb->shift) {
8040 /* round the start down, and the end up */
8041 sector_t next = s + sectors;
8042 s >>= bb->shift;
8043 next += (1<<bb->shift) - 1;
8044 next >>= bb->shift;
8045 sectors = next - s;
8046 }
8047
905b0297 8048 write_seqlock_irqsave(&bb->lock, flags);
2230dfe4
N
8049
8050 p = bb->page;
8051 lo = 0;
8052 hi = bb->count;
8053 /* Find the last range that starts at-or-before 's' */
8054 while (hi - lo > 1) {
8055 int mid = (lo + hi) / 2;
8056 sector_t a = BB_OFFSET(p[mid]);
8057 if (a <= s)
8058 lo = mid;
8059 else
8060 hi = mid;
8061 }
8062 if (hi > lo && BB_OFFSET(p[lo]) > s)
8063 hi = lo;
8064
8065 if (hi > lo) {
8066 /* we found a range that might merge with the start
8067 * of our new range
8068 */
8069 sector_t a = BB_OFFSET(p[lo]);
8070 sector_t e = a + BB_LEN(p[lo]);
8071 int ack = BB_ACK(p[lo]);
8072 if (e >= s) {
8073 /* Yes, we can merge with a previous range */
8074 if (s == a && s + sectors >= e)
8075 /* new range covers old */
8076 ack = acknowledged;
8077 else
8078 ack = ack && acknowledged;
8079
8080 if (e < s + sectors)
8081 e = s + sectors;
8082 if (e - a <= BB_MAX_LEN) {
8083 p[lo] = BB_MAKE(a, e-a, ack);
8084 s = e;
8085 } else {
8086 /* does not all fit in one range,
8087 * make p[lo] maximal
8088 */
8089 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8090 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8091 s = a + BB_MAX_LEN;
8092 }
8093 sectors = e - s;
8094 }
8095 }
8096 if (sectors && hi < bb->count) {
8097 /* 'hi' points to the first range that starts after 's'.
8098 * Maybe we can merge with the start of that range */
8099 sector_t a = BB_OFFSET(p[hi]);
8100 sector_t e = a + BB_LEN(p[hi]);
8101 int ack = BB_ACK(p[hi]);
8102 if (a <= s + sectors) {
8103 /* merging is possible */
8104 if (e <= s + sectors) {
8105 /* full overlap */
8106 e = s + sectors;
8107 ack = acknowledged;
8108 } else
8109 ack = ack && acknowledged;
8110
8111 a = s;
8112 if (e - a <= BB_MAX_LEN) {
8113 p[hi] = BB_MAKE(a, e-a, ack);
8114 s = e;
8115 } else {
8116 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8117 s = a + BB_MAX_LEN;
8118 }
8119 sectors = e - s;
8120 lo = hi;
8121 hi++;
8122 }
8123 }
8124 if (sectors == 0 && hi < bb->count) {
8125 /* we might be able to combine lo and hi */
8126 /* Note: 's' is at the end of 'lo' */
8127 sector_t a = BB_OFFSET(p[hi]);
8128 int lolen = BB_LEN(p[lo]);
8129 int hilen = BB_LEN(p[hi]);
8130 int newlen = lolen + hilen - (s - a);
8131 if (s >= a && newlen < BB_MAX_LEN) {
8132 /* yes, we can combine them */
8133 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8134 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8135 memmove(p + hi, p + hi + 1,
8136 (bb->count - hi - 1) * 8);
8137 bb->count--;
8138 }
8139 }
8140 while (sectors) {
8141 /* didn't merge (it all).
8142 * Need to add a range just before 'hi' */
8143 if (bb->count >= MD_MAX_BADBLOCKS) {
8144 /* No room for more */
8145 rv = 0;
8146 break;
8147 } else {
8148 int this_sectors = sectors;
8149 memmove(p + hi + 1, p + hi,
8150 (bb->count - hi) * 8);
8151 bb->count++;
8152
8153 if (this_sectors > BB_MAX_LEN)
8154 this_sectors = BB_MAX_LEN;
8155 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8156 sectors -= this_sectors;
8157 s += this_sectors;
8158 }
8159 }
8160
8161 bb->changed = 1;
de393cde
N
8162 if (!acknowledged)
8163 bb->unacked_exist = 1;
905b0297 8164 write_sequnlock_irqrestore(&bb->lock, flags);
2230dfe4
N
8165
8166 return rv;
8167}
8168
3cb03002 8169int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
c6563a8c 8170 int is_new)
2230dfe4 8171{
c6563a8c
N
8172 int rv;
8173 if (is_new)
8174 s += rdev->new_data_offset;
8175 else
8176 s += rdev->data_offset;
8177 rv = md_set_badblocks(&rdev->badblocks,
8178 s, sectors, 0);
2230dfe4
N
8179 if (rv) {
8180 /* Make sure they get written out promptly */
8bd2f0a0 8181 sysfs_notify_dirent_safe(rdev->sysfs_state);
2230dfe4
N
8182 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8183 md_wakeup_thread(rdev->mddev->thread);
8184 }
8185 return rv;
8186}
8187EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8188
8189/*
8190 * Remove a range of bad blocks from the table.
8191 * This may involve extending the table if we spilt a region,
8192 * but it must not fail. So if the table becomes full, we just
8193 * drop the remove request.
8194 */
8195static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8196{
8197 u64 *p;
8198 int lo, hi;
8199 sector_t target = s + sectors;
8200 int rv = 0;
8201
8202 if (bb->shift > 0) {
8203 /* When clearing we round the start up and the end down.
8204 * This should not matter as the shift should align with
8205 * the block size and no rounding should ever be needed.
8206 * However it is better the think a block is bad when it
8207 * isn't than to think a block is not bad when it is.
8208 */
8209 s += (1<<bb->shift) - 1;
8210 s >>= bb->shift;
8211 target >>= bb->shift;
8212 sectors = target - s;
8213 }
8214
8215 write_seqlock_irq(&bb->lock);
8216
8217 p = bb->page;
8218 lo = 0;
8219 hi = bb->count;
8220 /* Find the last range that starts before 'target' */
8221 while (hi - lo > 1) {
8222 int mid = (lo + hi) / 2;
8223 sector_t a = BB_OFFSET(p[mid]);
8224 if (a < target)
8225 lo = mid;
8226 else
8227 hi = mid;
8228 }
8229 if (hi > lo) {
8230 /* p[lo] is the last range that could overlap the
8231 * current range. Earlier ranges could also overlap,
8232 * but only this one can overlap the end of the range.
8233 */
8234 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8235 /* Partial overlap, leave the tail of this range */
8236 int ack = BB_ACK(p[lo]);
8237 sector_t a = BB_OFFSET(p[lo]);
8238 sector_t end = a + BB_LEN(p[lo]);
8239
8240 if (a < s) {
8241 /* we need to split this range */
8242 if (bb->count >= MD_MAX_BADBLOCKS) {
8b32bf5e 8243 rv = -ENOSPC;
2230dfe4
N
8244 goto out;
8245 }
8246 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8247 bb->count++;
8248 p[lo] = BB_MAKE(a, s-a, ack);
8249 lo++;
8250 }
8251 p[lo] = BB_MAKE(target, end - target, ack);
8252 /* there is no longer an overlap */
8253 hi = lo;
8254 lo--;
8255 }
8256 while (lo >= 0 &&
8257 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8258 /* This range does overlap */
8259 if (BB_OFFSET(p[lo]) < s) {
8260 /* Keep the early parts of this range. */
8261 int ack = BB_ACK(p[lo]);
8262 sector_t start = BB_OFFSET(p[lo]);
8263 p[lo] = BB_MAKE(start, s - start, ack);
8264 /* now low doesn't overlap, so.. */
8265 break;
8266 }
8267 lo--;
8268 }
8269 /* 'lo' is strictly before, 'hi' is strictly after,
8270 * anything between needs to be discarded
8271 */
8272 if (hi - lo > 1) {
8273 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8274 bb->count -= (hi - lo - 1);
8275 }
8276 }
8277
8278 bb->changed = 1;
8279out:
8280 write_sequnlock_irq(&bb->lock);
8281 return rv;
8282}
8283
c6563a8c
N
8284int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8285 int is_new)
2230dfe4 8286{
c6563a8c
N
8287 if (is_new)
8288 s += rdev->new_data_offset;
8289 else
8290 s += rdev->data_offset;
2230dfe4 8291 return md_clear_badblocks(&rdev->badblocks,
c6563a8c 8292 s, sectors);
2230dfe4
N
8293}
8294EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8295
8296/*
8297 * Acknowledge all bad blocks in a list.
8298 * This only succeeds if ->changed is clear. It is used by
8299 * in-kernel metadata updates
8300 */
8301void md_ack_all_badblocks(struct badblocks *bb)
8302{
8303 if (bb->page == NULL || bb->changed)
8304 /* no point even trying */
8305 return;
8306 write_seqlock_irq(&bb->lock);
8307
ecb178bb 8308 if (bb->changed == 0 && bb->unacked_exist) {
2230dfe4
N
8309 u64 *p = bb->page;
8310 int i;
8311 for (i = 0; i < bb->count ; i++) {
8312 if (!BB_ACK(p[i])) {
8313 sector_t start = BB_OFFSET(p[i]);
8314 int len = BB_LEN(p[i]);
8315 p[i] = BB_MAKE(start, len, 1);
8316 }
8317 }
de393cde 8318 bb->unacked_exist = 0;
2230dfe4
N
8319 }
8320 write_sequnlock_irq(&bb->lock);
8321}
8322EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8323
16c791a5
N
8324/* sysfs access to bad-blocks list.
8325 * We present two files.
8326 * 'bad-blocks' lists sector numbers and lengths of ranges that
8327 * are recorded as bad. The list is truncated to fit within
8328 * the one-page limit of sysfs.
8329 * Writing "sector length" to this file adds an acknowledged
8330 * bad block list.
8331 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8332 * been acknowledged. Writing to this file adds bad blocks
8333 * without acknowledging them. This is largely for testing.
8334 */
8335
8336static ssize_t
8337badblocks_show(struct badblocks *bb, char *page, int unack)
8338{
8339 size_t len;
8340 int i;
8341 u64 *p = bb->page;
8342 unsigned seq;
8343
8344 if (bb->shift < 0)
8345 return 0;
8346
8347retry:
8348 seq = read_seqbegin(&bb->lock);
8349
8350 len = 0;
8351 i = 0;
8352
8353 while (len < PAGE_SIZE && i < bb->count) {
8354 sector_t s = BB_OFFSET(p[i]);
8355 unsigned int length = BB_LEN(p[i]);
8356 int ack = BB_ACK(p[i]);
8357 i++;
8358
8359 if (unack && ack)
8360 continue;
8361
8362 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8363 (unsigned long long)s << bb->shift,
8364 length << bb->shift);
8365 }
de393cde
N
8366 if (unack && len == 0)
8367 bb->unacked_exist = 0;
16c791a5
N
8368
8369 if (read_seqretry(&bb->lock, seq))
8370 goto retry;
8371
8372 return len;
8373}
8374
8375#define DO_DEBUG 1
8376
8377static ssize_t
8378badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8379{
8380 unsigned long long sector;
8381 int length;
8382 char newline;
8383#ifdef DO_DEBUG
8384 /* Allow clearing via sysfs *only* for testing/debugging.
8385 * Normally only a successful write may clear a badblock
8386 */
8387 int clear = 0;
8388 if (page[0] == '-') {
8389 clear = 1;
8390 page++;
8391 }
8392#endif /* DO_DEBUG */
8393
8394 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8395 case 3:
8396 if (newline != '\n')
8397 return -EINVAL;
8398 case 2:
8399 if (length <= 0)
8400 return -EINVAL;
8401 break;
8402 default:
8403 return -EINVAL;
8404 }
8405
8406#ifdef DO_DEBUG
8407 if (clear) {
8408 md_clear_badblocks(bb, sector, length);
8409 return len;
8410 }
8411#endif /* DO_DEBUG */
8412 if (md_set_badblocks(bb, sector, length, !unack))
8413 return len;
8414 else
8415 return -ENOSPC;
8416}
8417
75c96f85
AB
8418static int md_notify_reboot(struct notifier_block *this,
8419 unsigned long code, void *x)
1da177e4
LT
8420{
8421 struct list_head *tmp;
fd01b88c 8422 struct mddev *mddev;
2dba6a91 8423 int need_delay = 0;
1da177e4 8424
c744a65c
N
8425 for_each_mddev(mddev, tmp) {
8426 if (mddev_trylock(mddev)) {
30b8aa91
N
8427 if (mddev->pers)
8428 __md_stop_writes(mddev);
0f62fb22
N
8429 if (mddev->persistent)
8430 mddev->safemode = 2;
c744a65c 8431 mddev_unlock(mddev);
2dba6a91 8432 }
c744a65c 8433 need_delay = 1;
1da177e4 8434 }
c744a65c
N
8435 /*
8436 * certain more exotic SCSI devices are known to be
8437 * volatile wrt too early system reboots. While the
8438 * right place to handle this issue is the given
8439 * driver, we do want to have a safe RAID driver ...
8440 */
8441 if (need_delay)
8442 mdelay(1000*1);
8443
1da177e4
LT
8444 return NOTIFY_DONE;
8445}
8446
75c96f85 8447static struct notifier_block md_notifier = {
1da177e4
LT
8448 .notifier_call = md_notify_reboot,
8449 .next = NULL,
8450 .priority = INT_MAX, /* before any real devices */
8451};
8452
8453static void md_geninit(void)
8454{
36a4e1fe 8455 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
1da177e4 8456
c7705f34 8457 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
1da177e4
LT
8458}
8459
75c96f85 8460static int __init md_init(void)
1da177e4 8461{
e804ac78
TH
8462 int ret = -ENOMEM;
8463
ada609ee 8464 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
e804ac78
TH
8465 if (!md_wq)
8466 goto err_wq;
8467
8468 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8469 if (!md_misc_wq)
8470 goto err_misc_wq;
8471
8472 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8473 goto err_md;
8474
8475 if ((ret = register_blkdev(0, "mdp")) < 0)
8476 goto err_mdp;
8477 mdp_major = ret;
8478
af5628f0 8479 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
e8703fe1
N
8480 md_probe, NULL, NULL);
8481 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
1da177e4
LT
8482 md_probe, NULL, NULL);
8483
1da177e4 8484 register_reboot_notifier(&md_notifier);
0b4d4147 8485 raid_table_header = register_sysctl_table(raid_root_table);
1da177e4
LT
8486
8487 md_geninit();
d710e138 8488 return 0;
1da177e4 8489
e804ac78
TH
8490err_mdp:
8491 unregister_blkdev(MD_MAJOR, "md");
8492err_md:
8493 destroy_workqueue(md_misc_wq);
8494err_misc_wq:
8495 destroy_workqueue(md_wq);
8496err_wq:
8497 return ret;
8498}
1da177e4
LT
8499
8500#ifndef MODULE
8501
8502/*
8503 * Searches all registered partitions for autorun RAID arrays
8504 * at boot time.
8505 */
4d936ec1
ME
8506
8507static LIST_HEAD(all_detected_devices);
8508struct detected_devices_node {
8509 struct list_head list;
8510 dev_t dev;
8511};
1da177e4
LT
8512
8513void md_autodetect_dev(dev_t dev)
8514{
4d936ec1
ME
8515 struct detected_devices_node *node_detected_dev;
8516
8517 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8518 if (node_detected_dev) {
8519 node_detected_dev->dev = dev;
8520 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8521 } else {
8522 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8523 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8524 }
1da177e4
LT
8525}
8526
1da177e4
LT
8527static void autostart_arrays(int part)
8528{
3cb03002 8529 struct md_rdev *rdev;
4d936ec1
ME
8530 struct detected_devices_node *node_detected_dev;
8531 dev_t dev;
8532 int i_scanned, i_passed;
1da177e4 8533
4d936ec1
ME
8534 i_scanned = 0;
8535 i_passed = 0;
1da177e4 8536
4d936ec1 8537 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
1da177e4 8538
4d936ec1
ME
8539 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8540 i_scanned++;
8541 node_detected_dev = list_entry(all_detected_devices.next,
8542 struct detected_devices_node, list);
8543 list_del(&node_detected_dev->list);
8544 dev = node_detected_dev->dev;
8545 kfree(node_detected_dev);
df968c4e 8546 rdev = md_import_device(dev,0, 90);
1da177e4
LT
8547 if (IS_ERR(rdev))
8548 continue;
8549
403df478 8550 if (test_bit(Faulty, &rdev->flags))
1da177e4 8551 continue;
403df478 8552
d0fae18f 8553 set_bit(AutoDetected, &rdev->flags);
1da177e4 8554 list_add(&rdev->same_set, &pending_raid_disks);
4d936ec1 8555 i_passed++;
1da177e4 8556 }
4d936ec1
ME
8557
8558 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8559 i_scanned, i_passed);
1da177e4
LT
8560
8561 autorun_devices(part);
8562}
8563
fdee8ae4 8564#endif /* !MODULE */
1da177e4
LT
8565
8566static __exit void md_exit(void)
8567{
fd01b88c 8568 struct mddev *mddev;
1da177e4 8569 struct list_head *tmp;
e2f23b60 8570 int delay = 1;
8ab5e4c1 8571
af5628f0 8572 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
e8703fe1 8573 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
1da177e4 8574
3dbd8c2e 8575 unregister_blkdev(MD_MAJOR,"md");
1da177e4
LT
8576 unregister_blkdev(mdp_major, "mdp");
8577 unregister_reboot_notifier(&md_notifier);
8578 unregister_sysctl_table(raid_table_header);
e2f23b60
N
8579
8580 /* We cannot unload the modules while some process is
8581 * waiting for us in select() or poll() - wake them up
8582 */
8583 md_unloading = 1;
8584 while (waitqueue_active(&md_event_waiters)) {
8585 /* not safe to leave yet */
8586 wake_up(&md_event_waiters);
8587 msleep(delay);
8588 delay += delay;
8589 }
1da177e4 8590 remove_proc_entry("mdstat", NULL);
e2f23b60 8591
29ac4aa3 8592 for_each_mddev(mddev, tmp) {
1da177e4 8593 export_array(mddev);
d3374825 8594 mddev->hold_active = 0;
1da177e4 8595 }
e804ac78
TH
8596 destroy_workqueue(md_misc_wq);
8597 destroy_workqueue(md_wq);
1da177e4
LT
8598}
8599
685784aa 8600subsys_initcall(md_init);
1da177e4
LT
8601module_exit(md_exit)
8602
f91de92e
N
8603static int get_ro(char *buffer, struct kernel_param *kp)
8604{
8605 return sprintf(buffer, "%d", start_readonly);
8606}
8607static int set_ro(const char *val, struct kernel_param *kp)
8608{
8609 char *e;
8610 int num = simple_strtoul(val, &e, 10);
8611 if (*val && (*e == '\0' || *e == '\n')) {
8612 start_readonly = num;
4dbcdc75 8613 return 0;
f91de92e
N
8614 }
8615 return -EINVAL;
8616}
8617
80ca3a44
N
8618module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8619module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
efeb53c0 8620module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
f91de92e 8621
1da177e4 8622MODULE_LICENSE("GPL");
0efb9e61 8623MODULE_DESCRIPTION("MD RAID framework");
aa1595e9 8624MODULE_ALIAS("md");
72008652 8625MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);