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