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