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