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