]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/md/md.c
[PATCH] md: improvements to raid5 handling of read errors
[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/module.h>
36 #include <linux/config.h>
37 #include <linux/kthread.h>
38 #include <linux/linkage.h>
39 #include <linux/raid/md.h>
40 #include <linux/raid/bitmap.h>
41 #include <linux/sysctl.h>
42 #include <linux/devfs_fs_kernel.h>
43 #include <linux/buffer_head.h> /* for invalidate_bdev */
44 #include <linux/suspend.h>
45
46 #include <linux/init.h>
47
48 #include <linux/file.h>
49
50 #ifdef CONFIG_KMOD
51 #include <linux/kmod.h>
52 #endif
53
54 #include <asm/unaligned.h>
55
56 #define MAJOR_NR MD_MAJOR
57 #define MD_DRIVER
58
59 /* 63 partitions with the alternate major number (mdp) */
60 #define MdpMinorShift 6
61
62 #define DEBUG 0
63 #define dprintk(x...) ((void)(DEBUG && printk(x)))
64
65
66 #ifndef MODULE
67 static void autostart_arrays (int part);
68 #endif
69
70 static mdk_personality_t *pers[MAX_PERSONALITY];
71 static DEFINE_SPINLOCK(pers_lock);
72
73 /*
74 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
75 * is 1000 KB/sec, so the extra system load does not show up that much.
76 * Increase it if you want to have more _guaranteed_ speed. Note that
77 * the RAID driver will use the maximum available bandwidth if the IO
78 * subsystem is idle. There is also an 'absolute maximum' reconstruction
79 * speed limit - in case reconstruction slows down your system despite
80 * idle IO detection.
81 *
82 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
83 */
84
85 static int sysctl_speed_limit_min = 1000;
86 static int sysctl_speed_limit_max = 200000;
87
88 static struct ctl_table_header *raid_table_header;
89
90 static ctl_table raid_table[] = {
91 {
92 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
93 .procname = "speed_limit_min",
94 .data = &sysctl_speed_limit_min,
95 .maxlen = sizeof(int),
96 .mode = 0644,
97 .proc_handler = &proc_dointvec,
98 },
99 {
100 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
101 .procname = "speed_limit_max",
102 .data = &sysctl_speed_limit_max,
103 .maxlen = sizeof(int),
104 .mode = 0644,
105 .proc_handler = &proc_dointvec,
106 },
107 { .ctl_name = 0 }
108 };
109
110 static ctl_table raid_dir_table[] = {
111 {
112 .ctl_name = DEV_RAID,
113 .procname = "raid",
114 .maxlen = 0,
115 .mode = 0555,
116 .child = raid_table,
117 },
118 { .ctl_name = 0 }
119 };
120
121 static ctl_table raid_root_table[] = {
122 {
123 .ctl_name = CTL_DEV,
124 .procname = "dev",
125 .maxlen = 0,
126 .mode = 0555,
127 .child = raid_dir_table,
128 },
129 { .ctl_name = 0 }
130 };
131
132 static struct block_device_operations md_fops;
133
134 /*
135 * Enables to iterate over all existing md arrays
136 * all_mddevs_lock protects this list.
137 */
138 static LIST_HEAD(all_mddevs);
139 static DEFINE_SPINLOCK(all_mddevs_lock);
140
141
142 /*
143 * iterates through all used mddevs in the system.
144 * We take care to grab the all_mddevs_lock whenever navigating
145 * the list, and to always hold a refcount when unlocked.
146 * Any code which breaks out of this loop while own
147 * a reference to the current mddev and must mddev_put it.
148 */
149 #define ITERATE_MDDEV(mddev,tmp) \
150 \
151 for (({ spin_lock(&all_mddevs_lock); \
152 tmp = all_mddevs.next; \
153 mddev = NULL;}); \
154 ({ if (tmp != &all_mddevs) \
155 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
156 spin_unlock(&all_mddevs_lock); \
157 if (mddev) mddev_put(mddev); \
158 mddev = list_entry(tmp, mddev_t, all_mddevs); \
159 tmp != &all_mddevs;}); \
160 ({ spin_lock(&all_mddevs_lock); \
161 tmp = tmp->next;}) \
162 )
163
164
165 static int md_fail_request (request_queue_t *q, struct bio *bio)
166 {
167 bio_io_error(bio, bio->bi_size);
168 return 0;
169 }
170
171 static inline mddev_t *mddev_get(mddev_t *mddev)
172 {
173 atomic_inc(&mddev->active);
174 return mddev;
175 }
176
177 static void mddev_put(mddev_t *mddev)
178 {
179 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
180 return;
181 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
182 list_del(&mddev->all_mddevs);
183 blk_put_queue(mddev->queue);
184 kobject_unregister(&mddev->kobj);
185 }
186 spin_unlock(&all_mddevs_lock);
187 }
188
189 static mddev_t * mddev_find(dev_t unit)
190 {
191 mddev_t *mddev, *new = NULL;
192
193 retry:
194 spin_lock(&all_mddevs_lock);
195 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
196 if (mddev->unit == unit) {
197 mddev_get(mddev);
198 spin_unlock(&all_mddevs_lock);
199 kfree(new);
200 return mddev;
201 }
202
203 if (new) {
204 list_add(&new->all_mddevs, &all_mddevs);
205 spin_unlock(&all_mddevs_lock);
206 return new;
207 }
208 spin_unlock(&all_mddevs_lock);
209
210 new = (mddev_t *) kmalloc(sizeof(*new), GFP_KERNEL);
211 if (!new)
212 return NULL;
213
214 memset(new, 0, sizeof(*new));
215
216 new->unit = unit;
217 if (MAJOR(unit) == MD_MAJOR)
218 new->md_minor = MINOR(unit);
219 else
220 new->md_minor = MINOR(unit) >> MdpMinorShift;
221
222 init_MUTEX(&new->reconfig_sem);
223 INIT_LIST_HEAD(&new->disks);
224 INIT_LIST_HEAD(&new->all_mddevs);
225 init_timer(&new->safemode_timer);
226 atomic_set(&new->active, 1);
227 spin_lock_init(&new->write_lock);
228 init_waitqueue_head(&new->sb_wait);
229
230 new->queue = blk_alloc_queue(GFP_KERNEL);
231 if (!new->queue) {
232 kfree(new);
233 return NULL;
234 }
235
236 blk_queue_make_request(new->queue, md_fail_request);
237
238 goto retry;
239 }
240
241 static inline int mddev_lock(mddev_t * mddev)
242 {
243 return down_interruptible(&mddev->reconfig_sem);
244 }
245
246 static inline void mddev_lock_uninterruptible(mddev_t * mddev)
247 {
248 down(&mddev->reconfig_sem);
249 }
250
251 static inline int mddev_trylock(mddev_t * mddev)
252 {
253 return down_trylock(&mddev->reconfig_sem);
254 }
255
256 static inline void mddev_unlock(mddev_t * mddev)
257 {
258 up(&mddev->reconfig_sem);
259
260 md_wakeup_thread(mddev->thread);
261 }
262
263 mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
264 {
265 mdk_rdev_t * rdev;
266 struct list_head *tmp;
267
268 ITERATE_RDEV(mddev,rdev,tmp) {
269 if (rdev->desc_nr == nr)
270 return rdev;
271 }
272 return NULL;
273 }
274
275 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
276 {
277 struct list_head *tmp;
278 mdk_rdev_t *rdev;
279
280 ITERATE_RDEV(mddev,rdev,tmp) {
281 if (rdev->bdev->bd_dev == dev)
282 return rdev;
283 }
284 return NULL;
285 }
286
287 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
288 {
289 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
290 return MD_NEW_SIZE_BLOCKS(size);
291 }
292
293 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
294 {
295 sector_t size;
296
297 size = rdev->sb_offset;
298
299 if (chunk_size)
300 size &= ~((sector_t)chunk_size/1024 - 1);
301 return size;
302 }
303
304 static int alloc_disk_sb(mdk_rdev_t * rdev)
305 {
306 if (rdev->sb_page)
307 MD_BUG();
308
309 rdev->sb_page = alloc_page(GFP_KERNEL);
310 if (!rdev->sb_page) {
311 printk(KERN_ALERT "md: out of memory.\n");
312 return -EINVAL;
313 }
314
315 return 0;
316 }
317
318 static void free_disk_sb(mdk_rdev_t * rdev)
319 {
320 if (rdev->sb_page) {
321 page_cache_release(rdev->sb_page);
322 rdev->sb_loaded = 0;
323 rdev->sb_page = NULL;
324 rdev->sb_offset = 0;
325 rdev->size = 0;
326 }
327 }
328
329
330 static int super_written(struct bio *bio, unsigned int bytes_done, int error)
331 {
332 mdk_rdev_t *rdev = bio->bi_private;
333 if (bio->bi_size)
334 return 1;
335
336 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
337 md_error(rdev->mddev, rdev);
338
339 if (atomic_dec_and_test(&rdev->mddev->pending_writes))
340 wake_up(&rdev->mddev->sb_wait);
341 bio_put(bio);
342 return 0;
343 }
344
345 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
346 sector_t sector, int size, struct page *page)
347 {
348 /* write first size bytes of page to sector of rdev
349 * Increment mddev->pending_writes before returning
350 * and decrement it on completion, waking up sb_wait
351 * if zero is reached.
352 * If an error occurred, call md_error
353 */
354 struct bio *bio = bio_alloc(GFP_NOIO, 1);
355
356 bio->bi_bdev = rdev->bdev;
357 bio->bi_sector = sector;
358 bio_add_page(bio, page, size, 0);
359 bio->bi_private = rdev;
360 bio->bi_end_io = super_written;
361 atomic_inc(&mddev->pending_writes);
362 submit_bio((1<<BIO_RW)|(1<<BIO_RW_SYNC), bio);
363 }
364
365 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
366 {
367 if (bio->bi_size)
368 return 1;
369
370 complete((struct completion*)bio->bi_private);
371 return 0;
372 }
373
374 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
375 struct page *page, int rw)
376 {
377 struct bio *bio = bio_alloc(GFP_NOIO, 1);
378 struct completion event;
379 int ret;
380
381 rw |= (1 << BIO_RW_SYNC);
382
383 bio->bi_bdev = bdev;
384 bio->bi_sector = sector;
385 bio_add_page(bio, page, size, 0);
386 init_completion(&event);
387 bio->bi_private = &event;
388 bio->bi_end_io = bi_complete;
389 submit_bio(rw, bio);
390 wait_for_completion(&event);
391
392 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
393 bio_put(bio);
394 return ret;
395 }
396
397 static int read_disk_sb(mdk_rdev_t * rdev, int size)
398 {
399 char b[BDEVNAME_SIZE];
400 if (!rdev->sb_page) {
401 MD_BUG();
402 return -EINVAL;
403 }
404 if (rdev->sb_loaded)
405 return 0;
406
407
408 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
409 goto fail;
410 rdev->sb_loaded = 1;
411 return 0;
412
413 fail:
414 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
415 bdevname(rdev->bdev,b));
416 return -EINVAL;
417 }
418
419 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
420 {
421 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
422 (sb1->set_uuid1 == sb2->set_uuid1) &&
423 (sb1->set_uuid2 == sb2->set_uuid2) &&
424 (sb1->set_uuid3 == sb2->set_uuid3))
425
426 return 1;
427
428 return 0;
429 }
430
431
432 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
433 {
434 int ret;
435 mdp_super_t *tmp1, *tmp2;
436
437 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
438 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
439
440 if (!tmp1 || !tmp2) {
441 ret = 0;
442 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
443 goto abort;
444 }
445
446 *tmp1 = *sb1;
447 *tmp2 = *sb2;
448
449 /*
450 * nr_disks is not constant
451 */
452 tmp1->nr_disks = 0;
453 tmp2->nr_disks = 0;
454
455 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
456 ret = 0;
457 else
458 ret = 1;
459
460 abort:
461 kfree(tmp1);
462 kfree(tmp2);
463 return ret;
464 }
465
466 static unsigned int calc_sb_csum(mdp_super_t * sb)
467 {
468 unsigned int disk_csum, csum;
469
470 disk_csum = sb->sb_csum;
471 sb->sb_csum = 0;
472 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
473 sb->sb_csum = disk_csum;
474 return csum;
475 }
476
477
478 /*
479 * Handle superblock details.
480 * We want to be able to handle multiple superblock formats
481 * so we have a common interface to them all, and an array of
482 * different handlers.
483 * We rely on user-space to write the initial superblock, and support
484 * reading and updating of superblocks.
485 * Interface methods are:
486 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
487 * loads and validates a superblock on dev.
488 * if refdev != NULL, compare superblocks on both devices
489 * Return:
490 * 0 - dev has a superblock that is compatible with refdev
491 * 1 - dev has a superblock that is compatible and newer than refdev
492 * so dev should be used as the refdev in future
493 * -EINVAL superblock incompatible or invalid
494 * -othererror e.g. -EIO
495 *
496 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
497 * Verify that dev is acceptable into mddev.
498 * The first time, mddev->raid_disks will be 0, and data from
499 * dev should be merged in. Subsequent calls check that dev
500 * is new enough. Return 0 or -EINVAL
501 *
502 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
503 * Update the superblock for rdev with data in mddev
504 * This does not write to disc.
505 *
506 */
507
508 struct super_type {
509 char *name;
510 struct module *owner;
511 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
512 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
513 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
514 };
515
516 /*
517 * load_super for 0.90.0
518 */
519 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
520 {
521 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
522 mdp_super_t *sb;
523 int ret;
524 sector_t sb_offset;
525
526 /*
527 * Calculate the position of the superblock,
528 * it's at the end of the disk.
529 *
530 * It also happens to be a multiple of 4Kb.
531 */
532 sb_offset = calc_dev_sboffset(rdev->bdev);
533 rdev->sb_offset = sb_offset;
534
535 ret = read_disk_sb(rdev, MD_SB_BYTES);
536 if (ret) return ret;
537
538 ret = -EINVAL;
539
540 bdevname(rdev->bdev, b);
541 sb = (mdp_super_t*)page_address(rdev->sb_page);
542
543 if (sb->md_magic != MD_SB_MAGIC) {
544 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
545 b);
546 goto abort;
547 }
548
549 if (sb->major_version != 0 ||
550 sb->minor_version != 90) {
551 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
552 sb->major_version, sb->minor_version,
553 b);
554 goto abort;
555 }
556
557 if (sb->raid_disks <= 0)
558 goto abort;
559
560 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
561 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
562 b);
563 goto abort;
564 }
565
566 rdev->preferred_minor = sb->md_minor;
567 rdev->data_offset = 0;
568 rdev->sb_size = MD_SB_BYTES;
569
570 if (sb->level == LEVEL_MULTIPATH)
571 rdev->desc_nr = -1;
572 else
573 rdev->desc_nr = sb->this_disk.number;
574
575 if (refdev == 0)
576 ret = 1;
577 else {
578 __u64 ev1, ev2;
579 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
580 if (!uuid_equal(refsb, sb)) {
581 printk(KERN_WARNING "md: %s has different UUID to %s\n",
582 b, bdevname(refdev->bdev,b2));
583 goto abort;
584 }
585 if (!sb_equal(refsb, sb)) {
586 printk(KERN_WARNING "md: %s has same UUID"
587 " but different superblock to %s\n",
588 b, bdevname(refdev->bdev, b2));
589 goto abort;
590 }
591 ev1 = md_event(sb);
592 ev2 = md_event(refsb);
593 if (ev1 > ev2)
594 ret = 1;
595 else
596 ret = 0;
597 }
598 rdev->size = calc_dev_size(rdev, sb->chunk_size);
599
600 abort:
601 return ret;
602 }
603
604 /*
605 * validate_super for 0.90.0
606 */
607 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
608 {
609 mdp_disk_t *desc;
610 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
611
612 rdev->raid_disk = -1;
613 rdev->in_sync = 0;
614 if (mddev->raid_disks == 0) {
615 mddev->major_version = 0;
616 mddev->minor_version = sb->minor_version;
617 mddev->patch_version = sb->patch_version;
618 mddev->persistent = ! sb->not_persistent;
619 mddev->chunk_size = sb->chunk_size;
620 mddev->ctime = sb->ctime;
621 mddev->utime = sb->utime;
622 mddev->level = sb->level;
623 mddev->layout = sb->layout;
624 mddev->raid_disks = sb->raid_disks;
625 mddev->size = sb->size;
626 mddev->events = md_event(sb);
627 mddev->bitmap_offset = 0;
628 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
629
630 if (sb->state & (1<<MD_SB_CLEAN))
631 mddev->recovery_cp = MaxSector;
632 else {
633 if (sb->events_hi == sb->cp_events_hi &&
634 sb->events_lo == sb->cp_events_lo) {
635 mddev->recovery_cp = sb->recovery_cp;
636 } else
637 mddev->recovery_cp = 0;
638 }
639
640 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
641 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
642 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
643 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
644
645 mddev->max_disks = MD_SB_DISKS;
646
647 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
648 mddev->bitmap_file == NULL) {
649 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6) {
650 /* FIXME use a better test */
651 printk(KERN_WARNING "md: bitmaps only support for raid1\n");
652 return -EINVAL;
653 }
654 mddev->bitmap_offset = mddev->default_bitmap_offset;
655 }
656
657 } else if (mddev->pers == NULL) {
658 /* Insist on good event counter while assembling */
659 __u64 ev1 = md_event(sb);
660 ++ev1;
661 if (ev1 < mddev->events)
662 return -EINVAL;
663 } else if (mddev->bitmap) {
664 /* if adding to array with a bitmap, then we can accept an
665 * older device ... but not too old.
666 */
667 __u64 ev1 = md_event(sb);
668 if (ev1 < mddev->bitmap->events_cleared)
669 return 0;
670 } else /* just a hot-add of a new device, leave raid_disk at -1 */
671 return 0;
672
673 if (mddev->level != LEVEL_MULTIPATH) {
674 rdev->faulty = 0;
675 rdev->flags = 0;
676 desc = sb->disks + rdev->desc_nr;
677
678 if (desc->state & (1<<MD_DISK_FAULTY))
679 rdev->faulty = 1;
680 else if (desc->state & (1<<MD_DISK_SYNC) &&
681 desc->raid_disk < mddev->raid_disks) {
682 rdev->in_sync = 1;
683 rdev->raid_disk = desc->raid_disk;
684 }
685 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
686 set_bit(WriteMostly, &rdev->flags);
687 } else /* MULTIPATH are always insync */
688 rdev->in_sync = 1;
689 return 0;
690 }
691
692 /*
693 * sync_super for 0.90.0
694 */
695 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
696 {
697 mdp_super_t *sb;
698 struct list_head *tmp;
699 mdk_rdev_t *rdev2;
700 int next_spare = mddev->raid_disks;
701 char nm[20];
702
703 /* make rdev->sb match mddev data..
704 *
705 * 1/ zero out disks
706 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
707 * 3/ any empty disks < next_spare become removed
708 *
709 * disks[0] gets initialised to REMOVED because
710 * we cannot be sure from other fields if it has
711 * been initialised or not.
712 */
713 int i;
714 int active=0, working=0,failed=0,spare=0,nr_disks=0;
715 unsigned int fixdesc=0;
716
717 rdev->sb_size = MD_SB_BYTES;
718
719 sb = (mdp_super_t*)page_address(rdev->sb_page);
720
721 memset(sb, 0, sizeof(*sb));
722
723 sb->md_magic = MD_SB_MAGIC;
724 sb->major_version = mddev->major_version;
725 sb->minor_version = mddev->minor_version;
726 sb->patch_version = mddev->patch_version;
727 sb->gvalid_words = 0; /* ignored */
728 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
729 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
730 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
731 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
732
733 sb->ctime = mddev->ctime;
734 sb->level = mddev->level;
735 sb->size = mddev->size;
736 sb->raid_disks = mddev->raid_disks;
737 sb->md_minor = mddev->md_minor;
738 sb->not_persistent = !mddev->persistent;
739 sb->utime = mddev->utime;
740 sb->state = 0;
741 sb->events_hi = (mddev->events>>32);
742 sb->events_lo = (u32)mddev->events;
743
744 if (mddev->in_sync)
745 {
746 sb->recovery_cp = mddev->recovery_cp;
747 sb->cp_events_hi = (mddev->events>>32);
748 sb->cp_events_lo = (u32)mddev->events;
749 if (mddev->recovery_cp == MaxSector)
750 sb->state = (1<< MD_SB_CLEAN);
751 } else
752 sb->recovery_cp = 0;
753
754 sb->layout = mddev->layout;
755 sb->chunk_size = mddev->chunk_size;
756
757 if (mddev->bitmap && mddev->bitmap_file == NULL)
758 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
759
760 sb->disks[0].state = (1<<MD_DISK_REMOVED);
761 ITERATE_RDEV(mddev,rdev2,tmp) {
762 mdp_disk_t *d;
763 int desc_nr;
764 if (rdev2->raid_disk >= 0 && rdev2->in_sync && !rdev2->faulty)
765 desc_nr = rdev2->raid_disk;
766 else
767 desc_nr = next_spare++;
768 if (desc_nr != rdev2->desc_nr) {
769 fixdesc |= (1 << desc_nr);
770 rdev2->desc_nr = desc_nr;
771 if (rdev2->raid_disk >= 0) {
772 sprintf(nm, "rd%d", rdev2->raid_disk);
773 sysfs_remove_link(&mddev->kobj, nm);
774 }
775 sysfs_remove_link(&rdev2->kobj, "block");
776 kobject_del(&rdev2->kobj);
777 }
778 d = &sb->disks[rdev2->desc_nr];
779 nr_disks++;
780 d->number = rdev2->desc_nr;
781 d->major = MAJOR(rdev2->bdev->bd_dev);
782 d->minor = MINOR(rdev2->bdev->bd_dev);
783 if (rdev2->raid_disk >= 0 && rdev2->in_sync && !rdev2->faulty)
784 d->raid_disk = rdev2->raid_disk;
785 else
786 d->raid_disk = rdev2->desc_nr; /* compatibility */
787 if (rdev2->faulty) {
788 d->state = (1<<MD_DISK_FAULTY);
789 failed++;
790 } else if (rdev2->in_sync) {
791 d->state = (1<<MD_DISK_ACTIVE);
792 d->state |= (1<<MD_DISK_SYNC);
793 active++;
794 working++;
795 } else {
796 d->state = 0;
797 spare++;
798 working++;
799 }
800 if (test_bit(WriteMostly, &rdev2->flags))
801 d->state |= (1<<MD_DISK_WRITEMOSTLY);
802 }
803 if (fixdesc)
804 ITERATE_RDEV(mddev,rdev2,tmp)
805 if (fixdesc & (1<<rdev2->desc_nr)) {
806 snprintf(rdev2->kobj.name, KOBJ_NAME_LEN, "dev%d",
807 rdev2->desc_nr);
808 /* kobject_add gets a ref on the parent, so
809 * we have to drop the one we already have
810 */
811 kobject_add(&rdev2->kobj);
812 kobject_put(rdev->kobj.parent);
813 sysfs_create_link(&rdev2->kobj,
814 &rdev2->bdev->bd_disk->kobj,
815 "block");
816 if (rdev2->raid_disk >= 0) {
817 sprintf(nm, "rd%d", rdev2->raid_disk);
818 sysfs_create_link(&mddev->kobj,
819 &rdev2->kobj, nm);
820 }
821 }
822 /* now set the "removed" and "faulty" bits on any missing devices */
823 for (i=0 ; i < mddev->raid_disks ; i++) {
824 mdp_disk_t *d = &sb->disks[i];
825 if (d->state == 0 && d->number == 0) {
826 d->number = i;
827 d->raid_disk = i;
828 d->state = (1<<MD_DISK_REMOVED);
829 d->state |= (1<<MD_DISK_FAULTY);
830 failed++;
831 }
832 }
833 sb->nr_disks = nr_disks;
834 sb->active_disks = active;
835 sb->working_disks = working;
836 sb->failed_disks = failed;
837 sb->spare_disks = spare;
838
839 sb->this_disk = sb->disks[rdev->desc_nr];
840 sb->sb_csum = calc_sb_csum(sb);
841 }
842
843 /*
844 * version 1 superblock
845 */
846
847 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
848 {
849 unsigned int disk_csum, csum;
850 unsigned long long newcsum;
851 int size = 256 + le32_to_cpu(sb->max_dev)*2;
852 unsigned int *isuper = (unsigned int*)sb;
853 int i;
854
855 disk_csum = sb->sb_csum;
856 sb->sb_csum = 0;
857 newcsum = 0;
858 for (i=0; size>=4; size -= 4 )
859 newcsum += le32_to_cpu(*isuper++);
860
861 if (size == 2)
862 newcsum += le16_to_cpu(*(unsigned short*) isuper);
863
864 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
865 sb->sb_csum = disk_csum;
866 return cpu_to_le32(csum);
867 }
868
869 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
870 {
871 struct mdp_superblock_1 *sb;
872 int ret;
873 sector_t sb_offset;
874 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
875 int bmask;
876
877 /*
878 * Calculate the position of the superblock.
879 * It is always aligned to a 4K boundary and
880 * depeding on minor_version, it can be:
881 * 0: At least 8K, but less than 12K, from end of device
882 * 1: At start of device
883 * 2: 4K from start of device.
884 */
885 switch(minor_version) {
886 case 0:
887 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
888 sb_offset -= 8*2;
889 sb_offset &= ~(sector_t)(4*2-1);
890 /* convert from sectors to K */
891 sb_offset /= 2;
892 break;
893 case 1:
894 sb_offset = 0;
895 break;
896 case 2:
897 sb_offset = 4;
898 break;
899 default:
900 return -EINVAL;
901 }
902 rdev->sb_offset = sb_offset;
903
904 /* superblock is rarely larger than 1K, but it can be larger,
905 * and it is safe to read 4k, so we do that
906 */
907 ret = read_disk_sb(rdev, 4096);
908 if (ret) return ret;
909
910
911 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
912
913 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
914 sb->major_version != cpu_to_le32(1) ||
915 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
916 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
917 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
918 return -EINVAL;
919
920 if (calc_sb_1_csum(sb) != sb->sb_csum) {
921 printk("md: invalid superblock checksum on %s\n",
922 bdevname(rdev->bdev,b));
923 return -EINVAL;
924 }
925 if (le64_to_cpu(sb->data_size) < 10) {
926 printk("md: data_size too small on %s\n",
927 bdevname(rdev->bdev,b));
928 return -EINVAL;
929 }
930 rdev->preferred_minor = 0xffff;
931 rdev->data_offset = le64_to_cpu(sb->data_offset);
932
933 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
934 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
935 if (rdev->sb_size & bmask)
936 rdev-> sb_size = (rdev->sb_size | bmask)+1;
937
938 if (refdev == 0)
939 return 1;
940 else {
941 __u64 ev1, ev2;
942 struct mdp_superblock_1 *refsb =
943 (struct mdp_superblock_1*)page_address(refdev->sb_page);
944
945 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
946 sb->level != refsb->level ||
947 sb->layout != refsb->layout ||
948 sb->chunksize != refsb->chunksize) {
949 printk(KERN_WARNING "md: %s has strangely different"
950 " superblock to %s\n",
951 bdevname(rdev->bdev,b),
952 bdevname(refdev->bdev,b2));
953 return -EINVAL;
954 }
955 ev1 = le64_to_cpu(sb->events);
956 ev2 = le64_to_cpu(refsb->events);
957
958 if (ev1 > ev2)
959 return 1;
960 }
961 if (minor_version)
962 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
963 else
964 rdev->size = rdev->sb_offset;
965 if (rdev->size < le64_to_cpu(sb->data_size)/2)
966 return -EINVAL;
967 rdev->size = le64_to_cpu(sb->data_size)/2;
968 if (le32_to_cpu(sb->chunksize))
969 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
970 return 0;
971 }
972
973 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
974 {
975 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
976
977 rdev->raid_disk = -1;
978 rdev->in_sync = 0;
979 if (mddev->raid_disks == 0) {
980 mddev->major_version = 1;
981 mddev->patch_version = 0;
982 mddev->persistent = 1;
983 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
984 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
985 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
986 mddev->level = le32_to_cpu(sb->level);
987 mddev->layout = le32_to_cpu(sb->layout);
988 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
989 mddev->size = le64_to_cpu(sb->size)/2;
990 mddev->events = le64_to_cpu(sb->events);
991 mddev->bitmap_offset = 0;
992 mddev->default_bitmap_offset = 0;
993 mddev->default_bitmap_offset = 1024;
994
995 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
996 memcpy(mddev->uuid, sb->set_uuid, 16);
997
998 mddev->max_disks = (4096-256)/2;
999
1000 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1001 mddev->bitmap_file == NULL ) {
1002 if (mddev->level != 1) {
1003 printk(KERN_WARNING "md: bitmaps only supported for raid1\n");
1004 return -EINVAL;
1005 }
1006 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1007 }
1008 } else if (mddev->pers == NULL) {
1009 /* Insist of good event counter while assembling */
1010 __u64 ev1 = le64_to_cpu(sb->events);
1011 ++ev1;
1012 if (ev1 < mddev->events)
1013 return -EINVAL;
1014 } else if (mddev->bitmap) {
1015 /* If adding to array with a bitmap, then we can accept an
1016 * older device, but not too old.
1017 */
1018 __u64 ev1 = le64_to_cpu(sb->events);
1019 if (ev1 < mddev->bitmap->events_cleared)
1020 return 0;
1021 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1022 return 0;
1023
1024 if (mddev->level != LEVEL_MULTIPATH) {
1025 int role;
1026 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1027 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1028 switch(role) {
1029 case 0xffff: /* spare */
1030 rdev->faulty = 0;
1031 break;
1032 case 0xfffe: /* faulty */
1033 rdev->faulty = 1;
1034 break;
1035 default:
1036 rdev->in_sync = 1;
1037 rdev->faulty = 0;
1038 rdev->raid_disk = role;
1039 break;
1040 }
1041 rdev->flags = 0;
1042 if (sb->devflags & WriteMostly1)
1043 set_bit(WriteMostly, &rdev->flags);
1044 } else /* MULTIPATH are always insync */
1045 rdev->in_sync = 1;
1046
1047 return 0;
1048 }
1049
1050 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1051 {
1052 struct mdp_superblock_1 *sb;
1053 struct list_head *tmp;
1054 mdk_rdev_t *rdev2;
1055 int max_dev, i;
1056 /* make rdev->sb match mddev and rdev data. */
1057
1058 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1059
1060 sb->feature_map = 0;
1061 sb->pad0 = 0;
1062 memset(sb->pad1, 0, sizeof(sb->pad1));
1063 memset(sb->pad2, 0, sizeof(sb->pad2));
1064 memset(sb->pad3, 0, sizeof(sb->pad3));
1065
1066 sb->utime = cpu_to_le64((__u64)mddev->utime);
1067 sb->events = cpu_to_le64(mddev->events);
1068 if (mddev->in_sync)
1069 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1070 else
1071 sb->resync_offset = cpu_to_le64(0);
1072
1073 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1074 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1075 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1076 }
1077
1078 max_dev = 0;
1079 ITERATE_RDEV(mddev,rdev2,tmp)
1080 if (rdev2->desc_nr+1 > max_dev)
1081 max_dev = rdev2->desc_nr+1;
1082
1083 sb->max_dev = cpu_to_le32(max_dev);
1084 for (i=0; i<max_dev;i++)
1085 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1086
1087 ITERATE_RDEV(mddev,rdev2,tmp) {
1088 i = rdev2->desc_nr;
1089 if (rdev2->faulty)
1090 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1091 else if (rdev2->in_sync)
1092 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1093 else
1094 sb->dev_roles[i] = cpu_to_le16(0xffff);
1095 }
1096
1097 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1098 sb->sb_csum = calc_sb_1_csum(sb);
1099 }
1100
1101
1102 static struct super_type super_types[] = {
1103 [0] = {
1104 .name = "0.90.0",
1105 .owner = THIS_MODULE,
1106 .load_super = super_90_load,
1107 .validate_super = super_90_validate,
1108 .sync_super = super_90_sync,
1109 },
1110 [1] = {
1111 .name = "md-1",
1112 .owner = THIS_MODULE,
1113 .load_super = super_1_load,
1114 .validate_super = super_1_validate,
1115 .sync_super = super_1_sync,
1116 },
1117 };
1118
1119 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1120 {
1121 struct list_head *tmp;
1122 mdk_rdev_t *rdev;
1123
1124 ITERATE_RDEV(mddev,rdev,tmp)
1125 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1126 return rdev;
1127
1128 return NULL;
1129 }
1130
1131 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1132 {
1133 struct list_head *tmp;
1134 mdk_rdev_t *rdev;
1135
1136 ITERATE_RDEV(mddev1,rdev,tmp)
1137 if (match_dev_unit(mddev2, rdev))
1138 return 1;
1139
1140 return 0;
1141 }
1142
1143 static LIST_HEAD(pending_raid_disks);
1144
1145 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1146 {
1147 mdk_rdev_t *same_pdev;
1148 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1149
1150 if (rdev->mddev) {
1151 MD_BUG();
1152 return -EINVAL;
1153 }
1154 same_pdev = match_dev_unit(mddev, rdev);
1155 if (same_pdev)
1156 printk(KERN_WARNING
1157 "%s: WARNING: %s appears to be on the same physical"
1158 " disk as %s. True\n protection against single-disk"
1159 " failure might be compromised.\n",
1160 mdname(mddev), bdevname(rdev->bdev,b),
1161 bdevname(same_pdev->bdev,b2));
1162
1163 /* Verify rdev->desc_nr is unique.
1164 * If it is -1, assign a free number, else
1165 * check number is not in use
1166 */
1167 if (rdev->desc_nr < 0) {
1168 int choice = 0;
1169 if (mddev->pers) choice = mddev->raid_disks;
1170 while (find_rdev_nr(mddev, choice))
1171 choice++;
1172 rdev->desc_nr = choice;
1173 } else {
1174 if (find_rdev_nr(mddev, rdev->desc_nr))
1175 return -EBUSY;
1176 }
1177
1178 list_add(&rdev->same_set, &mddev->disks);
1179 rdev->mddev = mddev;
1180 printk(KERN_INFO "md: bind<%s>\n", bdevname(rdev->bdev,b));
1181
1182 rdev->kobj.k_name = NULL;
1183 snprintf(rdev->kobj.name, KOBJ_NAME_LEN, "dev%d", rdev->desc_nr);
1184 rdev->kobj.parent = &mddev->kobj;
1185 kobject_add(&rdev->kobj);
1186
1187 sysfs_create_link(&rdev->kobj, &rdev->bdev->bd_disk->kobj, "block");
1188 return 0;
1189 }
1190
1191 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1192 {
1193 char b[BDEVNAME_SIZE];
1194 if (!rdev->mddev) {
1195 MD_BUG();
1196 return;
1197 }
1198 list_del_init(&rdev->same_set);
1199 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1200 rdev->mddev = NULL;
1201 sysfs_remove_link(&rdev->kobj, "block");
1202 kobject_del(&rdev->kobj);
1203 }
1204
1205 /*
1206 * prevent the device from being mounted, repartitioned or
1207 * otherwise reused by a RAID array (or any other kernel
1208 * subsystem), by bd_claiming the device.
1209 */
1210 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1211 {
1212 int err = 0;
1213 struct block_device *bdev;
1214 char b[BDEVNAME_SIZE];
1215
1216 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1217 if (IS_ERR(bdev)) {
1218 printk(KERN_ERR "md: could not open %s.\n",
1219 __bdevname(dev, b));
1220 return PTR_ERR(bdev);
1221 }
1222 err = bd_claim(bdev, rdev);
1223 if (err) {
1224 printk(KERN_ERR "md: could not bd_claim %s.\n",
1225 bdevname(bdev, b));
1226 blkdev_put(bdev);
1227 return err;
1228 }
1229 rdev->bdev = bdev;
1230 return err;
1231 }
1232
1233 static void unlock_rdev(mdk_rdev_t *rdev)
1234 {
1235 struct block_device *bdev = rdev->bdev;
1236 rdev->bdev = NULL;
1237 if (!bdev)
1238 MD_BUG();
1239 bd_release(bdev);
1240 blkdev_put(bdev);
1241 }
1242
1243 void md_autodetect_dev(dev_t dev);
1244
1245 static void export_rdev(mdk_rdev_t * rdev)
1246 {
1247 char b[BDEVNAME_SIZE];
1248 printk(KERN_INFO "md: export_rdev(%s)\n",
1249 bdevname(rdev->bdev,b));
1250 if (rdev->mddev)
1251 MD_BUG();
1252 free_disk_sb(rdev);
1253 list_del_init(&rdev->same_set);
1254 #ifndef MODULE
1255 md_autodetect_dev(rdev->bdev->bd_dev);
1256 #endif
1257 unlock_rdev(rdev);
1258 kobject_put(&rdev->kobj);
1259 }
1260
1261 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1262 {
1263 unbind_rdev_from_array(rdev);
1264 export_rdev(rdev);
1265 }
1266
1267 static void export_array(mddev_t *mddev)
1268 {
1269 struct list_head *tmp;
1270 mdk_rdev_t *rdev;
1271
1272 ITERATE_RDEV(mddev,rdev,tmp) {
1273 if (!rdev->mddev) {
1274 MD_BUG();
1275 continue;
1276 }
1277 kick_rdev_from_array(rdev);
1278 }
1279 if (!list_empty(&mddev->disks))
1280 MD_BUG();
1281 mddev->raid_disks = 0;
1282 mddev->major_version = 0;
1283 }
1284
1285 static void print_desc(mdp_disk_t *desc)
1286 {
1287 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1288 desc->major,desc->minor,desc->raid_disk,desc->state);
1289 }
1290
1291 static void print_sb(mdp_super_t *sb)
1292 {
1293 int i;
1294
1295 printk(KERN_INFO
1296 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1297 sb->major_version, sb->minor_version, sb->patch_version,
1298 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1299 sb->ctime);
1300 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1301 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1302 sb->md_minor, sb->layout, sb->chunk_size);
1303 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1304 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1305 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1306 sb->failed_disks, sb->spare_disks,
1307 sb->sb_csum, (unsigned long)sb->events_lo);
1308
1309 printk(KERN_INFO);
1310 for (i = 0; i < MD_SB_DISKS; i++) {
1311 mdp_disk_t *desc;
1312
1313 desc = sb->disks + i;
1314 if (desc->number || desc->major || desc->minor ||
1315 desc->raid_disk || (desc->state && (desc->state != 4))) {
1316 printk(" D %2d: ", i);
1317 print_desc(desc);
1318 }
1319 }
1320 printk(KERN_INFO "md: THIS: ");
1321 print_desc(&sb->this_disk);
1322
1323 }
1324
1325 static void print_rdev(mdk_rdev_t *rdev)
1326 {
1327 char b[BDEVNAME_SIZE];
1328 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1329 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1330 rdev->faulty, rdev->in_sync, rdev->desc_nr);
1331 if (rdev->sb_loaded) {
1332 printk(KERN_INFO "md: rdev superblock:\n");
1333 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1334 } else
1335 printk(KERN_INFO "md: no rdev superblock!\n");
1336 }
1337
1338 void md_print_devices(void)
1339 {
1340 struct list_head *tmp, *tmp2;
1341 mdk_rdev_t *rdev;
1342 mddev_t *mddev;
1343 char b[BDEVNAME_SIZE];
1344
1345 printk("\n");
1346 printk("md: **********************************\n");
1347 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1348 printk("md: **********************************\n");
1349 ITERATE_MDDEV(mddev,tmp) {
1350
1351 if (mddev->bitmap)
1352 bitmap_print_sb(mddev->bitmap);
1353 else
1354 printk("%s: ", mdname(mddev));
1355 ITERATE_RDEV(mddev,rdev,tmp2)
1356 printk("<%s>", bdevname(rdev->bdev,b));
1357 printk("\n");
1358
1359 ITERATE_RDEV(mddev,rdev,tmp2)
1360 print_rdev(rdev);
1361 }
1362 printk("md: **********************************\n");
1363 printk("\n");
1364 }
1365
1366
1367 static void sync_sbs(mddev_t * mddev)
1368 {
1369 mdk_rdev_t *rdev;
1370 struct list_head *tmp;
1371
1372 ITERATE_RDEV(mddev,rdev,tmp) {
1373 super_types[mddev->major_version].
1374 sync_super(mddev, rdev);
1375 rdev->sb_loaded = 1;
1376 }
1377 }
1378
1379 static void md_update_sb(mddev_t * mddev)
1380 {
1381 int err;
1382 struct list_head *tmp;
1383 mdk_rdev_t *rdev;
1384 int sync_req;
1385
1386 repeat:
1387 spin_lock(&mddev->write_lock);
1388 sync_req = mddev->in_sync;
1389 mddev->utime = get_seconds();
1390 mddev->events ++;
1391
1392 if (!mddev->events) {
1393 /*
1394 * oops, this 64-bit counter should never wrap.
1395 * Either we are in around ~1 trillion A.C., assuming
1396 * 1 reboot per second, or we have a bug:
1397 */
1398 MD_BUG();
1399 mddev->events --;
1400 }
1401 mddev->sb_dirty = 2;
1402 sync_sbs(mddev);
1403
1404 /*
1405 * do not write anything to disk if using
1406 * nonpersistent superblocks
1407 */
1408 if (!mddev->persistent) {
1409 mddev->sb_dirty = 0;
1410 spin_unlock(&mddev->write_lock);
1411 wake_up(&mddev->sb_wait);
1412 return;
1413 }
1414 spin_unlock(&mddev->write_lock);
1415
1416 dprintk(KERN_INFO
1417 "md: updating %s RAID superblock on device (in sync %d)\n",
1418 mdname(mddev),mddev->in_sync);
1419
1420 err = bitmap_update_sb(mddev->bitmap);
1421 ITERATE_RDEV(mddev,rdev,tmp) {
1422 char b[BDEVNAME_SIZE];
1423 dprintk(KERN_INFO "md: ");
1424 if (rdev->faulty)
1425 dprintk("(skipping faulty ");
1426
1427 dprintk("%s ", bdevname(rdev->bdev,b));
1428 if (!rdev->faulty) {
1429 md_super_write(mddev,rdev,
1430 rdev->sb_offset<<1, rdev->sb_size,
1431 rdev->sb_page);
1432 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1433 bdevname(rdev->bdev,b),
1434 (unsigned long long)rdev->sb_offset);
1435
1436 } else
1437 dprintk(")\n");
1438 if (mddev->level == LEVEL_MULTIPATH)
1439 /* only need to write one superblock... */
1440 break;
1441 }
1442 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
1443 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1444
1445 spin_lock(&mddev->write_lock);
1446 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
1447 /* have to write it out again */
1448 spin_unlock(&mddev->write_lock);
1449 goto repeat;
1450 }
1451 mddev->sb_dirty = 0;
1452 spin_unlock(&mddev->write_lock);
1453 wake_up(&mddev->sb_wait);
1454
1455 }
1456
1457 struct rdev_sysfs_entry {
1458 struct attribute attr;
1459 ssize_t (*show)(mdk_rdev_t *, char *);
1460 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1461 };
1462
1463 static ssize_t
1464 rdev_show_state(mdk_rdev_t *rdev, char *page)
1465 {
1466 char *sep = "";
1467 int len=0;
1468
1469 if (rdev->faulty) {
1470 len+= sprintf(page+len, "%sfaulty",sep);
1471 sep = ",";
1472 }
1473 if (rdev->in_sync) {
1474 len += sprintf(page+len, "%sin_sync",sep);
1475 sep = ",";
1476 }
1477 if (!rdev->faulty && !rdev->in_sync) {
1478 len += sprintf(page+len, "%sspare", sep);
1479 sep = ",";
1480 }
1481 return len+sprintf(page+len, "\n");
1482 }
1483
1484 static struct rdev_sysfs_entry rdev_state = {
1485 .attr = {.name = "state", .mode = S_IRUGO },
1486 .show = rdev_show_state,
1487 };
1488
1489 static ssize_t
1490 rdev_show_super(mdk_rdev_t *rdev, char *page)
1491 {
1492 if (rdev->sb_loaded && rdev->sb_size) {
1493 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1494 return rdev->sb_size;
1495 } else
1496 return 0;
1497 }
1498 static struct rdev_sysfs_entry rdev_super = {
1499 .attr = {.name = "super", .mode = S_IRUGO },
1500 .show = rdev_show_super,
1501 };
1502 static struct attribute *rdev_default_attrs[] = {
1503 &rdev_state.attr,
1504 &rdev_super.attr,
1505 NULL,
1506 };
1507 static ssize_t
1508 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1509 {
1510 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1511 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1512
1513 if (!entry->show)
1514 return -EIO;
1515 return entry->show(rdev, page);
1516 }
1517
1518 static ssize_t
1519 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1520 const char *page, size_t length)
1521 {
1522 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1523 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1524
1525 if (!entry->store)
1526 return -EIO;
1527 return entry->store(rdev, page, length);
1528 }
1529
1530 static void rdev_free(struct kobject *ko)
1531 {
1532 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1533 kfree(rdev);
1534 }
1535 static struct sysfs_ops rdev_sysfs_ops = {
1536 .show = rdev_attr_show,
1537 .store = rdev_attr_store,
1538 };
1539 static struct kobj_type rdev_ktype = {
1540 .release = rdev_free,
1541 .sysfs_ops = &rdev_sysfs_ops,
1542 .default_attrs = rdev_default_attrs,
1543 };
1544
1545 /*
1546 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1547 *
1548 * mark the device faulty if:
1549 *
1550 * - the device is nonexistent (zero size)
1551 * - the device has no valid superblock
1552 *
1553 * a faulty rdev _never_ has rdev->sb set.
1554 */
1555 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1556 {
1557 char b[BDEVNAME_SIZE];
1558 int err;
1559 mdk_rdev_t *rdev;
1560 sector_t size;
1561
1562 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1563 if (!rdev) {
1564 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1565 return ERR_PTR(-ENOMEM);
1566 }
1567 memset(rdev, 0, sizeof(*rdev));
1568
1569 if ((err = alloc_disk_sb(rdev)))
1570 goto abort_free;
1571
1572 err = lock_rdev(rdev, newdev);
1573 if (err)
1574 goto abort_free;
1575
1576 rdev->kobj.parent = NULL;
1577 rdev->kobj.ktype = &rdev_ktype;
1578 kobject_init(&rdev->kobj);
1579
1580 rdev->desc_nr = -1;
1581 rdev->faulty = 0;
1582 rdev->in_sync = 0;
1583 rdev->data_offset = 0;
1584 atomic_set(&rdev->nr_pending, 0);
1585 atomic_set(&rdev->read_errors, 0);
1586
1587 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1588 if (!size) {
1589 printk(KERN_WARNING
1590 "md: %s has zero or unknown size, marking faulty!\n",
1591 bdevname(rdev->bdev,b));
1592 err = -EINVAL;
1593 goto abort_free;
1594 }
1595
1596 if (super_format >= 0) {
1597 err = super_types[super_format].
1598 load_super(rdev, NULL, super_minor);
1599 if (err == -EINVAL) {
1600 printk(KERN_WARNING
1601 "md: %s has invalid sb, not importing!\n",
1602 bdevname(rdev->bdev,b));
1603 goto abort_free;
1604 }
1605 if (err < 0) {
1606 printk(KERN_WARNING
1607 "md: could not read %s's sb, not importing!\n",
1608 bdevname(rdev->bdev,b));
1609 goto abort_free;
1610 }
1611 }
1612 INIT_LIST_HEAD(&rdev->same_set);
1613
1614 return rdev;
1615
1616 abort_free:
1617 if (rdev->sb_page) {
1618 if (rdev->bdev)
1619 unlock_rdev(rdev);
1620 free_disk_sb(rdev);
1621 }
1622 kfree(rdev);
1623 return ERR_PTR(err);
1624 }
1625
1626 /*
1627 * Check a full RAID array for plausibility
1628 */
1629
1630
1631 static void analyze_sbs(mddev_t * mddev)
1632 {
1633 int i;
1634 struct list_head *tmp;
1635 mdk_rdev_t *rdev, *freshest;
1636 char b[BDEVNAME_SIZE];
1637
1638 freshest = NULL;
1639 ITERATE_RDEV(mddev,rdev,tmp)
1640 switch (super_types[mddev->major_version].
1641 load_super(rdev, freshest, mddev->minor_version)) {
1642 case 1:
1643 freshest = rdev;
1644 break;
1645 case 0:
1646 break;
1647 default:
1648 printk( KERN_ERR \
1649 "md: fatal superblock inconsistency in %s"
1650 " -- removing from array\n",
1651 bdevname(rdev->bdev,b));
1652 kick_rdev_from_array(rdev);
1653 }
1654
1655
1656 super_types[mddev->major_version].
1657 validate_super(mddev, freshest);
1658
1659 i = 0;
1660 ITERATE_RDEV(mddev,rdev,tmp) {
1661 if (rdev != freshest)
1662 if (super_types[mddev->major_version].
1663 validate_super(mddev, rdev)) {
1664 printk(KERN_WARNING "md: kicking non-fresh %s"
1665 " from array!\n",
1666 bdevname(rdev->bdev,b));
1667 kick_rdev_from_array(rdev);
1668 continue;
1669 }
1670 if (mddev->level == LEVEL_MULTIPATH) {
1671 rdev->desc_nr = i++;
1672 rdev->raid_disk = rdev->desc_nr;
1673 rdev->in_sync = 1;
1674 }
1675 }
1676
1677
1678
1679 if (mddev->recovery_cp != MaxSector &&
1680 mddev->level >= 1)
1681 printk(KERN_ERR "md: %s: raid array is not clean"
1682 " -- starting background reconstruction\n",
1683 mdname(mddev));
1684
1685 }
1686
1687 static ssize_t
1688 md_show_level(mddev_t *mddev, char *page)
1689 {
1690 mdk_personality_t *p = mddev->pers;
1691 if (p == NULL)
1692 return 0;
1693 if (mddev->level >= 0)
1694 return sprintf(page, "RAID-%d\n", mddev->level);
1695 else
1696 return sprintf(page, "%s\n", p->name);
1697 }
1698
1699 static struct md_sysfs_entry md_level = {
1700 .attr = {.name = "level", .mode = S_IRUGO },
1701 .show = md_show_level,
1702 };
1703
1704 static ssize_t
1705 md_show_rdisks(mddev_t *mddev, char *page)
1706 {
1707 return sprintf(page, "%d\n", mddev->raid_disks);
1708 }
1709
1710 static struct md_sysfs_entry md_raid_disks = {
1711 .attr = {.name = "raid_disks", .mode = S_IRUGO },
1712 .show = md_show_rdisks,
1713 };
1714
1715 static ssize_t
1716 md_show_scan(mddev_t *mddev, char *page)
1717 {
1718 char *type = "none";
1719 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1720 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
1721 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1722 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1723 type = "resync";
1724 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1725 type = "check";
1726 else
1727 type = "repair";
1728 } else
1729 type = "recover";
1730 }
1731 return sprintf(page, "%s\n", type);
1732 }
1733
1734 static ssize_t
1735 md_store_scan(mddev_t *mddev, const char *page, size_t len)
1736 {
1737 int canscan=0;
1738
1739 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1740 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
1741 return -EBUSY;
1742 down(&mddev->reconfig_sem);
1743 if (mddev->pers && mddev->pers->sync_request)
1744 canscan=1;
1745 up(&mddev->reconfig_sem);
1746 if (!canscan)
1747 return -EINVAL;
1748
1749 if (strcmp(page, "check")==0 || strcmp(page, "check\n")==0)
1750 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1751 else if (strcmp(page, "repair")!=0 && strcmp(page, "repair\n")!=0)
1752 return -EINVAL;
1753 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1754 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1755 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1756 md_wakeup_thread(mddev->thread);
1757 return len;
1758 }
1759
1760 static ssize_t
1761 md_show_mismatch(mddev_t *mddev, char *page)
1762 {
1763 return sprintf(page, "%llu\n",
1764 (unsigned long long) mddev->resync_mismatches);
1765 }
1766
1767 static struct md_sysfs_entry md_scan_mode = {
1768 .attr = {.name = "scan_mode", .mode = S_IRUGO|S_IWUSR },
1769 .show = md_show_scan,
1770 .store = md_store_scan,
1771 };
1772
1773 static struct md_sysfs_entry md_mismatches = {
1774 .attr = {.name = "mismatch_cnt", .mode = S_IRUGO },
1775 .show = md_show_mismatch,
1776 };
1777
1778 static struct attribute *md_default_attrs[] = {
1779 &md_level.attr,
1780 &md_raid_disks.attr,
1781 &md_scan_mode.attr,
1782 &md_mismatches.attr,
1783 NULL,
1784 };
1785
1786 static ssize_t
1787 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1788 {
1789 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1790 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1791
1792 if (!entry->show)
1793 return -EIO;
1794 return entry->show(mddev, page);
1795 }
1796
1797 static ssize_t
1798 md_attr_store(struct kobject *kobj, struct attribute *attr,
1799 const char *page, size_t length)
1800 {
1801 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1802 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1803
1804 if (!entry->store)
1805 return -EIO;
1806 return entry->store(mddev, page, length);
1807 }
1808
1809 static void md_free(struct kobject *ko)
1810 {
1811 mddev_t *mddev = container_of(ko, mddev_t, kobj);
1812 kfree(mddev);
1813 }
1814
1815 static struct sysfs_ops md_sysfs_ops = {
1816 .show = md_attr_show,
1817 .store = md_attr_store,
1818 };
1819 static struct kobj_type md_ktype = {
1820 .release = md_free,
1821 .sysfs_ops = &md_sysfs_ops,
1822 .default_attrs = md_default_attrs,
1823 };
1824
1825 int mdp_major = 0;
1826
1827 static struct kobject *md_probe(dev_t dev, int *part, void *data)
1828 {
1829 static DECLARE_MUTEX(disks_sem);
1830 mddev_t *mddev = mddev_find(dev);
1831 struct gendisk *disk;
1832 int partitioned = (MAJOR(dev) != MD_MAJOR);
1833 int shift = partitioned ? MdpMinorShift : 0;
1834 int unit = MINOR(dev) >> shift;
1835
1836 if (!mddev)
1837 return NULL;
1838
1839 down(&disks_sem);
1840 if (mddev->gendisk) {
1841 up(&disks_sem);
1842 mddev_put(mddev);
1843 return NULL;
1844 }
1845 disk = alloc_disk(1 << shift);
1846 if (!disk) {
1847 up(&disks_sem);
1848 mddev_put(mddev);
1849 return NULL;
1850 }
1851 disk->major = MAJOR(dev);
1852 disk->first_minor = unit << shift;
1853 if (partitioned) {
1854 sprintf(disk->disk_name, "md_d%d", unit);
1855 sprintf(disk->devfs_name, "md/d%d", unit);
1856 } else {
1857 sprintf(disk->disk_name, "md%d", unit);
1858 sprintf(disk->devfs_name, "md/%d", unit);
1859 }
1860 disk->fops = &md_fops;
1861 disk->private_data = mddev;
1862 disk->queue = mddev->queue;
1863 add_disk(disk);
1864 mddev->gendisk = disk;
1865 up(&disks_sem);
1866 mddev->kobj.parent = &disk->kobj;
1867 mddev->kobj.k_name = NULL;
1868 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
1869 mddev->kobj.ktype = &md_ktype;
1870 kobject_register(&mddev->kobj);
1871 return NULL;
1872 }
1873
1874 void md_wakeup_thread(mdk_thread_t *thread);
1875
1876 static void md_safemode_timeout(unsigned long data)
1877 {
1878 mddev_t *mddev = (mddev_t *) data;
1879
1880 mddev->safemode = 1;
1881 md_wakeup_thread(mddev->thread);
1882 }
1883
1884
1885 static int do_md_run(mddev_t * mddev)
1886 {
1887 int pnum, err;
1888 int chunk_size;
1889 struct list_head *tmp;
1890 mdk_rdev_t *rdev;
1891 struct gendisk *disk;
1892 char b[BDEVNAME_SIZE];
1893
1894 if (list_empty(&mddev->disks))
1895 /* cannot run an array with no devices.. */
1896 return -EINVAL;
1897
1898 if (mddev->pers)
1899 return -EBUSY;
1900
1901 /*
1902 * Analyze all RAID superblock(s)
1903 */
1904 if (!mddev->raid_disks)
1905 analyze_sbs(mddev);
1906
1907 chunk_size = mddev->chunk_size;
1908 pnum = level_to_pers(mddev->level);
1909
1910 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1911 if (!chunk_size) {
1912 /*
1913 * 'default chunksize' in the old md code used to
1914 * be PAGE_SIZE, baaad.
1915 * we abort here to be on the safe side. We don't
1916 * want to continue the bad practice.
1917 */
1918 printk(KERN_ERR
1919 "no chunksize specified, see 'man raidtab'\n");
1920 return -EINVAL;
1921 }
1922 if (chunk_size > MAX_CHUNK_SIZE) {
1923 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1924 chunk_size, MAX_CHUNK_SIZE);
1925 return -EINVAL;
1926 }
1927 /*
1928 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1929 */
1930 if ( (1 << ffz(~chunk_size)) != chunk_size) {
1931 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
1932 return -EINVAL;
1933 }
1934 if (chunk_size < PAGE_SIZE) {
1935 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1936 chunk_size, PAGE_SIZE);
1937 return -EINVAL;
1938 }
1939
1940 /* devices must have minimum size of one chunk */
1941 ITERATE_RDEV(mddev,rdev,tmp) {
1942 if (rdev->faulty)
1943 continue;
1944 if (rdev->size < chunk_size / 1024) {
1945 printk(KERN_WARNING
1946 "md: Dev %s smaller than chunk_size:"
1947 " %lluk < %dk\n",
1948 bdevname(rdev->bdev,b),
1949 (unsigned long long)rdev->size,
1950 chunk_size / 1024);
1951 return -EINVAL;
1952 }
1953 }
1954 }
1955
1956 #ifdef CONFIG_KMOD
1957 if (!pers[pnum])
1958 {
1959 request_module("md-personality-%d", pnum);
1960 }
1961 #endif
1962
1963 /*
1964 * Drop all container device buffers, from now on
1965 * the only valid external interface is through the md
1966 * device.
1967 * Also find largest hardsector size
1968 */
1969 ITERATE_RDEV(mddev,rdev,tmp) {
1970 if (rdev->faulty)
1971 continue;
1972 sync_blockdev(rdev->bdev);
1973 invalidate_bdev(rdev->bdev, 0);
1974 }
1975
1976 md_probe(mddev->unit, NULL, NULL);
1977 disk = mddev->gendisk;
1978 if (!disk)
1979 return -ENOMEM;
1980
1981 spin_lock(&pers_lock);
1982 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
1983 spin_unlock(&pers_lock);
1984 printk(KERN_WARNING "md: personality %d is not loaded!\n",
1985 pnum);
1986 return -EINVAL;
1987 }
1988
1989 mddev->pers = pers[pnum];
1990 spin_unlock(&pers_lock);
1991
1992 mddev->recovery = 0;
1993 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
1994
1995 /* before we start the array running, initialise the bitmap */
1996 err = bitmap_create(mddev);
1997 if (err)
1998 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
1999 mdname(mddev), err);
2000 else
2001 err = mddev->pers->run(mddev);
2002 if (err) {
2003 printk(KERN_ERR "md: pers->run() failed ...\n");
2004 module_put(mddev->pers->owner);
2005 mddev->pers = NULL;
2006 bitmap_destroy(mddev);
2007 return err;
2008 }
2009 atomic_set(&mddev->writes_pending,0);
2010 mddev->safemode = 0;
2011 mddev->safemode_timer.function = md_safemode_timeout;
2012 mddev->safemode_timer.data = (unsigned long) mddev;
2013 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2014 mddev->in_sync = 1;
2015
2016 ITERATE_RDEV(mddev,rdev,tmp)
2017 if (rdev->raid_disk >= 0) {
2018 char nm[20];
2019 sprintf(nm, "rd%d", rdev->raid_disk);
2020 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2021 }
2022
2023 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2024 md_wakeup_thread(mddev->thread);
2025
2026 if (mddev->sb_dirty)
2027 md_update_sb(mddev);
2028
2029 set_capacity(disk, mddev->array_size<<1);
2030
2031 /* If we call blk_queue_make_request here, it will
2032 * re-initialise max_sectors etc which may have been
2033 * refined inside -> run. So just set the bits we need to set.
2034 * Most initialisation happended when we called
2035 * blk_queue_make_request(..., md_fail_request)
2036 * earlier.
2037 */
2038 mddev->queue->queuedata = mddev;
2039 mddev->queue->make_request_fn = mddev->pers->make_request;
2040
2041 mddev->changed = 1;
2042 return 0;
2043 }
2044
2045 static int restart_array(mddev_t *mddev)
2046 {
2047 struct gendisk *disk = mddev->gendisk;
2048 int err;
2049
2050 /*
2051 * Complain if it has no devices
2052 */
2053 err = -ENXIO;
2054 if (list_empty(&mddev->disks))
2055 goto out;
2056
2057 if (mddev->pers) {
2058 err = -EBUSY;
2059 if (!mddev->ro)
2060 goto out;
2061
2062 mddev->safemode = 0;
2063 mddev->ro = 0;
2064 set_disk_ro(disk, 0);
2065
2066 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2067 mdname(mddev));
2068 /*
2069 * Kick recovery or resync if necessary
2070 */
2071 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2072 md_wakeup_thread(mddev->thread);
2073 err = 0;
2074 } else {
2075 printk(KERN_ERR "md: %s has no personality assigned.\n",
2076 mdname(mddev));
2077 err = -EINVAL;
2078 }
2079
2080 out:
2081 return err;
2082 }
2083
2084 static int do_md_stop(mddev_t * mddev, int ro)
2085 {
2086 int err = 0;
2087 struct gendisk *disk = mddev->gendisk;
2088
2089 if (mddev->pers) {
2090 if (atomic_read(&mddev->active)>2) {
2091 printk("md: %s still in use.\n",mdname(mddev));
2092 return -EBUSY;
2093 }
2094
2095 if (mddev->sync_thread) {
2096 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2097 md_unregister_thread(mddev->sync_thread);
2098 mddev->sync_thread = NULL;
2099 }
2100
2101 del_timer_sync(&mddev->safemode_timer);
2102
2103 invalidate_partition(disk, 0);
2104
2105 if (ro) {
2106 err = -ENXIO;
2107 if (mddev->ro)
2108 goto out;
2109 mddev->ro = 1;
2110 } else {
2111 bitmap_flush(mddev);
2112 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
2113 if (mddev->ro)
2114 set_disk_ro(disk, 0);
2115 blk_queue_make_request(mddev->queue, md_fail_request);
2116 mddev->pers->stop(mddev);
2117 module_put(mddev->pers->owner);
2118 mddev->pers = NULL;
2119 if (mddev->ro)
2120 mddev->ro = 0;
2121 }
2122 if (!mddev->in_sync) {
2123 /* mark array as shutdown cleanly */
2124 mddev->in_sync = 1;
2125 md_update_sb(mddev);
2126 }
2127 if (ro)
2128 set_disk_ro(disk, 1);
2129 }
2130
2131 bitmap_destroy(mddev);
2132 if (mddev->bitmap_file) {
2133 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2134 fput(mddev->bitmap_file);
2135 mddev->bitmap_file = NULL;
2136 }
2137 mddev->bitmap_offset = 0;
2138
2139 /*
2140 * Free resources if final stop
2141 */
2142 if (!ro) {
2143 mdk_rdev_t *rdev;
2144 struct list_head *tmp;
2145 struct gendisk *disk;
2146 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2147
2148 ITERATE_RDEV(mddev,rdev,tmp)
2149 if (rdev->raid_disk >= 0) {
2150 char nm[20];
2151 sprintf(nm, "rd%d", rdev->raid_disk);
2152 sysfs_remove_link(&mddev->kobj, nm);
2153 }
2154
2155 export_array(mddev);
2156
2157 mddev->array_size = 0;
2158 disk = mddev->gendisk;
2159 if (disk)
2160 set_capacity(disk, 0);
2161 mddev->changed = 1;
2162 } else
2163 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2164 mdname(mddev));
2165 err = 0;
2166 out:
2167 return err;
2168 }
2169
2170 static void autorun_array(mddev_t *mddev)
2171 {
2172 mdk_rdev_t *rdev;
2173 struct list_head *tmp;
2174 int err;
2175
2176 if (list_empty(&mddev->disks))
2177 return;
2178
2179 printk(KERN_INFO "md: running: ");
2180
2181 ITERATE_RDEV(mddev,rdev,tmp) {
2182 char b[BDEVNAME_SIZE];
2183 printk("<%s>", bdevname(rdev->bdev,b));
2184 }
2185 printk("\n");
2186
2187 err = do_md_run (mddev);
2188 if (err) {
2189 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2190 do_md_stop (mddev, 0);
2191 }
2192 }
2193
2194 /*
2195 * lets try to run arrays based on all disks that have arrived
2196 * until now. (those are in pending_raid_disks)
2197 *
2198 * the method: pick the first pending disk, collect all disks with
2199 * the same UUID, remove all from the pending list and put them into
2200 * the 'same_array' list. Then order this list based on superblock
2201 * update time (freshest comes first), kick out 'old' disks and
2202 * compare superblocks. If everything's fine then run it.
2203 *
2204 * If "unit" is allocated, then bump its reference count
2205 */
2206 static void autorun_devices(int part)
2207 {
2208 struct list_head candidates;
2209 struct list_head *tmp;
2210 mdk_rdev_t *rdev0, *rdev;
2211 mddev_t *mddev;
2212 char b[BDEVNAME_SIZE];
2213
2214 printk(KERN_INFO "md: autorun ...\n");
2215 while (!list_empty(&pending_raid_disks)) {
2216 dev_t dev;
2217 rdev0 = list_entry(pending_raid_disks.next,
2218 mdk_rdev_t, same_set);
2219
2220 printk(KERN_INFO "md: considering %s ...\n",
2221 bdevname(rdev0->bdev,b));
2222 INIT_LIST_HEAD(&candidates);
2223 ITERATE_RDEV_PENDING(rdev,tmp)
2224 if (super_90_load(rdev, rdev0, 0) >= 0) {
2225 printk(KERN_INFO "md: adding %s ...\n",
2226 bdevname(rdev->bdev,b));
2227 list_move(&rdev->same_set, &candidates);
2228 }
2229 /*
2230 * now we have a set of devices, with all of them having
2231 * mostly sane superblocks. It's time to allocate the
2232 * mddev.
2233 */
2234 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2235 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2236 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2237 break;
2238 }
2239 if (part)
2240 dev = MKDEV(mdp_major,
2241 rdev0->preferred_minor << MdpMinorShift);
2242 else
2243 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2244
2245 md_probe(dev, NULL, NULL);
2246 mddev = mddev_find(dev);
2247 if (!mddev) {
2248 printk(KERN_ERR
2249 "md: cannot allocate memory for md drive.\n");
2250 break;
2251 }
2252 if (mddev_lock(mddev))
2253 printk(KERN_WARNING "md: %s locked, cannot run\n",
2254 mdname(mddev));
2255 else if (mddev->raid_disks || mddev->major_version
2256 || !list_empty(&mddev->disks)) {
2257 printk(KERN_WARNING
2258 "md: %s already running, cannot run %s\n",
2259 mdname(mddev), bdevname(rdev0->bdev,b));
2260 mddev_unlock(mddev);
2261 } else {
2262 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2263 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2264 list_del_init(&rdev->same_set);
2265 if (bind_rdev_to_array(rdev, mddev))
2266 export_rdev(rdev);
2267 }
2268 autorun_array(mddev);
2269 mddev_unlock(mddev);
2270 }
2271 /* on success, candidates will be empty, on error
2272 * it won't...
2273 */
2274 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2275 export_rdev(rdev);
2276 mddev_put(mddev);
2277 }
2278 printk(KERN_INFO "md: ... autorun DONE.\n");
2279 }
2280
2281 /*
2282 * import RAID devices based on one partition
2283 * if possible, the array gets run as well.
2284 */
2285
2286 static int autostart_array(dev_t startdev)
2287 {
2288 char b[BDEVNAME_SIZE];
2289 int err = -EINVAL, i;
2290 mdp_super_t *sb = NULL;
2291 mdk_rdev_t *start_rdev = NULL, *rdev;
2292
2293 start_rdev = md_import_device(startdev, 0, 0);
2294 if (IS_ERR(start_rdev))
2295 return err;
2296
2297
2298 /* NOTE: this can only work for 0.90.0 superblocks */
2299 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2300 if (sb->major_version != 0 ||
2301 sb->minor_version != 90 ) {
2302 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2303 export_rdev(start_rdev);
2304 return err;
2305 }
2306
2307 if (start_rdev->faulty) {
2308 printk(KERN_WARNING
2309 "md: can not autostart based on faulty %s!\n",
2310 bdevname(start_rdev->bdev,b));
2311 export_rdev(start_rdev);
2312 return err;
2313 }
2314 list_add(&start_rdev->same_set, &pending_raid_disks);
2315
2316 for (i = 0; i < MD_SB_DISKS; i++) {
2317 mdp_disk_t *desc = sb->disks + i;
2318 dev_t dev = MKDEV(desc->major, desc->minor);
2319
2320 if (!dev)
2321 continue;
2322 if (dev == startdev)
2323 continue;
2324 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2325 continue;
2326 rdev = md_import_device(dev, 0, 0);
2327 if (IS_ERR(rdev))
2328 continue;
2329
2330 list_add(&rdev->same_set, &pending_raid_disks);
2331 }
2332
2333 /*
2334 * possibly return codes
2335 */
2336 autorun_devices(0);
2337 return 0;
2338
2339 }
2340
2341
2342 static int get_version(void __user * arg)
2343 {
2344 mdu_version_t ver;
2345
2346 ver.major = MD_MAJOR_VERSION;
2347 ver.minor = MD_MINOR_VERSION;
2348 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2349
2350 if (copy_to_user(arg, &ver, sizeof(ver)))
2351 return -EFAULT;
2352
2353 return 0;
2354 }
2355
2356 static int get_array_info(mddev_t * mddev, void __user * arg)
2357 {
2358 mdu_array_info_t info;
2359 int nr,working,active,failed,spare;
2360 mdk_rdev_t *rdev;
2361 struct list_head *tmp;
2362
2363 nr=working=active=failed=spare=0;
2364 ITERATE_RDEV(mddev,rdev,tmp) {
2365 nr++;
2366 if (rdev->faulty)
2367 failed++;
2368 else {
2369 working++;
2370 if (rdev->in_sync)
2371 active++;
2372 else
2373 spare++;
2374 }
2375 }
2376
2377 info.major_version = mddev->major_version;
2378 info.minor_version = mddev->minor_version;
2379 info.patch_version = MD_PATCHLEVEL_VERSION;
2380 info.ctime = mddev->ctime;
2381 info.level = mddev->level;
2382 info.size = mddev->size;
2383 info.nr_disks = nr;
2384 info.raid_disks = mddev->raid_disks;
2385 info.md_minor = mddev->md_minor;
2386 info.not_persistent= !mddev->persistent;
2387
2388 info.utime = mddev->utime;
2389 info.state = 0;
2390 if (mddev->in_sync)
2391 info.state = (1<<MD_SB_CLEAN);
2392 if (mddev->bitmap && mddev->bitmap_offset)
2393 info.state = (1<<MD_SB_BITMAP_PRESENT);
2394 info.active_disks = active;
2395 info.working_disks = working;
2396 info.failed_disks = failed;
2397 info.spare_disks = spare;
2398
2399 info.layout = mddev->layout;
2400 info.chunk_size = mddev->chunk_size;
2401
2402 if (copy_to_user(arg, &info, sizeof(info)))
2403 return -EFAULT;
2404
2405 return 0;
2406 }
2407
2408 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
2409 {
2410 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2411 char *ptr, *buf = NULL;
2412 int err = -ENOMEM;
2413
2414 file = kmalloc(sizeof(*file), GFP_KERNEL);
2415 if (!file)
2416 goto out;
2417
2418 /* bitmap disabled, zero the first byte and copy out */
2419 if (!mddev->bitmap || !mddev->bitmap->file) {
2420 file->pathname[0] = '\0';
2421 goto copy_out;
2422 }
2423
2424 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2425 if (!buf)
2426 goto out;
2427
2428 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2429 if (!ptr)
2430 goto out;
2431
2432 strcpy(file->pathname, ptr);
2433
2434 copy_out:
2435 err = 0;
2436 if (copy_to_user(arg, file, sizeof(*file)))
2437 err = -EFAULT;
2438 out:
2439 kfree(buf);
2440 kfree(file);
2441 return err;
2442 }
2443
2444 static int get_disk_info(mddev_t * mddev, void __user * arg)
2445 {
2446 mdu_disk_info_t info;
2447 unsigned int nr;
2448 mdk_rdev_t *rdev;
2449
2450 if (copy_from_user(&info, arg, sizeof(info)))
2451 return -EFAULT;
2452
2453 nr = info.number;
2454
2455 rdev = find_rdev_nr(mddev, nr);
2456 if (rdev) {
2457 info.major = MAJOR(rdev->bdev->bd_dev);
2458 info.minor = MINOR(rdev->bdev->bd_dev);
2459 info.raid_disk = rdev->raid_disk;
2460 info.state = 0;
2461 if (rdev->faulty)
2462 info.state |= (1<<MD_DISK_FAULTY);
2463 else if (rdev->in_sync) {
2464 info.state |= (1<<MD_DISK_ACTIVE);
2465 info.state |= (1<<MD_DISK_SYNC);
2466 }
2467 if (test_bit(WriteMostly, &rdev->flags))
2468 info.state |= (1<<MD_DISK_WRITEMOSTLY);
2469 } else {
2470 info.major = info.minor = 0;
2471 info.raid_disk = -1;
2472 info.state = (1<<MD_DISK_REMOVED);
2473 }
2474
2475 if (copy_to_user(arg, &info, sizeof(info)))
2476 return -EFAULT;
2477
2478 return 0;
2479 }
2480
2481 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2482 {
2483 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2484 mdk_rdev_t *rdev;
2485 dev_t dev = MKDEV(info->major,info->minor);
2486
2487 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2488 return -EOVERFLOW;
2489
2490 if (!mddev->raid_disks) {
2491 int err;
2492 /* expecting a device which has a superblock */
2493 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2494 if (IS_ERR(rdev)) {
2495 printk(KERN_WARNING
2496 "md: md_import_device returned %ld\n",
2497 PTR_ERR(rdev));
2498 return PTR_ERR(rdev);
2499 }
2500 if (!list_empty(&mddev->disks)) {
2501 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2502 mdk_rdev_t, same_set);
2503 int err = super_types[mddev->major_version]
2504 .load_super(rdev, rdev0, mddev->minor_version);
2505 if (err < 0) {
2506 printk(KERN_WARNING
2507 "md: %s has different UUID to %s\n",
2508 bdevname(rdev->bdev,b),
2509 bdevname(rdev0->bdev,b2));
2510 export_rdev(rdev);
2511 return -EINVAL;
2512 }
2513 }
2514 err = bind_rdev_to_array(rdev, mddev);
2515 if (err)
2516 export_rdev(rdev);
2517 return err;
2518 }
2519
2520 /*
2521 * add_new_disk can be used once the array is assembled
2522 * to add "hot spares". They must already have a superblock
2523 * written
2524 */
2525 if (mddev->pers) {
2526 int err;
2527 if (!mddev->pers->hot_add_disk) {
2528 printk(KERN_WARNING
2529 "%s: personality does not support diskops!\n",
2530 mdname(mddev));
2531 return -EINVAL;
2532 }
2533 if (mddev->persistent)
2534 rdev = md_import_device(dev, mddev->major_version,
2535 mddev->minor_version);
2536 else
2537 rdev = md_import_device(dev, -1, -1);
2538 if (IS_ERR(rdev)) {
2539 printk(KERN_WARNING
2540 "md: md_import_device returned %ld\n",
2541 PTR_ERR(rdev));
2542 return PTR_ERR(rdev);
2543 }
2544 /* set save_raid_disk if appropriate */
2545 if (!mddev->persistent) {
2546 if (info->state & (1<<MD_DISK_SYNC) &&
2547 info->raid_disk < mddev->raid_disks)
2548 rdev->raid_disk = info->raid_disk;
2549 else
2550 rdev->raid_disk = -1;
2551 } else
2552 super_types[mddev->major_version].
2553 validate_super(mddev, rdev);
2554 rdev->saved_raid_disk = rdev->raid_disk;
2555
2556 rdev->in_sync = 0; /* just to be sure */
2557 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2558 set_bit(WriteMostly, &rdev->flags);
2559
2560 rdev->raid_disk = -1;
2561 err = bind_rdev_to_array(rdev, mddev);
2562 if (err)
2563 export_rdev(rdev);
2564
2565 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2566 md_wakeup_thread(mddev->thread);
2567 return err;
2568 }
2569
2570 /* otherwise, add_new_disk is only allowed
2571 * for major_version==0 superblocks
2572 */
2573 if (mddev->major_version != 0) {
2574 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2575 mdname(mddev));
2576 return -EINVAL;
2577 }
2578
2579 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2580 int err;
2581 rdev = md_import_device (dev, -1, 0);
2582 if (IS_ERR(rdev)) {
2583 printk(KERN_WARNING
2584 "md: error, md_import_device() returned %ld\n",
2585 PTR_ERR(rdev));
2586 return PTR_ERR(rdev);
2587 }
2588 rdev->desc_nr = info->number;
2589 if (info->raid_disk < mddev->raid_disks)
2590 rdev->raid_disk = info->raid_disk;
2591 else
2592 rdev->raid_disk = -1;
2593
2594 rdev->faulty = 0;
2595 if (rdev->raid_disk < mddev->raid_disks)
2596 rdev->in_sync = (info->state & (1<<MD_DISK_SYNC));
2597 else
2598 rdev->in_sync = 0;
2599
2600 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2601 set_bit(WriteMostly, &rdev->flags);
2602
2603 err = bind_rdev_to_array(rdev, mddev);
2604 if (err) {
2605 export_rdev(rdev);
2606 return err;
2607 }
2608
2609 if (!mddev->persistent) {
2610 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2611 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2612 } else
2613 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2614 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2615
2616 if (!mddev->size || (mddev->size > rdev->size))
2617 mddev->size = rdev->size;
2618 }
2619
2620 return 0;
2621 }
2622
2623 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2624 {
2625 char b[BDEVNAME_SIZE];
2626 mdk_rdev_t *rdev;
2627
2628 if (!mddev->pers)
2629 return -ENODEV;
2630
2631 rdev = find_rdev(mddev, dev);
2632 if (!rdev)
2633 return -ENXIO;
2634
2635 if (rdev->raid_disk >= 0)
2636 goto busy;
2637
2638 kick_rdev_from_array(rdev);
2639 md_update_sb(mddev);
2640
2641 return 0;
2642 busy:
2643 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2644 bdevname(rdev->bdev,b), mdname(mddev));
2645 return -EBUSY;
2646 }
2647
2648 static int hot_add_disk(mddev_t * mddev, dev_t dev)
2649 {
2650 char b[BDEVNAME_SIZE];
2651 int err;
2652 unsigned int size;
2653 mdk_rdev_t *rdev;
2654
2655 if (!mddev->pers)
2656 return -ENODEV;
2657
2658 if (mddev->major_version != 0) {
2659 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2660 " version-0 superblocks.\n",
2661 mdname(mddev));
2662 return -EINVAL;
2663 }
2664 if (!mddev->pers->hot_add_disk) {
2665 printk(KERN_WARNING
2666 "%s: personality does not support diskops!\n",
2667 mdname(mddev));
2668 return -EINVAL;
2669 }
2670
2671 rdev = md_import_device (dev, -1, 0);
2672 if (IS_ERR(rdev)) {
2673 printk(KERN_WARNING
2674 "md: error, md_import_device() returned %ld\n",
2675 PTR_ERR(rdev));
2676 return -EINVAL;
2677 }
2678
2679 if (mddev->persistent)
2680 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2681 else
2682 rdev->sb_offset =
2683 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2684
2685 size = calc_dev_size(rdev, mddev->chunk_size);
2686 rdev->size = size;
2687
2688 if (size < mddev->size) {
2689 printk(KERN_WARNING
2690 "%s: disk size %llu blocks < array size %llu\n",
2691 mdname(mddev), (unsigned long long)size,
2692 (unsigned long long)mddev->size);
2693 err = -ENOSPC;
2694 goto abort_export;
2695 }
2696
2697 if (rdev->faulty) {
2698 printk(KERN_WARNING
2699 "md: can not hot-add faulty %s disk to %s!\n",
2700 bdevname(rdev->bdev,b), mdname(mddev));
2701 err = -EINVAL;
2702 goto abort_export;
2703 }
2704 rdev->in_sync = 0;
2705 rdev->desc_nr = -1;
2706 bind_rdev_to_array(rdev, mddev);
2707
2708 /*
2709 * The rest should better be atomic, we can have disk failures
2710 * noticed in interrupt contexts ...
2711 */
2712
2713 if (rdev->desc_nr == mddev->max_disks) {
2714 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2715 mdname(mddev));
2716 err = -EBUSY;
2717 goto abort_unbind_export;
2718 }
2719
2720 rdev->raid_disk = -1;
2721
2722 md_update_sb(mddev);
2723
2724 /*
2725 * Kick recovery, maybe this spare has to be added to the
2726 * array immediately.
2727 */
2728 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2729 md_wakeup_thread(mddev->thread);
2730
2731 return 0;
2732
2733 abort_unbind_export:
2734 unbind_rdev_from_array(rdev);
2735
2736 abort_export:
2737 export_rdev(rdev);
2738 return err;
2739 }
2740
2741 /* similar to deny_write_access, but accounts for our holding a reference
2742 * to the file ourselves */
2743 static int deny_bitmap_write_access(struct file * file)
2744 {
2745 struct inode *inode = file->f_mapping->host;
2746
2747 spin_lock(&inode->i_lock);
2748 if (atomic_read(&inode->i_writecount) > 1) {
2749 spin_unlock(&inode->i_lock);
2750 return -ETXTBSY;
2751 }
2752 atomic_set(&inode->i_writecount, -1);
2753 spin_unlock(&inode->i_lock);
2754
2755 return 0;
2756 }
2757
2758 static int set_bitmap_file(mddev_t *mddev, int fd)
2759 {
2760 int err;
2761
2762 if (mddev->pers) {
2763 if (!mddev->pers->quiesce)
2764 return -EBUSY;
2765 if (mddev->recovery || mddev->sync_thread)
2766 return -EBUSY;
2767 /* we should be able to change the bitmap.. */
2768 }
2769
2770
2771 if (fd >= 0) {
2772 if (mddev->bitmap)
2773 return -EEXIST; /* cannot add when bitmap is present */
2774 mddev->bitmap_file = fget(fd);
2775
2776 if (mddev->bitmap_file == NULL) {
2777 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2778 mdname(mddev));
2779 return -EBADF;
2780 }
2781
2782 err = deny_bitmap_write_access(mddev->bitmap_file);
2783 if (err) {
2784 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2785 mdname(mddev));
2786 fput(mddev->bitmap_file);
2787 mddev->bitmap_file = NULL;
2788 return err;
2789 }
2790 mddev->bitmap_offset = 0; /* file overrides offset */
2791 } else if (mddev->bitmap == NULL)
2792 return -ENOENT; /* cannot remove what isn't there */
2793 err = 0;
2794 if (mddev->pers) {
2795 mddev->pers->quiesce(mddev, 1);
2796 if (fd >= 0)
2797 err = bitmap_create(mddev);
2798 if (fd < 0 || err)
2799 bitmap_destroy(mddev);
2800 mddev->pers->quiesce(mddev, 0);
2801 } else if (fd < 0) {
2802 if (mddev->bitmap_file)
2803 fput(mddev->bitmap_file);
2804 mddev->bitmap_file = NULL;
2805 }
2806
2807 return err;
2808 }
2809
2810 /*
2811 * set_array_info is used two different ways
2812 * The original usage is when creating a new array.
2813 * In this usage, raid_disks is > 0 and it together with
2814 * level, size, not_persistent,layout,chunksize determine the
2815 * shape of the array.
2816 * This will always create an array with a type-0.90.0 superblock.
2817 * The newer usage is when assembling an array.
2818 * In this case raid_disks will be 0, and the major_version field is
2819 * use to determine which style super-blocks are to be found on the devices.
2820 * The minor and patch _version numbers are also kept incase the
2821 * super_block handler wishes to interpret them.
2822 */
2823 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2824 {
2825
2826 if (info->raid_disks == 0) {
2827 /* just setting version number for superblock loading */
2828 if (info->major_version < 0 ||
2829 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2830 super_types[info->major_version].name == NULL) {
2831 /* maybe try to auto-load a module? */
2832 printk(KERN_INFO
2833 "md: superblock version %d not known\n",
2834 info->major_version);
2835 return -EINVAL;
2836 }
2837 mddev->major_version = info->major_version;
2838 mddev->minor_version = info->minor_version;
2839 mddev->patch_version = info->patch_version;
2840 return 0;
2841 }
2842 mddev->major_version = MD_MAJOR_VERSION;
2843 mddev->minor_version = MD_MINOR_VERSION;
2844 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2845 mddev->ctime = get_seconds();
2846
2847 mddev->level = info->level;
2848 mddev->size = info->size;
2849 mddev->raid_disks = info->raid_disks;
2850 /* don't set md_minor, it is determined by which /dev/md* was
2851 * openned
2852 */
2853 if (info->state & (1<<MD_SB_CLEAN))
2854 mddev->recovery_cp = MaxSector;
2855 else
2856 mddev->recovery_cp = 0;
2857 mddev->persistent = ! info->not_persistent;
2858
2859 mddev->layout = info->layout;
2860 mddev->chunk_size = info->chunk_size;
2861
2862 mddev->max_disks = MD_SB_DISKS;
2863
2864 mddev->sb_dirty = 1;
2865
2866 /*
2867 * Generate a 128 bit UUID
2868 */
2869 get_random_bytes(mddev->uuid, 16);
2870
2871 return 0;
2872 }
2873
2874 /*
2875 * update_array_info is used to change the configuration of an
2876 * on-line array.
2877 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2878 * fields in the info are checked against the array.
2879 * Any differences that cannot be handled will cause an error.
2880 * Normally, only one change can be managed at a time.
2881 */
2882 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2883 {
2884 int rv = 0;
2885 int cnt = 0;
2886 int state = 0;
2887
2888 /* calculate expected state,ignoring low bits */
2889 if (mddev->bitmap && mddev->bitmap_offset)
2890 state |= (1 << MD_SB_BITMAP_PRESENT);
2891
2892 if (mddev->major_version != info->major_version ||
2893 mddev->minor_version != info->minor_version ||
2894 /* mddev->patch_version != info->patch_version || */
2895 mddev->ctime != info->ctime ||
2896 mddev->level != info->level ||
2897 /* mddev->layout != info->layout || */
2898 !mddev->persistent != info->not_persistent||
2899 mddev->chunk_size != info->chunk_size ||
2900 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2901 ((state^info->state) & 0xfffffe00)
2902 )
2903 return -EINVAL;
2904 /* Check there is only one change */
2905 if (mddev->size != info->size) cnt++;
2906 if (mddev->raid_disks != info->raid_disks) cnt++;
2907 if (mddev->layout != info->layout) cnt++;
2908 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
2909 if (cnt == 0) return 0;
2910 if (cnt > 1) return -EINVAL;
2911
2912 if (mddev->layout != info->layout) {
2913 /* Change layout
2914 * we don't need to do anything at the md level, the
2915 * personality will take care of it all.
2916 */
2917 if (mddev->pers->reconfig == NULL)
2918 return -EINVAL;
2919 else
2920 return mddev->pers->reconfig(mddev, info->layout, -1);
2921 }
2922 if (mddev->size != info->size) {
2923 mdk_rdev_t * rdev;
2924 struct list_head *tmp;
2925 if (mddev->pers->resize == NULL)
2926 return -EINVAL;
2927 /* The "size" is the amount of each device that is used.
2928 * This can only make sense for arrays with redundancy.
2929 * linear and raid0 always use whatever space is available
2930 * We can only consider changing the size if no resync
2931 * or reconstruction is happening, and if the new size
2932 * is acceptable. It must fit before the sb_offset or,
2933 * if that is <data_offset, it must fit before the
2934 * size of each device.
2935 * If size is zero, we find the largest size that fits.
2936 */
2937 if (mddev->sync_thread)
2938 return -EBUSY;
2939 ITERATE_RDEV(mddev,rdev,tmp) {
2940 sector_t avail;
2941 int fit = (info->size == 0);
2942 if (rdev->sb_offset > rdev->data_offset)
2943 avail = (rdev->sb_offset*2) - rdev->data_offset;
2944 else
2945 avail = get_capacity(rdev->bdev->bd_disk)
2946 - rdev->data_offset;
2947 if (fit && (info->size == 0 || info->size > avail/2))
2948 info->size = avail/2;
2949 if (avail < ((sector_t)info->size << 1))
2950 return -ENOSPC;
2951 }
2952 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
2953 if (!rv) {
2954 struct block_device *bdev;
2955
2956 bdev = bdget_disk(mddev->gendisk, 0);
2957 if (bdev) {
2958 down(&bdev->bd_inode->i_sem);
2959 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2960 up(&bdev->bd_inode->i_sem);
2961 bdput(bdev);
2962 }
2963 }
2964 }
2965 if (mddev->raid_disks != info->raid_disks) {
2966 /* change the number of raid disks */
2967 if (mddev->pers->reshape == NULL)
2968 return -EINVAL;
2969 if (info->raid_disks <= 0 ||
2970 info->raid_disks >= mddev->max_disks)
2971 return -EINVAL;
2972 if (mddev->sync_thread)
2973 return -EBUSY;
2974 rv = mddev->pers->reshape(mddev, info->raid_disks);
2975 if (!rv) {
2976 struct block_device *bdev;
2977
2978 bdev = bdget_disk(mddev->gendisk, 0);
2979 if (bdev) {
2980 down(&bdev->bd_inode->i_sem);
2981 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2982 up(&bdev->bd_inode->i_sem);
2983 bdput(bdev);
2984 }
2985 }
2986 }
2987 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
2988 if (mddev->pers->quiesce == NULL)
2989 return -EINVAL;
2990 if (mddev->recovery || mddev->sync_thread)
2991 return -EBUSY;
2992 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
2993 /* add the bitmap */
2994 if (mddev->bitmap)
2995 return -EEXIST;
2996 if (mddev->default_bitmap_offset == 0)
2997 return -EINVAL;
2998 mddev->bitmap_offset = mddev->default_bitmap_offset;
2999 mddev->pers->quiesce(mddev, 1);
3000 rv = bitmap_create(mddev);
3001 if (rv)
3002 bitmap_destroy(mddev);
3003 mddev->pers->quiesce(mddev, 0);
3004 } else {
3005 /* remove the bitmap */
3006 if (!mddev->bitmap)
3007 return -ENOENT;
3008 if (mddev->bitmap->file)
3009 return -EINVAL;
3010 mddev->pers->quiesce(mddev, 1);
3011 bitmap_destroy(mddev);
3012 mddev->pers->quiesce(mddev, 0);
3013 mddev->bitmap_offset = 0;
3014 }
3015 }
3016 md_update_sb(mddev);
3017 return rv;
3018 }
3019
3020 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3021 {
3022 mdk_rdev_t *rdev;
3023
3024 if (mddev->pers == NULL)
3025 return -ENODEV;
3026
3027 rdev = find_rdev(mddev, dev);
3028 if (!rdev)
3029 return -ENODEV;
3030
3031 md_error(mddev, rdev);
3032 return 0;
3033 }
3034
3035 static int md_ioctl(struct inode *inode, struct file *file,
3036 unsigned int cmd, unsigned long arg)
3037 {
3038 int err = 0;
3039 void __user *argp = (void __user *)arg;
3040 struct hd_geometry __user *loc = argp;
3041 mddev_t *mddev = NULL;
3042
3043 if (!capable(CAP_SYS_ADMIN))
3044 return -EACCES;
3045
3046 /*
3047 * Commands dealing with the RAID driver but not any
3048 * particular array:
3049 */
3050 switch (cmd)
3051 {
3052 case RAID_VERSION:
3053 err = get_version(argp);
3054 goto done;
3055
3056 case PRINT_RAID_DEBUG:
3057 err = 0;
3058 md_print_devices();
3059 goto done;
3060
3061 #ifndef MODULE
3062 case RAID_AUTORUN:
3063 err = 0;
3064 autostart_arrays(arg);
3065 goto done;
3066 #endif
3067 default:;
3068 }
3069
3070 /*
3071 * Commands creating/starting a new array:
3072 */
3073
3074 mddev = inode->i_bdev->bd_disk->private_data;
3075
3076 if (!mddev) {
3077 BUG();
3078 goto abort;
3079 }
3080
3081
3082 if (cmd == START_ARRAY) {
3083 /* START_ARRAY doesn't need to lock the array as autostart_array
3084 * does the locking, and it could even be a different array
3085 */
3086 static int cnt = 3;
3087 if (cnt > 0 ) {
3088 printk(KERN_WARNING
3089 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
3090 "This will not be supported beyond 2.6\n",
3091 current->comm, current->pid);
3092 cnt--;
3093 }
3094 err = autostart_array(new_decode_dev(arg));
3095 if (err) {
3096 printk(KERN_WARNING "md: autostart failed!\n");
3097 goto abort;
3098 }
3099 goto done;
3100 }
3101
3102 err = mddev_lock(mddev);
3103 if (err) {
3104 printk(KERN_INFO
3105 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3106 err, cmd);
3107 goto abort;
3108 }
3109
3110 switch (cmd)
3111 {
3112 case SET_ARRAY_INFO:
3113 {
3114 mdu_array_info_t info;
3115 if (!arg)
3116 memset(&info, 0, sizeof(info));
3117 else if (copy_from_user(&info, argp, sizeof(info))) {
3118 err = -EFAULT;
3119 goto abort_unlock;
3120 }
3121 if (mddev->pers) {
3122 err = update_array_info(mddev, &info);
3123 if (err) {
3124 printk(KERN_WARNING "md: couldn't update"
3125 " array info. %d\n", err);
3126 goto abort_unlock;
3127 }
3128 goto done_unlock;
3129 }
3130 if (!list_empty(&mddev->disks)) {
3131 printk(KERN_WARNING
3132 "md: array %s already has disks!\n",
3133 mdname(mddev));
3134 err = -EBUSY;
3135 goto abort_unlock;
3136 }
3137 if (mddev->raid_disks) {
3138 printk(KERN_WARNING
3139 "md: array %s already initialised!\n",
3140 mdname(mddev));
3141 err = -EBUSY;
3142 goto abort_unlock;
3143 }
3144 err = set_array_info(mddev, &info);
3145 if (err) {
3146 printk(KERN_WARNING "md: couldn't set"
3147 " array info. %d\n", err);
3148 goto abort_unlock;
3149 }
3150 }
3151 goto done_unlock;
3152
3153 default:;
3154 }
3155
3156 /*
3157 * Commands querying/configuring an existing array:
3158 */
3159 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3160 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3161 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3162 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
3163 err = -ENODEV;
3164 goto abort_unlock;
3165 }
3166
3167 /*
3168 * Commands even a read-only array can execute:
3169 */
3170 switch (cmd)
3171 {
3172 case GET_ARRAY_INFO:
3173 err = get_array_info(mddev, argp);
3174 goto done_unlock;
3175
3176 case GET_BITMAP_FILE:
3177 err = get_bitmap_file(mddev, argp);
3178 goto done_unlock;
3179
3180 case GET_DISK_INFO:
3181 err = get_disk_info(mddev, argp);
3182 goto done_unlock;
3183
3184 case RESTART_ARRAY_RW:
3185 err = restart_array(mddev);
3186 goto done_unlock;
3187
3188 case STOP_ARRAY:
3189 err = do_md_stop (mddev, 0);
3190 goto done_unlock;
3191
3192 case STOP_ARRAY_RO:
3193 err = do_md_stop (mddev, 1);
3194 goto done_unlock;
3195
3196 /*
3197 * We have a problem here : there is no easy way to give a CHS
3198 * virtual geometry. We currently pretend that we have a 2 heads
3199 * 4 sectors (with a BIG number of cylinders...). This drives
3200 * dosfs just mad... ;-)
3201 */
3202 case HDIO_GETGEO:
3203 if (!loc) {
3204 err = -EINVAL;
3205 goto abort_unlock;
3206 }
3207 err = put_user (2, (char __user *) &loc->heads);
3208 if (err)
3209 goto abort_unlock;
3210 err = put_user (4, (char __user *) &loc->sectors);
3211 if (err)
3212 goto abort_unlock;
3213 err = put_user(get_capacity(mddev->gendisk)/8,
3214 (short __user *) &loc->cylinders);
3215 if (err)
3216 goto abort_unlock;
3217 err = put_user (get_start_sect(inode->i_bdev),
3218 (long __user *) &loc->start);
3219 goto done_unlock;
3220 }
3221
3222 /*
3223 * The remaining ioctls are changing the state of the
3224 * superblock, so we do not allow read-only arrays
3225 * here:
3226 */
3227 if (mddev->ro) {
3228 err = -EROFS;
3229 goto abort_unlock;
3230 }
3231
3232 switch (cmd)
3233 {
3234 case ADD_NEW_DISK:
3235 {
3236 mdu_disk_info_t info;
3237 if (copy_from_user(&info, argp, sizeof(info)))
3238 err = -EFAULT;
3239 else
3240 err = add_new_disk(mddev, &info);
3241 goto done_unlock;
3242 }
3243
3244 case HOT_REMOVE_DISK:
3245 err = hot_remove_disk(mddev, new_decode_dev(arg));
3246 goto done_unlock;
3247
3248 case HOT_ADD_DISK:
3249 err = hot_add_disk(mddev, new_decode_dev(arg));
3250 goto done_unlock;
3251
3252 case SET_DISK_FAULTY:
3253 err = set_disk_faulty(mddev, new_decode_dev(arg));
3254 goto done_unlock;
3255
3256 case RUN_ARRAY:
3257 err = do_md_run (mddev);
3258 goto done_unlock;
3259
3260 case SET_BITMAP_FILE:
3261 err = set_bitmap_file(mddev, (int)arg);
3262 goto done_unlock;
3263
3264 default:
3265 if (_IOC_TYPE(cmd) == MD_MAJOR)
3266 printk(KERN_WARNING "md: %s(pid %d) used"
3267 " obsolete MD ioctl, upgrade your"
3268 " software to use new ictls.\n",
3269 current->comm, current->pid);
3270 err = -EINVAL;
3271 goto abort_unlock;
3272 }
3273
3274 done_unlock:
3275 abort_unlock:
3276 mddev_unlock(mddev);
3277
3278 return err;
3279 done:
3280 if (err)
3281 MD_BUG();
3282 abort:
3283 return err;
3284 }
3285
3286 static int md_open(struct inode *inode, struct file *file)
3287 {
3288 /*
3289 * Succeed if we can lock the mddev, which confirms that
3290 * it isn't being stopped right now.
3291 */
3292 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3293 int err;
3294
3295 if ((err = mddev_lock(mddev)))
3296 goto out;
3297
3298 err = 0;
3299 mddev_get(mddev);
3300 mddev_unlock(mddev);
3301
3302 check_disk_change(inode->i_bdev);
3303 out:
3304 return err;
3305 }
3306
3307 static int md_release(struct inode *inode, struct file * file)
3308 {
3309 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3310
3311 if (!mddev)
3312 BUG();
3313 mddev_put(mddev);
3314
3315 return 0;
3316 }
3317
3318 static int md_media_changed(struct gendisk *disk)
3319 {
3320 mddev_t *mddev = disk->private_data;
3321
3322 return mddev->changed;
3323 }
3324
3325 static int md_revalidate(struct gendisk *disk)
3326 {
3327 mddev_t *mddev = disk->private_data;
3328
3329 mddev->changed = 0;
3330 return 0;
3331 }
3332 static struct block_device_operations md_fops =
3333 {
3334 .owner = THIS_MODULE,
3335 .open = md_open,
3336 .release = md_release,
3337 .ioctl = md_ioctl,
3338 .media_changed = md_media_changed,
3339 .revalidate_disk= md_revalidate,
3340 };
3341
3342 static int md_thread(void * arg)
3343 {
3344 mdk_thread_t *thread = arg;
3345
3346 /*
3347 * md_thread is a 'system-thread', it's priority should be very
3348 * high. We avoid resource deadlocks individually in each
3349 * raid personality. (RAID5 does preallocation) We also use RR and
3350 * the very same RT priority as kswapd, thus we will never get
3351 * into a priority inversion deadlock.
3352 *
3353 * we definitely have to have equal or higher priority than
3354 * bdflush, otherwise bdflush will deadlock if there are too
3355 * many dirty RAID5 blocks.
3356 */
3357
3358 allow_signal(SIGKILL);
3359 complete(thread->event);
3360 while (!kthread_should_stop()) {
3361 void (*run)(mddev_t *);
3362
3363 wait_event_interruptible_timeout(thread->wqueue,
3364 test_bit(THREAD_WAKEUP, &thread->flags)
3365 || kthread_should_stop(),
3366 thread->timeout);
3367 try_to_freeze();
3368
3369 clear_bit(THREAD_WAKEUP, &thread->flags);
3370
3371 run = thread->run;
3372 if (run)
3373 run(thread->mddev);
3374 }
3375
3376 return 0;
3377 }
3378
3379 void md_wakeup_thread(mdk_thread_t *thread)
3380 {
3381 if (thread) {
3382 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3383 set_bit(THREAD_WAKEUP, &thread->flags);
3384 wake_up(&thread->wqueue);
3385 }
3386 }
3387
3388 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3389 const char *name)
3390 {
3391 mdk_thread_t *thread;
3392 struct completion event;
3393
3394 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
3395 if (!thread)
3396 return NULL;
3397
3398 memset(thread, 0, sizeof(mdk_thread_t));
3399 init_waitqueue_head(&thread->wqueue);
3400
3401 init_completion(&event);
3402 thread->event = &event;
3403 thread->run = run;
3404 thread->mddev = mddev;
3405 thread->name = name;
3406 thread->timeout = MAX_SCHEDULE_TIMEOUT;
3407 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
3408 if (IS_ERR(thread->tsk)) {
3409 kfree(thread);
3410 return NULL;
3411 }
3412 wait_for_completion(&event);
3413 return thread;
3414 }
3415
3416 void md_unregister_thread(mdk_thread_t *thread)
3417 {
3418 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
3419
3420 kthread_stop(thread->tsk);
3421 kfree(thread);
3422 }
3423
3424 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3425 {
3426 if (!mddev) {
3427 MD_BUG();
3428 return;
3429 }
3430
3431 if (!rdev || rdev->faulty)
3432 return;
3433 /*
3434 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3435 mdname(mddev),
3436 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3437 __builtin_return_address(0),__builtin_return_address(1),
3438 __builtin_return_address(2),__builtin_return_address(3));
3439 */
3440 if (!mddev->pers->error_handler)
3441 return;
3442 mddev->pers->error_handler(mddev,rdev);
3443 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3444 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3445 md_wakeup_thread(mddev->thread);
3446 }
3447
3448 /* seq_file implementation /proc/mdstat */
3449
3450 static void status_unused(struct seq_file *seq)
3451 {
3452 int i = 0;
3453 mdk_rdev_t *rdev;
3454 struct list_head *tmp;
3455
3456 seq_printf(seq, "unused devices: ");
3457
3458 ITERATE_RDEV_PENDING(rdev,tmp) {
3459 char b[BDEVNAME_SIZE];
3460 i++;
3461 seq_printf(seq, "%s ",
3462 bdevname(rdev->bdev,b));
3463 }
3464 if (!i)
3465 seq_printf(seq, "<none>");
3466
3467 seq_printf(seq, "\n");
3468 }
3469
3470
3471 static void status_resync(struct seq_file *seq, mddev_t * mddev)
3472 {
3473 unsigned long max_blocks, resync, res, dt, db, rt;
3474
3475 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3476
3477 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3478 max_blocks = mddev->resync_max_sectors >> 1;
3479 else
3480 max_blocks = mddev->size;
3481
3482 /*
3483 * Should not happen.
3484 */
3485 if (!max_blocks) {
3486 MD_BUG();
3487 return;
3488 }
3489 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3490 {
3491 int i, x = res/50, y = 20-x;
3492 seq_printf(seq, "[");
3493 for (i = 0; i < x; i++)
3494 seq_printf(seq, "=");
3495 seq_printf(seq, ">");
3496 for (i = 0; i < y; i++)
3497 seq_printf(seq, ".");
3498 seq_printf(seq, "] ");
3499 }
3500 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3501 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3502 "resync" : "recovery"),
3503 res/10, res % 10, resync, max_blocks);
3504
3505 /*
3506 * We do not want to overflow, so the order of operands and
3507 * the * 100 / 100 trick are important. We do a +1 to be
3508 * safe against division by zero. We only estimate anyway.
3509 *
3510 * dt: time from mark until now
3511 * db: blocks written from mark until now
3512 * rt: remaining time
3513 */
3514 dt = ((jiffies - mddev->resync_mark) / HZ);
3515 if (!dt) dt++;
3516 db = resync - (mddev->resync_mark_cnt/2);
3517 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3518
3519 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3520
3521 seq_printf(seq, " speed=%ldK/sec", db/dt);
3522 }
3523
3524 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3525 {
3526 struct list_head *tmp;
3527 loff_t l = *pos;
3528 mddev_t *mddev;
3529
3530 if (l >= 0x10000)
3531 return NULL;
3532 if (!l--)
3533 /* header */
3534 return (void*)1;
3535
3536 spin_lock(&all_mddevs_lock);
3537 list_for_each(tmp,&all_mddevs)
3538 if (!l--) {
3539 mddev = list_entry(tmp, mddev_t, all_mddevs);
3540 mddev_get(mddev);
3541 spin_unlock(&all_mddevs_lock);
3542 return mddev;
3543 }
3544 spin_unlock(&all_mddevs_lock);
3545 if (!l--)
3546 return (void*)2;/* tail */
3547 return NULL;
3548 }
3549
3550 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3551 {
3552 struct list_head *tmp;
3553 mddev_t *next_mddev, *mddev = v;
3554
3555 ++*pos;
3556 if (v == (void*)2)
3557 return NULL;
3558
3559 spin_lock(&all_mddevs_lock);
3560 if (v == (void*)1)
3561 tmp = all_mddevs.next;
3562 else
3563 tmp = mddev->all_mddevs.next;
3564 if (tmp != &all_mddevs)
3565 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3566 else {
3567 next_mddev = (void*)2;
3568 *pos = 0x10000;
3569 }
3570 spin_unlock(&all_mddevs_lock);
3571
3572 if (v != (void*)1)
3573 mddev_put(mddev);
3574 return next_mddev;
3575
3576 }
3577
3578 static void md_seq_stop(struct seq_file *seq, void *v)
3579 {
3580 mddev_t *mddev = v;
3581
3582 if (mddev && v != (void*)1 && v != (void*)2)
3583 mddev_put(mddev);
3584 }
3585
3586 static int md_seq_show(struct seq_file *seq, void *v)
3587 {
3588 mddev_t *mddev = v;
3589 sector_t size;
3590 struct list_head *tmp2;
3591 mdk_rdev_t *rdev;
3592 int i;
3593 struct bitmap *bitmap;
3594
3595 if (v == (void*)1) {
3596 seq_printf(seq, "Personalities : ");
3597 spin_lock(&pers_lock);
3598 for (i = 0; i < MAX_PERSONALITY; i++)
3599 if (pers[i])
3600 seq_printf(seq, "[%s] ", pers[i]->name);
3601
3602 spin_unlock(&pers_lock);
3603 seq_printf(seq, "\n");
3604 return 0;
3605 }
3606 if (v == (void*)2) {
3607 status_unused(seq);
3608 return 0;
3609 }
3610
3611 if (mddev_lock(mddev)!=0)
3612 return -EINTR;
3613 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3614 seq_printf(seq, "%s : %sactive", mdname(mddev),
3615 mddev->pers ? "" : "in");
3616 if (mddev->pers) {
3617 if (mddev->ro)
3618 seq_printf(seq, " (read-only)");
3619 seq_printf(seq, " %s", mddev->pers->name);
3620 }
3621
3622 size = 0;
3623 ITERATE_RDEV(mddev,rdev,tmp2) {
3624 char b[BDEVNAME_SIZE];
3625 seq_printf(seq, " %s[%d]",
3626 bdevname(rdev->bdev,b), rdev->desc_nr);
3627 if (test_bit(WriteMostly, &rdev->flags))
3628 seq_printf(seq, "(W)");
3629 if (rdev->faulty) {
3630 seq_printf(seq, "(F)");
3631 continue;
3632 } else if (rdev->raid_disk < 0)
3633 seq_printf(seq, "(S)"); /* spare */
3634 size += rdev->size;
3635 }
3636
3637 if (!list_empty(&mddev->disks)) {
3638 if (mddev->pers)
3639 seq_printf(seq, "\n %llu blocks",
3640 (unsigned long long)mddev->array_size);
3641 else
3642 seq_printf(seq, "\n %llu blocks",
3643 (unsigned long long)size);
3644 }
3645 if (mddev->persistent) {
3646 if (mddev->major_version != 0 ||
3647 mddev->minor_version != 90) {
3648 seq_printf(seq," super %d.%d",
3649 mddev->major_version,
3650 mddev->minor_version);
3651 }
3652 } else
3653 seq_printf(seq, " super non-persistent");
3654
3655 if (mddev->pers) {
3656 mddev->pers->status (seq, mddev);
3657 seq_printf(seq, "\n ");
3658 if (mddev->curr_resync > 2) {
3659 status_resync (seq, mddev);
3660 seq_printf(seq, "\n ");
3661 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3662 seq_printf(seq, " resync=DELAYED\n ");
3663 } else
3664 seq_printf(seq, "\n ");
3665
3666 if ((bitmap = mddev->bitmap)) {
3667 unsigned long chunk_kb;
3668 unsigned long flags;
3669 spin_lock_irqsave(&bitmap->lock, flags);
3670 chunk_kb = bitmap->chunksize >> 10;
3671 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3672 "%lu%s chunk",
3673 bitmap->pages - bitmap->missing_pages,
3674 bitmap->pages,
3675 (bitmap->pages - bitmap->missing_pages)
3676 << (PAGE_SHIFT - 10),
3677 chunk_kb ? chunk_kb : bitmap->chunksize,
3678 chunk_kb ? "KB" : "B");
3679 if (bitmap->file) {
3680 seq_printf(seq, ", file: ");
3681 seq_path(seq, bitmap->file->f_vfsmnt,
3682 bitmap->file->f_dentry," \t\n");
3683 }
3684
3685 seq_printf(seq, "\n");
3686 spin_unlock_irqrestore(&bitmap->lock, flags);
3687 }
3688
3689 seq_printf(seq, "\n");
3690 }
3691 mddev_unlock(mddev);
3692
3693 return 0;
3694 }
3695
3696 static struct seq_operations md_seq_ops = {
3697 .start = md_seq_start,
3698 .next = md_seq_next,
3699 .stop = md_seq_stop,
3700 .show = md_seq_show,
3701 };
3702
3703 static int md_seq_open(struct inode *inode, struct file *file)
3704 {
3705 int error;
3706
3707 error = seq_open(file, &md_seq_ops);
3708 return error;
3709 }
3710
3711 static struct file_operations md_seq_fops = {
3712 .open = md_seq_open,
3713 .read = seq_read,
3714 .llseek = seq_lseek,
3715 .release = seq_release,
3716 };
3717
3718 int register_md_personality(int pnum, mdk_personality_t *p)
3719 {
3720 if (pnum >= MAX_PERSONALITY) {
3721 printk(KERN_ERR
3722 "md: tried to install personality %s as nr %d, but max is %lu\n",
3723 p->name, pnum, MAX_PERSONALITY-1);
3724 return -EINVAL;
3725 }
3726
3727 spin_lock(&pers_lock);
3728 if (pers[pnum]) {
3729 spin_unlock(&pers_lock);
3730 return -EBUSY;
3731 }
3732
3733 pers[pnum] = p;
3734 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3735 spin_unlock(&pers_lock);
3736 return 0;
3737 }
3738
3739 int unregister_md_personality(int pnum)
3740 {
3741 if (pnum >= MAX_PERSONALITY)
3742 return -EINVAL;
3743
3744 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3745 spin_lock(&pers_lock);
3746 pers[pnum] = NULL;
3747 spin_unlock(&pers_lock);
3748 return 0;
3749 }
3750
3751 static int is_mddev_idle(mddev_t *mddev)
3752 {
3753 mdk_rdev_t * rdev;
3754 struct list_head *tmp;
3755 int idle;
3756 unsigned long curr_events;
3757
3758 idle = 1;
3759 ITERATE_RDEV(mddev,rdev,tmp) {
3760 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
3761 curr_events = disk_stat_read(disk, sectors[0]) +
3762 disk_stat_read(disk, sectors[1]) -
3763 atomic_read(&disk->sync_io);
3764 /* Allow some slack between valud of curr_events and last_events,
3765 * as there are some uninteresting races.
3766 * Note: the following is an unsigned comparison.
3767 */
3768 if ((curr_events - rdev->last_events + 32) > 64) {
3769 rdev->last_events = curr_events;
3770 idle = 0;
3771 }
3772 }
3773 return idle;
3774 }
3775
3776 void md_done_sync(mddev_t *mddev, int blocks, int ok)
3777 {
3778 /* another "blocks" (512byte) blocks have been synced */
3779 atomic_sub(blocks, &mddev->recovery_active);
3780 wake_up(&mddev->recovery_wait);
3781 if (!ok) {
3782 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3783 md_wakeup_thread(mddev->thread);
3784 // stop recovery, signal do_sync ....
3785 }
3786 }
3787
3788
3789 /* md_write_start(mddev, bi)
3790 * If we need to update some array metadata (e.g. 'active' flag
3791 * in superblock) before writing, schedule a superblock update
3792 * and wait for it to complete.
3793 */
3794 void md_write_start(mddev_t *mddev, struct bio *bi)
3795 {
3796 if (bio_data_dir(bi) != WRITE)
3797 return;
3798
3799 atomic_inc(&mddev->writes_pending);
3800 if (mddev->in_sync) {
3801 spin_lock(&mddev->write_lock);
3802 if (mddev->in_sync) {
3803 mddev->in_sync = 0;
3804 mddev->sb_dirty = 1;
3805 md_wakeup_thread(mddev->thread);
3806 }
3807 spin_unlock(&mddev->write_lock);
3808 }
3809 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
3810 }
3811
3812 void md_write_end(mddev_t *mddev)
3813 {
3814 if (atomic_dec_and_test(&mddev->writes_pending)) {
3815 if (mddev->safemode == 2)
3816 md_wakeup_thread(mddev->thread);
3817 else
3818 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3819 }
3820 }
3821
3822 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
3823
3824 #define SYNC_MARKS 10
3825 #define SYNC_MARK_STEP (3*HZ)
3826 static void md_do_sync(mddev_t *mddev)
3827 {
3828 mddev_t *mddev2;
3829 unsigned int currspeed = 0,
3830 window;
3831 sector_t max_sectors,j, io_sectors;
3832 unsigned long mark[SYNC_MARKS];
3833 sector_t mark_cnt[SYNC_MARKS];
3834 int last_mark,m;
3835 struct list_head *tmp;
3836 sector_t last_check;
3837 int skipped = 0;
3838
3839 /* just incase thread restarts... */
3840 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3841 return;
3842
3843 /* we overload curr_resync somewhat here.
3844 * 0 == not engaged in resync at all
3845 * 2 == checking that there is no conflict with another sync
3846 * 1 == like 2, but have yielded to allow conflicting resync to
3847 * commense
3848 * other == active in resync - this many blocks
3849 *
3850 * Before starting a resync we must have set curr_resync to
3851 * 2, and then checked that every "conflicting" array has curr_resync
3852 * less than ours. When we find one that is the same or higher
3853 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3854 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3855 * This will mean we have to start checking from the beginning again.
3856 *
3857 */
3858
3859 do {
3860 mddev->curr_resync = 2;
3861
3862 try_again:
3863 if (signal_pending(current) ||
3864 kthread_should_stop()) {
3865 flush_signals(current);
3866 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3867 goto skip;
3868 }
3869 ITERATE_MDDEV(mddev2,tmp) {
3870 if (mddev2 == mddev)
3871 continue;
3872 if (mddev2->curr_resync &&
3873 match_mddev_units(mddev,mddev2)) {
3874 DEFINE_WAIT(wq);
3875 if (mddev < mddev2 && mddev->curr_resync == 2) {
3876 /* arbitrarily yield */
3877 mddev->curr_resync = 1;
3878 wake_up(&resync_wait);
3879 }
3880 if (mddev > mddev2 && mddev->curr_resync == 1)
3881 /* no need to wait here, we can wait the next
3882 * time 'round when curr_resync == 2
3883 */
3884 continue;
3885 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
3886 if (!signal_pending(current) &&
3887 !kthread_should_stop() &&
3888 mddev2->curr_resync >= mddev->curr_resync) {
3889 printk(KERN_INFO "md: delaying resync of %s"
3890 " until %s has finished resync (they"
3891 " share one or more physical units)\n",
3892 mdname(mddev), mdname(mddev2));
3893 mddev_put(mddev2);
3894 schedule();
3895 finish_wait(&resync_wait, &wq);
3896 goto try_again;
3897 }
3898 finish_wait(&resync_wait, &wq);
3899 }
3900 }
3901 } while (mddev->curr_resync < 2);
3902
3903 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3904 /* resync follows the size requested by the personality,
3905 * which defaults to physical size, but can be virtual size
3906 */
3907 max_sectors = mddev->resync_max_sectors;
3908 mddev->resync_mismatches = 0;
3909 } else
3910 /* recovery follows the physical size of devices */
3911 max_sectors = mddev->size << 1;
3912
3913 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
3914 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
3915 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
3916 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
3917 "(but not more than %d KB/sec) for reconstruction.\n",
3918 sysctl_speed_limit_max);
3919
3920 is_mddev_idle(mddev); /* this also initializes IO event counters */
3921 /* we don't use the checkpoint if there's a bitmap */
3922 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
3923 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3924 j = mddev->recovery_cp;
3925 else
3926 j = 0;
3927 io_sectors = 0;
3928 for (m = 0; m < SYNC_MARKS; m++) {
3929 mark[m] = jiffies;
3930 mark_cnt[m] = io_sectors;
3931 }
3932 last_mark = 0;
3933 mddev->resync_mark = mark[last_mark];
3934 mddev->resync_mark_cnt = mark_cnt[last_mark];
3935
3936 /*
3937 * Tune reconstruction:
3938 */
3939 window = 32*(PAGE_SIZE/512);
3940 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
3941 window/2,(unsigned long long) max_sectors/2);
3942
3943 atomic_set(&mddev->recovery_active, 0);
3944 init_waitqueue_head(&mddev->recovery_wait);
3945 last_check = 0;
3946
3947 if (j>2) {
3948 printk(KERN_INFO
3949 "md: resuming recovery of %s from checkpoint.\n",
3950 mdname(mddev));
3951 mddev->curr_resync = j;
3952 }
3953
3954 while (j < max_sectors) {
3955 sector_t sectors;
3956
3957 skipped = 0;
3958 sectors = mddev->pers->sync_request(mddev, j, &skipped,
3959 currspeed < sysctl_speed_limit_min);
3960 if (sectors == 0) {
3961 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3962 goto out;
3963 }
3964
3965 if (!skipped) { /* actual IO requested */
3966 io_sectors += sectors;
3967 atomic_add(sectors, &mddev->recovery_active);
3968 }
3969
3970 j += sectors;
3971 if (j>1) mddev->curr_resync = j;
3972
3973
3974 if (last_check + window > io_sectors || j == max_sectors)
3975 continue;
3976
3977 last_check = io_sectors;
3978
3979 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
3980 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
3981 break;
3982
3983 repeat:
3984 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
3985 /* step marks */
3986 int next = (last_mark+1) % SYNC_MARKS;
3987
3988 mddev->resync_mark = mark[next];
3989 mddev->resync_mark_cnt = mark_cnt[next];
3990 mark[next] = jiffies;
3991 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
3992 last_mark = next;
3993 }
3994
3995
3996 if (signal_pending(current) || kthread_should_stop()) {
3997 /*
3998 * got a signal, exit.
3999 */
4000 printk(KERN_INFO
4001 "md: md_do_sync() got signal ... exiting\n");
4002 flush_signals(current);
4003 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4004 goto out;
4005 }
4006
4007 /*
4008 * this loop exits only if either when we are slower than
4009 * the 'hard' speed limit, or the system was IO-idle for
4010 * a jiffy.
4011 * the system might be non-idle CPU-wise, but we only care
4012 * about not overloading the IO subsystem. (things like an
4013 * e2fsck being done on the RAID array should execute fast)
4014 */
4015 mddev->queue->unplug_fn(mddev->queue);
4016 cond_resched();
4017
4018 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4019 /((jiffies-mddev->resync_mark)/HZ +1) +1;
4020
4021 if (currspeed > sysctl_speed_limit_min) {
4022 if ((currspeed > sysctl_speed_limit_max) ||
4023 !is_mddev_idle(mddev)) {
4024 msleep_interruptible(250);
4025 goto repeat;
4026 }
4027 }
4028 }
4029 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4030 /*
4031 * this also signals 'finished resyncing' to md_stop
4032 */
4033 out:
4034 mddev->queue->unplug_fn(mddev->queue);
4035
4036 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4037
4038 /* tell personality that we are finished */
4039 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
4040
4041 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4042 mddev->curr_resync > 2 &&
4043 mddev->curr_resync >= mddev->recovery_cp) {
4044 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4045 printk(KERN_INFO
4046 "md: checkpointing recovery of %s.\n",
4047 mdname(mddev));
4048 mddev->recovery_cp = mddev->curr_resync;
4049 } else
4050 mddev->recovery_cp = MaxSector;
4051 }
4052
4053 skip:
4054 mddev->curr_resync = 0;
4055 wake_up(&resync_wait);
4056 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4057 md_wakeup_thread(mddev->thread);
4058 }
4059
4060
4061 /*
4062 * This routine is regularly called by all per-raid-array threads to
4063 * deal with generic issues like resync and super-block update.
4064 * Raid personalities that don't have a thread (linear/raid0) do not
4065 * need this as they never do any recovery or update the superblock.
4066 *
4067 * It does not do any resync itself, but rather "forks" off other threads
4068 * to do that as needed.
4069 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4070 * "->recovery" and create a thread at ->sync_thread.
4071 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4072 * and wakeups up this thread which will reap the thread and finish up.
4073 * This thread also removes any faulty devices (with nr_pending == 0).
4074 *
4075 * The overall approach is:
4076 * 1/ if the superblock needs updating, update it.
4077 * 2/ If a recovery thread is running, don't do anything else.
4078 * 3/ If recovery has finished, clean up, possibly marking spares active.
4079 * 4/ If there are any faulty devices, remove them.
4080 * 5/ If array is degraded, try to add spares devices
4081 * 6/ If array has spares or is not in-sync, start a resync thread.
4082 */
4083 void md_check_recovery(mddev_t *mddev)
4084 {
4085 mdk_rdev_t *rdev;
4086 struct list_head *rtmp;
4087
4088
4089 if (mddev->bitmap)
4090 bitmap_daemon_work(mddev->bitmap);
4091
4092 if (mddev->ro)
4093 return;
4094
4095 if (signal_pending(current)) {
4096 if (mddev->pers->sync_request) {
4097 printk(KERN_INFO "md: %s in immediate safe mode\n",
4098 mdname(mddev));
4099 mddev->safemode = 2;
4100 }
4101 flush_signals(current);
4102 }
4103
4104 if ( ! (
4105 mddev->sb_dirty ||
4106 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
4107 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4108 (mddev->safemode == 1) ||
4109 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4110 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
4111 ))
4112 return;
4113
4114 if (mddev_trylock(mddev)==0) {
4115 int spares =0;
4116
4117 spin_lock(&mddev->write_lock);
4118 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4119 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4120 mddev->in_sync = 1;
4121 mddev->sb_dirty = 1;
4122 }
4123 if (mddev->safemode == 1)
4124 mddev->safemode = 0;
4125 spin_unlock(&mddev->write_lock);
4126
4127 if (mddev->sb_dirty)
4128 md_update_sb(mddev);
4129
4130
4131 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4132 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4133 /* resync/recovery still happening */
4134 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4135 goto unlock;
4136 }
4137 if (mddev->sync_thread) {
4138 /* resync has finished, collect result */
4139 md_unregister_thread(mddev->sync_thread);
4140 mddev->sync_thread = NULL;
4141 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4142 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4143 /* success...*/
4144 /* activate any spares */
4145 mddev->pers->spare_active(mddev);
4146 }
4147 md_update_sb(mddev);
4148
4149 /* if array is no-longer degraded, then any saved_raid_disk
4150 * information must be scrapped
4151 */
4152 if (!mddev->degraded)
4153 ITERATE_RDEV(mddev,rdev,rtmp)
4154 rdev->saved_raid_disk = -1;
4155
4156 mddev->recovery = 0;
4157 /* flag recovery needed just to double check */
4158 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4159 goto unlock;
4160 }
4161 /* Clear some bits that don't mean anything, but
4162 * might be left set
4163 */
4164 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4165 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4166 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4167 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4168
4169 /* no recovery is running.
4170 * remove any failed drives, then
4171 * add spares if possible.
4172 * Spare are also removed and re-added, to allow
4173 * the personality to fail the re-add.
4174 */
4175 ITERATE_RDEV(mddev,rdev,rtmp)
4176 if (rdev->raid_disk >= 0 &&
4177 (rdev->faulty || ! rdev->in_sync) &&
4178 atomic_read(&rdev->nr_pending)==0) {
4179 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4180 char nm[20];
4181 sprintf(nm,"rd%d", rdev->raid_disk);
4182 sysfs_remove_link(&mddev->kobj, nm);
4183 rdev->raid_disk = -1;
4184 }
4185 }
4186
4187 if (mddev->degraded) {
4188 ITERATE_RDEV(mddev,rdev,rtmp)
4189 if (rdev->raid_disk < 0
4190 && !rdev->faulty) {
4191 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4192 char nm[20];
4193 sprintf(nm, "rd%d", rdev->raid_disk);
4194 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
4195 spares++;
4196 } else
4197 break;
4198 }
4199 }
4200
4201 if (spares) {
4202 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4203 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4204 } else if (mddev->recovery_cp < MaxSector) {
4205 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4206 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4207 /* nothing to be done ... */
4208 goto unlock;
4209
4210 if (mddev->pers->sync_request) {
4211 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4212 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4213 /* We are adding a device or devices to an array
4214 * which has the bitmap stored on all devices.
4215 * So make sure all bitmap pages get written
4216 */
4217 bitmap_write_all(mddev->bitmap);
4218 }
4219 mddev->sync_thread = md_register_thread(md_do_sync,
4220 mddev,
4221 "%s_resync");
4222 if (!mddev->sync_thread) {
4223 printk(KERN_ERR "%s: could not start resync"
4224 " thread...\n",
4225 mdname(mddev));
4226 /* leave the spares where they are, it shouldn't hurt */
4227 mddev->recovery = 0;
4228 } else {
4229 md_wakeup_thread(mddev->sync_thread);
4230 }
4231 }
4232 unlock:
4233 mddev_unlock(mddev);
4234 }
4235 }
4236
4237 static int md_notify_reboot(struct notifier_block *this,
4238 unsigned long code, void *x)
4239 {
4240 struct list_head *tmp;
4241 mddev_t *mddev;
4242
4243 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4244
4245 printk(KERN_INFO "md: stopping all md devices.\n");
4246
4247 ITERATE_MDDEV(mddev,tmp)
4248 if (mddev_trylock(mddev)==0)
4249 do_md_stop (mddev, 1);
4250 /*
4251 * certain more exotic SCSI devices are known to be
4252 * volatile wrt too early system reboots. While the
4253 * right place to handle this issue is the given
4254 * driver, we do want to have a safe RAID driver ...
4255 */
4256 mdelay(1000*1);
4257 }
4258 return NOTIFY_DONE;
4259 }
4260
4261 static struct notifier_block md_notifier = {
4262 .notifier_call = md_notify_reboot,
4263 .next = NULL,
4264 .priority = INT_MAX, /* before any real devices */
4265 };
4266
4267 static void md_geninit(void)
4268 {
4269 struct proc_dir_entry *p;
4270
4271 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4272
4273 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4274 if (p)
4275 p->proc_fops = &md_seq_fops;
4276 }
4277
4278 static int __init md_init(void)
4279 {
4280 int minor;
4281
4282 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4283 " MD_SB_DISKS=%d\n",
4284 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4285 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
4286 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR,
4287 BITMAP_MINOR);
4288
4289 if (register_blkdev(MAJOR_NR, "md"))
4290 return -1;
4291 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4292 unregister_blkdev(MAJOR_NR, "md");
4293 return -1;
4294 }
4295 devfs_mk_dir("md");
4296 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4297 md_probe, NULL, NULL);
4298 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4299 md_probe, NULL, NULL);
4300
4301 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4302 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4303 S_IFBLK|S_IRUSR|S_IWUSR,
4304 "md/%d", minor);
4305
4306 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4307 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4308 S_IFBLK|S_IRUSR|S_IWUSR,
4309 "md/mdp%d", minor);
4310
4311
4312 register_reboot_notifier(&md_notifier);
4313 raid_table_header = register_sysctl_table(raid_root_table, 1);
4314
4315 md_geninit();
4316 return (0);
4317 }
4318
4319
4320 #ifndef MODULE
4321
4322 /*
4323 * Searches all registered partitions for autorun RAID arrays
4324 * at boot time.
4325 */
4326 static dev_t detected_devices[128];
4327 static int dev_cnt;
4328
4329 void md_autodetect_dev(dev_t dev)
4330 {
4331 if (dev_cnt >= 0 && dev_cnt < 127)
4332 detected_devices[dev_cnt++] = dev;
4333 }
4334
4335
4336 static void autostart_arrays(int part)
4337 {
4338 mdk_rdev_t *rdev;
4339 int i;
4340
4341 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4342
4343 for (i = 0; i < dev_cnt; i++) {
4344 dev_t dev = detected_devices[i];
4345
4346 rdev = md_import_device(dev,0, 0);
4347 if (IS_ERR(rdev))
4348 continue;
4349
4350 if (rdev->faulty) {
4351 MD_BUG();
4352 continue;
4353 }
4354 list_add(&rdev->same_set, &pending_raid_disks);
4355 }
4356 dev_cnt = 0;
4357
4358 autorun_devices(part);
4359 }
4360
4361 #endif
4362
4363 static __exit void md_exit(void)
4364 {
4365 mddev_t *mddev;
4366 struct list_head *tmp;
4367 int i;
4368 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4369 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4370 for (i=0; i < MAX_MD_DEVS; i++)
4371 devfs_remove("md/%d", i);
4372 for (i=0; i < MAX_MD_DEVS; i++)
4373 devfs_remove("md/d%d", i);
4374
4375 devfs_remove("md");
4376
4377 unregister_blkdev(MAJOR_NR,"md");
4378 unregister_blkdev(mdp_major, "mdp");
4379 unregister_reboot_notifier(&md_notifier);
4380 unregister_sysctl_table(raid_table_header);
4381 remove_proc_entry("mdstat", NULL);
4382 ITERATE_MDDEV(mddev,tmp) {
4383 struct gendisk *disk = mddev->gendisk;
4384 if (!disk)
4385 continue;
4386 export_array(mddev);
4387 del_gendisk(disk);
4388 put_disk(disk);
4389 mddev->gendisk = NULL;
4390 mddev_put(mddev);
4391 }
4392 }
4393
4394 module_init(md_init)
4395 module_exit(md_exit)
4396
4397 EXPORT_SYMBOL(register_md_personality);
4398 EXPORT_SYMBOL(unregister_md_personality);
4399 EXPORT_SYMBOL(md_error);
4400 EXPORT_SYMBOL(md_done_sync);
4401 EXPORT_SYMBOL(md_write_start);
4402 EXPORT_SYMBOL(md_write_end);
4403 EXPORT_SYMBOL(md_register_thread);
4404 EXPORT_SYMBOL(md_unregister_thread);
4405 EXPORT_SYMBOL(md_wakeup_thread);
4406 EXPORT_SYMBOL(md_print_devices);
4407 EXPORT_SYMBOL(md_check_recovery);
4408 MODULE_LICENSE("GPL");
4409 MODULE_ALIAS("md");
4410 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);