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