blob: 3fb80397f8aa6bce192177759d4812bec099e8a0 [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);
NeilBrownba22dcb2005-11-08 21:39:31 -08001585 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1588 if (!size) {
1589 printk(KERN_WARNING
1590 "md: %s has zero or unknown size, marking faulty!\n",
1591 bdevname(rdev->bdev,b));
1592 err = -EINVAL;
1593 goto abort_free;
1594 }
1595
1596 if (super_format >= 0) {
1597 err = super_types[super_format].
1598 load_super(rdev, NULL, super_minor);
1599 if (err == -EINVAL) {
1600 printk(KERN_WARNING
1601 "md: %s has invalid sb, not importing!\n",
1602 bdevname(rdev->bdev,b));
1603 goto abort_free;
1604 }
1605 if (err < 0) {
1606 printk(KERN_WARNING
1607 "md: could not read %s's sb, not importing!\n",
1608 bdevname(rdev->bdev,b));
1609 goto abort_free;
1610 }
1611 }
1612 INIT_LIST_HEAD(&rdev->same_set);
1613
1614 return rdev;
1615
1616abort_free:
1617 if (rdev->sb_page) {
1618 if (rdev->bdev)
1619 unlock_rdev(rdev);
1620 free_disk_sb(rdev);
1621 }
1622 kfree(rdev);
1623 return ERR_PTR(err);
1624}
1625
1626/*
1627 * Check a full RAID array for plausibility
1628 */
1629
1630
NeilBrowna757e642005-04-16 15:26:42 -07001631static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 int i;
1634 struct list_head *tmp;
1635 mdk_rdev_t *rdev, *freshest;
1636 char b[BDEVNAME_SIZE];
1637
1638 freshest = NULL;
1639 ITERATE_RDEV(mddev,rdev,tmp)
1640 switch (super_types[mddev->major_version].
1641 load_super(rdev, freshest, mddev->minor_version)) {
1642 case 1:
1643 freshest = rdev;
1644 break;
1645 case 0:
1646 break;
1647 default:
1648 printk( KERN_ERR \
1649 "md: fatal superblock inconsistency in %s"
1650 " -- removing from array\n",
1651 bdevname(rdev->bdev,b));
1652 kick_rdev_from_array(rdev);
1653 }
1654
1655
1656 super_types[mddev->major_version].
1657 validate_super(mddev, freshest);
1658
1659 i = 0;
1660 ITERATE_RDEV(mddev,rdev,tmp) {
1661 if (rdev != freshest)
1662 if (super_types[mddev->major_version].
1663 validate_super(mddev, rdev)) {
1664 printk(KERN_WARNING "md: kicking non-fresh %s"
1665 " from array!\n",
1666 bdevname(rdev->bdev,b));
1667 kick_rdev_from_array(rdev);
1668 continue;
1669 }
1670 if (mddev->level == LEVEL_MULTIPATH) {
1671 rdev->desc_nr = i++;
1672 rdev->raid_disk = rdev->desc_nr;
1673 rdev->in_sync = 1;
1674 }
1675 }
1676
1677
1678
1679 if (mddev->recovery_cp != MaxSector &&
1680 mddev->level >= 1)
1681 printk(KERN_ERR "md: %s: raid array is not clean"
1682 " -- starting background reconstruction\n",
1683 mdname(mddev));
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
NeilBrowneae17012005-11-08 21:39:23 -08001687static ssize_t
1688md_show_level(mddev_t *mddev, char *page)
1689{
1690 mdk_personality_t *p = mddev->pers;
1691 if (p == NULL)
1692 return 0;
1693 if (mddev->level >= 0)
1694 return sprintf(page, "RAID-%d\n", mddev->level);
1695 else
1696 return sprintf(page, "%s\n", p->name);
1697}
1698
1699static struct md_sysfs_entry md_level = {
1700 .attr = {.name = "level", .mode = S_IRUGO },
1701 .show = md_show_level,
1702};
1703
1704static ssize_t
1705md_show_rdisks(mddev_t *mddev, char *page)
1706{
1707 return sprintf(page, "%d\n", mddev->raid_disks);
1708}
1709
1710static struct md_sysfs_entry md_raid_disks = {
1711 .attr = {.name = "raid_disks", .mode = S_IRUGO },
1712 .show = md_show_rdisks,
1713};
1714
NeilBrown24dd4692005-11-08 21:39:26 -08001715static ssize_t
1716md_show_scan(mddev_t *mddev, char *page)
1717{
1718 char *type = "none";
NeilBrown31399d92005-11-08 21:39:29 -08001719 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1720 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
1721 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08001722 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1723 type = "resync";
1724 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1725 type = "check";
1726 else
1727 type = "repair";
1728 } else
1729 type = "recover";
1730 }
1731 return sprintf(page, "%s\n", type);
1732}
1733
1734static ssize_t
1735md_store_scan(mddev_t *mddev, const char *page, size_t len)
1736{
1737 int canscan=0;
NeilBrown31399d92005-11-08 21:39:29 -08001738
1739 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1740 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08001741 return -EBUSY;
1742 down(&mddev->reconfig_sem);
1743 if (mddev->pers && mddev->pers->sync_request)
1744 canscan=1;
1745 up(&mddev->reconfig_sem);
1746 if (!canscan)
1747 return -EINVAL;
1748
1749 if (strcmp(page, "check")==0 || strcmp(page, "check\n")==0)
1750 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1751 else if (strcmp(page, "repair")!=0 && strcmp(page, "repair\n")!=0)
1752 return -EINVAL;
1753 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1754 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1755 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1756 md_wakeup_thread(mddev->thread);
1757 return len;
1758}
1759
NeilBrown9d888832005-11-08 21:39:26 -08001760static ssize_t
1761md_show_mismatch(mddev_t *mddev, char *page)
1762{
1763 return sprintf(page, "%llu\n",
1764 (unsigned long long) mddev->resync_mismatches);
1765}
1766
NeilBrown24dd4692005-11-08 21:39:26 -08001767static struct md_sysfs_entry md_scan_mode = {
1768 .attr = {.name = "scan_mode", .mode = S_IRUGO|S_IWUSR },
1769 .show = md_show_scan,
1770 .store = md_store_scan,
1771};
1772
NeilBrown9d888832005-11-08 21:39:26 -08001773static struct md_sysfs_entry md_mismatches = {
1774 .attr = {.name = "mismatch_cnt", .mode = S_IRUGO },
1775 .show = md_show_mismatch,
1776};
1777
NeilBrowneae17012005-11-08 21:39:23 -08001778static struct attribute *md_default_attrs[] = {
1779 &md_level.attr,
1780 &md_raid_disks.attr,
NeilBrown24dd4692005-11-08 21:39:26 -08001781 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08001782 &md_mismatches.attr,
NeilBrowneae17012005-11-08 21:39:23 -08001783 NULL,
1784};
1785
1786static ssize_t
1787md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1788{
1789 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1790 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1791
1792 if (!entry->show)
1793 return -EIO;
1794 return entry->show(mddev, page);
1795}
1796
1797static ssize_t
1798md_attr_store(struct kobject *kobj, struct attribute *attr,
1799 const char *page, size_t length)
1800{
1801 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1802 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1803
1804 if (!entry->store)
1805 return -EIO;
1806 return entry->store(mddev, page, length);
1807}
1808
1809static void md_free(struct kobject *ko)
1810{
1811 mddev_t *mddev = container_of(ko, mddev_t, kobj);
1812 kfree(mddev);
1813}
1814
1815static struct sysfs_ops md_sysfs_ops = {
1816 .show = md_attr_show,
1817 .store = md_attr_store,
1818};
1819static struct kobj_type md_ktype = {
1820 .release = md_free,
1821 .sysfs_ops = &md_sysfs_ops,
1822 .default_attrs = md_default_attrs,
1823};
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825int mdp_major = 0;
1826
1827static struct kobject *md_probe(dev_t dev, int *part, void *data)
1828{
1829 static DECLARE_MUTEX(disks_sem);
1830 mddev_t *mddev = mddev_find(dev);
1831 struct gendisk *disk;
1832 int partitioned = (MAJOR(dev) != MD_MAJOR);
1833 int shift = partitioned ? MdpMinorShift : 0;
1834 int unit = MINOR(dev) >> shift;
1835
1836 if (!mddev)
1837 return NULL;
1838
1839 down(&disks_sem);
1840 if (mddev->gendisk) {
1841 up(&disks_sem);
1842 mddev_put(mddev);
1843 return NULL;
1844 }
1845 disk = alloc_disk(1 << shift);
1846 if (!disk) {
1847 up(&disks_sem);
1848 mddev_put(mddev);
1849 return NULL;
1850 }
1851 disk->major = MAJOR(dev);
1852 disk->first_minor = unit << shift;
1853 if (partitioned) {
1854 sprintf(disk->disk_name, "md_d%d", unit);
1855 sprintf(disk->devfs_name, "md/d%d", unit);
1856 } else {
1857 sprintf(disk->disk_name, "md%d", unit);
1858 sprintf(disk->devfs_name, "md/%d", unit);
1859 }
1860 disk->fops = &md_fops;
1861 disk->private_data = mddev;
1862 disk->queue = mddev->queue;
1863 add_disk(disk);
1864 mddev->gendisk = disk;
1865 up(&disks_sem);
NeilBrown9c791972005-11-08 21:39:28 -08001866 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08001867 mddev->kobj.k_name = NULL;
1868 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
1869 mddev->kobj.ktype = &md_ktype;
1870 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return NULL;
1872}
1873
1874void md_wakeup_thread(mdk_thread_t *thread);
1875
1876static void md_safemode_timeout(unsigned long data)
1877{
1878 mddev_t *mddev = (mddev_t *) data;
1879
1880 mddev->safemode = 1;
1881 md_wakeup_thread(mddev->thread);
1882}
1883
1884
1885static int do_md_run(mddev_t * mddev)
1886{
1887 int pnum, err;
1888 int chunk_size;
1889 struct list_head *tmp;
1890 mdk_rdev_t *rdev;
1891 struct gendisk *disk;
1892 char b[BDEVNAME_SIZE];
1893
NeilBrowna757e642005-04-16 15:26:42 -07001894 if (list_empty(&mddev->disks))
1895 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 if (mddev->pers)
1899 return -EBUSY;
1900
1901 /*
1902 * Analyze all RAID superblock(s)
1903 */
NeilBrowna757e642005-04-16 15:26:42 -07001904 if (!mddev->raid_disks)
1905 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 chunk_size = mddev->chunk_size;
1908 pnum = level_to_pers(mddev->level);
1909
1910 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1911 if (!chunk_size) {
1912 /*
1913 * 'default chunksize' in the old md code used to
1914 * be PAGE_SIZE, baaad.
1915 * we abort here to be on the safe side. We don't
1916 * want to continue the bad practice.
1917 */
1918 printk(KERN_ERR
1919 "no chunksize specified, see 'man raidtab'\n");
1920 return -EINVAL;
1921 }
1922 if (chunk_size > MAX_CHUNK_SIZE) {
1923 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1924 chunk_size, MAX_CHUNK_SIZE);
1925 return -EINVAL;
1926 }
1927 /*
1928 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1929 */
1930 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07001931 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return -EINVAL;
1933 }
1934 if (chunk_size < PAGE_SIZE) {
1935 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1936 chunk_size, PAGE_SIZE);
1937 return -EINVAL;
1938 }
1939
1940 /* devices must have minimum size of one chunk */
1941 ITERATE_RDEV(mddev,rdev,tmp) {
1942 if (rdev->faulty)
1943 continue;
1944 if (rdev->size < chunk_size / 1024) {
1945 printk(KERN_WARNING
1946 "md: Dev %s smaller than chunk_size:"
1947 " %lluk < %dk\n",
1948 bdevname(rdev->bdev,b),
1949 (unsigned long long)rdev->size,
1950 chunk_size / 1024);
1951 return -EINVAL;
1952 }
1953 }
1954 }
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956#ifdef CONFIG_KMOD
1957 if (!pers[pnum])
1958 {
1959 request_module("md-personality-%d", pnum);
1960 }
1961#endif
1962
1963 /*
1964 * Drop all container device buffers, from now on
1965 * the only valid external interface is through the md
1966 * device.
1967 * Also find largest hardsector size
1968 */
1969 ITERATE_RDEV(mddev,rdev,tmp) {
1970 if (rdev->faulty)
1971 continue;
1972 sync_blockdev(rdev->bdev);
1973 invalidate_bdev(rdev->bdev, 0);
1974 }
1975
1976 md_probe(mddev->unit, NULL, NULL);
1977 disk = mddev->gendisk;
1978 if (!disk)
1979 return -ENOMEM;
1980
1981 spin_lock(&pers_lock);
1982 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
1983 spin_unlock(&pers_lock);
1984 printk(KERN_WARNING "md: personality %d is not loaded!\n",
1985 pnum);
1986 return -EINVAL;
1987 }
1988
1989 mddev->pers = pers[pnum];
1990 spin_unlock(&pers_lock);
1991
NeilBrown657390d2005-08-26 18:34:16 -07001992 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
1994
NeilBrown32a76272005-06-21 17:17:14 -07001995 /* before we start the array running, initialise the bitmap */
1996 err = bitmap_create(mddev);
1997 if (err)
1998 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
1999 mdname(mddev), err);
2000 else
2001 err = mddev->pers->run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (err) {
2003 printk(KERN_ERR "md: pers->run() failed ...\n");
2004 module_put(mddev->pers->owner);
2005 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002006 bitmap_destroy(mddev);
2007 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009 atomic_set(&mddev->writes_pending,0);
2010 mddev->safemode = 0;
2011 mddev->safemode_timer.function = md_safemode_timeout;
2012 mddev->safemode_timer.data = (unsigned long) mddev;
2013 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2014 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002015
2016 ITERATE_RDEV(mddev,rdev,tmp)
2017 if (rdev->raid_disk >= 0) {
2018 char nm[20];
2019 sprintf(nm, "rd%d", rdev->raid_disk);
2020 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002024 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 if (mddev->sb_dirty)
2027 md_update_sb(mddev);
2028
2029 set_capacity(disk, mddev->array_size<<1);
2030
2031 /* If we call blk_queue_make_request here, it will
2032 * re-initialise max_sectors etc which may have been
2033 * refined inside -> run. So just set the bits we need to set.
2034 * Most initialisation happended when we called
2035 * blk_queue_make_request(..., md_fail_request)
2036 * earlier.
2037 */
2038 mddev->queue->queuedata = mddev;
2039 mddev->queue->make_request_fn = mddev->pers->make_request;
2040
2041 mddev->changed = 1;
2042 return 0;
2043}
2044
2045static int restart_array(mddev_t *mddev)
2046{
2047 struct gendisk *disk = mddev->gendisk;
2048 int err;
2049
2050 /*
2051 * Complain if it has no devices
2052 */
2053 err = -ENXIO;
2054 if (list_empty(&mddev->disks))
2055 goto out;
2056
2057 if (mddev->pers) {
2058 err = -EBUSY;
2059 if (!mddev->ro)
2060 goto out;
2061
2062 mddev->safemode = 0;
2063 mddev->ro = 0;
2064 set_disk_ro(disk, 0);
2065
2066 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2067 mdname(mddev));
2068 /*
2069 * Kick recovery or resync if necessary
2070 */
2071 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2072 md_wakeup_thread(mddev->thread);
2073 err = 0;
2074 } else {
2075 printk(KERN_ERR "md: %s has no personality assigned.\n",
2076 mdname(mddev));
2077 err = -EINVAL;
2078 }
2079
2080out:
2081 return err;
2082}
2083
2084static int do_md_stop(mddev_t * mddev, int ro)
2085{
2086 int err = 0;
2087 struct gendisk *disk = mddev->gendisk;
2088
2089 if (mddev->pers) {
2090 if (atomic_read(&mddev->active)>2) {
2091 printk("md: %s still in use.\n",mdname(mddev));
2092 return -EBUSY;
2093 }
2094
2095 if (mddev->sync_thread) {
2096 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2097 md_unregister_thread(mddev->sync_thread);
2098 mddev->sync_thread = NULL;
2099 }
2100
2101 del_timer_sync(&mddev->safemode_timer);
2102
2103 invalidate_partition(disk, 0);
2104
2105 if (ro) {
2106 err = -ENXIO;
2107 if (mddev->ro)
2108 goto out;
2109 mddev->ro = 1;
2110 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002111 bitmap_flush(mddev);
2112 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (mddev->ro)
2114 set_disk_ro(disk, 0);
2115 blk_queue_make_request(mddev->queue, md_fail_request);
2116 mddev->pers->stop(mddev);
2117 module_put(mddev->pers->owner);
2118 mddev->pers = NULL;
2119 if (mddev->ro)
2120 mddev->ro = 0;
2121 }
2122 if (!mddev->in_sync) {
2123 /* mark array as shutdown cleanly */
2124 mddev->in_sync = 1;
2125 md_update_sb(mddev);
2126 }
2127 if (ro)
2128 set_disk_ro(disk, 1);
2129 }
NeilBrown32a76272005-06-21 17:17:14 -07002130
2131 bitmap_destroy(mddev);
2132 if (mddev->bitmap_file) {
2133 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2134 fput(mddev->bitmap_file);
2135 mddev->bitmap_file = NULL;
2136 }
NeilBrown92232142005-08-18 11:24:16 -07002137 mddev->bitmap_offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07002138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 /*
2140 * Free resources if final stop
2141 */
2142 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002143 mdk_rdev_t *rdev;
2144 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 struct gendisk *disk;
2146 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2147
NeilBrown86e6ffd2005-11-08 21:39:24 -08002148 ITERATE_RDEV(mddev,rdev,tmp)
2149 if (rdev->raid_disk >= 0) {
2150 char nm[20];
2151 sprintf(nm, "rd%d", rdev->raid_disk);
2152 sysfs_remove_link(&mddev->kobj, nm);
2153 }
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 export_array(mddev);
2156
2157 mddev->array_size = 0;
2158 disk = mddev->gendisk;
2159 if (disk)
2160 set_capacity(disk, 0);
2161 mddev->changed = 1;
2162 } else
2163 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2164 mdname(mddev));
2165 err = 0;
2166out:
2167 return err;
2168}
2169
2170static void autorun_array(mddev_t *mddev)
2171{
2172 mdk_rdev_t *rdev;
2173 struct list_head *tmp;
2174 int err;
2175
NeilBrowna757e642005-04-16 15:26:42 -07002176 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 printk(KERN_INFO "md: running: ");
2180
2181 ITERATE_RDEV(mddev,rdev,tmp) {
2182 char b[BDEVNAME_SIZE];
2183 printk("<%s>", bdevname(rdev->bdev,b));
2184 }
2185 printk("\n");
2186
2187 err = do_md_run (mddev);
2188 if (err) {
2189 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2190 do_md_stop (mddev, 0);
2191 }
2192}
2193
2194/*
2195 * lets try to run arrays based on all disks that have arrived
2196 * until now. (those are in pending_raid_disks)
2197 *
2198 * the method: pick the first pending disk, collect all disks with
2199 * the same UUID, remove all from the pending list and put them into
2200 * the 'same_array' list. Then order this list based on superblock
2201 * update time (freshest comes first), kick out 'old' disks and
2202 * compare superblocks. If everything's fine then run it.
2203 *
2204 * If "unit" is allocated, then bump its reference count
2205 */
2206static void autorun_devices(int part)
2207{
2208 struct list_head candidates;
2209 struct list_head *tmp;
2210 mdk_rdev_t *rdev0, *rdev;
2211 mddev_t *mddev;
2212 char b[BDEVNAME_SIZE];
2213
2214 printk(KERN_INFO "md: autorun ...\n");
2215 while (!list_empty(&pending_raid_disks)) {
2216 dev_t dev;
2217 rdev0 = list_entry(pending_raid_disks.next,
2218 mdk_rdev_t, same_set);
2219
2220 printk(KERN_INFO "md: considering %s ...\n",
2221 bdevname(rdev0->bdev,b));
2222 INIT_LIST_HEAD(&candidates);
2223 ITERATE_RDEV_PENDING(rdev,tmp)
2224 if (super_90_load(rdev, rdev0, 0) >= 0) {
2225 printk(KERN_INFO "md: adding %s ...\n",
2226 bdevname(rdev->bdev,b));
2227 list_move(&rdev->same_set, &candidates);
2228 }
2229 /*
2230 * now we have a set of devices, with all of them having
2231 * mostly sane superblocks. It's time to allocate the
2232 * mddev.
2233 */
2234 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2235 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2236 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2237 break;
2238 }
2239 if (part)
2240 dev = MKDEV(mdp_major,
2241 rdev0->preferred_minor << MdpMinorShift);
2242 else
2243 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2244
2245 md_probe(dev, NULL, NULL);
2246 mddev = mddev_find(dev);
2247 if (!mddev) {
2248 printk(KERN_ERR
2249 "md: cannot allocate memory for md drive.\n");
2250 break;
2251 }
2252 if (mddev_lock(mddev))
2253 printk(KERN_WARNING "md: %s locked, cannot run\n",
2254 mdname(mddev));
2255 else if (mddev->raid_disks || mddev->major_version
2256 || !list_empty(&mddev->disks)) {
2257 printk(KERN_WARNING
2258 "md: %s already running, cannot run %s\n",
2259 mdname(mddev), bdevname(rdev0->bdev,b));
2260 mddev_unlock(mddev);
2261 } else {
2262 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2263 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2264 list_del_init(&rdev->same_set);
2265 if (bind_rdev_to_array(rdev, mddev))
2266 export_rdev(rdev);
2267 }
2268 autorun_array(mddev);
2269 mddev_unlock(mddev);
2270 }
2271 /* on success, candidates will be empty, on error
2272 * it won't...
2273 */
2274 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2275 export_rdev(rdev);
2276 mddev_put(mddev);
2277 }
2278 printk(KERN_INFO "md: ... autorun DONE.\n");
2279}
2280
2281/*
2282 * import RAID devices based on one partition
2283 * if possible, the array gets run as well.
2284 */
2285
2286static int autostart_array(dev_t startdev)
2287{
2288 char b[BDEVNAME_SIZE];
2289 int err = -EINVAL, i;
2290 mdp_super_t *sb = NULL;
2291 mdk_rdev_t *start_rdev = NULL, *rdev;
2292
2293 start_rdev = md_import_device(startdev, 0, 0);
2294 if (IS_ERR(start_rdev))
2295 return err;
2296
2297
2298 /* NOTE: this can only work for 0.90.0 superblocks */
2299 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2300 if (sb->major_version != 0 ||
2301 sb->minor_version != 90 ) {
2302 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2303 export_rdev(start_rdev);
2304 return err;
2305 }
2306
2307 if (start_rdev->faulty) {
2308 printk(KERN_WARNING
2309 "md: can not autostart based on faulty %s!\n",
2310 bdevname(start_rdev->bdev,b));
2311 export_rdev(start_rdev);
2312 return err;
2313 }
2314 list_add(&start_rdev->same_set, &pending_raid_disks);
2315
2316 for (i = 0; i < MD_SB_DISKS; i++) {
2317 mdp_disk_t *desc = sb->disks + i;
2318 dev_t dev = MKDEV(desc->major, desc->minor);
2319
2320 if (!dev)
2321 continue;
2322 if (dev == startdev)
2323 continue;
2324 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2325 continue;
2326 rdev = md_import_device(dev, 0, 0);
2327 if (IS_ERR(rdev))
2328 continue;
2329
2330 list_add(&rdev->same_set, &pending_raid_disks);
2331 }
2332
2333 /*
2334 * possibly return codes
2335 */
2336 autorun_devices(0);
2337 return 0;
2338
2339}
2340
2341
2342static int get_version(void __user * arg)
2343{
2344 mdu_version_t ver;
2345
2346 ver.major = MD_MAJOR_VERSION;
2347 ver.minor = MD_MINOR_VERSION;
2348 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2349
2350 if (copy_to_user(arg, &ver, sizeof(ver)))
2351 return -EFAULT;
2352
2353 return 0;
2354}
2355
2356static int get_array_info(mddev_t * mddev, void __user * arg)
2357{
2358 mdu_array_info_t info;
2359 int nr,working,active,failed,spare;
2360 mdk_rdev_t *rdev;
2361 struct list_head *tmp;
2362
2363 nr=working=active=failed=spare=0;
2364 ITERATE_RDEV(mddev,rdev,tmp) {
2365 nr++;
2366 if (rdev->faulty)
2367 failed++;
2368 else {
2369 working++;
2370 if (rdev->in_sync)
2371 active++;
2372 else
2373 spare++;
2374 }
2375 }
2376
2377 info.major_version = mddev->major_version;
2378 info.minor_version = mddev->minor_version;
2379 info.patch_version = MD_PATCHLEVEL_VERSION;
2380 info.ctime = mddev->ctime;
2381 info.level = mddev->level;
2382 info.size = mddev->size;
2383 info.nr_disks = nr;
2384 info.raid_disks = mddev->raid_disks;
2385 info.md_minor = mddev->md_minor;
2386 info.not_persistent= !mddev->persistent;
2387
2388 info.utime = mddev->utime;
2389 info.state = 0;
2390 if (mddev->in_sync)
2391 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07002392 if (mddev->bitmap && mddev->bitmap_offset)
2393 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 info.active_disks = active;
2395 info.working_disks = working;
2396 info.failed_disks = failed;
2397 info.spare_disks = spare;
2398
2399 info.layout = mddev->layout;
2400 info.chunk_size = mddev->chunk_size;
2401
2402 if (copy_to_user(arg, &info, sizeof(info)))
2403 return -EFAULT;
2404
2405 return 0;
2406}
2407
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002408static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07002409{
2410 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2411 char *ptr, *buf = NULL;
2412 int err = -ENOMEM;
2413
2414 file = kmalloc(sizeof(*file), GFP_KERNEL);
2415 if (!file)
2416 goto out;
2417
2418 /* bitmap disabled, zero the first byte and copy out */
2419 if (!mddev->bitmap || !mddev->bitmap->file) {
2420 file->pathname[0] = '\0';
2421 goto copy_out;
2422 }
2423
2424 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2425 if (!buf)
2426 goto out;
2427
2428 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2429 if (!ptr)
2430 goto out;
2431
2432 strcpy(file->pathname, ptr);
2433
2434copy_out:
2435 err = 0;
2436 if (copy_to_user(arg, file, sizeof(*file)))
2437 err = -EFAULT;
2438out:
2439 kfree(buf);
2440 kfree(file);
2441 return err;
2442}
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444static int get_disk_info(mddev_t * mddev, void __user * arg)
2445{
2446 mdu_disk_info_t info;
2447 unsigned int nr;
2448 mdk_rdev_t *rdev;
2449
2450 if (copy_from_user(&info, arg, sizeof(info)))
2451 return -EFAULT;
2452
2453 nr = info.number;
2454
2455 rdev = find_rdev_nr(mddev, nr);
2456 if (rdev) {
2457 info.major = MAJOR(rdev->bdev->bd_dev);
2458 info.minor = MINOR(rdev->bdev->bd_dev);
2459 info.raid_disk = rdev->raid_disk;
2460 info.state = 0;
2461 if (rdev->faulty)
2462 info.state |= (1<<MD_DISK_FAULTY);
2463 else if (rdev->in_sync) {
2464 info.state |= (1<<MD_DISK_ACTIVE);
2465 info.state |= (1<<MD_DISK_SYNC);
2466 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002467 if (test_bit(WriteMostly, &rdev->flags))
2468 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 } else {
2470 info.major = info.minor = 0;
2471 info.raid_disk = -1;
2472 info.state = (1<<MD_DISK_REMOVED);
2473 }
2474
2475 if (copy_to_user(arg, &info, sizeof(info)))
2476 return -EFAULT;
2477
2478 return 0;
2479}
2480
2481static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2482{
2483 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2484 mdk_rdev_t *rdev;
2485 dev_t dev = MKDEV(info->major,info->minor);
2486
2487 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2488 return -EOVERFLOW;
2489
2490 if (!mddev->raid_disks) {
2491 int err;
2492 /* expecting a device which has a superblock */
2493 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2494 if (IS_ERR(rdev)) {
2495 printk(KERN_WARNING
2496 "md: md_import_device returned %ld\n",
2497 PTR_ERR(rdev));
2498 return PTR_ERR(rdev);
2499 }
2500 if (!list_empty(&mddev->disks)) {
2501 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2502 mdk_rdev_t, same_set);
2503 int err = super_types[mddev->major_version]
2504 .load_super(rdev, rdev0, mddev->minor_version);
2505 if (err < 0) {
2506 printk(KERN_WARNING
2507 "md: %s has different UUID to %s\n",
2508 bdevname(rdev->bdev,b),
2509 bdevname(rdev0->bdev,b2));
2510 export_rdev(rdev);
2511 return -EINVAL;
2512 }
2513 }
2514 err = bind_rdev_to_array(rdev, mddev);
2515 if (err)
2516 export_rdev(rdev);
2517 return err;
2518 }
2519
2520 /*
2521 * add_new_disk can be used once the array is assembled
2522 * to add "hot spares". They must already have a superblock
2523 * written
2524 */
2525 if (mddev->pers) {
2526 int err;
2527 if (!mddev->pers->hot_add_disk) {
2528 printk(KERN_WARNING
2529 "%s: personality does not support diskops!\n",
2530 mdname(mddev));
2531 return -EINVAL;
2532 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07002533 if (mddev->persistent)
2534 rdev = md_import_device(dev, mddev->major_version,
2535 mddev->minor_version);
2536 else
2537 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 if (IS_ERR(rdev)) {
2539 printk(KERN_WARNING
2540 "md: md_import_device returned %ld\n",
2541 PTR_ERR(rdev));
2542 return PTR_ERR(rdev);
2543 }
NeilBrown41158c72005-06-21 17:17:25 -07002544 /* set save_raid_disk if appropriate */
2545 if (!mddev->persistent) {
2546 if (info->state & (1<<MD_DISK_SYNC) &&
2547 info->raid_disk < mddev->raid_disks)
2548 rdev->raid_disk = info->raid_disk;
2549 else
2550 rdev->raid_disk = -1;
2551 } else
2552 super_types[mddev->major_version].
2553 validate_super(mddev, rdev);
2554 rdev->saved_raid_disk = rdev->raid_disk;
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 rdev->in_sync = 0; /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002557 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2558 set_bit(WriteMostly, &rdev->flags);
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 rdev->raid_disk = -1;
2561 err = bind_rdev_to_array(rdev, mddev);
2562 if (err)
2563 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07002564
2565 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002566 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return err;
2568 }
2569
2570 /* otherwise, add_new_disk is only allowed
2571 * for major_version==0 superblocks
2572 */
2573 if (mddev->major_version != 0) {
2574 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2575 mdname(mddev));
2576 return -EINVAL;
2577 }
2578
2579 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2580 int err;
2581 rdev = md_import_device (dev, -1, 0);
2582 if (IS_ERR(rdev)) {
2583 printk(KERN_WARNING
2584 "md: error, md_import_device() returned %ld\n",
2585 PTR_ERR(rdev));
2586 return PTR_ERR(rdev);
2587 }
2588 rdev->desc_nr = info->number;
2589 if (info->raid_disk < mddev->raid_disks)
2590 rdev->raid_disk = info->raid_disk;
2591 else
2592 rdev->raid_disk = -1;
2593
2594 rdev->faulty = 0;
2595 if (rdev->raid_disk < mddev->raid_disks)
2596 rdev->in_sync = (info->state & (1<<MD_DISK_SYNC));
2597 else
2598 rdev->in_sync = 0;
2599
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002600 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2601 set_bit(WriteMostly, &rdev->flags);
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 err = bind_rdev_to_array(rdev, mddev);
2604 if (err) {
2605 export_rdev(rdev);
2606 return err;
2607 }
2608
2609 if (!mddev->persistent) {
2610 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2611 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2612 } else
2613 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2614 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2615
2616 if (!mddev->size || (mddev->size > rdev->size))
2617 mddev->size = rdev->size;
2618 }
2619
2620 return 0;
2621}
2622
2623static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2624{
2625 char b[BDEVNAME_SIZE];
2626 mdk_rdev_t *rdev;
2627
2628 if (!mddev->pers)
2629 return -ENODEV;
2630
2631 rdev = find_rdev(mddev, dev);
2632 if (!rdev)
2633 return -ENXIO;
2634
2635 if (rdev->raid_disk >= 0)
2636 goto busy;
2637
2638 kick_rdev_from_array(rdev);
2639 md_update_sb(mddev);
2640
2641 return 0;
2642busy:
2643 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2644 bdevname(rdev->bdev,b), mdname(mddev));
2645 return -EBUSY;
2646}
2647
2648static int hot_add_disk(mddev_t * mddev, dev_t dev)
2649{
2650 char b[BDEVNAME_SIZE];
2651 int err;
2652 unsigned int size;
2653 mdk_rdev_t *rdev;
2654
2655 if (!mddev->pers)
2656 return -ENODEV;
2657
2658 if (mddev->major_version != 0) {
2659 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2660 " version-0 superblocks.\n",
2661 mdname(mddev));
2662 return -EINVAL;
2663 }
2664 if (!mddev->pers->hot_add_disk) {
2665 printk(KERN_WARNING
2666 "%s: personality does not support diskops!\n",
2667 mdname(mddev));
2668 return -EINVAL;
2669 }
2670
2671 rdev = md_import_device (dev, -1, 0);
2672 if (IS_ERR(rdev)) {
2673 printk(KERN_WARNING
2674 "md: error, md_import_device() returned %ld\n",
2675 PTR_ERR(rdev));
2676 return -EINVAL;
2677 }
2678
2679 if (mddev->persistent)
2680 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2681 else
2682 rdev->sb_offset =
2683 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2684
2685 size = calc_dev_size(rdev, mddev->chunk_size);
2686 rdev->size = size;
2687
2688 if (size < mddev->size) {
2689 printk(KERN_WARNING
2690 "%s: disk size %llu blocks < array size %llu\n",
2691 mdname(mddev), (unsigned long long)size,
2692 (unsigned long long)mddev->size);
2693 err = -ENOSPC;
2694 goto abort_export;
2695 }
2696
2697 if (rdev->faulty) {
2698 printk(KERN_WARNING
2699 "md: can not hot-add faulty %s disk to %s!\n",
2700 bdevname(rdev->bdev,b), mdname(mddev));
2701 err = -EINVAL;
2702 goto abort_export;
2703 }
2704 rdev->in_sync = 0;
2705 rdev->desc_nr = -1;
2706 bind_rdev_to_array(rdev, mddev);
2707
2708 /*
2709 * The rest should better be atomic, we can have disk failures
2710 * noticed in interrupt contexts ...
2711 */
2712
2713 if (rdev->desc_nr == mddev->max_disks) {
2714 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2715 mdname(mddev));
2716 err = -EBUSY;
2717 goto abort_unbind_export;
2718 }
2719
2720 rdev->raid_disk = -1;
2721
2722 md_update_sb(mddev);
2723
2724 /*
2725 * Kick recovery, maybe this spare has to be added to the
2726 * array immediately.
2727 */
2728 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2729 md_wakeup_thread(mddev->thread);
2730
2731 return 0;
2732
2733abort_unbind_export:
2734 unbind_rdev_from_array(rdev);
2735
2736abort_export:
2737 export_rdev(rdev);
2738 return err;
2739}
2740
NeilBrown32a76272005-06-21 17:17:14 -07002741/* similar to deny_write_access, but accounts for our holding a reference
2742 * to the file ourselves */
2743static int deny_bitmap_write_access(struct file * file)
2744{
2745 struct inode *inode = file->f_mapping->host;
2746
2747 spin_lock(&inode->i_lock);
2748 if (atomic_read(&inode->i_writecount) > 1) {
2749 spin_unlock(&inode->i_lock);
2750 return -ETXTBSY;
2751 }
2752 atomic_set(&inode->i_writecount, -1);
2753 spin_unlock(&inode->i_lock);
2754
2755 return 0;
2756}
2757
2758static int set_bitmap_file(mddev_t *mddev, int fd)
2759{
2760 int err;
2761
NeilBrown36fa3062005-09-09 16:23:45 -07002762 if (mddev->pers) {
2763 if (!mddev->pers->quiesce)
2764 return -EBUSY;
2765 if (mddev->recovery || mddev->sync_thread)
2766 return -EBUSY;
2767 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07002768 }
2769
NeilBrown36fa3062005-09-09 16:23:45 -07002770
2771 if (fd >= 0) {
2772 if (mddev->bitmap)
2773 return -EEXIST; /* cannot add when bitmap is present */
2774 mddev->bitmap_file = fget(fd);
2775
2776 if (mddev->bitmap_file == NULL) {
2777 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2778 mdname(mddev));
2779 return -EBADF;
2780 }
2781
2782 err = deny_bitmap_write_access(mddev->bitmap_file);
2783 if (err) {
2784 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2785 mdname(mddev));
2786 fput(mddev->bitmap_file);
2787 mddev->bitmap_file = NULL;
2788 return err;
2789 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002790 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07002791 } else if (mddev->bitmap == NULL)
2792 return -ENOENT; /* cannot remove what isn't there */
2793 err = 0;
2794 if (mddev->pers) {
2795 mddev->pers->quiesce(mddev, 1);
2796 if (fd >= 0)
2797 err = bitmap_create(mddev);
2798 if (fd < 0 || err)
2799 bitmap_destroy(mddev);
2800 mddev->pers->quiesce(mddev, 0);
2801 } else if (fd < 0) {
2802 if (mddev->bitmap_file)
2803 fput(mddev->bitmap_file);
2804 mddev->bitmap_file = NULL;
2805 }
2806
NeilBrown32a76272005-06-21 17:17:14 -07002807 return err;
2808}
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810/*
2811 * set_array_info is used two different ways
2812 * The original usage is when creating a new array.
2813 * In this usage, raid_disks is > 0 and it together with
2814 * level, size, not_persistent,layout,chunksize determine the
2815 * shape of the array.
2816 * This will always create an array with a type-0.90.0 superblock.
2817 * The newer usage is when assembling an array.
2818 * In this case raid_disks will be 0, and the major_version field is
2819 * use to determine which style super-blocks are to be found on the devices.
2820 * The minor and patch _version numbers are also kept incase the
2821 * super_block handler wishes to interpret them.
2822 */
2823static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2824{
2825
2826 if (info->raid_disks == 0) {
2827 /* just setting version number for superblock loading */
2828 if (info->major_version < 0 ||
2829 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2830 super_types[info->major_version].name == NULL) {
2831 /* maybe try to auto-load a module? */
2832 printk(KERN_INFO
2833 "md: superblock version %d not known\n",
2834 info->major_version);
2835 return -EINVAL;
2836 }
2837 mddev->major_version = info->major_version;
2838 mddev->minor_version = info->minor_version;
2839 mddev->patch_version = info->patch_version;
2840 return 0;
2841 }
2842 mddev->major_version = MD_MAJOR_VERSION;
2843 mddev->minor_version = MD_MINOR_VERSION;
2844 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2845 mddev->ctime = get_seconds();
2846
2847 mddev->level = info->level;
2848 mddev->size = info->size;
2849 mddev->raid_disks = info->raid_disks;
2850 /* don't set md_minor, it is determined by which /dev/md* was
2851 * openned
2852 */
2853 if (info->state & (1<<MD_SB_CLEAN))
2854 mddev->recovery_cp = MaxSector;
2855 else
2856 mddev->recovery_cp = 0;
2857 mddev->persistent = ! info->not_persistent;
2858
2859 mddev->layout = info->layout;
2860 mddev->chunk_size = info->chunk_size;
2861
2862 mddev->max_disks = MD_SB_DISKS;
2863
2864 mddev->sb_dirty = 1;
2865
2866 /*
2867 * Generate a 128 bit UUID
2868 */
2869 get_random_bytes(mddev->uuid, 16);
2870
2871 return 0;
2872}
2873
2874/*
2875 * update_array_info is used to change the configuration of an
2876 * on-line array.
2877 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2878 * fields in the info are checked against the array.
2879 * Any differences that cannot be handled will cause an error.
2880 * Normally, only one change can be managed at a time.
2881 */
2882static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2883{
2884 int rv = 0;
2885 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07002886 int state = 0;
2887
2888 /* calculate expected state,ignoring low bits */
2889 if (mddev->bitmap && mddev->bitmap_offset)
2890 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 if (mddev->major_version != info->major_version ||
2893 mddev->minor_version != info->minor_version ||
2894/* mddev->patch_version != info->patch_version || */
2895 mddev->ctime != info->ctime ||
2896 mddev->level != info->level ||
2897/* mddev->layout != info->layout || */
2898 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07002899 mddev->chunk_size != info->chunk_size ||
2900 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2901 ((state^info->state) & 0xfffffe00)
2902 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return -EINVAL;
2904 /* Check there is only one change */
2905 if (mddev->size != info->size) cnt++;
2906 if (mddev->raid_disks != info->raid_disks) cnt++;
2907 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07002908 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (cnt == 0) return 0;
2910 if (cnt > 1) return -EINVAL;
2911
2912 if (mddev->layout != info->layout) {
2913 /* Change layout
2914 * we don't need to do anything at the md level, the
2915 * personality will take care of it all.
2916 */
2917 if (mddev->pers->reconfig == NULL)
2918 return -EINVAL;
2919 else
2920 return mddev->pers->reconfig(mddev, info->layout, -1);
2921 }
2922 if (mddev->size != info->size) {
2923 mdk_rdev_t * rdev;
2924 struct list_head *tmp;
2925 if (mddev->pers->resize == NULL)
2926 return -EINVAL;
2927 /* The "size" is the amount of each device that is used.
2928 * This can only make sense for arrays with redundancy.
2929 * linear and raid0 always use whatever space is available
2930 * We can only consider changing the size if no resync
2931 * or reconstruction is happening, and if the new size
2932 * is acceptable. It must fit before the sb_offset or,
2933 * if that is <data_offset, it must fit before the
2934 * size of each device.
2935 * If size is zero, we find the largest size that fits.
2936 */
2937 if (mddev->sync_thread)
2938 return -EBUSY;
2939 ITERATE_RDEV(mddev,rdev,tmp) {
2940 sector_t avail;
2941 int fit = (info->size == 0);
2942 if (rdev->sb_offset > rdev->data_offset)
2943 avail = (rdev->sb_offset*2) - rdev->data_offset;
2944 else
2945 avail = get_capacity(rdev->bdev->bd_disk)
2946 - rdev->data_offset;
2947 if (fit && (info->size == 0 || info->size > avail/2))
2948 info->size = avail/2;
2949 if (avail < ((sector_t)info->size << 1))
2950 return -ENOSPC;
2951 }
2952 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
2953 if (!rv) {
2954 struct block_device *bdev;
2955
2956 bdev = bdget_disk(mddev->gendisk, 0);
2957 if (bdev) {
2958 down(&bdev->bd_inode->i_sem);
2959 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2960 up(&bdev->bd_inode->i_sem);
2961 bdput(bdev);
2962 }
2963 }
2964 }
2965 if (mddev->raid_disks != info->raid_disks) {
2966 /* change the number of raid disks */
2967 if (mddev->pers->reshape == NULL)
2968 return -EINVAL;
2969 if (info->raid_disks <= 0 ||
2970 info->raid_disks >= mddev->max_disks)
2971 return -EINVAL;
2972 if (mddev->sync_thread)
2973 return -EBUSY;
2974 rv = mddev->pers->reshape(mddev, info->raid_disks);
2975 if (!rv) {
2976 struct block_device *bdev;
2977
2978 bdev = bdget_disk(mddev->gendisk, 0);
2979 if (bdev) {
2980 down(&bdev->bd_inode->i_sem);
2981 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2982 up(&bdev->bd_inode->i_sem);
2983 bdput(bdev);
2984 }
2985 }
2986 }
NeilBrown36fa3062005-09-09 16:23:45 -07002987 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
2988 if (mddev->pers->quiesce == NULL)
2989 return -EINVAL;
2990 if (mddev->recovery || mddev->sync_thread)
2991 return -EBUSY;
2992 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
2993 /* add the bitmap */
2994 if (mddev->bitmap)
2995 return -EEXIST;
2996 if (mddev->default_bitmap_offset == 0)
2997 return -EINVAL;
2998 mddev->bitmap_offset = mddev->default_bitmap_offset;
2999 mddev->pers->quiesce(mddev, 1);
3000 rv = bitmap_create(mddev);
3001 if (rv)
3002 bitmap_destroy(mddev);
3003 mddev->pers->quiesce(mddev, 0);
3004 } else {
3005 /* remove the bitmap */
3006 if (!mddev->bitmap)
3007 return -ENOENT;
3008 if (mddev->bitmap->file)
3009 return -EINVAL;
3010 mddev->pers->quiesce(mddev, 1);
3011 bitmap_destroy(mddev);
3012 mddev->pers->quiesce(mddev, 0);
3013 mddev->bitmap_offset = 0;
3014 }
3015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 md_update_sb(mddev);
3017 return rv;
3018}
3019
3020static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3021{
3022 mdk_rdev_t *rdev;
3023
3024 if (mddev->pers == NULL)
3025 return -ENODEV;
3026
3027 rdev = find_rdev(mddev, dev);
3028 if (!rdev)
3029 return -ENODEV;
3030
3031 md_error(mddev, rdev);
3032 return 0;
3033}
3034
3035static int md_ioctl(struct inode *inode, struct file *file,
3036 unsigned int cmd, unsigned long arg)
3037{
3038 int err = 0;
3039 void __user *argp = (void __user *)arg;
3040 struct hd_geometry __user *loc = argp;
3041 mddev_t *mddev = NULL;
3042
3043 if (!capable(CAP_SYS_ADMIN))
3044 return -EACCES;
3045
3046 /*
3047 * Commands dealing with the RAID driver but not any
3048 * particular array:
3049 */
3050 switch (cmd)
3051 {
3052 case RAID_VERSION:
3053 err = get_version(argp);
3054 goto done;
3055
3056 case PRINT_RAID_DEBUG:
3057 err = 0;
3058 md_print_devices();
3059 goto done;
3060
3061#ifndef MODULE
3062 case RAID_AUTORUN:
3063 err = 0;
3064 autostart_arrays(arg);
3065 goto done;
3066#endif
3067 default:;
3068 }
3069
3070 /*
3071 * Commands creating/starting a new array:
3072 */
3073
3074 mddev = inode->i_bdev->bd_disk->private_data;
3075
3076 if (!mddev) {
3077 BUG();
3078 goto abort;
3079 }
3080
3081
3082 if (cmd == START_ARRAY) {
3083 /* START_ARRAY doesn't need to lock the array as autostart_array
3084 * does the locking, and it could even be a different array
3085 */
3086 static int cnt = 3;
3087 if (cnt > 0 ) {
3088 printk(KERN_WARNING
3089 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
3090 "This will not be supported beyond 2.6\n",
3091 current->comm, current->pid);
3092 cnt--;
3093 }
3094 err = autostart_array(new_decode_dev(arg));
3095 if (err) {
3096 printk(KERN_WARNING "md: autostart failed!\n");
3097 goto abort;
3098 }
3099 goto done;
3100 }
3101
3102 err = mddev_lock(mddev);
3103 if (err) {
3104 printk(KERN_INFO
3105 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3106 err, cmd);
3107 goto abort;
3108 }
3109
3110 switch (cmd)
3111 {
3112 case SET_ARRAY_INFO:
3113 {
3114 mdu_array_info_t info;
3115 if (!arg)
3116 memset(&info, 0, sizeof(info));
3117 else if (copy_from_user(&info, argp, sizeof(info))) {
3118 err = -EFAULT;
3119 goto abort_unlock;
3120 }
3121 if (mddev->pers) {
3122 err = update_array_info(mddev, &info);
3123 if (err) {
3124 printk(KERN_WARNING "md: couldn't update"
3125 " array info. %d\n", err);
3126 goto abort_unlock;
3127 }
3128 goto done_unlock;
3129 }
3130 if (!list_empty(&mddev->disks)) {
3131 printk(KERN_WARNING
3132 "md: array %s already has disks!\n",
3133 mdname(mddev));
3134 err = -EBUSY;
3135 goto abort_unlock;
3136 }
3137 if (mddev->raid_disks) {
3138 printk(KERN_WARNING
3139 "md: array %s already initialised!\n",
3140 mdname(mddev));
3141 err = -EBUSY;
3142 goto abort_unlock;
3143 }
3144 err = set_array_info(mddev, &info);
3145 if (err) {
3146 printk(KERN_WARNING "md: couldn't set"
3147 " array info. %d\n", err);
3148 goto abort_unlock;
3149 }
3150 }
3151 goto done_unlock;
3152
3153 default:;
3154 }
3155
3156 /*
3157 * Commands querying/configuring an existing array:
3158 */
NeilBrown32a76272005-06-21 17:17:14 -07003159 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3160 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3161 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3162 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 err = -ENODEV;
3164 goto abort_unlock;
3165 }
3166
3167 /*
3168 * Commands even a read-only array can execute:
3169 */
3170 switch (cmd)
3171 {
3172 case GET_ARRAY_INFO:
3173 err = get_array_info(mddev, argp);
3174 goto done_unlock;
3175
NeilBrown32a76272005-06-21 17:17:14 -07003176 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003177 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003178 goto done_unlock;
3179
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 case GET_DISK_INFO:
3181 err = get_disk_info(mddev, argp);
3182 goto done_unlock;
3183
3184 case RESTART_ARRAY_RW:
3185 err = restart_array(mddev);
3186 goto done_unlock;
3187
3188 case STOP_ARRAY:
3189 err = do_md_stop (mddev, 0);
3190 goto done_unlock;
3191
3192 case STOP_ARRAY_RO:
3193 err = do_md_stop (mddev, 1);
3194 goto done_unlock;
3195
3196 /*
3197 * We have a problem here : there is no easy way to give a CHS
3198 * virtual geometry. We currently pretend that we have a 2 heads
3199 * 4 sectors (with a BIG number of cylinders...). This drives
3200 * dosfs just mad... ;-)
3201 */
3202 case HDIO_GETGEO:
3203 if (!loc) {
3204 err = -EINVAL;
3205 goto abort_unlock;
3206 }
3207 err = put_user (2, (char __user *) &loc->heads);
3208 if (err)
3209 goto abort_unlock;
3210 err = put_user (4, (char __user *) &loc->sectors);
3211 if (err)
3212 goto abort_unlock;
3213 err = put_user(get_capacity(mddev->gendisk)/8,
3214 (short __user *) &loc->cylinders);
3215 if (err)
3216 goto abort_unlock;
3217 err = put_user (get_start_sect(inode->i_bdev),
3218 (long __user *) &loc->start);
3219 goto done_unlock;
3220 }
3221
3222 /*
3223 * The remaining ioctls are changing the state of the
3224 * superblock, so we do not allow read-only arrays
3225 * here:
3226 */
3227 if (mddev->ro) {
3228 err = -EROFS;
3229 goto abort_unlock;
3230 }
3231
3232 switch (cmd)
3233 {
3234 case ADD_NEW_DISK:
3235 {
3236 mdu_disk_info_t info;
3237 if (copy_from_user(&info, argp, sizeof(info)))
3238 err = -EFAULT;
3239 else
3240 err = add_new_disk(mddev, &info);
3241 goto done_unlock;
3242 }
3243
3244 case HOT_REMOVE_DISK:
3245 err = hot_remove_disk(mddev, new_decode_dev(arg));
3246 goto done_unlock;
3247
3248 case HOT_ADD_DISK:
3249 err = hot_add_disk(mddev, new_decode_dev(arg));
3250 goto done_unlock;
3251
3252 case SET_DISK_FAULTY:
3253 err = set_disk_faulty(mddev, new_decode_dev(arg));
3254 goto done_unlock;
3255
3256 case RUN_ARRAY:
3257 err = do_md_run (mddev);
3258 goto done_unlock;
3259
NeilBrown32a76272005-06-21 17:17:14 -07003260 case SET_BITMAP_FILE:
3261 err = set_bitmap_file(mddev, (int)arg);
3262 goto done_unlock;
3263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 default:
3265 if (_IOC_TYPE(cmd) == MD_MAJOR)
3266 printk(KERN_WARNING "md: %s(pid %d) used"
3267 " obsolete MD ioctl, upgrade your"
3268 " software to use new ictls.\n",
3269 current->comm, current->pid);
3270 err = -EINVAL;
3271 goto abort_unlock;
3272 }
3273
3274done_unlock:
3275abort_unlock:
3276 mddev_unlock(mddev);
3277
3278 return err;
3279done:
3280 if (err)
3281 MD_BUG();
3282abort:
3283 return err;
3284}
3285
3286static int md_open(struct inode *inode, struct file *file)
3287{
3288 /*
3289 * Succeed if we can lock the mddev, which confirms that
3290 * it isn't being stopped right now.
3291 */
3292 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3293 int err;
3294
3295 if ((err = mddev_lock(mddev)))
3296 goto out;
3297
3298 err = 0;
3299 mddev_get(mddev);
3300 mddev_unlock(mddev);
3301
3302 check_disk_change(inode->i_bdev);
3303 out:
3304 return err;
3305}
3306
3307static int md_release(struct inode *inode, struct file * file)
3308{
3309 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3310
3311 if (!mddev)
3312 BUG();
3313 mddev_put(mddev);
3314
3315 return 0;
3316}
3317
3318static int md_media_changed(struct gendisk *disk)
3319{
3320 mddev_t *mddev = disk->private_data;
3321
3322 return mddev->changed;
3323}
3324
3325static int md_revalidate(struct gendisk *disk)
3326{
3327 mddev_t *mddev = disk->private_data;
3328
3329 mddev->changed = 0;
3330 return 0;
3331}
3332static struct block_device_operations md_fops =
3333{
3334 .owner = THIS_MODULE,
3335 .open = md_open,
3336 .release = md_release,
3337 .ioctl = md_ioctl,
3338 .media_changed = md_media_changed,
3339 .revalidate_disk= md_revalidate,
3340};
3341
Adrian Bunk75c96f82005-05-05 16:16:09 -07003342static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343{
3344 mdk_thread_t *thread = arg;
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 /*
3347 * md_thread is a 'system-thread', it's priority should be very
3348 * high. We avoid resource deadlocks individually in each
3349 * raid personality. (RAID5 does preallocation) We also use RR and
3350 * the very same RT priority as kswapd, thus we will never get
3351 * into a priority inversion deadlock.
3352 *
3353 * we definitely have to have equal or higher priority than
3354 * bdflush, otherwise bdflush will deadlock if there are too
3355 * many dirty RAID5 blocks.
3356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
NeilBrown6985c432005-10-19 21:23:47 -07003358 allow_signal(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 complete(thread->event);
NeilBrowna6fb0932005-09-09 16:23:56 -07003360 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 void (*run)(mddev_t *);
3362
NeilBrown32a76272005-06-21 17:17:14 -07003363 wait_event_interruptible_timeout(thread->wqueue,
NeilBrowna6fb0932005-09-09 16:23:56 -07003364 test_bit(THREAD_WAKEUP, &thread->flags)
3365 || kthread_should_stop(),
NeilBrown32a76272005-06-21 17:17:14 -07003366 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003367 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
3369 clear_bit(THREAD_WAKEUP, &thread->flags);
3370
3371 run = thread->run;
3372 if (run)
3373 run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
NeilBrowna6fb0932005-09-09 16:23:56 -07003375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 return 0;
3377}
3378
3379void md_wakeup_thread(mdk_thread_t *thread)
3380{
3381 if (thread) {
3382 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3383 set_bit(THREAD_WAKEUP, &thread->flags);
3384 wake_up(&thread->wqueue);
3385 }
3386}
3387
3388mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3389 const char *name)
3390{
3391 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 struct completion event;
3393
NeilBrowna6fb0932005-09-09 16:23:56 -07003394 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 if (!thread)
3396 return NULL;
3397
3398 memset(thread, 0, sizeof(mdk_thread_t));
3399 init_waitqueue_head(&thread->wqueue);
3400
3401 init_completion(&event);
3402 thread->event = &event;
3403 thread->run = run;
3404 thread->mddev = mddev;
3405 thread->name = name;
NeilBrown32a76272005-06-21 17:17:14 -07003406 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07003407 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07003408 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 kfree(thread);
3410 return NULL;
3411 }
3412 wait_for_completion(&event);
3413 return thread;
3414}
3415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416void md_unregister_thread(mdk_thread_t *thread)
3417{
NeilBrownd28446f2005-04-16 15:26:41 -07003418 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07003419
3420 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 kfree(thread);
3422}
3423
3424void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3425{
3426 if (!mddev) {
3427 MD_BUG();
3428 return;
3429 }
3430
3431 if (!rdev || rdev->faulty)
3432 return;
NeilBrown32a76272005-06-21 17:17:14 -07003433/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3435 mdname(mddev),
3436 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3437 __builtin_return_address(0),__builtin_return_address(1),
3438 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07003439*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 if (!mddev->pers->error_handler)
3441 return;
3442 mddev->pers->error_handler(mddev,rdev);
3443 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3444 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3445 md_wakeup_thread(mddev->thread);
3446}
3447
3448/* seq_file implementation /proc/mdstat */
3449
3450static void status_unused(struct seq_file *seq)
3451{
3452 int i = 0;
3453 mdk_rdev_t *rdev;
3454 struct list_head *tmp;
3455
3456 seq_printf(seq, "unused devices: ");
3457
3458 ITERATE_RDEV_PENDING(rdev,tmp) {
3459 char b[BDEVNAME_SIZE];
3460 i++;
3461 seq_printf(seq, "%s ",
3462 bdevname(rdev->bdev,b));
3463 }
3464 if (!i)
3465 seq_printf(seq, "<none>");
3466
3467 seq_printf(seq, "\n");
3468}
3469
3470
3471static void status_resync(struct seq_file *seq, mddev_t * mddev)
3472{
3473 unsigned long max_blocks, resync, res, dt, db, rt;
3474
3475 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3476
3477 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3478 max_blocks = mddev->resync_max_sectors >> 1;
3479 else
3480 max_blocks = mddev->size;
3481
3482 /*
3483 * Should not happen.
3484 */
3485 if (!max_blocks) {
3486 MD_BUG();
3487 return;
3488 }
3489 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3490 {
3491 int i, x = res/50, y = 20-x;
3492 seq_printf(seq, "[");
3493 for (i = 0; i < x; i++)
3494 seq_printf(seq, "=");
3495 seq_printf(seq, ">");
3496 for (i = 0; i < y; i++)
3497 seq_printf(seq, ".");
3498 seq_printf(seq, "] ");
3499 }
3500 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3501 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3502 "resync" : "recovery"),
3503 res/10, res % 10, resync, max_blocks);
3504
3505 /*
3506 * We do not want to overflow, so the order of operands and
3507 * the * 100 / 100 trick are important. We do a +1 to be
3508 * safe against division by zero. We only estimate anyway.
3509 *
3510 * dt: time from mark until now
3511 * db: blocks written from mark until now
3512 * rt: remaining time
3513 */
3514 dt = ((jiffies - mddev->resync_mark) / HZ);
3515 if (!dt) dt++;
3516 db = resync - (mddev->resync_mark_cnt/2);
3517 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3518
3519 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3520
3521 seq_printf(seq, " speed=%ldK/sec", db/dt);
3522}
3523
3524static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3525{
3526 struct list_head *tmp;
3527 loff_t l = *pos;
3528 mddev_t *mddev;
3529
3530 if (l >= 0x10000)
3531 return NULL;
3532 if (!l--)
3533 /* header */
3534 return (void*)1;
3535
3536 spin_lock(&all_mddevs_lock);
3537 list_for_each(tmp,&all_mddevs)
3538 if (!l--) {
3539 mddev = list_entry(tmp, mddev_t, all_mddevs);
3540 mddev_get(mddev);
3541 spin_unlock(&all_mddevs_lock);
3542 return mddev;
3543 }
3544 spin_unlock(&all_mddevs_lock);
3545 if (!l--)
3546 return (void*)2;/* tail */
3547 return NULL;
3548}
3549
3550static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3551{
3552 struct list_head *tmp;
3553 mddev_t *next_mddev, *mddev = v;
3554
3555 ++*pos;
3556 if (v == (void*)2)
3557 return NULL;
3558
3559 spin_lock(&all_mddevs_lock);
3560 if (v == (void*)1)
3561 tmp = all_mddevs.next;
3562 else
3563 tmp = mddev->all_mddevs.next;
3564 if (tmp != &all_mddevs)
3565 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3566 else {
3567 next_mddev = (void*)2;
3568 *pos = 0x10000;
3569 }
3570 spin_unlock(&all_mddevs_lock);
3571
3572 if (v != (void*)1)
3573 mddev_put(mddev);
3574 return next_mddev;
3575
3576}
3577
3578static void md_seq_stop(struct seq_file *seq, void *v)
3579{
3580 mddev_t *mddev = v;
3581
3582 if (mddev && v != (void*)1 && v != (void*)2)
3583 mddev_put(mddev);
3584}
3585
3586static int md_seq_show(struct seq_file *seq, void *v)
3587{
3588 mddev_t *mddev = v;
3589 sector_t size;
3590 struct list_head *tmp2;
3591 mdk_rdev_t *rdev;
3592 int i;
NeilBrown32a76272005-06-21 17:17:14 -07003593 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
3595 if (v == (void*)1) {
3596 seq_printf(seq, "Personalities : ");
3597 spin_lock(&pers_lock);
3598 for (i = 0; i < MAX_PERSONALITY; i++)
3599 if (pers[i])
3600 seq_printf(seq, "[%s] ", pers[i]->name);
3601
3602 spin_unlock(&pers_lock);
3603 seq_printf(seq, "\n");
3604 return 0;
3605 }
3606 if (v == (void*)2) {
3607 status_unused(seq);
3608 return 0;
3609 }
3610
3611 if (mddev_lock(mddev)!=0)
3612 return -EINTR;
3613 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3614 seq_printf(seq, "%s : %sactive", mdname(mddev),
3615 mddev->pers ? "" : "in");
3616 if (mddev->pers) {
3617 if (mddev->ro)
3618 seq_printf(seq, " (read-only)");
3619 seq_printf(seq, " %s", mddev->pers->name);
3620 }
3621
3622 size = 0;
3623 ITERATE_RDEV(mddev,rdev,tmp2) {
3624 char b[BDEVNAME_SIZE];
3625 seq_printf(seq, " %s[%d]",
3626 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003627 if (test_bit(WriteMostly, &rdev->flags))
3628 seq_printf(seq, "(W)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 if (rdev->faulty) {
3630 seq_printf(seq, "(F)");
3631 continue;
NeilBrownb325a322005-09-09 16:24:00 -07003632 } else if (rdev->raid_disk < 0)
3633 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 size += rdev->size;
3635 }
3636
3637 if (!list_empty(&mddev->disks)) {
3638 if (mddev->pers)
3639 seq_printf(seq, "\n %llu blocks",
3640 (unsigned long long)mddev->array_size);
3641 else
3642 seq_printf(seq, "\n %llu blocks",
3643 (unsigned long long)size);
3644 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07003645 if (mddev->persistent) {
3646 if (mddev->major_version != 0 ||
3647 mddev->minor_version != 90) {
3648 seq_printf(seq," super %d.%d",
3649 mddev->major_version,
3650 mddev->minor_version);
3651 }
3652 } else
3653 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
3655 if (mddev->pers) {
3656 mddev->pers->status (seq, mddev);
3657 seq_printf(seq, "\n ");
NeilBrown32a76272005-06-21 17:17:14 -07003658 if (mddev->curr_resync > 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 status_resync (seq, mddev);
NeilBrown32a76272005-06-21 17:17:14 -07003660 seq_printf(seq, "\n ");
3661 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3662 seq_printf(seq, " resync=DELAYED\n ");
3663 } else
3664 seq_printf(seq, "\n ");
3665
3666 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07003667 unsigned long chunk_kb;
3668 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07003669 spin_lock_irqsave(&bitmap->lock, flags);
3670 chunk_kb = bitmap->chunksize >> 10;
3671 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3672 "%lu%s chunk",
3673 bitmap->pages - bitmap->missing_pages,
3674 bitmap->pages,
3675 (bitmap->pages - bitmap->missing_pages)
3676 << (PAGE_SHIFT - 10),
3677 chunk_kb ? chunk_kb : bitmap->chunksize,
3678 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07003679 if (bitmap->file) {
3680 seq_printf(seq, ", file: ");
3681 seq_path(seq, bitmap->file->f_vfsmnt,
3682 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07003683 }
NeilBrown78d742d2005-06-21 17:17:15 -07003684
NeilBrown32a76272005-06-21 17:17:14 -07003685 seq_printf(seq, "\n");
3686 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 }
3688
3689 seq_printf(seq, "\n");
3690 }
3691 mddev_unlock(mddev);
3692
3693 return 0;
3694}
3695
3696static struct seq_operations md_seq_ops = {
3697 .start = md_seq_start,
3698 .next = md_seq_next,
3699 .stop = md_seq_stop,
3700 .show = md_seq_show,
3701};
3702
3703static int md_seq_open(struct inode *inode, struct file *file)
3704{
3705 int error;
3706
3707 error = seq_open(file, &md_seq_ops);
3708 return error;
3709}
3710
3711static struct file_operations md_seq_fops = {
3712 .open = md_seq_open,
3713 .read = seq_read,
3714 .llseek = seq_lseek,
3715 .release = seq_release,
3716};
3717
3718int register_md_personality(int pnum, mdk_personality_t *p)
3719{
3720 if (pnum >= MAX_PERSONALITY) {
3721 printk(KERN_ERR
3722 "md: tried to install personality %s as nr %d, but max is %lu\n",
3723 p->name, pnum, MAX_PERSONALITY-1);
3724 return -EINVAL;
3725 }
3726
3727 spin_lock(&pers_lock);
3728 if (pers[pnum]) {
3729 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 return -EBUSY;
3731 }
3732
3733 pers[pnum] = p;
3734 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3735 spin_unlock(&pers_lock);
3736 return 0;
3737}
3738
3739int unregister_md_personality(int pnum)
3740{
NeilBrowna757e642005-04-16 15:26:42 -07003741 if (pnum >= MAX_PERSONALITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
3744 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3745 spin_lock(&pers_lock);
3746 pers[pnum] = NULL;
3747 spin_unlock(&pers_lock);
3748 return 0;
3749}
3750
3751static int is_mddev_idle(mddev_t *mddev)
3752{
3753 mdk_rdev_t * rdev;
3754 struct list_head *tmp;
3755 int idle;
3756 unsigned long curr_events;
3757
3758 idle = 1;
3759 ITERATE_RDEV(mddev,rdev,tmp) {
3760 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01003761 curr_events = disk_stat_read(disk, sectors[0]) +
3762 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 atomic_read(&disk->sync_io);
3764 /* Allow some slack between valud of curr_events and last_events,
3765 * as there are some uninteresting races.
3766 * Note: the following is an unsigned comparison.
3767 */
3768 if ((curr_events - rdev->last_events + 32) > 64) {
3769 rdev->last_events = curr_events;
3770 idle = 0;
3771 }
3772 }
3773 return idle;
3774}
3775
3776void md_done_sync(mddev_t *mddev, int blocks, int ok)
3777{
3778 /* another "blocks" (512byte) blocks have been synced */
3779 atomic_sub(blocks, &mddev->recovery_active);
3780 wake_up(&mddev->recovery_wait);
3781 if (!ok) {
3782 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3783 md_wakeup_thread(mddev->thread);
3784 // stop recovery, signal do_sync ....
3785 }
3786}
3787
3788
NeilBrown06d91a52005-06-21 17:17:12 -07003789/* md_write_start(mddev, bi)
3790 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07003791 * in superblock) before writing, schedule a superblock update
3792 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07003793 */
NeilBrown3d310eb2005-06-21 17:17:26 -07003794void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
NeilBrown06d91a52005-06-21 17:17:12 -07003796 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07003797 return;
NeilBrown06d91a52005-06-21 17:17:12 -07003798
3799 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07003800 if (mddev->in_sync) {
NeilBrown3d310eb2005-06-21 17:17:26 -07003801 spin_lock(&mddev->write_lock);
3802 if (mddev->in_sync) {
3803 mddev->in_sync = 0;
3804 mddev->sb_dirty = 1;
3805 md_wakeup_thread(mddev->thread);
3806 }
3807 spin_unlock(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07003808 }
NeilBrown3d310eb2005-06-21 17:17:26 -07003809 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810}
3811
3812void md_write_end(mddev_t *mddev)
3813{
3814 if (atomic_dec_and_test(&mddev->writes_pending)) {
3815 if (mddev->safemode == 2)
3816 md_wakeup_thread(mddev->thread);
3817 else
3818 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3819 }
3820}
3821
Adrian Bunk75c96f82005-05-05 16:16:09 -07003822static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
3824#define SYNC_MARKS 10
3825#define SYNC_MARK_STEP (3*HZ)
3826static void md_do_sync(mddev_t *mddev)
3827{
3828 mddev_t *mddev2;
3829 unsigned int currspeed = 0,
3830 window;
NeilBrown57afd892005-06-21 17:17:13 -07003831 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 unsigned long mark[SYNC_MARKS];
3833 sector_t mark_cnt[SYNC_MARKS];
3834 int last_mark,m;
3835 struct list_head *tmp;
3836 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07003837 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
3839 /* just incase thread restarts... */
3840 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3841 return;
3842
3843 /* we overload curr_resync somewhat here.
3844 * 0 == not engaged in resync at all
3845 * 2 == checking that there is no conflict with another sync
3846 * 1 == like 2, but have yielded to allow conflicting resync to
3847 * commense
3848 * other == active in resync - this many blocks
3849 *
3850 * Before starting a resync we must have set curr_resync to
3851 * 2, and then checked that every "conflicting" array has curr_resync
3852 * less than ours. When we find one that is the same or higher
3853 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3854 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3855 * This will mean we have to start checking from the beginning again.
3856 *
3857 */
3858
3859 do {
3860 mddev->curr_resync = 2;
3861
3862 try_again:
NeilBrown8712e552005-10-26 01:58:58 -07003863 if (signal_pending(current) ||
3864 kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 flush_signals(current);
NeilBrown6985c432005-10-19 21:23:47 -07003866 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 goto skip;
3868 }
3869 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 if (mddev2 == mddev)
3871 continue;
3872 if (mddev2->curr_resync &&
3873 match_mddev_units(mddev,mddev2)) {
3874 DEFINE_WAIT(wq);
3875 if (mddev < mddev2 && mddev->curr_resync == 2) {
3876 /* arbitrarily yield */
3877 mddev->curr_resync = 1;
3878 wake_up(&resync_wait);
3879 }
3880 if (mddev > mddev2 && mddev->curr_resync == 1)
3881 /* no need to wait here, we can wait the next
3882 * time 'round when curr_resync == 2
3883 */
3884 continue;
3885 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown8712e552005-10-26 01:58:58 -07003886 if (!signal_pending(current) &&
3887 !kthread_should_stop() &&
3888 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 printk(KERN_INFO "md: delaying resync of %s"
3890 " until %s has finished resync (they"
3891 " share one or more physical units)\n",
3892 mdname(mddev), mdname(mddev2));
3893 mddev_put(mddev2);
3894 schedule();
3895 finish_wait(&resync_wait, &wq);
3896 goto try_again;
3897 }
3898 finish_wait(&resync_wait, &wq);
3899 }
3900 }
3901 } while (mddev->curr_resync < 2);
3902
NeilBrown9d888832005-11-08 21:39:26 -08003903 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07003905 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 */
3907 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08003908 mddev->resync_mismatches = 0;
3909 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 /* recovery follows the physical size of devices */
3911 max_sectors = mddev->size << 1;
3912
3913 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
3914 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
3915 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
Adrian Bunk338cec32005-09-10 00:26:54 -07003916 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 "(but not more than %d KB/sec) for reconstruction.\n",
3918 sysctl_speed_limit_max);
3919
3920 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07003921 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08003922 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
3923 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 j = mddev->recovery_cp;
3925 else
3926 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07003927 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 for (m = 0; m < SYNC_MARKS; m++) {
3929 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07003930 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
3932 last_mark = 0;
3933 mddev->resync_mark = mark[last_mark];
3934 mddev->resync_mark_cnt = mark_cnt[last_mark];
3935
3936 /*
3937 * Tune reconstruction:
3938 */
3939 window = 32*(PAGE_SIZE/512);
3940 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
3941 window/2,(unsigned long long) max_sectors/2);
3942
3943 atomic_set(&mddev->recovery_active, 0);
3944 init_waitqueue_head(&mddev->recovery_wait);
3945 last_check = 0;
3946
3947 if (j>2) {
3948 printk(KERN_INFO
3949 "md: resuming recovery of %s from checkpoint.\n",
3950 mdname(mddev));
3951 mddev->curr_resync = j;
3952 }
3953
3954 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07003955 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
NeilBrown57afd892005-06-21 17:17:13 -07003957 skipped = 0;
3958 sectors = mddev->pers->sync_request(mddev, j, &skipped,
3959 currspeed < sysctl_speed_limit_min);
3960 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3962 goto out;
3963 }
NeilBrown57afd892005-06-21 17:17:13 -07003964
3965 if (!skipped) { /* actual IO requested */
3966 io_sectors += sectors;
3967 atomic_add(sectors, &mddev->recovery_active);
3968 }
3969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 j += sectors;
3971 if (j>1) mddev->curr_resync = j;
3972
NeilBrown57afd892005-06-21 17:17:13 -07003973
3974 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 continue;
3976
NeilBrown57afd892005-06-21 17:17:13 -07003977 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
3979 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
3980 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
3981 break;
3982
3983 repeat:
3984 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
3985 /* step marks */
3986 int next = (last_mark+1) % SYNC_MARKS;
3987
3988 mddev->resync_mark = mark[next];
3989 mddev->resync_mark_cnt = mark_cnt[next];
3990 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07003991 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 last_mark = next;
3993 }
3994
3995
NeilBrown8712e552005-10-26 01:58:58 -07003996 if (signal_pending(current) || kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 /*
3998 * got a signal, exit.
3999 */
4000 printk(KERN_INFO
4001 "md: md_do_sync() got signal ... exiting\n");
4002 flush_signals(current);
4003 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4004 goto out;
4005 }
4006
4007 /*
4008 * this loop exits only if either when we are slower than
4009 * the 'hard' speed limit, or the system was IO-idle for
4010 * a jiffy.
4011 * the system might be non-idle CPU-wise, but we only care
4012 * about not overloading the IO subsystem. (things like an
4013 * e2fsck being done on the RAID array should execute fast)
4014 */
4015 mddev->queue->unplug_fn(mddev->queue);
4016 cond_resched();
4017
NeilBrown57afd892005-06-21 17:17:13 -07004018 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4019 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 if (currspeed > sysctl_speed_limit_min) {
4022 if ((currspeed > sysctl_speed_limit_max) ||
4023 !is_mddev_idle(mddev)) {
4024 msleep_interruptible(250);
4025 goto repeat;
4026 }
4027 }
4028 }
4029 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4030 /*
4031 * this also signals 'finished resyncing' to md_stop
4032 */
4033 out:
4034 mddev->queue->unplug_fn(mddev->queue);
4035
4036 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4037
4038 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004039 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
4041 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4042 mddev->curr_resync > 2 &&
4043 mddev->curr_resync >= mddev->recovery_cp) {
4044 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4045 printk(KERN_INFO
4046 "md: checkpointing recovery of %s.\n",
4047 mdname(mddev));
4048 mddev->recovery_cp = mddev->curr_resync;
4049 } else
4050 mddev->recovery_cp = MaxSector;
4051 }
4052
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 skip:
4054 mddev->curr_resync = 0;
4055 wake_up(&resync_wait);
4056 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4057 md_wakeup_thread(mddev->thread);
4058}
4059
4060
4061/*
4062 * This routine is regularly called by all per-raid-array threads to
4063 * deal with generic issues like resync and super-block update.
4064 * Raid personalities that don't have a thread (linear/raid0) do not
4065 * need this as they never do any recovery or update the superblock.
4066 *
4067 * It does not do any resync itself, but rather "forks" off other threads
4068 * to do that as needed.
4069 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4070 * "->recovery" and create a thread at ->sync_thread.
4071 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4072 * and wakeups up this thread which will reap the thread and finish up.
4073 * This thread also removes any faulty devices (with nr_pending == 0).
4074 *
4075 * The overall approach is:
4076 * 1/ if the superblock needs updating, update it.
4077 * 2/ If a recovery thread is running, don't do anything else.
4078 * 3/ If recovery has finished, clean up, possibly marking spares active.
4079 * 4/ If there are any faulty devices, remove them.
4080 * 5/ If array is degraded, try to add spares devices
4081 * 6/ If array has spares or is not in-sync, start a resync thread.
4082 */
4083void md_check_recovery(mddev_t *mddev)
4084{
4085 mdk_rdev_t *rdev;
4086 struct list_head *rtmp;
4087
4088
NeilBrown5f404022005-06-21 17:17:16 -07004089 if (mddev->bitmap)
4090 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
4092 if (mddev->ro)
4093 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004094
4095 if (signal_pending(current)) {
4096 if (mddev->pers->sync_request) {
4097 printk(KERN_INFO "md: %s in immediate safe mode\n",
4098 mdname(mddev));
4099 mddev->safemode = 2;
4100 }
4101 flush_signals(current);
4102 }
4103
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 if ( ! (
4105 mddev->sb_dirty ||
4106 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004107 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4108 (mddev->safemode == 1) ||
4109 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4110 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 ))
4112 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 if (mddev_trylock(mddev)==0) {
4115 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004116
NeilBrown06d91a52005-06-21 17:17:12 -07004117 spin_lock(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004118 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4119 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4120 mddev->in_sync = 1;
4121 mddev->sb_dirty = 1;
4122 }
4123 if (mddev->safemode == 1)
4124 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07004125 spin_unlock(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 if (mddev->sb_dirty)
4128 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004129
NeilBrown06d91a52005-06-21 17:17:12 -07004130
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4132 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4133 /* resync/recovery still happening */
4134 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4135 goto unlock;
4136 }
4137 if (mddev->sync_thread) {
4138 /* resync has finished, collect result */
4139 md_unregister_thread(mddev->sync_thread);
4140 mddev->sync_thread = NULL;
4141 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4142 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4143 /* success...*/
4144 /* activate any spares */
4145 mddev->pers->spare_active(mddev);
4146 }
4147 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004148
4149 /* if array is no-longer degraded, then any saved_raid_disk
4150 * information must be scrapped
4151 */
4152 if (!mddev->degraded)
4153 ITERATE_RDEV(mddev,rdev,rtmp)
4154 rdev->saved_raid_disk = -1;
4155
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 mddev->recovery = 0;
4157 /* flag recovery needed just to double check */
4158 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4159 goto unlock;
4160 }
NeilBrown24dd4692005-11-08 21:39:26 -08004161 /* Clear some bits that don't mean anything, but
4162 * might be left set
4163 */
4164 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4165 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4166 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4167 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
4169 /* no recovery is running.
4170 * remove any failed drives, then
4171 * add spares if possible.
4172 * Spare are also removed and re-added, to allow
4173 * the personality to fail the re-add.
4174 */
4175 ITERATE_RDEV(mddev,rdev,rtmp)
4176 if (rdev->raid_disk >= 0 &&
4177 (rdev->faulty || ! rdev->in_sync) &&
4178 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004179 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4180 char nm[20];
4181 sprintf(nm,"rd%d", rdev->raid_disk);
4182 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 }
4186
4187 if (mddev->degraded) {
4188 ITERATE_RDEV(mddev,rdev,rtmp)
4189 if (rdev->raid_disk < 0
4190 && !rdev->faulty) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004191 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4192 char nm[20];
4193 sprintf(nm, "rd%d", rdev->raid_disk);
4194 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 spares++;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004196 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 break;
4198 }
4199 }
4200
NeilBrown24dd4692005-11-08 21:39:26 -08004201 if (spares) {
4202 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4203 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4204 } else if (mddev->recovery_cp < MaxSector) {
4205 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4206 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4207 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08004209
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 if (mddev->pers->sync_request) {
4211 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07004212 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4213 /* We are adding a device or devices to an array
4214 * which has the bitmap stored on all devices.
4215 * So make sure all bitmap pages get written
4216 */
4217 bitmap_write_all(mddev->bitmap);
4218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 mddev->sync_thread = md_register_thread(md_do_sync,
4220 mddev,
4221 "%s_resync");
4222 if (!mddev->sync_thread) {
4223 printk(KERN_ERR "%s: could not start resync"
4224 " thread...\n",
4225 mdname(mddev));
4226 /* leave the spares where they are, it shouldn't hurt */
4227 mddev->recovery = 0;
4228 } else {
4229 md_wakeup_thread(mddev->sync_thread);
4230 }
4231 }
4232 unlock:
4233 mddev_unlock(mddev);
4234 }
4235}
4236
Adrian Bunk75c96f82005-05-05 16:16:09 -07004237static int md_notify_reboot(struct notifier_block *this,
4238 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239{
4240 struct list_head *tmp;
4241 mddev_t *mddev;
4242
4243 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4244
4245 printk(KERN_INFO "md: stopping all md devices.\n");
4246
4247 ITERATE_MDDEV(mddev,tmp)
4248 if (mddev_trylock(mddev)==0)
4249 do_md_stop (mddev, 1);
4250 /*
4251 * certain more exotic SCSI devices are known to be
4252 * volatile wrt too early system reboots. While the
4253 * right place to handle this issue is the given
4254 * driver, we do want to have a safe RAID driver ...
4255 */
4256 mdelay(1000*1);
4257 }
4258 return NOTIFY_DONE;
4259}
4260
Adrian Bunk75c96f82005-05-05 16:16:09 -07004261static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 .notifier_call = md_notify_reboot,
4263 .next = NULL,
4264 .priority = INT_MAX, /* before any real devices */
4265};
4266
4267static void md_geninit(void)
4268{
4269 struct proc_dir_entry *p;
4270
4271 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4272
4273 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4274 if (p)
4275 p->proc_fops = &md_seq_fops;
4276}
4277
Adrian Bunk75c96f82005-05-05 16:16:09 -07004278static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
4280 int minor;
4281
4282 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4283 " MD_SB_DISKS=%d\n",
4284 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4285 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrown32a76272005-06-21 17:17:14 -07004286 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR,
4287 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
4289 if (register_blkdev(MAJOR_NR, "md"))
4290 return -1;
4291 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4292 unregister_blkdev(MAJOR_NR, "md");
4293 return -1;
4294 }
4295 devfs_mk_dir("md");
4296 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4297 md_probe, NULL, NULL);
4298 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4299 md_probe, NULL, NULL);
4300
4301 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4302 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4303 S_IFBLK|S_IRUSR|S_IWUSR,
4304 "md/%d", minor);
4305
4306 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4307 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4308 S_IFBLK|S_IRUSR|S_IWUSR,
4309 "md/mdp%d", minor);
4310
4311
4312 register_reboot_notifier(&md_notifier);
4313 raid_table_header = register_sysctl_table(raid_root_table, 1);
4314
4315 md_geninit();
4316 return (0);
4317}
4318
4319
4320#ifndef MODULE
4321
4322/*
4323 * Searches all registered partitions for autorun RAID arrays
4324 * at boot time.
4325 */
4326static dev_t detected_devices[128];
4327static int dev_cnt;
4328
4329void md_autodetect_dev(dev_t dev)
4330{
4331 if (dev_cnt >= 0 && dev_cnt < 127)
4332 detected_devices[dev_cnt++] = dev;
4333}
4334
4335
4336static void autostart_arrays(int part)
4337{
4338 mdk_rdev_t *rdev;
4339 int i;
4340
4341 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4342
4343 for (i = 0; i < dev_cnt; i++) {
4344 dev_t dev = detected_devices[i];
4345
4346 rdev = md_import_device(dev,0, 0);
4347 if (IS_ERR(rdev))
4348 continue;
4349
4350 if (rdev->faulty) {
4351 MD_BUG();
4352 continue;
4353 }
4354 list_add(&rdev->same_set, &pending_raid_disks);
4355 }
4356 dev_cnt = 0;
4357
4358 autorun_devices(part);
4359}
4360
4361#endif
4362
4363static __exit void md_exit(void)
4364{
4365 mddev_t *mddev;
4366 struct list_head *tmp;
4367 int i;
4368 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4369 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4370 for (i=0; i < MAX_MD_DEVS; i++)
4371 devfs_remove("md/%d", i);
4372 for (i=0; i < MAX_MD_DEVS; i++)
4373 devfs_remove("md/d%d", i);
4374
4375 devfs_remove("md");
4376
4377 unregister_blkdev(MAJOR_NR,"md");
4378 unregister_blkdev(mdp_major, "mdp");
4379 unregister_reboot_notifier(&md_notifier);
4380 unregister_sysctl_table(raid_table_header);
4381 remove_proc_entry("mdstat", NULL);
4382 ITERATE_MDDEV(mddev,tmp) {
4383 struct gendisk *disk = mddev->gendisk;
4384 if (!disk)
4385 continue;
4386 export_array(mddev);
4387 del_gendisk(disk);
4388 put_disk(disk);
4389 mddev->gendisk = NULL;
4390 mddev_put(mddev);
4391 }
4392}
4393
4394module_init(md_init)
4395module_exit(md_exit)
4396
4397EXPORT_SYMBOL(register_md_personality);
4398EXPORT_SYMBOL(unregister_md_personality);
4399EXPORT_SYMBOL(md_error);
4400EXPORT_SYMBOL(md_done_sync);
4401EXPORT_SYMBOL(md_write_start);
4402EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403EXPORT_SYMBOL(md_register_thread);
4404EXPORT_SYMBOL(md_unregister_thread);
4405EXPORT_SYMBOL(md_wakeup_thread);
4406EXPORT_SYMBOL(md_print_devices);
4407EXPORT_SYMBOL(md_check_recovery);
4408MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07004409MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07004410MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);