blob: f1ac356e656d6423929aab6f860d30db22b33193 [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;
NeilBrown36fa3062005-09-09 16:23:45 -0700626 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (sb->state & (1<<MD_SB_CLEAN))
629 mddev->recovery_cp = MaxSector;
630 else {
631 if (sb->events_hi == sb->cp_events_hi &&
632 sb->events_lo == sb->cp_events_lo) {
633 mddev->recovery_cp = sb->recovery_cp;
634 } else
635 mddev->recovery_cp = 0;
636 }
637
638 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
639 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
640 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
641 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
642
643 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700644
645 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
646 mddev->bitmap_file == NULL) {
647 if (mddev->level != 1) {
648 /* FIXME use a better test */
649 printk(KERN_WARNING "md: bitmaps only support for raid1\n");
650 return -EINVAL;
651 }
NeilBrown36fa3062005-09-09 16:23:45 -0700652 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700653 }
654
NeilBrown41158c72005-06-21 17:17:25 -0700655 } else if (mddev->pers == NULL) {
656 /* Insist on good event counter while assembling */
657 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 ++ev1;
659 if (ev1 < mddev->events)
660 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700661 } else if (mddev->bitmap) {
662 /* if adding to array with a bitmap, then we can accept an
663 * older device ... but not too old.
664 */
665 __u64 ev1 = md_event(sb);
666 if (ev1 < mddev->bitmap->events_cleared)
667 return 0;
668 } else /* just a hot-add of a new device, leave raid_disk at -1 */
669 return 0;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (mddev->level != LEVEL_MULTIPATH) {
NeilBrown41158c72005-06-21 17:17:25 -0700672 rdev->faulty = 0;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700673 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 desc = sb->disks + rdev->desc_nr;
675
676 if (desc->state & (1<<MD_DISK_FAULTY))
677 rdev->faulty = 1;
678 else if (desc->state & (1<<MD_DISK_SYNC) &&
679 desc->raid_disk < mddev->raid_disks) {
680 rdev->in_sync = 1;
681 rdev->raid_disk = desc->raid_disk;
682 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700683 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
684 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700685 } else /* MULTIPATH are always insync */
686 rdev->in_sync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
688}
689
690/*
691 * sync_super for 0.90.0
692 */
693static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
694{
695 mdp_super_t *sb;
696 struct list_head *tmp;
697 mdk_rdev_t *rdev2;
698 int next_spare = mddev->raid_disks;
699
700 /* make rdev->sb match mddev data..
701 *
702 * 1/ zero out disks
703 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
704 * 3/ any empty disks < next_spare become removed
705 *
706 * disks[0] gets initialised to REMOVED because
707 * we cannot be sure from other fields if it has
708 * been initialised or not.
709 */
710 int i;
711 int active=0, working=0,failed=0,spare=0,nr_disks=0;
712
713 sb = (mdp_super_t*)page_address(rdev->sb_page);
714
715 memset(sb, 0, sizeof(*sb));
716
717 sb->md_magic = MD_SB_MAGIC;
718 sb->major_version = mddev->major_version;
719 sb->minor_version = mddev->minor_version;
720 sb->patch_version = mddev->patch_version;
721 sb->gvalid_words = 0; /* ignored */
722 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
723 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
724 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
725 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
726
727 sb->ctime = mddev->ctime;
728 sb->level = mddev->level;
729 sb->size = mddev->size;
730 sb->raid_disks = mddev->raid_disks;
731 sb->md_minor = mddev->md_minor;
732 sb->not_persistent = !mddev->persistent;
733 sb->utime = mddev->utime;
734 sb->state = 0;
735 sb->events_hi = (mddev->events>>32);
736 sb->events_lo = (u32)mddev->events;
737
738 if (mddev->in_sync)
739 {
740 sb->recovery_cp = mddev->recovery_cp;
741 sb->cp_events_hi = (mddev->events>>32);
742 sb->cp_events_lo = (u32)mddev->events;
743 if (mddev->recovery_cp == MaxSector)
744 sb->state = (1<< MD_SB_CLEAN);
745 } else
746 sb->recovery_cp = 0;
747
748 sb->layout = mddev->layout;
749 sb->chunk_size = mddev->chunk_size;
750
NeilBrowna654b9d82005-06-21 17:17:27 -0700751 if (mddev->bitmap && mddev->bitmap_file == NULL)
752 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 sb->disks[0].state = (1<<MD_DISK_REMOVED);
755 ITERATE_RDEV(mddev,rdev2,tmp) {
756 mdp_disk_t *d;
757 if (rdev2->raid_disk >= 0 && rdev2->in_sync && !rdev2->faulty)
758 rdev2->desc_nr = rdev2->raid_disk;
759 else
760 rdev2->desc_nr = next_spare++;
761 d = &sb->disks[rdev2->desc_nr];
762 nr_disks++;
763 d->number = rdev2->desc_nr;
764 d->major = MAJOR(rdev2->bdev->bd_dev);
765 d->minor = MINOR(rdev2->bdev->bd_dev);
766 if (rdev2->raid_disk >= 0 && rdev->in_sync && !rdev2->faulty)
767 d->raid_disk = rdev2->raid_disk;
768 else
769 d->raid_disk = rdev2->desc_nr; /* compatibility */
770 if (rdev2->faulty) {
771 d->state = (1<<MD_DISK_FAULTY);
772 failed++;
773 } else if (rdev2->in_sync) {
774 d->state = (1<<MD_DISK_ACTIVE);
775 d->state |= (1<<MD_DISK_SYNC);
776 active++;
777 working++;
778 } else {
779 d->state = 0;
780 spare++;
781 working++;
782 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700783 if (test_bit(WriteMostly, &rdev2->flags))
784 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786
787 /* now set the "removed" and "faulty" bits on any missing devices */
788 for (i=0 ; i < mddev->raid_disks ; i++) {
789 mdp_disk_t *d = &sb->disks[i];
790 if (d->state == 0 && d->number == 0) {
791 d->number = i;
792 d->raid_disk = i;
793 d->state = (1<<MD_DISK_REMOVED);
794 d->state |= (1<<MD_DISK_FAULTY);
795 failed++;
796 }
797 }
798 sb->nr_disks = nr_disks;
799 sb->active_disks = active;
800 sb->working_disks = working;
801 sb->failed_disks = failed;
802 sb->spare_disks = spare;
803
804 sb->this_disk = sb->disks[rdev->desc_nr];
805 sb->sb_csum = calc_sb_csum(sb);
806}
807
808/*
809 * version 1 superblock
810 */
811
812static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
813{
814 unsigned int disk_csum, csum;
815 unsigned long long newcsum;
816 int size = 256 + le32_to_cpu(sb->max_dev)*2;
817 unsigned int *isuper = (unsigned int*)sb;
818 int i;
819
820 disk_csum = sb->sb_csum;
821 sb->sb_csum = 0;
822 newcsum = 0;
823 for (i=0; size>=4; size -= 4 )
824 newcsum += le32_to_cpu(*isuper++);
825
826 if (size == 2)
827 newcsum += le16_to_cpu(*(unsigned short*) isuper);
828
829 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
830 sb->sb_csum = disk_csum;
831 return cpu_to_le32(csum);
832}
833
834static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
835{
836 struct mdp_superblock_1 *sb;
837 int ret;
838 sector_t sb_offset;
839 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
840
841 /*
842 * Calculate the position of the superblock.
843 * It is always aligned to a 4K boundary and
844 * depeding on minor_version, it can be:
845 * 0: At least 8K, but less than 12K, from end of device
846 * 1: At start of device
847 * 2: 4K from start of device.
848 */
849 switch(minor_version) {
850 case 0:
851 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
852 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -0700853 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* convert from sectors to K */
855 sb_offset /= 2;
856 break;
857 case 1:
858 sb_offset = 0;
859 break;
860 case 2:
861 sb_offset = 4;
862 break;
863 default:
864 return -EINVAL;
865 }
866 rdev->sb_offset = sb_offset;
867
868 ret = read_disk_sb(rdev);
869 if (ret) return ret;
870
871
872 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
873
874 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
875 sb->major_version != cpu_to_le32(1) ||
876 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
877 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
878 sb->feature_map != 0)
879 return -EINVAL;
880
881 if (calc_sb_1_csum(sb) != sb->sb_csum) {
882 printk("md: invalid superblock checksum on %s\n",
883 bdevname(rdev->bdev,b));
884 return -EINVAL;
885 }
886 if (le64_to_cpu(sb->data_size) < 10) {
887 printk("md: data_size too small on %s\n",
888 bdevname(rdev->bdev,b));
889 return -EINVAL;
890 }
891 rdev->preferred_minor = 0xffff;
892 rdev->data_offset = le64_to_cpu(sb->data_offset);
893
894 if (refdev == 0)
895 return 1;
896 else {
897 __u64 ev1, ev2;
898 struct mdp_superblock_1 *refsb =
899 (struct mdp_superblock_1*)page_address(refdev->sb_page);
900
901 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
902 sb->level != refsb->level ||
903 sb->layout != refsb->layout ||
904 sb->chunksize != refsb->chunksize) {
905 printk(KERN_WARNING "md: %s has strangely different"
906 " superblock to %s\n",
907 bdevname(rdev->bdev,b),
908 bdevname(refdev->bdev,b2));
909 return -EINVAL;
910 }
911 ev1 = le64_to_cpu(sb->events);
912 ev2 = le64_to_cpu(refsb->events);
913
914 if (ev1 > ev2)
915 return 1;
916 }
917 if (minor_version)
918 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
919 else
920 rdev->size = rdev->sb_offset;
921 if (rdev->size < le64_to_cpu(sb->data_size)/2)
922 return -EINVAL;
923 rdev->size = le64_to_cpu(sb->data_size)/2;
924 if (le32_to_cpu(sb->chunksize))
925 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
926 return 0;
927}
928
929static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
930{
931 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
932
NeilBrown41158c72005-06-21 17:17:25 -0700933 rdev->raid_disk = -1;
934 rdev->in_sync = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (mddev->raid_disks == 0) {
936 mddev->major_version = 1;
937 mddev->patch_version = 0;
938 mddev->persistent = 1;
939 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
940 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
941 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
942 mddev->level = le32_to_cpu(sb->level);
943 mddev->layout = le32_to_cpu(sb->layout);
944 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
945 mddev->size = le64_to_cpu(sb->size)/2;
946 mddev->events = le64_to_cpu(sb->events);
NeilBrown92232142005-08-18 11:24:16 -0700947 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700948 mddev->default_bitmap_offset = 0;
949 if (mddev->minor_version == 0)
950 mddev->default_bitmap_offset = -(64*1024)/512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
953 memcpy(mddev->uuid, sb->set_uuid, 16);
954
955 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -0700956
957 if ((le32_to_cpu(sb->feature_map) & 1) &&
958 mddev->bitmap_file == NULL ) {
959 if (mddev->level != 1) {
960 printk(KERN_WARNING "md: bitmaps only supported for raid1\n");
961 return -EINVAL;
962 }
963 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
964 }
NeilBrown41158c72005-06-21 17:17:25 -0700965 } else if (mddev->pers == NULL) {
966 /* Insist of good event counter while assembling */
967 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 ++ev1;
969 if (ev1 < mddev->events)
970 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700971 } else if (mddev->bitmap) {
972 /* If adding to array with a bitmap, then we can accept an
973 * older device, but not too old.
974 */
975 __u64 ev1 = le64_to_cpu(sb->events);
976 if (ev1 < mddev->bitmap->events_cleared)
977 return 0;
978 } else /* just a hot-add of a new device, leave raid_disk at -1 */
979 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if (mddev->level != LEVEL_MULTIPATH) {
982 int role;
983 rdev->desc_nr = le32_to_cpu(sb->dev_number);
984 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
985 switch(role) {
986 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 rdev->faulty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 break;
989 case 0xfffe: /* faulty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 rdev->faulty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 break;
992 default:
993 rdev->in_sync = 1;
994 rdev->faulty = 0;
995 rdev->raid_disk = role;
996 break;
997 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700998 rdev->flags = 0;
999 if (sb->devflags & WriteMostly1)
1000 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001001 } else /* MULTIPATH are always insync */
1002 rdev->in_sync = 1;
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return 0;
1005}
1006
1007static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1008{
1009 struct mdp_superblock_1 *sb;
1010 struct list_head *tmp;
1011 mdk_rdev_t *rdev2;
1012 int max_dev, i;
1013 /* make rdev->sb match mddev and rdev data. */
1014
1015 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1016
1017 sb->feature_map = 0;
1018 sb->pad0 = 0;
1019 memset(sb->pad1, 0, sizeof(sb->pad1));
1020 memset(sb->pad2, 0, sizeof(sb->pad2));
1021 memset(sb->pad3, 0, sizeof(sb->pad3));
1022
1023 sb->utime = cpu_to_le64((__u64)mddev->utime);
1024 sb->events = cpu_to_le64(mddev->events);
1025 if (mddev->in_sync)
1026 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1027 else
1028 sb->resync_offset = cpu_to_le64(0);
1029
NeilBrowna654b9d82005-06-21 17:17:27 -07001030 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1031 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1032 sb->feature_map = cpu_to_le32(1);
1033 }
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 max_dev = 0;
1036 ITERATE_RDEV(mddev,rdev2,tmp)
1037 if (rdev2->desc_nr+1 > max_dev)
1038 max_dev = rdev2->desc_nr+1;
1039
1040 sb->max_dev = cpu_to_le32(max_dev);
1041 for (i=0; i<max_dev;i++)
1042 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1043
1044 ITERATE_RDEV(mddev,rdev2,tmp) {
1045 i = rdev2->desc_nr;
1046 if (rdev2->faulty)
1047 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1048 else if (rdev2->in_sync)
1049 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1050 else
1051 sb->dev_roles[i] = cpu_to_le16(0xffff);
1052 }
1053
1054 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1055 sb->sb_csum = calc_sb_1_csum(sb);
1056}
1057
1058
Adrian Bunk75c96f82005-05-05 16:16:09 -07001059static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 [0] = {
1061 .name = "0.90.0",
1062 .owner = THIS_MODULE,
1063 .load_super = super_90_load,
1064 .validate_super = super_90_validate,
1065 .sync_super = super_90_sync,
1066 },
1067 [1] = {
1068 .name = "md-1",
1069 .owner = THIS_MODULE,
1070 .load_super = super_1_load,
1071 .validate_super = super_1_validate,
1072 .sync_super = super_1_sync,
1073 },
1074};
1075
1076static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1077{
1078 struct list_head *tmp;
1079 mdk_rdev_t *rdev;
1080
1081 ITERATE_RDEV(mddev,rdev,tmp)
1082 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1083 return rdev;
1084
1085 return NULL;
1086}
1087
1088static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1089{
1090 struct list_head *tmp;
1091 mdk_rdev_t *rdev;
1092
1093 ITERATE_RDEV(mddev1,rdev,tmp)
1094 if (match_dev_unit(mddev2, rdev))
1095 return 1;
1096
1097 return 0;
1098}
1099
1100static LIST_HEAD(pending_raid_disks);
1101
1102static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1103{
1104 mdk_rdev_t *same_pdev;
1105 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1106
1107 if (rdev->mddev) {
1108 MD_BUG();
1109 return -EINVAL;
1110 }
1111 same_pdev = match_dev_unit(mddev, rdev);
1112 if (same_pdev)
1113 printk(KERN_WARNING
1114 "%s: WARNING: %s appears to be on the same physical"
1115 " disk as %s. True\n protection against single-disk"
1116 " failure might be compromised.\n",
1117 mdname(mddev), bdevname(rdev->bdev,b),
1118 bdevname(same_pdev->bdev,b2));
1119
1120 /* Verify rdev->desc_nr is unique.
1121 * If it is -1, assign a free number, else
1122 * check number is not in use
1123 */
1124 if (rdev->desc_nr < 0) {
1125 int choice = 0;
1126 if (mddev->pers) choice = mddev->raid_disks;
1127 while (find_rdev_nr(mddev, choice))
1128 choice++;
1129 rdev->desc_nr = choice;
1130 } else {
1131 if (find_rdev_nr(mddev, rdev->desc_nr))
1132 return -EBUSY;
1133 }
1134
1135 list_add(&rdev->same_set, &mddev->disks);
1136 rdev->mddev = mddev;
1137 printk(KERN_INFO "md: bind<%s>\n", bdevname(rdev->bdev,b));
1138 return 0;
1139}
1140
1141static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1142{
1143 char b[BDEVNAME_SIZE];
1144 if (!rdev->mddev) {
1145 MD_BUG();
1146 return;
1147 }
1148 list_del_init(&rdev->same_set);
1149 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1150 rdev->mddev = NULL;
1151}
1152
1153/*
1154 * prevent the device from being mounted, repartitioned or
1155 * otherwise reused by a RAID array (or any other kernel
1156 * subsystem), by bd_claiming the device.
1157 */
1158static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1159{
1160 int err = 0;
1161 struct block_device *bdev;
1162 char b[BDEVNAME_SIZE];
1163
1164 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1165 if (IS_ERR(bdev)) {
1166 printk(KERN_ERR "md: could not open %s.\n",
1167 __bdevname(dev, b));
1168 return PTR_ERR(bdev);
1169 }
1170 err = bd_claim(bdev, rdev);
1171 if (err) {
1172 printk(KERN_ERR "md: could not bd_claim %s.\n",
1173 bdevname(bdev, b));
1174 blkdev_put(bdev);
1175 return err;
1176 }
1177 rdev->bdev = bdev;
1178 return err;
1179}
1180
1181static void unlock_rdev(mdk_rdev_t *rdev)
1182{
1183 struct block_device *bdev = rdev->bdev;
1184 rdev->bdev = NULL;
1185 if (!bdev)
1186 MD_BUG();
1187 bd_release(bdev);
1188 blkdev_put(bdev);
1189}
1190
1191void md_autodetect_dev(dev_t dev);
1192
1193static void export_rdev(mdk_rdev_t * rdev)
1194{
1195 char b[BDEVNAME_SIZE];
1196 printk(KERN_INFO "md: export_rdev(%s)\n",
1197 bdevname(rdev->bdev,b));
1198 if (rdev->mddev)
1199 MD_BUG();
1200 free_disk_sb(rdev);
1201 list_del_init(&rdev->same_set);
1202#ifndef MODULE
1203 md_autodetect_dev(rdev->bdev->bd_dev);
1204#endif
1205 unlock_rdev(rdev);
1206 kfree(rdev);
1207}
1208
1209static void kick_rdev_from_array(mdk_rdev_t * rdev)
1210{
1211 unbind_rdev_from_array(rdev);
1212 export_rdev(rdev);
1213}
1214
1215static void export_array(mddev_t *mddev)
1216{
1217 struct list_head *tmp;
1218 mdk_rdev_t *rdev;
1219
1220 ITERATE_RDEV(mddev,rdev,tmp) {
1221 if (!rdev->mddev) {
1222 MD_BUG();
1223 continue;
1224 }
1225 kick_rdev_from_array(rdev);
1226 }
1227 if (!list_empty(&mddev->disks))
1228 MD_BUG();
1229 mddev->raid_disks = 0;
1230 mddev->major_version = 0;
1231}
1232
1233static void print_desc(mdp_disk_t *desc)
1234{
1235 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1236 desc->major,desc->minor,desc->raid_disk,desc->state);
1237}
1238
1239static void print_sb(mdp_super_t *sb)
1240{
1241 int i;
1242
1243 printk(KERN_INFO
1244 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1245 sb->major_version, sb->minor_version, sb->patch_version,
1246 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1247 sb->ctime);
1248 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1249 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1250 sb->md_minor, sb->layout, sb->chunk_size);
1251 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1252 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1253 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1254 sb->failed_disks, sb->spare_disks,
1255 sb->sb_csum, (unsigned long)sb->events_lo);
1256
1257 printk(KERN_INFO);
1258 for (i = 0; i < MD_SB_DISKS; i++) {
1259 mdp_disk_t *desc;
1260
1261 desc = sb->disks + i;
1262 if (desc->number || desc->major || desc->minor ||
1263 desc->raid_disk || (desc->state && (desc->state != 4))) {
1264 printk(" D %2d: ", i);
1265 print_desc(desc);
1266 }
1267 }
1268 printk(KERN_INFO "md: THIS: ");
1269 print_desc(&sb->this_disk);
1270
1271}
1272
1273static void print_rdev(mdk_rdev_t *rdev)
1274{
1275 char b[BDEVNAME_SIZE];
1276 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1277 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1278 rdev->faulty, rdev->in_sync, rdev->desc_nr);
1279 if (rdev->sb_loaded) {
1280 printk(KERN_INFO "md: rdev superblock:\n");
1281 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1282 } else
1283 printk(KERN_INFO "md: no rdev superblock!\n");
1284}
1285
1286void md_print_devices(void)
1287{
1288 struct list_head *tmp, *tmp2;
1289 mdk_rdev_t *rdev;
1290 mddev_t *mddev;
1291 char b[BDEVNAME_SIZE];
1292
1293 printk("\n");
1294 printk("md: **********************************\n");
1295 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1296 printk("md: **********************************\n");
1297 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
NeilBrown32a76272005-06-21 17:17:14 -07001299 if (mddev->bitmap)
1300 bitmap_print_sb(mddev->bitmap);
1301 else
1302 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 ITERATE_RDEV(mddev,rdev,tmp2)
1304 printk("<%s>", bdevname(rdev->bdev,b));
1305 printk("\n");
1306
1307 ITERATE_RDEV(mddev,rdev,tmp2)
1308 print_rdev(rdev);
1309 }
1310 printk("md: **********************************\n");
1311 printk("\n");
1312}
1313
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315static void sync_sbs(mddev_t * mddev)
1316{
1317 mdk_rdev_t *rdev;
1318 struct list_head *tmp;
1319
1320 ITERATE_RDEV(mddev,rdev,tmp) {
1321 super_types[mddev->major_version].
1322 sync_super(mddev, rdev);
1323 rdev->sb_loaded = 1;
1324 }
1325}
1326
1327static void md_update_sb(mddev_t * mddev)
1328{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001329 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 struct list_head *tmp;
1331 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001332 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334repeat:
NeilBrown06d91a52005-06-21 17:17:12 -07001335 spin_lock(&mddev->write_lock);
1336 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 mddev->utime = get_seconds();
1338 mddev->events ++;
1339
1340 if (!mddev->events) {
1341 /*
1342 * oops, this 64-bit counter should never wrap.
1343 * Either we are in around ~1 trillion A.C., assuming
1344 * 1 reboot per second, or we have a bug:
1345 */
1346 MD_BUG();
1347 mddev->events --;
1348 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001349 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 sync_sbs(mddev);
1351
1352 /*
1353 * do not write anything to disk if using
1354 * nonpersistent superblocks
1355 */
NeilBrown06d91a52005-06-21 17:17:12 -07001356 if (!mddev->persistent) {
1357 mddev->sb_dirty = 0;
1358 spin_unlock(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001359 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001361 }
1362 spin_unlock(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 dprintk(KERN_INFO
1365 "md: updating %s RAID superblock on device (in sync %d)\n",
1366 mdname(mddev),mddev->in_sync);
1367
NeilBrown32a76272005-06-21 17:17:14 -07001368 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 ITERATE_RDEV(mddev,rdev,tmp) {
1370 char b[BDEVNAME_SIZE];
1371 dprintk(KERN_INFO "md: ");
1372 if (rdev->faulty)
1373 dprintk("(skipping faulty ");
1374
1375 dprintk("%s ", bdevname(rdev->bdev,b));
1376 if (!rdev->faulty) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001377 md_super_write(mddev,rdev,
1378 rdev->sb_offset<<1, MD_SB_BYTES,
1379 rdev->sb_page);
1380 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1381 bdevname(rdev->bdev,b),
1382 (unsigned long long)rdev->sb_offset);
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 } else
1385 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001386 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /* only need to write one superblock... */
1388 break;
1389 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001390 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
1391 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1392
NeilBrown06d91a52005-06-21 17:17:12 -07001393 spin_lock(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001394 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001395 /* have to write it out again */
1396 spin_unlock(&mddev->write_lock);
1397 goto repeat;
1398 }
1399 mddev->sb_dirty = 0;
1400 spin_unlock(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001401 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405/*
1406 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1407 *
1408 * mark the device faulty if:
1409 *
1410 * - the device is nonexistent (zero size)
1411 * - the device has no valid superblock
1412 *
1413 * a faulty rdev _never_ has rdev->sb set.
1414 */
1415static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1416{
1417 char b[BDEVNAME_SIZE];
1418 int err;
1419 mdk_rdev_t *rdev;
1420 sector_t size;
1421
1422 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1423 if (!rdev) {
1424 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1425 return ERR_PTR(-ENOMEM);
1426 }
1427 memset(rdev, 0, sizeof(*rdev));
1428
1429 if ((err = alloc_disk_sb(rdev)))
1430 goto abort_free;
1431
1432 err = lock_rdev(rdev, newdev);
1433 if (err)
1434 goto abort_free;
1435
1436 rdev->desc_nr = -1;
1437 rdev->faulty = 0;
1438 rdev->in_sync = 0;
1439 rdev->data_offset = 0;
1440 atomic_set(&rdev->nr_pending, 0);
1441
1442 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1443 if (!size) {
1444 printk(KERN_WARNING
1445 "md: %s has zero or unknown size, marking faulty!\n",
1446 bdevname(rdev->bdev,b));
1447 err = -EINVAL;
1448 goto abort_free;
1449 }
1450
1451 if (super_format >= 0) {
1452 err = super_types[super_format].
1453 load_super(rdev, NULL, super_minor);
1454 if (err == -EINVAL) {
1455 printk(KERN_WARNING
1456 "md: %s has invalid sb, not importing!\n",
1457 bdevname(rdev->bdev,b));
1458 goto abort_free;
1459 }
1460 if (err < 0) {
1461 printk(KERN_WARNING
1462 "md: could not read %s's sb, not importing!\n",
1463 bdevname(rdev->bdev,b));
1464 goto abort_free;
1465 }
1466 }
1467 INIT_LIST_HEAD(&rdev->same_set);
1468
1469 return rdev;
1470
1471abort_free:
1472 if (rdev->sb_page) {
1473 if (rdev->bdev)
1474 unlock_rdev(rdev);
1475 free_disk_sb(rdev);
1476 }
1477 kfree(rdev);
1478 return ERR_PTR(err);
1479}
1480
1481/*
1482 * Check a full RAID array for plausibility
1483 */
1484
1485
NeilBrowna757e642005-04-16 15:26:42 -07001486static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 int i;
1489 struct list_head *tmp;
1490 mdk_rdev_t *rdev, *freshest;
1491 char b[BDEVNAME_SIZE];
1492
1493 freshest = NULL;
1494 ITERATE_RDEV(mddev,rdev,tmp)
1495 switch (super_types[mddev->major_version].
1496 load_super(rdev, freshest, mddev->minor_version)) {
1497 case 1:
1498 freshest = rdev;
1499 break;
1500 case 0:
1501 break;
1502 default:
1503 printk( KERN_ERR \
1504 "md: fatal superblock inconsistency in %s"
1505 " -- removing from array\n",
1506 bdevname(rdev->bdev,b));
1507 kick_rdev_from_array(rdev);
1508 }
1509
1510
1511 super_types[mddev->major_version].
1512 validate_super(mddev, freshest);
1513
1514 i = 0;
1515 ITERATE_RDEV(mddev,rdev,tmp) {
1516 if (rdev != freshest)
1517 if (super_types[mddev->major_version].
1518 validate_super(mddev, rdev)) {
1519 printk(KERN_WARNING "md: kicking non-fresh %s"
1520 " from array!\n",
1521 bdevname(rdev->bdev,b));
1522 kick_rdev_from_array(rdev);
1523 continue;
1524 }
1525 if (mddev->level == LEVEL_MULTIPATH) {
1526 rdev->desc_nr = i++;
1527 rdev->raid_disk = rdev->desc_nr;
1528 rdev->in_sync = 1;
1529 }
1530 }
1531
1532
1533
1534 if (mddev->recovery_cp != MaxSector &&
1535 mddev->level >= 1)
1536 printk(KERN_ERR "md: %s: raid array is not clean"
1537 " -- starting background reconstruction\n",
1538 mdname(mddev));
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
1542int mdp_major = 0;
1543
1544static struct kobject *md_probe(dev_t dev, int *part, void *data)
1545{
1546 static DECLARE_MUTEX(disks_sem);
1547 mddev_t *mddev = mddev_find(dev);
1548 struct gendisk *disk;
1549 int partitioned = (MAJOR(dev) != MD_MAJOR);
1550 int shift = partitioned ? MdpMinorShift : 0;
1551 int unit = MINOR(dev) >> shift;
1552
1553 if (!mddev)
1554 return NULL;
1555
1556 down(&disks_sem);
1557 if (mddev->gendisk) {
1558 up(&disks_sem);
1559 mddev_put(mddev);
1560 return NULL;
1561 }
1562 disk = alloc_disk(1 << shift);
1563 if (!disk) {
1564 up(&disks_sem);
1565 mddev_put(mddev);
1566 return NULL;
1567 }
1568 disk->major = MAJOR(dev);
1569 disk->first_minor = unit << shift;
1570 if (partitioned) {
1571 sprintf(disk->disk_name, "md_d%d", unit);
1572 sprintf(disk->devfs_name, "md/d%d", unit);
1573 } else {
1574 sprintf(disk->disk_name, "md%d", unit);
1575 sprintf(disk->devfs_name, "md/%d", unit);
1576 }
1577 disk->fops = &md_fops;
1578 disk->private_data = mddev;
1579 disk->queue = mddev->queue;
1580 add_disk(disk);
1581 mddev->gendisk = disk;
1582 up(&disks_sem);
1583 return NULL;
1584}
1585
1586void md_wakeup_thread(mdk_thread_t *thread);
1587
1588static void md_safemode_timeout(unsigned long data)
1589{
1590 mddev_t *mddev = (mddev_t *) data;
1591
1592 mddev->safemode = 1;
1593 md_wakeup_thread(mddev->thread);
1594}
1595
1596
1597static int do_md_run(mddev_t * mddev)
1598{
1599 int pnum, err;
1600 int chunk_size;
1601 struct list_head *tmp;
1602 mdk_rdev_t *rdev;
1603 struct gendisk *disk;
1604 char b[BDEVNAME_SIZE];
1605
NeilBrowna757e642005-04-16 15:26:42 -07001606 if (list_empty(&mddev->disks))
1607 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 if (mddev->pers)
1611 return -EBUSY;
1612
1613 /*
1614 * Analyze all RAID superblock(s)
1615 */
NeilBrowna757e642005-04-16 15:26:42 -07001616 if (!mddev->raid_disks)
1617 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 chunk_size = mddev->chunk_size;
1620 pnum = level_to_pers(mddev->level);
1621
1622 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1623 if (!chunk_size) {
1624 /*
1625 * 'default chunksize' in the old md code used to
1626 * be PAGE_SIZE, baaad.
1627 * we abort here to be on the safe side. We don't
1628 * want to continue the bad practice.
1629 */
1630 printk(KERN_ERR
1631 "no chunksize specified, see 'man raidtab'\n");
1632 return -EINVAL;
1633 }
1634 if (chunk_size > MAX_CHUNK_SIZE) {
1635 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1636 chunk_size, MAX_CHUNK_SIZE);
1637 return -EINVAL;
1638 }
1639 /*
1640 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1641 */
1642 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07001643 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return -EINVAL;
1645 }
1646 if (chunk_size < PAGE_SIZE) {
1647 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1648 chunk_size, PAGE_SIZE);
1649 return -EINVAL;
1650 }
1651
1652 /* devices must have minimum size of one chunk */
1653 ITERATE_RDEV(mddev,rdev,tmp) {
1654 if (rdev->faulty)
1655 continue;
1656 if (rdev->size < chunk_size / 1024) {
1657 printk(KERN_WARNING
1658 "md: Dev %s smaller than chunk_size:"
1659 " %lluk < %dk\n",
1660 bdevname(rdev->bdev,b),
1661 (unsigned long long)rdev->size,
1662 chunk_size / 1024);
1663 return -EINVAL;
1664 }
1665 }
1666 }
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668#ifdef CONFIG_KMOD
1669 if (!pers[pnum])
1670 {
1671 request_module("md-personality-%d", pnum);
1672 }
1673#endif
1674
1675 /*
1676 * Drop all container device buffers, from now on
1677 * the only valid external interface is through the md
1678 * device.
1679 * Also find largest hardsector size
1680 */
1681 ITERATE_RDEV(mddev,rdev,tmp) {
1682 if (rdev->faulty)
1683 continue;
1684 sync_blockdev(rdev->bdev);
1685 invalidate_bdev(rdev->bdev, 0);
1686 }
1687
1688 md_probe(mddev->unit, NULL, NULL);
1689 disk = mddev->gendisk;
1690 if (!disk)
1691 return -ENOMEM;
1692
1693 spin_lock(&pers_lock);
1694 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
1695 spin_unlock(&pers_lock);
1696 printk(KERN_WARNING "md: personality %d is not loaded!\n",
1697 pnum);
1698 return -EINVAL;
1699 }
1700
1701 mddev->pers = pers[pnum];
1702 spin_unlock(&pers_lock);
1703
NeilBrown657390d2005-08-26 18:34:16 -07001704 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
1706
NeilBrown32a76272005-06-21 17:17:14 -07001707 /* before we start the array running, initialise the bitmap */
1708 err = bitmap_create(mddev);
1709 if (err)
1710 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
1711 mdname(mddev), err);
1712 else
1713 err = mddev->pers->run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (err) {
1715 printk(KERN_ERR "md: pers->run() failed ...\n");
1716 module_put(mddev->pers->owner);
1717 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001718 bitmap_destroy(mddev);
1719 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721 atomic_set(&mddev->writes_pending,0);
1722 mddev->safemode = 0;
1723 mddev->safemode_timer.function = md_safemode_timeout;
1724 mddev->safemode_timer.data = (unsigned long) mddev;
1725 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
1726 mddev->in_sync = 1;
1727
1728 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07001729 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 if (mddev->sb_dirty)
1732 md_update_sb(mddev);
1733
1734 set_capacity(disk, mddev->array_size<<1);
1735
1736 /* If we call blk_queue_make_request here, it will
1737 * re-initialise max_sectors etc which may have been
1738 * refined inside -> run. So just set the bits we need to set.
1739 * Most initialisation happended when we called
1740 * blk_queue_make_request(..., md_fail_request)
1741 * earlier.
1742 */
1743 mddev->queue->queuedata = mddev;
1744 mddev->queue->make_request_fn = mddev->pers->make_request;
1745
1746 mddev->changed = 1;
1747 return 0;
1748}
1749
1750static int restart_array(mddev_t *mddev)
1751{
1752 struct gendisk *disk = mddev->gendisk;
1753 int err;
1754
1755 /*
1756 * Complain if it has no devices
1757 */
1758 err = -ENXIO;
1759 if (list_empty(&mddev->disks))
1760 goto out;
1761
1762 if (mddev->pers) {
1763 err = -EBUSY;
1764 if (!mddev->ro)
1765 goto out;
1766
1767 mddev->safemode = 0;
1768 mddev->ro = 0;
1769 set_disk_ro(disk, 0);
1770
1771 printk(KERN_INFO "md: %s switched to read-write mode.\n",
1772 mdname(mddev));
1773 /*
1774 * Kick recovery or resync if necessary
1775 */
1776 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1777 md_wakeup_thread(mddev->thread);
1778 err = 0;
1779 } else {
1780 printk(KERN_ERR "md: %s has no personality assigned.\n",
1781 mdname(mddev));
1782 err = -EINVAL;
1783 }
1784
1785out:
1786 return err;
1787}
1788
1789static int do_md_stop(mddev_t * mddev, int ro)
1790{
1791 int err = 0;
1792 struct gendisk *disk = mddev->gendisk;
1793
1794 if (mddev->pers) {
1795 if (atomic_read(&mddev->active)>2) {
1796 printk("md: %s still in use.\n",mdname(mddev));
1797 return -EBUSY;
1798 }
1799
1800 if (mddev->sync_thread) {
1801 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1802 md_unregister_thread(mddev->sync_thread);
1803 mddev->sync_thread = NULL;
1804 }
1805
1806 del_timer_sync(&mddev->safemode_timer);
1807
1808 invalidate_partition(disk, 0);
1809
1810 if (ro) {
1811 err = -ENXIO;
1812 if (mddev->ro)
1813 goto out;
1814 mddev->ro = 1;
1815 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07001816 bitmap_flush(mddev);
1817 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (mddev->ro)
1819 set_disk_ro(disk, 0);
1820 blk_queue_make_request(mddev->queue, md_fail_request);
1821 mddev->pers->stop(mddev);
1822 module_put(mddev->pers->owner);
1823 mddev->pers = NULL;
1824 if (mddev->ro)
1825 mddev->ro = 0;
1826 }
1827 if (!mddev->in_sync) {
1828 /* mark array as shutdown cleanly */
1829 mddev->in_sync = 1;
1830 md_update_sb(mddev);
1831 }
1832 if (ro)
1833 set_disk_ro(disk, 1);
1834 }
NeilBrown32a76272005-06-21 17:17:14 -07001835
1836 bitmap_destroy(mddev);
1837 if (mddev->bitmap_file) {
1838 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
1839 fput(mddev->bitmap_file);
1840 mddev->bitmap_file = NULL;
1841 }
NeilBrown92232142005-08-18 11:24:16 -07001842 mddev->bitmap_offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /*
1845 * Free resources if final stop
1846 */
1847 if (!ro) {
1848 struct gendisk *disk;
1849 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
1850
1851 export_array(mddev);
1852
1853 mddev->array_size = 0;
1854 disk = mddev->gendisk;
1855 if (disk)
1856 set_capacity(disk, 0);
1857 mddev->changed = 1;
1858 } else
1859 printk(KERN_INFO "md: %s switched to read-only mode.\n",
1860 mdname(mddev));
1861 err = 0;
1862out:
1863 return err;
1864}
1865
1866static void autorun_array(mddev_t *mddev)
1867{
1868 mdk_rdev_t *rdev;
1869 struct list_head *tmp;
1870 int err;
1871
NeilBrowna757e642005-04-16 15:26:42 -07001872 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 printk(KERN_INFO "md: running: ");
1876
1877 ITERATE_RDEV(mddev,rdev,tmp) {
1878 char b[BDEVNAME_SIZE];
1879 printk("<%s>", bdevname(rdev->bdev,b));
1880 }
1881 printk("\n");
1882
1883 err = do_md_run (mddev);
1884 if (err) {
1885 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
1886 do_md_stop (mddev, 0);
1887 }
1888}
1889
1890/*
1891 * lets try to run arrays based on all disks that have arrived
1892 * until now. (those are in pending_raid_disks)
1893 *
1894 * the method: pick the first pending disk, collect all disks with
1895 * the same UUID, remove all from the pending list and put them into
1896 * the 'same_array' list. Then order this list based on superblock
1897 * update time (freshest comes first), kick out 'old' disks and
1898 * compare superblocks. If everything's fine then run it.
1899 *
1900 * If "unit" is allocated, then bump its reference count
1901 */
1902static void autorun_devices(int part)
1903{
1904 struct list_head candidates;
1905 struct list_head *tmp;
1906 mdk_rdev_t *rdev0, *rdev;
1907 mddev_t *mddev;
1908 char b[BDEVNAME_SIZE];
1909
1910 printk(KERN_INFO "md: autorun ...\n");
1911 while (!list_empty(&pending_raid_disks)) {
1912 dev_t dev;
1913 rdev0 = list_entry(pending_raid_disks.next,
1914 mdk_rdev_t, same_set);
1915
1916 printk(KERN_INFO "md: considering %s ...\n",
1917 bdevname(rdev0->bdev,b));
1918 INIT_LIST_HEAD(&candidates);
1919 ITERATE_RDEV_PENDING(rdev,tmp)
1920 if (super_90_load(rdev, rdev0, 0) >= 0) {
1921 printk(KERN_INFO "md: adding %s ...\n",
1922 bdevname(rdev->bdev,b));
1923 list_move(&rdev->same_set, &candidates);
1924 }
1925 /*
1926 * now we have a set of devices, with all of them having
1927 * mostly sane superblocks. It's time to allocate the
1928 * mddev.
1929 */
1930 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
1931 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
1932 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
1933 break;
1934 }
1935 if (part)
1936 dev = MKDEV(mdp_major,
1937 rdev0->preferred_minor << MdpMinorShift);
1938 else
1939 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
1940
1941 md_probe(dev, NULL, NULL);
1942 mddev = mddev_find(dev);
1943 if (!mddev) {
1944 printk(KERN_ERR
1945 "md: cannot allocate memory for md drive.\n");
1946 break;
1947 }
1948 if (mddev_lock(mddev))
1949 printk(KERN_WARNING "md: %s locked, cannot run\n",
1950 mdname(mddev));
1951 else if (mddev->raid_disks || mddev->major_version
1952 || !list_empty(&mddev->disks)) {
1953 printk(KERN_WARNING
1954 "md: %s already running, cannot run %s\n",
1955 mdname(mddev), bdevname(rdev0->bdev,b));
1956 mddev_unlock(mddev);
1957 } else {
1958 printk(KERN_INFO "md: created %s\n", mdname(mddev));
1959 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
1960 list_del_init(&rdev->same_set);
1961 if (bind_rdev_to_array(rdev, mddev))
1962 export_rdev(rdev);
1963 }
1964 autorun_array(mddev);
1965 mddev_unlock(mddev);
1966 }
1967 /* on success, candidates will be empty, on error
1968 * it won't...
1969 */
1970 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
1971 export_rdev(rdev);
1972 mddev_put(mddev);
1973 }
1974 printk(KERN_INFO "md: ... autorun DONE.\n");
1975}
1976
1977/*
1978 * import RAID devices based on one partition
1979 * if possible, the array gets run as well.
1980 */
1981
1982static int autostart_array(dev_t startdev)
1983{
1984 char b[BDEVNAME_SIZE];
1985 int err = -EINVAL, i;
1986 mdp_super_t *sb = NULL;
1987 mdk_rdev_t *start_rdev = NULL, *rdev;
1988
1989 start_rdev = md_import_device(startdev, 0, 0);
1990 if (IS_ERR(start_rdev))
1991 return err;
1992
1993
1994 /* NOTE: this can only work for 0.90.0 superblocks */
1995 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
1996 if (sb->major_version != 0 ||
1997 sb->minor_version != 90 ) {
1998 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
1999 export_rdev(start_rdev);
2000 return err;
2001 }
2002
2003 if (start_rdev->faulty) {
2004 printk(KERN_WARNING
2005 "md: can not autostart based on faulty %s!\n",
2006 bdevname(start_rdev->bdev,b));
2007 export_rdev(start_rdev);
2008 return err;
2009 }
2010 list_add(&start_rdev->same_set, &pending_raid_disks);
2011
2012 for (i = 0; i < MD_SB_DISKS; i++) {
2013 mdp_disk_t *desc = sb->disks + i;
2014 dev_t dev = MKDEV(desc->major, desc->minor);
2015
2016 if (!dev)
2017 continue;
2018 if (dev == startdev)
2019 continue;
2020 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2021 continue;
2022 rdev = md_import_device(dev, 0, 0);
2023 if (IS_ERR(rdev))
2024 continue;
2025
2026 list_add(&rdev->same_set, &pending_raid_disks);
2027 }
2028
2029 /*
2030 * possibly return codes
2031 */
2032 autorun_devices(0);
2033 return 0;
2034
2035}
2036
2037
2038static int get_version(void __user * arg)
2039{
2040 mdu_version_t ver;
2041
2042 ver.major = MD_MAJOR_VERSION;
2043 ver.minor = MD_MINOR_VERSION;
2044 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2045
2046 if (copy_to_user(arg, &ver, sizeof(ver)))
2047 return -EFAULT;
2048
2049 return 0;
2050}
2051
2052static int get_array_info(mddev_t * mddev, void __user * arg)
2053{
2054 mdu_array_info_t info;
2055 int nr,working,active,failed,spare;
2056 mdk_rdev_t *rdev;
2057 struct list_head *tmp;
2058
2059 nr=working=active=failed=spare=0;
2060 ITERATE_RDEV(mddev,rdev,tmp) {
2061 nr++;
2062 if (rdev->faulty)
2063 failed++;
2064 else {
2065 working++;
2066 if (rdev->in_sync)
2067 active++;
2068 else
2069 spare++;
2070 }
2071 }
2072
2073 info.major_version = mddev->major_version;
2074 info.minor_version = mddev->minor_version;
2075 info.patch_version = MD_PATCHLEVEL_VERSION;
2076 info.ctime = mddev->ctime;
2077 info.level = mddev->level;
2078 info.size = mddev->size;
2079 info.nr_disks = nr;
2080 info.raid_disks = mddev->raid_disks;
2081 info.md_minor = mddev->md_minor;
2082 info.not_persistent= !mddev->persistent;
2083
2084 info.utime = mddev->utime;
2085 info.state = 0;
2086 if (mddev->in_sync)
2087 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07002088 if (mddev->bitmap && mddev->bitmap_offset)
2089 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 info.active_disks = active;
2091 info.working_disks = working;
2092 info.failed_disks = failed;
2093 info.spare_disks = spare;
2094
2095 info.layout = mddev->layout;
2096 info.chunk_size = mddev->chunk_size;
2097
2098 if (copy_to_user(arg, &info, sizeof(info)))
2099 return -EFAULT;
2100
2101 return 0;
2102}
2103
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002104static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07002105{
2106 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2107 char *ptr, *buf = NULL;
2108 int err = -ENOMEM;
2109
2110 file = kmalloc(sizeof(*file), GFP_KERNEL);
2111 if (!file)
2112 goto out;
2113
2114 /* bitmap disabled, zero the first byte and copy out */
2115 if (!mddev->bitmap || !mddev->bitmap->file) {
2116 file->pathname[0] = '\0';
2117 goto copy_out;
2118 }
2119
2120 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2121 if (!buf)
2122 goto out;
2123
2124 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2125 if (!ptr)
2126 goto out;
2127
2128 strcpy(file->pathname, ptr);
2129
2130copy_out:
2131 err = 0;
2132 if (copy_to_user(arg, file, sizeof(*file)))
2133 err = -EFAULT;
2134out:
2135 kfree(buf);
2136 kfree(file);
2137 return err;
2138}
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140static int get_disk_info(mddev_t * mddev, void __user * arg)
2141{
2142 mdu_disk_info_t info;
2143 unsigned int nr;
2144 mdk_rdev_t *rdev;
2145
2146 if (copy_from_user(&info, arg, sizeof(info)))
2147 return -EFAULT;
2148
2149 nr = info.number;
2150
2151 rdev = find_rdev_nr(mddev, nr);
2152 if (rdev) {
2153 info.major = MAJOR(rdev->bdev->bd_dev);
2154 info.minor = MINOR(rdev->bdev->bd_dev);
2155 info.raid_disk = rdev->raid_disk;
2156 info.state = 0;
2157 if (rdev->faulty)
2158 info.state |= (1<<MD_DISK_FAULTY);
2159 else if (rdev->in_sync) {
2160 info.state |= (1<<MD_DISK_ACTIVE);
2161 info.state |= (1<<MD_DISK_SYNC);
2162 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002163 if (test_bit(WriteMostly, &rdev->flags))
2164 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 } else {
2166 info.major = info.minor = 0;
2167 info.raid_disk = -1;
2168 info.state = (1<<MD_DISK_REMOVED);
2169 }
2170
2171 if (copy_to_user(arg, &info, sizeof(info)))
2172 return -EFAULT;
2173
2174 return 0;
2175}
2176
2177static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2178{
2179 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2180 mdk_rdev_t *rdev;
2181 dev_t dev = MKDEV(info->major,info->minor);
2182
2183 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2184 return -EOVERFLOW;
2185
2186 if (!mddev->raid_disks) {
2187 int err;
2188 /* expecting a device which has a superblock */
2189 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2190 if (IS_ERR(rdev)) {
2191 printk(KERN_WARNING
2192 "md: md_import_device returned %ld\n",
2193 PTR_ERR(rdev));
2194 return PTR_ERR(rdev);
2195 }
2196 if (!list_empty(&mddev->disks)) {
2197 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2198 mdk_rdev_t, same_set);
2199 int err = super_types[mddev->major_version]
2200 .load_super(rdev, rdev0, mddev->minor_version);
2201 if (err < 0) {
2202 printk(KERN_WARNING
2203 "md: %s has different UUID to %s\n",
2204 bdevname(rdev->bdev,b),
2205 bdevname(rdev0->bdev,b2));
2206 export_rdev(rdev);
2207 return -EINVAL;
2208 }
2209 }
2210 err = bind_rdev_to_array(rdev, mddev);
2211 if (err)
2212 export_rdev(rdev);
2213 return err;
2214 }
2215
2216 /*
2217 * add_new_disk can be used once the array is assembled
2218 * to add "hot spares". They must already have a superblock
2219 * written
2220 */
2221 if (mddev->pers) {
2222 int err;
2223 if (!mddev->pers->hot_add_disk) {
2224 printk(KERN_WARNING
2225 "%s: personality does not support diskops!\n",
2226 mdname(mddev));
2227 return -EINVAL;
2228 }
2229 rdev = md_import_device(dev, mddev->major_version,
2230 mddev->minor_version);
2231 if (IS_ERR(rdev)) {
2232 printk(KERN_WARNING
2233 "md: md_import_device returned %ld\n",
2234 PTR_ERR(rdev));
2235 return PTR_ERR(rdev);
2236 }
NeilBrown41158c72005-06-21 17:17:25 -07002237 /* set save_raid_disk if appropriate */
2238 if (!mddev->persistent) {
2239 if (info->state & (1<<MD_DISK_SYNC) &&
2240 info->raid_disk < mddev->raid_disks)
2241 rdev->raid_disk = info->raid_disk;
2242 else
2243 rdev->raid_disk = -1;
2244 } else
2245 super_types[mddev->major_version].
2246 validate_super(mddev, rdev);
2247 rdev->saved_raid_disk = rdev->raid_disk;
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 rdev->in_sync = 0; /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002250 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2251 set_bit(WriteMostly, &rdev->flags);
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 rdev->raid_disk = -1;
2254 err = bind_rdev_to_array(rdev, mddev);
2255 if (err)
2256 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07002257
2258 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002259 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return err;
2261 }
2262
2263 /* otherwise, add_new_disk is only allowed
2264 * for major_version==0 superblocks
2265 */
2266 if (mddev->major_version != 0) {
2267 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2268 mdname(mddev));
2269 return -EINVAL;
2270 }
2271
2272 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2273 int err;
2274 rdev = md_import_device (dev, -1, 0);
2275 if (IS_ERR(rdev)) {
2276 printk(KERN_WARNING
2277 "md: error, md_import_device() returned %ld\n",
2278 PTR_ERR(rdev));
2279 return PTR_ERR(rdev);
2280 }
2281 rdev->desc_nr = info->number;
2282 if (info->raid_disk < mddev->raid_disks)
2283 rdev->raid_disk = info->raid_disk;
2284 else
2285 rdev->raid_disk = -1;
2286
2287 rdev->faulty = 0;
2288 if (rdev->raid_disk < mddev->raid_disks)
2289 rdev->in_sync = (info->state & (1<<MD_DISK_SYNC));
2290 else
2291 rdev->in_sync = 0;
2292
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002293 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2294 set_bit(WriteMostly, &rdev->flags);
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 err = bind_rdev_to_array(rdev, mddev);
2297 if (err) {
2298 export_rdev(rdev);
2299 return err;
2300 }
2301
2302 if (!mddev->persistent) {
2303 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2304 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2305 } else
2306 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2307 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2308
2309 if (!mddev->size || (mddev->size > rdev->size))
2310 mddev->size = rdev->size;
2311 }
2312
2313 return 0;
2314}
2315
2316static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2317{
2318 char b[BDEVNAME_SIZE];
2319 mdk_rdev_t *rdev;
2320
2321 if (!mddev->pers)
2322 return -ENODEV;
2323
2324 rdev = find_rdev(mddev, dev);
2325 if (!rdev)
2326 return -ENXIO;
2327
2328 if (rdev->raid_disk >= 0)
2329 goto busy;
2330
2331 kick_rdev_from_array(rdev);
2332 md_update_sb(mddev);
2333
2334 return 0;
2335busy:
2336 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2337 bdevname(rdev->bdev,b), mdname(mddev));
2338 return -EBUSY;
2339}
2340
2341static int hot_add_disk(mddev_t * mddev, dev_t dev)
2342{
2343 char b[BDEVNAME_SIZE];
2344 int err;
2345 unsigned int size;
2346 mdk_rdev_t *rdev;
2347
2348 if (!mddev->pers)
2349 return -ENODEV;
2350
2351 if (mddev->major_version != 0) {
2352 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2353 " version-0 superblocks.\n",
2354 mdname(mddev));
2355 return -EINVAL;
2356 }
2357 if (!mddev->pers->hot_add_disk) {
2358 printk(KERN_WARNING
2359 "%s: personality does not support diskops!\n",
2360 mdname(mddev));
2361 return -EINVAL;
2362 }
2363
2364 rdev = md_import_device (dev, -1, 0);
2365 if (IS_ERR(rdev)) {
2366 printk(KERN_WARNING
2367 "md: error, md_import_device() returned %ld\n",
2368 PTR_ERR(rdev));
2369 return -EINVAL;
2370 }
2371
2372 if (mddev->persistent)
2373 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2374 else
2375 rdev->sb_offset =
2376 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2377
2378 size = calc_dev_size(rdev, mddev->chunk_size);
2379 rdev->size = size;
2380
2381 if (size < mddev->size) {
2382 printk(KERN_WARNING
2383 "%s: disk size %llu blocks < array size %llu\n",
2384 mdname(mddev), (unsigned long long)size,
2385 (unsigned long long)mddev->size);
2386 err = -ENOSPC;
2387 goto abort_export;
2388 }
2389
2390 if (rdev->faulty) {
2391 printk(KERN_WARNING
2392 "md: can not hot-add faulty %s disk to %s!\n",
2393 bdevname(rdev->bdev,b), mdname(mddev));
2394 err = -EINVAL;
2395 goto abort_export;
2396 }
2397 rdev->in_sync = 0;
2398 rdev->desc_nr = -1;
2399 bind_rdev_to_array(rdev, mddev);
2400
2401 /*
2402 * The rest should better be atomic, we can have disk failures
2403 * noticed in interrupt contexts ...
2404 */
2405
2406 if (rdev->desc_nr == mddev->max_disks) {
2407 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2408 mdname(mddev));
2409 err = -EBUSY;
2410 goto abort_unbind_export;
2411 }
2412
2413 rdev->raid_disk = -1;
2414
2415 md_update_sb(mddev);
2416
2417 /*
2418 * Kick recovery, maybe this spare has to be added to the
2419 * array immediately.
2420 */
2421 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2422 md_wakeup_thread(mddev->thread);
2423
2424 return 0;
2425
2426abort_unbind_export:
2427 unbind_rdev_from_array(rdev);
2428
2429abort_export:
2430 export_rdev(rdev);
2431 return err;
2432}
2433
NeilBrown32a76272005-06-21 17:17:14 -07002434/* similar to deny_write_access, but accounts for our holding a reference
2435 * to the file ourselves */
2436static int deny_bitmap_write_access(struct file * file)
2437{
2438 struct inode *inode = file->f_mapping->host;
2439
2440 spin_lock(&inode->i_lock);
2441 if (atomic_read(&inode->i_writecount) > 1) {
2442 spin_unlock(&inode->i_lock);
2443 return -ETXTBSY;
2444 }
2445 atomic_set(&inode->i_writecount, -1);
2446 spin_unlock(&inode->i_lock);
2447
2448 return 0;
2449}
2450
2451static int set_bitmap_file(mddev_t *mddev, int fd)
2452{
2453 int err;
2454
NeilBrown36fa3062005-09-09 16:23:45 -07002455 if (mddev->pers) {
2456 if (!mddev->pers->quiesce)
2457 return -EBUSY;
2458 if (mddev->recovery || mddev->sync_thread)
2459 return -EBUSY;
2460 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07002461 }
2462
NeilBrown36fa3062005-09-09 16:23:45 -07002463
2464 if (fd >= 0) {
2465 if (mddev->bitmap)
2466 return -EEXIST; /* cannot add when bitmap is present */
2467 mddev->bitmap_file = fget(fd);
2468
2469 if (mddev->bitmap_file == NULL) {
2470 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2471 mdname(mddev));
2472 return -EBADF;
2473 }
2474
2475 err = deny_bitmap_write_access(mddev->bitmap_file);
2476 if (err) {
2477 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2478 mdname(mddev));
2479 fput(mddev->bitmap_file);
2480 mddev->bitmap_file = NULL;
2481 return err;
2482 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002483 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07002484 } else if (mddev->bitmap == NULL)
2485 return -ENOENT; /* cannot remove what isn't there */
2486 err = 0;
2487 if (mddev->pers) {
2488 mddev->pers->quiesce(mddev, 1);
2489 if (fd >= 0)
2490 err = bitmap_create(mddev);
2491 if (fd < 0 || err)
2492 bitmap_destroy(mddev);
2493 mddev->pers->quiesce(mddev, 0);
2494 } else if (fd < 0) {
2495 if (mddev->bitmap_file)
2496 fput(mddev->bitmap_file);
2497 mddev->bitmap_file = NULL;
2498 }
2499
NeilBrown32a76272005-06-21 17:17:14 -07002500 return err;
2501}
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503/*
2504 * set_array_info is used two different ways
2505 * The original usage is when creating a new array.
2506 * In this usage, raid_disks is > 0 and it together with
2507 * level, size, not_persistent,layout,chunksize determine the
2508 * shape of the array.
2509 * This will always create an array with a type-0.90.0 superblock.
2510 * The newer usage is when assembling an array.
2511 * In this case raid_disks will be 0, and the major_version field is
2512 * use to determine which style super-blocks are to be found on the devices.
2513 * The minor and patch _version numbers are also kept incase the
2514 * super_block handler wishes to interpret them.
2515 */
2516static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2517{
2518
2519 if (info->raid_disks == 0) {
2520 /* just setting version number for superblock loading */
2521 if (info->major_version < 0 ||
2522 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2523 super_types[info->major_version].name == NULL) {
2524 /* maybe try to auto-load a module? */
2525 printk(KERN_INFO
2526 "md: superblock version %d not known\n",
2527 info->major_version);
2528 return -EINVAL;
2529 }
2530 mddev->major_version = info->major_version;
2531 mddev->minor_version = info->minor_version;
2532 mddev->patch_version = info->patch_version;
2533 return 0;
2534 }
2535 mddev->major_version = MD_MAJOR_VERSION;
2536 mddev->minor_version = MD_MINOR_VERSION;
2537 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2538 mddev->ctime = get_seconds();
2539
2540 mddev->level = info->level;
2541 mddev->size = info->size;
2542 mddev->raid_disks = info->raid_disks;
2543 /* don't set md_minor, it is determined by which /dev/md* was
2544 * openned
2545 */
2546 if (info->state & (1<<MD_SB_CLEAN))
2547 mddev->recovery_cp = MaxSector;
2548 else
2549 mddev->recovery_cp = 0;
2550 mddev->persistent = ! info->not_persistent;
2551
2552 mddev->layout = info->layout;
2553 mddev->chunk_size = info->chunk_size;
2554
2555 mddev->max_disks = MD_SB_DISKS;
2556
2557 mddev->sb_dirty = 1;
2558
2559 /*
2560 * Generate a 128 bit UUID
2561 */
2562 get_random_bytes(mddev->uuid, 16);
2563
2564 return 0;
2565}
2566
2567/*
2568 * update_array_info is used to change the configuration of an
2569 * on-line array.
2570 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2571 * fields in the info are checked against the array.
2572 * Any differences that cannot be handled will cause an error.
2573 * Normally, only one change can be managed at a time.
2574 */
2575static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2576{
2577 int rv = 0;
2578 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07002579 int state = 0;
2580
2581 /* calculate expected state,ignoring low bits */
2582 if (mddev->bitmap && mddev->bitmap_offset)
2583 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 if (mddev->major_version != info->major_version ||
2586 mddev->minor_version != info->minor_version ||
2587/* mddev->patch_version != info->patch_version || */
2588 mddev->ctime != info->ctime ||
2589 mddev->level != info->level ||
2590/* mddev->layout != info->layout || */
2591 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07002592 mddev->chunk_size != info->chunk_size ||
2593 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2594 ((state^info->state) & 0xfffffe00)
2595 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return -EINVAL;
2597 /* Check there is only one change */
2598 if (mddev->size != info->size) cnt++;
2599 if (mddev->raid_disks != info->raid_disks) cnt++;
2600 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07002601 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if (cnt == 0) return 0;
2603 if (cnt > 1) return -EINVAL;
2604
2605 if (mddev->layout != info->layout) {
2606 /* Change layout
2607 * we don't need to do anything at the md level, the
2608 * personality will take care of it all.
2609 */
2610 if (mddev->pers->reconfig == NULL)
2611 return -EINVAL;
2612 else
2613 return mddev->pers->reconfig(mddev, info->layout, -1);
2614 }
2615 if (mddev->size != info->size) {
2616 mdk_rdev_t * rdev;
2617 struct list_head *tmp;
2618 if (mddev->pers->resize == NULL)
2619 return -EINVAL;
2620 /* The "size" is the amount of each device that is used.
2621 * This can only make sense for arrays with redundancy.
2622 * linear and raid0 always use whatever space is available
2623 * We can only consider changing the size if no resync
2624 * or reconstruction is happening, and if the new size
2625 * is acceptable. It must fit before the sb_offset or,
2626 * if that is <data_offset, it must fit before the
2627 * size of each device.
2628 * If size is zero, we find the largest size that fits.
2629 */
2630 if (mddev->sync_thread)
2631 return -EBUSY;
2632 ITERATE_RDEV(mddev,rdev,tmp) {
2633 sector_t avail;
2634 int fit = (info->size == 0);
2635 if (rdev->sb_offset > rdev->data_offset)
2636 avail = (rdev->sb_offset*2) - rdev->data_offset;
2637 else
2638 avail = get_capacity(rdev->bdev->bd_disk)
2639 - rdev->data_offset;
2640 if (fit && (info->size == 0 || info->size > avail/2))
2641 info->size = avail/2;
2642 if (avail < ((sector_t)info->size << 1))
2643 return -ENOSPC;
2644 }
2645 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
2646 if (!rv) {
2647 struct block_device *bdev;
2648
2649 bdev = bdget_disk(mddev->gendisk, 0);
2650 if (bdev) {
2651 down(&bdev->bd_inode->i_sem);
2652 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2653 up(&bdev->bd_inode->i_sem);
2654 bdput(bdev);
2655 }
2656 }
2657 }
2658 if (mddev->raid_disks != info->raid_disks) {
2659 /* change the number of raid disks */
2660 if (mddev->pers->reshape == NULL)
2661 return -EINVAL;
2662 if (info->raid_disks <= 0 ||
2663 info->raid_disks >= mddev->max_disks)
2664 return -EINVAL;
2665 if (mddev->sync_thread)
2666 return -EBUSY;
2667 rv = mddev->pers->reshape(mddev, info->raid_disks);
2668 if (!rv) {
2669 struct block_device *bdev;
2670
2671 bdev = bdget_disk(mddev->gendisk, 0);
2672 if (bdev) {
2673 down(&bdev->bd_inode->i_sem);
2674 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2675 up(&bdev->bd_inode->i_sem);
2676 bdput(bdev);
2677 }
2678 }
2679 }
NeilBrown36fa3062005-09-09 16:23:45 -07002680 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
2681 if (mddev->pers->quiesce == NULL)
2682 return -EINVAL;
2683 if (mddev->recovery || mddev->sync_thread)
2684 return -EBUSY;
2685 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
2686 /* add the bitmap */
2687 if (mddev->bitmap)
2688 return -EEXIST;
2689 if (mddev->default_bitmap_offset == 0)
2690 return -EINVAL;
2691 mddev->bitmap_offset = mddev->default_bitmap_offset;
2692 mddev->pers->quiesce(mddev, 1);
2693 rv = bitmap_create(mddev);
2694 if (rv)
2695 bitmap_destroy(mddev);
2696 mddev->pers->quiesce(mddev, 0);
2697 } else {
2698 /* remove the bitmap */
2699 if (!mddev->bitmap)
2700 return -ENOENT;
2701 if (mddev->bitmap->file)
2702 return -EINVAL;
2703 mddev->pers->quiesce(mddev, 1);
2704 bitmap_destroy(mddev);
2705 mddev->pers->quiesce(mddev, 0);
2706 mddev->bitmap_offset = 0;
2707 }
2708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 md_update_sb(mddev);
2710 return rv;
2711}
2712
2713static int set_disk_faulty(mddev_t *mddev, dev_t dev)
2714{
2715 mdk_rdev_t *rdev;
2716
2717 if (mddev->pers == NULL)
2718 return -ENODEV;
2719
2720 rdev = find_rdev(mddev, dev);
2721 if (!rdev)
2722 return -ENODEV;
2723
2724 md_error(mddev, rdev);
2725 return 0;
2726}
2727
2728static int md_ioctl(struct inode *inode, struct file *file,
2729 unsigned int cmd, unsigned long arg)
2730{
2731 int err = 0;
2732 void __user *argp = (void __user *)arg;
2733 struct hd_geometry __user *loc = argp;
2734 mddev_t *mddev = NULL;
2735
2736 if (!capable(CAP_SYS_ADMIN))
2737 return -EACCES;
2738
2739 /*
2740 * Commands dealing with the RAID driver but not any
2741 * particular array:
2742 */
2743 switch (cmd)
2744 {
2745 case RAID_VERSION:
2746 err = get_version(argp);
2747 goto done;
2748
2749 case PRINT_RAID_DEBUG:
2750 err = 0;
2751 md_print_devices();
2752 goto done;
2753
2754#ifndef MODULE
2755 case RAID_AUTORUN:
2756 err = 0;
2757 autostart_arrays(arg);
2758 goto done;
2759#endif
2760 default:;
2761 }
2762
2763 /*
2764 * Commands creating/starting a new array:
2765 */
2766
2767 mddev = inode->i_bdev->bd_disk->private_data;
2768
2769 if (!mddev) {
2770 BUG();
2771 goto abort;
2772 }
2773
2774
2775 if (cmd == START_ARRAY) {
2776 /* START_ARRAY doesn't need to lock the array as autostart_array
2777 * does the locking, and it could even be a different array
2778 */
2779 static int cnt = 3;
2780 if (cnt > 0 ) {
2781 printk(KERN_WARNING
2782 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
2783 "This will not be supported beyond 2.6\n",
2784 current->comm, current->pid);
2785 cnt--;
2786 }
2787 err = autostart_array(new_decode_dev(arg));
2788 if (err) {
2789 printk(KERN_WARNING "md: autostart failed!\n");
2790 goto abort;
2791 }
2792 goto done;
2793 }
2794
2795 err = mddev_lock(mddev);
2796 if (err) {
2797 printk(KERN_INFO
2798 "md: ioctl lock interrupted, reason %d, cmd %d\n",
2799 err, cmd);
2800 goto abort;
2801 }
2802
2803 switch (cmd)
2804 {
2805 case SET_ARRAY_INFO:
2806 {
2807 mdu_array_info_t info;
2808 if (!arg)
2809 memset(&info, 0, sizeof(info));
2810 else if (copy_from_user(&info, argp, sizeof(info))) {
2811 err = -EFAULT;
2812 goto abort_unlock;
2813 }
2814 if (mddev->pers) {
2815 err = update_array_info(mddev, &info);
2816 if (err) {
2817 printk(KERN_WARNING "md: couldn't update"
2818 " array info. %d\n", err);
2819 goto abort_unlock;
2820 }
2821 goto done_unlock;
2822 }
2823 if (!list_empty(&mddev->disks)) {
2824 printk(KERN_WARNING
2825 "md: array %s already has disks!\n",
2826 mdname(mddev));
2827 err = -EBUSY;
2828 goto abort_unlock;
2829 }
2830 if (mddev->raid_disks) {
2831 printk(KERN_WARNING
2832 "md: array %s already initialised!\n",
2833 mdname(mddev));
2834 err = -EBUSY;
2835 goto abort_unlock;
2836 }
2837 err = set_array_info(mddev, &info);
2838 if (err) {
2839 printk(KERN_WARNING "md: couldn't set"
2840 " array info. %d\n", err);
2841 goto abort_unlock;
2842 }
2843 }
2844 goto done_unlock;
2845
2846 default:;
2847 }
2848
2849 /*
2850 * Commands querying/configuring an existing array:
2851 */
NeilBrown32a76272005-06-21 17:17:14 -07002852 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
2853 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
2854 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
2855 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 err = -ENODEV;
2857 goto abort_unlock;
2858 }
2859
2860 /*
2861 * Commands even a read-only array can execute:
2862 */
2863 switch (cmd)
2864 {
2865 case GET_ARRAY_INFO:
2866 err = get_array_info(mddev, argp);
2867 goto done_unlock;
2868
NeilBrown32a76272005-06-21 17:17:14 -07002869 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002870 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07002871 goto done_unlock;
2872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 case GET_DISK_INFO:
2874 err = get_disk_info(mddev, argp);
2875 goto done_unlock;
2876
2877 case RESTART_ARRAY_RW:
2878 err = restart_array(mddev);
2879 goto done_unlock;
2880
2881 case STOP_ARRAY:
2882 err = do_md_stop (mddev, 0);
2883 goto done_unlock;
2884
2885 case STOP_ARRAY_RO:
2886 err = do_md_stop (mddev, 1);
2887 goto done_unlock;
2888
2889 /*
2890 * We have a problem here : there is no easy way to give a CHS
2891 * virtual geometry. We currently pretend that we have a 2 heads
2892 * 4 sectors (with a BIG number of cylinders...). This drives
2893 * dosfs just mad... ;-)
2894 */
2895 case HDIO_GETGEO:
2896 if (!loc) {
2897 err = -EINVAL;
2898 goto abort_unlock;
2899 }
2900 err = put_user (2, (char __user *) &loc->heads);
2901 if (err)
2902 goto abort_unlock;
2903 err = put_user (4, (char __user *) &loc->sectors);
2904 if (err)
2905 goto abort_unlock;
2906 err = put_user(get_capacity(mddev->gendisk)/8,
2907 (short __user *) &loc->cylinders);
2908 if (err)
2909 goto abort_unlock;
2910 err = put_user (get_start_sect(inode->i_bdev),
2911 (long __user *) &loc->start);
2912 goto done_unlock;
2913 }
2914
2915 /*
2916 * The remaining ioctls are changing the state of the
2917 * superblock, so we do not allow read-only arrays
2918 * here:
2919 */
2920 if (mddev->ro) {
2921 err = -EROFS;
2922 goto abort_unlock;
2923 }
2924
2925 switch (cmd)
2926 {
2927 case ADD_NEW_DISK:
2928 {
2929 mdu_disk_info_t info;
2930 if (copy_from_user(&info, argp, sizeof(info)))
2931 err = -EFAULT;
2932 else
2933 err = add_new_disk(mddev, &info);
2934 goto done_unlock;
2935 }
2936
2937 case HOT_REMOVE_DISK:
2938 err = hot_remove_disk(mddev, new_decode_dev(arg));
2939 goto done_unlock;
2940
2941 case HOT_ADD_DISK:
2942 err = hot_add_disk(mddev, new_decode_dev(arg));
2943 goto done_unlock;
2944
2945 case SET_DISK_FAULTY:
2946 err = set_disk_faulty(mddev, new_decode_dev(arg));
2947 goto done_unlock;
2948
2949 case RUN_ARRAY:
2950 err = do_md_run (mddev);
2951 goto done_unlock;
2952
NeilBrown32a76272005-06-21 17:17:14 -07002953 case SET_BITMAP_FILE:
2954 err = set_bitmap_file(mddev, (int)arg);
2955 goto done_unlock;
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 default:
2958 if (_IOC_TYPE(cmd) == MD_MAJOR)
2959 printk(KERN_WARNING "md: %s(pid %d) used"
2960 " obsolete MD ioctl, upgrade your"
2961 " software to use new ictls.\n",
2962 current->comm, current->pid);
2963 err = -EINVAL;
2964 goto abort_unlock;
2965 }
2966
2967done_unlock:
2968abort_unlock:
2969 mddev_unlock(mddev);
2970
2971 return err;
2972done:
2973 if (err)
2974 MD_BUG();
2975abort:
2976 return err;
2977}
2978
2979static int md_open(struct inode *inode, struct file *file)
2980{
2981 /*
2982 * Succeed if we can lock the mddev, which confirms that
2983 * it isn't being stopped right now.
2984 */
2985 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
2986 int err;
2987
2988 if ((err = mddev_lock(mddev)))
2989 goto out;
2990
2991 err = 0;
2992 mddev_get(mddev);
2993 mddev_unlock(mddev);
2994
2995 check_disk_change(inode->i_bdev);
2996 out:
2997 return err;
2998}
2999
3000static int md_release(struct inode *inode, struct file * file)
3001{
3002 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3003
3004 if (!mddev)
3005 BUG();
3006 mddev_put(mddev);
3007
3008 return 0;
3009}
3010
3011static int md_media_changed(struct gendisk *disk)
3012{
3013 mddev_t *mddev = disk->private_data;
3014
3015 return mddev->changed;
3016}
3017
3018static int md_revalidate(struct gendisk *disk)
3019{
3020 mddev_t *mddev = disk->private_data;
3021
3022 mddev->changed = 0;
3023 return 0;
3024}
3025static struct block_device_operations md_fops =
3026{
3027 .owner = THIS_MODULE,
3028 .open = md_open,
3029 .release = md_release,
3030 .ioctl = md_ioctl,
3031 .media_changed = md_media_changed,
3032 .revalidate_disk= md_revalidate,
3033};
3034
Adrian Bunk75c96f82005-05-05 16:16:09 -07003035static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
3037 mdk_thread_t *thread = arg;
3038
3039 lock_kernel();
3040
3041 /*
3042 * Detach thread
3043 */
3044
3045 daemonize(thread->name, mdname(thread->mddev));
3046
3047 current->exit_signal = SIGCHLD;
3048 allow_signal(SIGKILL);
3049 thread->tsk = current;
3050
3051 /*
3052 * md_thread is a 'system-thread', it's priority should be very
3053 * high. We avoid resource deadlocks individually in each
3054 * raid personality. (RAID5 does preallocation) We also use RR and
3055 * the very same RT priority as kswapd, thus we will never get
3056 * into a priority inversion deadlock.
3057 *
3058 * we definitely have to have equal or higher priority than
3059 * bdflush, otherwise bdflush will deadlock if there are too
3060 * many dirty RAID5 blocks.
3061 */
3062 unlock_kernel();
3063
3064 complete(thread->event);
3065 while (thread->run) {
3066 void (*run)(mddev_t *);
3067
NeilBrown32a76272005-06-21 17:17:14 -07003068 wait_event_interruptible_timeout(thread->wqueue,
3069 test_bit(THREAD_WAKEUP, &thread->flags),
3070 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003071 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 clear_bit(THREAD_WAKEUP, &thread->flags);
3074
3075 run = thread->run;
3076 if (run)
3077 run(thread->mddev);
3078
3079 if (signal_pending(current))
3080 flush_signals(current);
3081 }
3082 complete(thread->event);
3083 return 0;
3084}
3085
3086void md_wakeup_thread(mdk_thread_t *thread)
3087{
3088 if (thread) {
3089 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3090 set_bit(THREAD_WAKEUP, &thread->flags);
3091 wake_up(&thread->wqueue);
3092 }
3093}
3094
3095mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3096 const char *name)
3097{
3098 mdk_thread_t *thread;
3099 int ret;
3100 struct completion event;
3101
3102 thread = (mdk_thread_t *) kmalloc
3103 (sizeof(mdk_thread_t), GFP_KERNEL);
3104 if (!thread)
3105 return NULL;
3106
3107 memset(thread, 0, sizeof(mdk_thread_t));
3108 init_waitqueue_head(&thread->wqueue);
3109
3110 init_completion(&event);
3111 thread->event = &event;
3112 thread->run = run;
3113 thread->mddev = mddev;
3114 thread->name = name;
NeilBrown32a76272005-06-21 17:17:14 -07003115 thread->timeout = MAX_SCHEDULE_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 ret = kernel_thread(md_thread, thread, 0);
3117 if (ret < 0) {
3118 kfree(thread);
3119 return NULL;
3120 }
3121 wait_for_completion(&event);
3122 return thread;
3123}
3124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125void md_unregister_thread(mdk_thread_t *thread)
3126{
3127 struct completion event;
3128
3129 init_completion(&event);
3130
3131 thread->event = &event;
NeilBrownd28446f2005-04-16 15:26:41 -07003132
3133 /* As soon as ->run is set to NULL, the task could disappear,
3134 * so we need to hold tasklist_lock until we have sent the signal
3135 */
3136 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
3137 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 thread->run = NULL;
NeilBrownd28446f2005-04-16 15:26:41 -07003139 send_sig(SIGKILL, thread->tsk, 1);
3140 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 wait_for_completion(&event);
3142 kfree(thread);
3143}
3144
3145void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3146{
3147 if (!mddev) {
3148 MD_BUG();
3149 return;
3150 }
3151
3152 if (!rdev || rdev->faulty)
3153 return;
NeilBrown32a76272005-06-21 17:17:14 -07003154/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3156 mdname(mddev),
3157 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3158 __builtin_return_address(0),__builtin_return_address(1),
3159 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07003160*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (!mddev->pers->error_handler)
3162 return;
3163 mddev->pers->error_handler(mddev,rdev);
3164 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3165 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3166 md_wakeup_thread(mddev->thread);
3167}
3168
3169/* seq_file implementation /proc/mdstat */
3170
3171static void status_unused(struct seq_file *seq)
3172{
3173 int i = 0;
3174 mdk_rdev_t *rdev;
3175 struct list_head *tmp;
3176
3177 seq_printf(seq, "unused devices: ");
3178
3179 ITERATE_RDEV_PENDING(rdev,tmp) {
3180 char b[BDEVNAME_SIZE];
3181 i++;
3182 seq_printf(seq, "%s ",
3183 bdevname(rdev->bdev,b));
3184 }
3185 if (!i)
3186 seq_printf(seq, "<none>");
3187
3188 seq_printf(seq, "\n");
3189}
3190
3191
3192static void status_resync(struct seq_file *seq, mddev_t * mddev)
3193{
3194 unsigned long max_blocks, resync, res, dt, db, rt;
3195
3196 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3197
3198 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3199 max_blocks = mddev->resync_max_sectors >> 1;
3200 else
3201 max_blocks = mddev->size;
3202
3203 /*
3204 * Should not happen.
3205 */
3206 if (!max_blocks) {
3207 MD_BUG();
3208 return;
3209 }
3210 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3211 {
3212 int i, x = res/50, y = 20-x;
3213 seq_printf(seq, "[");
3214 for (i = 0; i < x; i++)
3215 seq_printf(seq, "=");
3216 seq_printf(seq, ">");
3217 for (i = 0; i < y; i++)
3218 seq_printf(seq, ".");
3219 seq_printf(seq, "] ");
3220 }
3221 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3222 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3223 "resync" : "recovery"),
3224 res/10, res % 10, resync, max_blocks);
3225
3226 /*
3227 * We do not want to overflow, so the order of operands and
3228 * the * 100 / 100 trick are important. We do a +1 to be
3229 * safe against division by zero. We only estimate anyway.
3230 *
3231 * dt: time from mark until now
3232 * db: blocks written from mark until now
3233 * rt: remaining time
3234 */
3235 dt = ((jiffies - mddev->resync_mark) / HZ);
3236 if (!dt) dt++;
3237 db = resync - (mddev->resync_mark_cnt/2);
3238 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3239
3240 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3241
3242 seq_printf(seq, " speed=%ldK/sec", db/dt);
3243}
3244
3245static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3246{
3247 struct list_head *tmp;
3248 loff_t l = *pos;
3249 mddev_t *mddev;
3250
3251 if (l >= 0x10000)
3252 return NULL;
3253 if (!l--)
3254 /* header */
3255 return (void*)1;
3256
3257 spin_lock(&all_mddevs_lock);
3258 list_for_each(tmp,&all_mddevs)
3259 if (!l--) {
3260 mddev = list_entry(tmp, mddev_t, all_mddevs);
3261 mddev_get(mddev);
3262 spin_unlock(&all_mddevs_lock);
3263 return mddev;
3264 }
3265 spin_unlock(&all_mddevs_lock);
3266 if (!l--)
3267 return (void*)2;/* tail */
3268 return NULL;
3269}
3270
3271static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3272{
3273 struct list_head *tmp;
3274 mddev_t *next_mddev, *mddev = v;
3275
3276 ++*pos;
3277 if (v == (void*)2)
3278 return NULL;
3279
3280 spin_lock(&all_mddevs_lock);
3281 if (v == (void*)1)
3282 tmp = all_mddevs.next;
3283 else
3284 tmp = mddev->all_mddevs.next;
3285 if (tmp != &all_mddevs)
3286 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3287 else {
3288 next_mddev = (void*)2;
3289 *pos = 0x10000;
3290 }
3291 spin_unlock(&all_mddevs_lock);
3292
3293 if (v != (void*)1)
3294 mddev_put(mddev);
3295 return next_mddev;
3296
3297}
3298
3299static void md_seq_stop(struct seq_file *seq, void *v)
3300{
3301 mddev_t *mddev = v;
3302
3303 if (mddev && v != (void*)1 && v != (void*)2)
3304 mddev_put(mddev);
3305}
3306
3307static int md_seq_show(struct seq_file *seq, void *v)
3308{
3309 mddev_t *mddev = v;
3310 sector_t size;
3311 struct list_head *tmp2;
3312 mdk_rdev_t *rdev;
3313 int i;
NeilBrown32a76272005-06-21 17:17:14 -07003314 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316 if (v == (void*)1) {
3317 seq_printf(seq, "Personalities : ");
3318 spin_lock(&pers_lock);
3319 for (i = 0; i < MAX_PERSONALITY; i++)
3320 if (pers[i])
3321 seq_printf(seq, "[%s] ", pers[i]->name);
3322
3323 spin_unlock(&pers_lock);
3324 seq_printf(seq, "\n");
3325 return 0;
3326 }
3327 if (v == (void*)2) {
3328 status_unused(seq);
3329 return 0;
3330 }
3331
3332 if (mddev_lock(mddev)!=0)
3333 return -EINTR;
3334 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3335 seq_printf(seq, "%s : %sactive", mdname(mddev),
3336 mddev->pers ? "" : "in");
3337 if (mddev->pers) {
3338 if (mddev->ro)
3339 seq_printf(seq, " (read-only)");
3340 seq_printf(seq, " %s", mddev->pers->name);
3341 }
3342
3343 size = 0;
3344 ITERATE_RDEV(mddev,rdev,tmp2) {
3345 char b[BDEVNAME_SIZE];
3346 seq_printf(seq, " %s[%d]",
3347 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003348 if (test_bit(WriteMostly, &rdev->flags))
3349 seq_printf(seq, "(W)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 if (rdev->faulty) {
3351 seq_printf(seq, "(F)");
3352 continue;
3353 }
3354 size += rdev->size;
3355 }
3356
3357 if (!list_empty(&mddev->disks)) {
3358 if (mddev->pers)
3359 seq_printf(seq, "\n %llu blocks",
3360 (unsigned long long)mddev->array_size);
3361 else
3362 seq_printf(seq, "\n %llu blocks",
3363 (unsigned long long)size);
3364 }
3365
3366 if (mddev->pers) {
3367 mddev->pers->status (seq, mddev);
3368 seq_printf(seq, "\n ");
NeilBrown32a76272005-06-21 17:17:14 -07003369 if (mddev->curr_resync > 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 status_resync (seq, mddev);
NeilBrown32a76272005-06-21 17:17:14 -07003371 seq_printf(seq, "\n ");
3372 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3373 seq_printf(seq, " resync=DELAYED\n ");
3374 } else
3375 seq_printf(seq, "\n ");
3376
3377 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07003378 unsigned long chunk_kb;
3379 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07003380 spin_lock_irqsave(&bitmap->lock, flags);
3381 chunk_kb = bitmap->chunksize >> 10;
3382 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3383 "%lu%s chunk",
3384 bitmap->pages - bitmap->missing_pages,
3385 bitmap->pages,
3386 (bitmap->pages - bitmap->missing_pages)
3387 << (PAGE_SHIFT - 10),
3388 chunk_kb ? chunk_kb : bitmap->chunksize,
3389 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07003390 if (bitmap->file) {
3391 seq_printf(seq, ", file: ");
3392 seq_path(seq, bitmap->file->f_vfsmnt,
3393 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07003394 }
NeilBrown78d742d2005-06-21 17:17:15 -07003395
NeilBrown32a76272005-06-21 17:17:14 -07003396 seq_printf(seq, "\n");
3397 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 }
3399
3400 seq_printf(seq, "\n");
3401 }
3402 mddev_unlock(mddev);
3403
3404 return 0;
3405}
3406
3407static struct seq_operations md_seq_ops = {
3408 .start = md_seq_start,
3409 .next = md_seq_next,
3410 .stop = md_seq_stop,
3411 .show = md_seq_show,
3412};
3413
3414static int md_seq_open(struct inode *inode, struct file *file)
3415{
3416 int error;
3417
3418 error = seq_open(file, &md_seq_ops);
3419 return error;
3420}
3421
3422static struct file_operations md_seq_fops = {
3423 .open = md_seq_open,
3424 .read = seq_read,
3425 .llseek = seq_lseek,
3426 .release = seq_release,
3427};
3428
3429int register_md_personality(int pnum, mdk_personality_t *p)
3430{
3431 if (pnum >= MAX_PERSONALITY) {
3432 printk(KERN_ERR
3433 "md: tried to install personality %s as nr %d, but max is %lu\n",
3434 p->name, pnum, MAX_PERSONALITY-1);
3435 return -EINVAL;
3436 }
3437
3438 spin_lock(&pers_lock);
3439 if (pers[pnum]) {
3440 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return -EBUSY;
3442 }
3443
3444 pers[pnum] = p;
3445 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3446 spin_unlock(&pers_lock);
3447 return 0;
3448}
3449
3450int unregister_md_personality(int pnum)
3451{
NeilBrowna757e642005-04-16 15:26:42 -07003452 if (pnum >= MAX_PERSONALITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
3455 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3456 spin_lock(&pers_lock);
3457 pers[pnum] = NULL;
3458 spin_unlock(&pers_lock);
3459 return 0;
3460}
3461
3462static int is_mddev_idle(mddev_t *mddev)
3463{
3464 mdk_rdev_t * rdev;
3465 struct list_head *tmp;
3466 int idle;
3467 unsigned long curr_events;
3468
3469 idle = 1;
3470 ITERATE_RDEV(mddev,rdev,tmp) {
3471 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
3472 curr_events = disk_stat_read(disk, read_sectors) +
3473 disk_stat_read(disk, write_sectors) -
3474 atomic_read(&disk->sync_io);
3475 /* Allow some slack between valud of curr_events and last_events,
3476 * as there are some uninteresting races.
3477 * Note: the following is an unsigned comparison.
3478 */
3479 if ((curr_events - rdev->last_events + 32) > 64) {
3480 rdev->last_events = curr_events;
3481 idle = 0;
3482 }
3483 }
3484 return idle;
3485}
3486
3487void md_done_sync(mddev_t *mddev, int blocks, int ok)
3488{
3489 /* another "blocks" (512byte) blocks have been synced */
3490 atomic_sub(blocks, &mddev->recovery_active);
3491 wake_up(&mddev->recovery_wait);
3492 if (!ok) {
3493 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3494 md_wakeup_thread(mddev->thread);
3495 // stop recovery, signal do_sync ....
3496 }
3497}
3498
3499
NeilBrown06d91a52005-06-21 17:17:12 -07003500/* md_write_start(mddev, bi)
3501 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07003502 * in superblock) before writing, schedule a superblock update
3503 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07003504 */
NeilBrown3d310eb2005-06-21 17:17:26 -07003505void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506{
NeilBrown3d310eb2005-06-21 17:17:26 -07003507 DEFINE_WAIT(w);
NeilBrown06d91a52005-06-21 17:17:12 -07003508 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07003509 return;
NeilBrown06d91a52005-06-21 17:17:12 -07003510
3511 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07003512 if (mddev->in_sync) {
NeilBrown3d310eb2005-06-21 17:17:26 -07003513 spin_lock(&mddev->write_lock);
3514 if (mddev->in_sync) {
3515 mddev->in_sync = 0;
3516 mddev->sb_dirty = 1;
3517 md_wakeup_thread(mddev->thread);
3518 }
3519 spin_unlock(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07003520 }
NeilBrown3d310eb2005-06-21 17:17:26 -07003521 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522}
3523
3524void md_write_end(mddev_t *mddev)
3525{
3526 if (atomic_dec_and_test(&mddev->writes_pending)) {
3527 if (mddev->safemode == 2)
3528 md_wakeup_thread(mddev->thread);
3529 else
3530 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3531 }
3532}
3533
Adrian Bunk75c96f82005-05-05 16:16:09 -07003534static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
3536#define SYNC_MARKS 10
3537#define SYNC_MARK_STEP (3*HZ)
3538static void md_do_sync(mddev_t *mddev)
3539{
3540 mddev_t *mddev2;
3541 unsigned int currspeed = 0,
3542 window;
NeilBrown57afd892005-06-21 17:17:13 -07003543 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 unsigned long mark[SYNC_MARKS];
3545 sector_t mark_cnt[SYNC_MARKS];
3546 int last_mark,m;
3547 struct list_head *tmp;
3548 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07003549 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
3551 /* just incase thread restarts... */
3552 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3553 return;
3554
3555 /* we overload curr_resync somewhat here.
3556 * 0 == not engaged in resync at all
3557 * 2 == checking that there is no conflict with another sync
3558 * 1 == like 2, but have yielded to allow conflicting resync to
3559 * commense
3560 * other == active in resync - this many blocks
3561 *
3562 * Before starting a resync we must have set curr_resync to
3563 * 2, and then checked that every "conflicting" array has curr_resync
3564 * less than ours. When we find one that is the same or higher
3565 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3566 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3567 * This will mean we have to start checking from the beginning again.
3568 *
3569 */
3570
3571 do {
3572 mddev->curr_resync = 2;
3573
3574 try_again:
3575 if (signal_pending(current)) {
3576 flush_signals(current);
3577 goto skip;
3578 }
3579 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 if (mddev2 == mddev)
3581 continue;
3582 if (mddev2->curr_resync &&
3583 match_mddev_units(mddev,mddev2)) {
3584 DEFINE_WAIT(wq);
3585 if (mddev < mddev2 && mddev->curr_resync == 2) {
3586 /* arbitrarily yield */
3587 mddev->curr_resync = 1;
3588 wake_up(&resync_wait);
3589 }
3590 if (mddev > mddev2 && mddev->curr_resync == 1)
3591 /* no need to wait here, we can wait the next
3592 * time 'round when curr_resync == 2
3593 */
3594 continue;
3595 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
3596 if (!signal_pending(current)
3597 && mddev2->curr_resync >= mddev->curr_resync) {
3598 printk(KERN_INFO "md: delaying resync of %s"
3599 " until %s has finished resync (they"
3600 " share one or more physical units)\n",
3601 mdname(mddev), mdname(mddev2));
3602 mddev_put(mddev2);
3603 schedule();
3604 finish_wait(&resync_wait, &wq);
3605 goto try_again;
3606 }
3607 finish_wait(&resync_wait, &wq);
3608 }
3609 }
3610 } while (mddev->curr_resync < 2);
3611
3612 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3613 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07003614 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 */
3616 max_sectors = mddev->resync_max_sectors;
3617 else
3618 /* recovery follows the physical size of devices */
3619 max_sectors = mddev->size << 1;
3620
3621 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
3622 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
3623 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
3624 printk(KERN_INFO "md: using maximum available idle IO bandwith "
3625 "(but not more than %d KB/sec) for reconstruction.\n",
3626 sysctl_speed_limit_max);
3627
3628 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07003629 /* we don't use the checkpoint if there's a bitmap */
3630 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 j = mddev->recovery_cp;
3632 else
3633 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07003634 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 for (m = 0; m < SYNC_MARKS; m++) {
3636 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07003637 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 }
3639 last_mark = 0;
3640 mddev->resync_mark = mark[last_mark];
3641 mddev->resync_mark_cnt = mark_cnt[last_mark];
3642
3643 /*
3644 * Tune reconstruction:
3645 */
3646 window = 32*(PAGE_SIZE/512);
3647 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
3648 window/2,(unsigned long long) max_sectors/2);
3649
3650 atomic_set(&mddev->recovery_active, 0);
3651 init_waitqueue_head(&mddev->recovery_wait);
3652 last_check = 0;
3653
3654 if (j>2) {
3655 printk(KERN_INFO
3656 "md: resuming recovery of %s from checkpoint.\n",
3657 mdname(mddev));
3658 mddev->curr_resync = j;
3659 }
3660
3661 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07003662 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
NeilBrown57afd892005-06-21 17:17:13 -07003664 skipped = 0;
3665 sectors = mddev->pers->sync_request(mddev, j, &skipped,
3666 currspeed < sysctl_speed_limit_min);
3667 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3669 goto out;
3670 }
NeilBrown57afd892005-06-21 17:17:13 -07003671
3672 if (!skipped) { /* actual IO requested */
3673 io_sectors += sectors;
3674 atomic_add(sectors, &mddev->recovery_active);
3675 }
3676
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 j += sectors;
3678 if (j>1) mddev->curr_resync = j;
3679
NeilBrown57afd892005-06-21 17:17:13 -07003680
3681 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 continue;
3683
NeilBrown57afd892005-06-21 17:17:13 -07003684 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
3686 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
3687 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
3688 break;
3689
3690 repeat:
3691 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
3692 /* step marks */
3693 int next = (last_mark+1) % SYNC_MARKS;
3694
3695 mddev->resync_mark = mark[next];
3696 mddev->resync_mark_cnt = mark_cnt[next];
3697 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07003698 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 last_mark = next;
3700 }
3701
3702
3703 if (signal_pending(current)) {
3704 /*
3705 * got a signal, exit.
3706 */
3707 printk(KERN_INFO
3708 "md: md_do_sync() got signal ... exiting\n");
3709 flush_signals(current);
3710 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3711 goto out;
3712 }
3713
3714 /*
3715 * this loop exits only if either when we are slower than
3716 * the 'hard' speed limit, or the system was IO-idle for
3717 * a jiffy.
3718 * the system might be non-idle CPU-wise, but we only care
3719 * about not overloading the IO subsystem. (things like an
3720 * e2fsck being done on the RAID array should execute fast)
3721 */
3722 mddev->queue->unplug_fn(mddev->queue);
3723 cond_resched();
3724
NeilBrown57afd892005-06-21 17:17:13 -07003725 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
3726 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
3728 if (currspeed > sysctl_speed_limit_min) {
3729 if ((currspeed > sysctl_speed_limit_max) ||
3730 !is_mddev_idle(mddev)) {
3731 msleep_interruptible(250);
3732 goto repeat;
3733 }
3734 }
3735 }
3736 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
3737 /*
3738 * this also signals 'finished resyncing' to md_stop
3739 */
3740 out:
3741 mddev->queue->unplug_fn(mddev->queue);
3742
3743 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
3744
3745 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07003746 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
3748 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
3749 mddev->curr_resync > 2 &&
3750 mddev->curr_resync >= mddev->recovery_cp) {
3751 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
3752 printk(KERN_INFO
3753 "md: checkpointing recovery of %s.\n",
3754 mdname(mddev));
3755 mddev->recovery_cp = mddev->curr_resync;
3756 } else
3757 mddev->recovery_cp = MaxSector;
3758 }
3759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 skip:
3761 mddev->curr_resync = 0;
3762 wake_up(&resync_wait);
3763 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
3764 md_wakeup_thread(mddev->thread);
3765}
3766
3767
3768/*
3769 * This routine is regularly called by all per-raid-array threads to
3770 * deal with generic issues like resync and super-block update.
3771 * Raid personalities that don't have a thread (linear/raid0) do not
3772 * need this as they never do any recovery or update the superblock.
3773 *
3774 * It does not do any resync itself, but rather "forks" off other threads
3775 * to do that as needed.
3776 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
3777 * "->recovery" and create a thread at ->sync_thread.
3778 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
3779 * and wakeups up this thread which will reap the thread and finish up.
3780 * This thread also removes any faulty devices (with nr_pending == 0).
3781 *
3782 * The overall approach is:
3783 * 1/ if the superblock needs updating, update it.
3784 * 2/ If a recovery thread is running, don't do anything else.
3785 * 3/ If recovery has finished, clean up, possibly marking spares active.
3786 * 4/ If there are any faulty devices, remove them.
3787 * 5/ If array is degraded, try to add spares devices
3788 * 6/ If array has spares or is not in-sync, start a resync thread.
3789 */
3790void md_check_recovery(mddev_t *mddev)
3791{
3792 mdk_rdev_t *rdev;
3793 struct list_head *rtmp;
3794
3795
NeilBrown5f404022005-06-21 17:17:16 -07003796 if (mddev->bitmap)
3797 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799 if (mddev->ro)
3800 return;
NeilBrownfca4d842005-06-21 17:17:11 -07003801
3802 if (signal_pending(current)) {
3803 if (mddev->pers->sync_request) {
3804 printk(KERN_INFO "md: %s in immediate safe mode\n",
3805 mdname(mddev));
3806 mddev->safemode = 2;
3807 }
3808 flush_signals(current);
3809 }
3810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 if ( ! (
3812 mddev->sb_dirty ||
3813 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07003814 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
3815 (mddev->safemode == 1) ||
3816 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
3817 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 ))
3819 return;
NeilBrownfca4d842005-06-21 17:17:11 -07003820
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 if (mddev_trylock(mddev)==0) {
3822 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07003823
NeilBrown06d91a52005-06-21 17:17:12 -07003824 spin_lock(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07003825 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
3826 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
3827 mddev->in_sync = 1;
3828 mddev->sb_dirty = 1;
3829 }
3830 if (mddev->safemode == 1)
3831 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07003832 spin_unlock(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07003833
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 if (mddev->sb_dirty)
3835 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07003836
NeilBrown06d91a52005-06-21 17:17:12 -07003837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
3839 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
3840 /* resync/recovery still happening */
3841 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3842 goto unlock;
3843 }
3844 if (mddev->sync_thread) {
3845 /* resync has finished, collect result */
3846 md_unregister_thread(mddev->sync_thread);
3847 mddev->sync_thread = NULL;
3848 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
3849 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
3850 /* success...*/
3851 /* activate any spares */
3852 mddev->pers->spare_active(mddev);
3853 }
3854 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07003855
3856 /* if array is no-longer degraded, then any saved_raid_disk
3857 * information must be scrapped
3858 */
3859 if (!mddev->degraded)
3860 ITERATE_RDEV(mddev,rdev,rtmp)
3861 rdev->saved_raid_disk = -1;
3862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 mddev->recovery = 0;
3864 /* flag recovery needed just to double check */
3865 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3866 goto unlock;
3867 }
3868 if (mddev->recovery)
3869 /* probably just the RECOVERY_NEEDED flag */
3870 mddev->recovery = 0;
3871
3872 /* no recovery is running.
3873 * remove any failed drives, then
3874 * add spares if possible.
3875 * Spare are also removed and re-added, to allow
3876 * the personality to fail the re-add.
3877 */
3878 ITERATE_RDEV(mddev,rdev,rtmp)
3879 if (rdev->raid_disk >= 0 &&
3880 (rdev->faulty || ! rdev->in_sync) &&
3881 atomic_read(&rdev->nr_pending)==0) {
3882 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0)
3883 rdev->raid_disk = -1;
3884 }
3885
3886 if (mddev->degraded) {
3887 ITERATE_RDEV(mddev,rdev,rtmp)
3888 if (rdev->raid_disk < 0
3889 && !rdev->faulty) {
3890 if (mddev->pers->hot_add_disk(mddev,rdev))
3891 spares++;
3892 else
3893 break;
3894 }
3895 }
3896
3897 if (!spares && (mddev->recovery_cp == MaxSector )) {
3898 /* nothing we can do ... */
3899 goto unlock;
3900 }
3901 if (mddev->pers->sync_request) {
3902 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3903 if (!spares)
3904 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07003905 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
3906 /* We are adding a device or devices to an array
3907 * which has the bitmap stored on all devices.
3908 * So make sure all bitmap pages get written
3909 */
3910 bitmap_write_all(mddev->bitmap);
3911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 mddev->sync_thread = md_register_thread(md_do_sync,
3913 mddev,
3914 "%s_resync");
3915 if (!mddev->sync_thread) {
3916 printk(KERN_ERR "%s: could not start resync"
3917 " thread...\n",
3918 mdname(mddev));
3919 /* leave the spares where they are, it shouldn't hurt */
3920 mddev->recovery = 0;
3921 } else {
3922 md_wakeup_thread(mddev->sync_thread);
3923 }
3924 }
3925 unlock:
3926 mddev_unlock(mddev);
3927 }
3928}
3929
Adrian Bunk75c96f82005-05-05 16:16:09 -07003930static int md_notify_reboot(struct notifier_block *this,
3931 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932{
3933 struct list_head *tmp;
3934 mddev_t *mddev;
3935
3936 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
3937
3938 printk(KERN_INFO "md: stopping all md devices.\n");
3939
3940 ITERATE_MDDEV(mddev,tmp)
3941 if (mddev_trylock(mddev)==0)
3942 do_md_stop (mddev, 1);
3943 /*
3944 * certain more exotic SCSI devices are known to be
3945 * volatile wrt too early system reboots. While the
3946 * right place to handle this issue is the given
3947 * driver, we do want to have a safe RAID driver ...
3948 */
3949 mdelay(1000*1);
3950 }
3951 return NOTIFY_DONE;
3952}
3953
Adrian Bunk75c96f82005-05-05 16:16:09 -07003954static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 .notifier_call = md_notify_reboot,
3956 .next = NULL,
3957 .priority = INT_MAX, /* before any real devices */
3958};
3959
3960static void md_geninit(void)
3961{
3962 struct proc_dir_entry *p;
3963
3964 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
3965
3966 p = create_proc_entry("mdstat", S_IRUGO, NULL);
3967 if (p)
3968 p->proc_fops = &md_seq_fops;
3969}
3970
Adrian Bunk75c96f82005-05-05 16:16:09 -07003971static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972{
3973 int minor;
3974
3975 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
3976 " MD_SB_DISKS=%d\n",
3977 MD_MAJOR_VERSION, MD_MINOR_VERSION,
3978 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrown32a76272005-06-21 17:17:14 -07003979 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR,
3980 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 if (register_blkdev(MAJOR_NR, "md"))
3983 return -1;
3984 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
3985 unregister_blkdev(MAJOR_NR, "md");
3986 return -1;
3987 }
3988 devfs_mk_dir("md");
3989 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
3990 md_probe, NULL, NULL);
3991 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
3992 md_probe, NULL, NULL);
3993
3994 for (minor=0; minor < MAX_MD_DEVS; ++minor)
3995 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
3996 S_IFBLK|S_IRUSR|S_IWUSR,
3997 "md/%d", minor);
3998
3999 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4000 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4001 S_IFBLK|S_IRUSR|S_IWUSR,
4002 "md/mdp%d", minor);
4003
4004
4005 register_reboot_notifier(&md_notifier);
4006 raid_table_header = register_sysctl_table(raid_root_table, 1);
4007
4008 md_geninit();
4009 return (0);
4010}
4011
4012
4013#ifndef MODULE
4014
4015/*
4016 * Searches all registered partitions for autorun RAID arrays
4017 * at boot time.
4018 */
4019static dev_t detected_devices[128];
4020static int dev_cnt;
4021
4022void md_autodetect_dev(dev_t dev)
4023{
4024 if (dev_cnt >= 0 && dev_cnt < 127)
4025 detected_devices[dev_cnt++] = dev;
4026}
4027
4028
4029static void autostart_arrays(int part)
4030{
4031 mdk_rdev_t *rdev;
4032 int i;
4033
4034 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4035
4036 for (i = 0; i < dev_cnt; i++) {
4037 dev_t dev = detected_devices[i];
4038
4039 rdev = md_import_device(dev,0, 0);
4040 if (IS_ERR(rdev))
4041 continue;
4042
4043 if (rdev->faulty) {
4044 MD_BUG();
4045 continue;
4046 }
4047 list_add(&rdev->same_set, &pending_raid_disks);
4048 }
4049 dev_cnt = 0;
4050
4051 autorun_devices(part);
4052}
4053
4054#endif
4055
4056static __exit void md_exit(void)
4057{
4058 mddev_t *mddev;
4059 struct list_head *tmp;
4060 int i;
4061 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4062 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4063 for (i=0; i < MAX_MD_DEVS; i++)
4064 devfs_remove("md/%d", i);
4065 for (i=0; i < MAX_MD_DEVS; i++)
4066 devfs_remove("md/d%d", i);
4067
4068 devfs_remove("md");
4069
4070 unregister_blkdev(MAJOR_NR,"md");
4071 unregister_blkdev(mdp_major, "mdp");
4072 unregister_reboot_notifier(&md_notifier);
4073 unregister_sysctl_table(raid_table_header);
4074 remove_proc_entry("mdstat", NULL);
4075 ITERATE_MDDEV(mddev,tmp) {
4076 struct gendisk *disk = mddev->gendisk;
4077 if (!disk)
4078 continue;
4079 export_array(mddev);
4080 del_gendisk(disk);
4081 put_disk(disk);
4082 mddev->gendisk = NULL;
4083 mddev_put(mddev);
4084 }
4085}
4086
4087module_init(md_init)
4088module_exit(md_exit)
4089
4090EXPORT_SYMBOL(register_md_personality);
4091EXPORT_SYMBOL(unregister_md_personality);
4092EXPORT_SYMBOL(md_error);
4093EXPORT_SYMBOL(md_done_sync);
4094EXPORT_SYMBOL(md_write_start);
4095EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096EXPORT_SYMBOL(md_register_thread);
4097EXPORT_SYMBOL(md_unregister_thread);
4098EXPORT_SYMBOL(md_wakeup_thread);
4099EXPORT_SYMBOL(md_print_devices);
4100EXPORT_SYMBOL(md_check_recovery);
4101MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07004102MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07004103MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);