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