blob: e58d61d9f31bb4b1947e3611ec4347fc007b70d1 [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;
701
702 /* make rdev->sb match mddev data..
703 *
704 * 1/ zero out disks
705 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
706 * 3/ any empty disks < next_spare become removed
707 *
708 * disks[0] gets initialised to REMOVED because
709 * we cannot be sure from other fields if it has
710 * been initialised or not.
711 */
712 int i;
713 int active=0, working=0,failed=0,spare=0,nr_disks=0;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800714 unsigned int fixdesc=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
NeilBrown61181562005-09-09 16:24:02 -0700716 rdev->sb_size = MD_SB_BYTES;
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 sb = (mdp_super_t*)page_address(rdev->sb_page);
719
720 memset(sb, 0, sizeof(*sb));
721
722 sb->md_magic = MD_SB_MAGIC;
723 sb->major_version = mddev->major_version;
724 sb->minor_version = mddev->minor_version;
725 sb->patch_version = mddev->patch_version;
726 sb->gvalid_words = 0; /* ignored */
727 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
728 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
729 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
730 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
731
732 sb->ctime = mddev->ctime;
733 sb->level = mddev->level;
734 sb->size = mddev->size;
735 sb->raid_disks = mddev->raid_disks;
736 sb->md_minor = mddev->md_minor;
737 sb->not_persistent = !mddev->persistent;
738 sb->utime = mddev->utime;
739 sb->state = 0;
740 sb->events_hi = (mddev->events>>32);
741 sb->events_lo = (u32)mddev->events;
742
743 if (mddev->in_sync)
744 {
745 sb->recovery_cp = mddev->recovery_cp;
746 sb->cp_events_hi = (mddev->events>>32);
747 sb->cp_events_lo = (u32)mddev->events;
748 if (mddev->recovery_cp == MaxSector)
749 sb->state = (1<< MD_SB_CLEAN);
750 } else
751 sb->recovery_cp = 0;
752
753 sb->layout = mddev->layout;
754 sb->chunk_size = mddev->chunk_size;
755
NeilBrowna654b9d82005-06-21 17:17:27 -0700756 if (mddev->bitmap && mddev->bitmap_file == NULL)
757 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 sb->disks[0].state = (1<<MD_DISK_REMOVED);
760 ITERATE_RDEV(mddev,rdev2,tmp) {
761 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800762 int desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (rdev2->raid_disk >= 0 && rdev2->in_sync && !rdev2->faulty)
NeilBrown86e6ffd2005-11-08 21:39:24 -0800764 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800766 desc_nr = next_spare++;
767 if (desc_nr != rdev2->desc_nr) {
768 fixdesc |= (1 << desc_nr);
769 rdev2->desc_nr = desc_nr;
770 if (rdev2->raid_disk >= 0) {
771 char nm[20];
772 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);
808 kobject_add(&rdev2->kobj);
809 sysfs_create_link(&rdev2->kobj,
810 &rdev2->bdev->bd_disk->kobj,
811 "block");
812 if (rdev2->raid_disk >= 0) {
813 char nm[20];
814 sprintf(nm, "rd%d", rdev2->raid_disk);
815 sysfs_create_link(&mddev->kobj,
816 &rdev2->kobj, nm);
817 }
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* now set the "removed" and "faulty" bits on any missing devices */
820 for (i=0 ; i < mddev->raid_disks ; i++) {
821 mdp_disk_t *d = &sb->disks[i];
822 if (d->state == 0 && d->number == 0) {
823 d->number = i;
824 d->raid_disk = i;
825 d->state = (1<<MD_DISK_REMOVED);
826 d->state |= (1<<MD_DISK_FAULTY);
827 failed++;
828 }
829 }
830 sb->nr_disks = nr_disks;
831 sb->active_disks = active;
832 sb->working_disks = working;
833 sb->failed_disks = failed;
834 sb->spare_disks = spare;
835
836 sb->this_disk = sb->disks[rdev->desc_nr];
837 sb->sb_csum = calc_sb_csum(sb);
838}
839
840/*
841 * version 1 superblock
842 */
843
844static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
845{
846 unsigned int disk_csum, csum;
847 unsigned long long newcsum;
848 int size = 256 + le32_to_cpu(sb->max_dev)*2;
849 unsigned int *isuper = (unsigned int*)sb;
850 int i;
851
852 disk_csum = sb->sb_csum;
853 sb->sb_csum = 0;
854 newcsum = 0;
855 for (i=0; size>=4; size -= 4 )
856 newcsum += le32_to_cpu(*isuper++);
857
858 if (size == 2)
859 newcsum += le16_to_cpu(*(unsigned short*) isuper);
860
861 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
862 sb->sb_csum = disk_csum;
863 return cpu_to_le32(csum);
864}
865
866static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
867{
868 struct mdp_superblock_1 *sb;
869 int ret;
870 sector_t sb_offset;
871 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -0700872 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /*
875 * Calculate the position of the superblock.
876 * It is always aligned to a 4K boundary and
877 * depeding on minor_version, it can be:
878 * 0: At least 8K, but less than 12K, from end of device
879 * 1: At start of device
880 * 2: 4K from start of device.
881 */
882 switch(minor_version) {
883 case 0:
884 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
885 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -0700886 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* convert from sectors to K */
888 sb_offset /= 2;
889 break;
890 case 1:
891 sb_offset = 0;
892 break;
893 case 2:
894 sb_offset = 4;
895 break;
896 default:
897 return -EINVAL;
898 }
899 rdev->sb_offset = sb_offset;
900
NeilBrown0002b272005-09-09 16:23:53 -0700901 /* superblock is rarely larger than 1K, but it can be larger,
902 * and it is safe to read 4k, so we do that
903 */
904 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (ret) return ret;
906
907
908 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
909
910 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
911 sb->major_version != cpu_to_le32(1) ||
912 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
913 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -0700914 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return -EINVAL;
916
917 if (calc_sb_1_csum(sb) != sb->sb_csum) {
918 printk("md: invalid superblock checksum on %s\n",
919 bdevname(rdev->bdev,b));
920 return -EINVAL;
921 }
922 if (le64_to_cpu(sb->data_size) < 10) {
923 printk("md: data_size too small on %s\n",
924 bdevname(rdev->bdev,b));
925 return -EINVAL;
926 }
927 rdev->preferred_minor = 0xffff;
928 rdev->data_offset = le64_to_cpu(sb->data_offset);
929
NeilBrown0002b272005-09-09 16:23:53 -0700930 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -0700931 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -0700932 if (rdev->sb_size & bmask)
933 rdev-> sb_size = (rdev->sb_size | bmask)+1;
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (refdev == 0)
936 return 1;
937 else {
938 __u64 ev1, ev2;
939 struct mdp_superblock_1 *refsb =
940 (struct mdp_superblock_1*)page_address(refdev->sb_page);
941
942 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
943 sb->level != refsb->level ||
944 sb->layout != refsb->layout ||
945 sb->chunksize != refsb->chunksize) {
946 printk(KERN_WARNING "md: %s has strangely different"
947 " superblock to %s\n",
948 bdevname(rdev->bdev,b),
949 bdevname(refdev->bdev,b2));
950 return -EINVAL;
951 }
952 ev1 = le64_to_cpu(sb->events);
953 ev2 = le64_to_cpu(refsb->events);
954
955 if (ev1 > ev2)
956 return 1;
957 }
958 if (minor_version)
959 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
960 else
961 rdev->size = rdev->sb_offset;
962 if (rdev->size < le64_to_cpu(sb->data_size)/2)
963 return -EINVAL;
964 rdev->size = le64_to_cpu(sb->data_size)/2;
965 if (le32_to_cpu(sb->chunksize))
966 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
967 return 0;
968}
969
970static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
971{
972 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
973
NeilBrown41158c72005-06-21 17:17:25 -0700974 rdev->raid_disk = -1;
975 rdev->in_sync = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (mddev->raid_disks == 0) {
977 mddev->major_version = 1;
978 mddev->patch_version = 0;
979 mddev->persistent = 1;
980 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
981 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
982 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
983 mddev->level = le32_to_cpu(sb->level);
984 mddev->layout = le32_to_cpu(sb->layout);
985 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
986 mddev->size = le64_to_cpu(sb->size)/2;
987 mddev->events = le64_to_cpu(sb->events);
NeilBrown92232142005-08-18 11:24:16 -0700988 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700989 mddev->default_bitmap_offset = 0;
NeilBrown53e87fb2005-09-09 16:23:58 -0700990 mddev->default_bitmap_offset = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
993 memcpy(mddev->uuid, sb->set_uuid, 16);
994
995 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -0700996
NeilBrown71c08052005-09-09 16:23:51 -0700997 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -0700998 mddev->bitmap_file == NULL ) {
999 if (mddev->level != 1) {
1000 printk(KERN_WARNING "md: bitmaps only supported for raid1\n");
1001 return -EINVAL;
1002 }
1003 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1004 }
NeilBrown41158c72005-06-21 17:17:25 -07001005 } else if (mddev->pers == NULL) {
1006 /* Insist of good event counter while assembling */
1007 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 ++ev1;
1009 if (ev1 < mddev->events)
1010 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001011 } else if (mddev->bitmap) {
1012 /* If adding to array with a bitmap, then we can accept an
1013 * older device, but not too old.
1014 */
1015 __u64 ev1 = le64_to_cpu(sb->events);
1016 if (ev1 < mddev->bitmap->events_cleared)
1017 return 0;
1018 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (mddev->level != LEVEL_MULTIPATH) {
1022 int role;
1023 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1024 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1025 switch(role) {
1026 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 rdev->faulty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 break;
1029 case 0xfffe: /* faulty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 rdev->faulty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 break;
1032 default:
1033 rdev->in_sync = 1;
1034 rdev->faulty = 0;
1035 rdev->raid_disk = role;
1036 break;
1037 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001038 rdev->flags = 0;
1039 if (sb->devflags & WriteMostly1)
1040 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001041 } else /* MULTIPATH are always insync */
1042 rdev->in_sync = 1;
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return 0;
1045}
1046
1047static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1048{
1049 struct mdp_superblock_1 *sb;
1050 struct list_head *tmp;
1051 mdk_rdev_t *rdev2;
1052 int max_dev, i;
1053 /* make rdev->sb match mddev and rdev data. */
1054
1055 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1056
1057 sb->feature_map = 0;
1058 sb->pad0 = 0;
1059 memset(sb->pad1, 0, sizeof(sb->pad1));
1060 memset(sb->pad2, 0, sizeof(sb->pad2));
1061 memset(sb->pad3, 0, sizeof(sb->pad3));
1062
1063 sb->utime = cpu_to_le64((__u64)mddev->utime);
1064 sb->events = cpu_to_le64(mddev->events);
1065 if (mddev->in_sync)
1066 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1067 else
1068 sb->resync_offset = cpu_to_le64(0);
1069
NeilBrowna654b9d82005-06-21 17:17:27 -07001070 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1071 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001072 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001073 }
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 max_dev = 0;
1076 ITERATE_RDEV(mddev,rdev2,tmp)
1077 if (rdev2->desc_nr+1 > max_dev)
1078 max_dev = rdev2->desc_nr+1;
1079
1080 sb->max_dev = cpu_to_le32(max_dev);
1081 for (i=0; i<max_dev;i++)
1082 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1083
1084 ITERATE_RDEV(mddev,rdev2,tmp) {
1085 i = rdev2->desc_nr;
1086 if (rdev2->faulty)
1087 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1088 else if (rdev2->in_sync)
1089 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1090 else
1091 sb->dev_roles[i] = cpu_to_le16(0xffff);
1092 }
1093
1094 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1095 sb->sb_csum = calc_sb_1_csum(sb);
1096}
1097
1098
Adrian Bunk75c96f82005-05-05 16:16:09 -07001099static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 [0] = {
1101 .name = "0.90.0",
1102 .owner = THIS_MODULE,
1103 .load_super = super_90_load,
1104 .validate_super = super_90_validate,
1105 .sync_super = super_90_sync,
1106 },
1107 [1] = {
1108 .name = "md-1",
1109 .owner = THIS_MODULE,
1110 .load_super = super_1_load,
1111 .validate_super = super_1_validate,
1112 .sync_super = super_1_sync,
1113 },
1114};
1115
1116static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1117{
1118 struct list_head *tmp;
1119 mdk_rdev_t *rdev;
1120
1121 ITERATE_RDEV(mddev,rdev,tmp)
1122 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1123 return rdev;
1124
1125 return NULL;
1126}
1127
1128static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1129{
1130 struct list_head *tmp;
1131 mdk_rdev_t *rdev;
1132
1133 ITERATE_RDEV(mddev1,rdev,tmp)
1134 if (match_dev_unit(mddev2, rdev))
1135 return 1;
1136
1137 return 0;
1138}
1139
1140static LIST_HEAD(pending_raid_disks);
1141
1142static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1143{
1144 mdk_rdev_t *same_pdev;
1145 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1146
1147 if (rdev->mddev) {
1148 MD_BUG();
1149 return -EINVAL;
1150 }
1151 same_pdev = match_dev_unit(mddev, rdev);
1152 if (same_pdev)
1153 printk(KERN_WARNING
1154 "%s: WARNING: %s appears to be on the same physical"
1155 " disk as %s. True\n protection against single-disk"
1156 " failure might be compromised.\n",
1157 mdname(mddev), bdevname(rdev->bdev,b),
1158 bdevname(same_pdev->bdev,b2));
1159
1160 /* Verify rdev->desc_nr is unique.
1161 * If it is -1, assign a free number, else
1162 * check number is not in use
1163 */
1164 if (rdev->desc_nr < 0) {
1165 int choice = 0;
1166 if (mddev->pers) choice = mddev->raid_disks;
1167 while (find_rdev_nr(mddev, choice))
1168 choice++;
1169 rdev->desc_nr = choice;
1170 } else {
1171 if (find_rdev_nr(mddev, rdev->desc_nr))
1172 return -EBUSY;
1173 }
1174
1175 list_add(&rdev->same_set, &mddev->disks);
1176 rdev->mddev = mddev;
1177 printk(KERN_INFO "md: bind<%s>\n", bdevname(rdev->bdev,b));
NeilBrown86e6ffd2005-11-08 21:39:24 -08001178
1179 rdev->kobj.k_name = NULL;
1180 snprintf(rdev->kobj.name, KOBJ_NAME_LEN, "dev%d", rdev->desc_nr);
1181 rdev->kobj.parent = kobject_get(&mddev->kobj);
1182 kobject_add(&rdev->kobj);
1183
1184 sysfs_create_link(&rdev->kobj, &rdev->bdev->bd_disk->kobj, "block");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return 0;
1186}
1187
1188static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1189{
1190 char b[BDEVNAME_SIZE];
1191 if (!rdev->mddev) {
1192 MD_BUG();
1193 return;
1194 }
1195 list_del_init(&rdev->same_set);
1196 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1197 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001198 sysfs_remove_link(&rdev->kobj, "block");
1199 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202/*
1203 * prevent the device from being mounted, repartitioned or
1204 * otherwise reused by a RAID array (or any other kernel
1205 * subsystem), by bd_claiming the device.
1206 */
1207static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1208{
1209 int err = 0;
1210 struct block_device *bdev;
1211 char b[BDEVNAME_SIZE];
1212
1213 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1214 if (IS_ERR(bdev)) {
1215 printk(KERN_ERR "md: could not open %s.\n",
1216 __bdevname(dev, b));
1217 return PTR_ERR(bdev);
1218 }
1219 err = bd_claim(bdev, rdev);
1220 if (err) {
1221 printk(KERN_ERR "md: could not bd_claim %s.\n",
1222 bdevname(bdev, b));
1223 blkdev_put(bdev);
1224 return err;
1225 }
1226 rdev->bdev = bdev;
1227 return err;
1228}
1229
1230static void unlock_rdev(mdk_rdev_t *rdev)
1231{
1232 struct block_device *bdev = rdev->bdev;
1233 rdev->bdev = NULL;
1234 if (!bdev)
1235 MD_BUG();
1236 bd_release(bdev);
1237 blkdev_put(bdev);
1238}
1239
1240void md_autodetect_dev(dev_t dev);
1241
1242static void export_rdev(mdk_rdev_t * rdev)
1243{
1244 char b[BDEVNAME_SIZE];
1245 printk(KERN_INFO "md: export_rdev(%s)\n",
1246 bdevname(rdev->bdev,b));
1247 if (rdev->mddev)
1248 MD_BUG();
1249 free_disk_sb(rdev);
1250 list_del_init(&rdev->same_set);
1251#ifndef MODULE
1252 md_autodetect_dev(rdev->bdev->bd_dev);
1253#endif
1254 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001255 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
1258static void kick_rdev_from_array(mdk_rdev_t * rdev)
1259{
1260 unbind_rdev_from_array(rdev);
1261 export_rdev(rdev);
1262}
1263
1264static void export_array(mddev_t *mddev)
1265{
1266 struct list_head *tmp;
1267 mdk_rdev_t *rdev;
1268
1269 ITERATE_RDEV(mddev,rdev,tmp) {
1270 if (!rdev->mddev) {
1271 MD_BUG();
1272 continue;
1273 }
1274 kick_rdev_from_array(rdev);
1275 }
1276 if (!list_empty(&mddev->disks))
1277 MD_BUG();
1278 mddev->raid_disks = 0;
1279 mddev->major_version = 0;
1280}
1281
1282static void print_desc(mdp_disk_t *desc)
1283{
1284 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1285 desc->major,desc->minor,desc->raid_disk,desc->state);
1286}
1287
1288static void print_sb(mdp_super_t *sb)
1289{
1290 int i;
1291
1292 printk(KERN_INFO
1293 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1294 sb->major_version, sb->minor_version, sb->patch_version,
1295 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1296 sb->ctime);
1297 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1298 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1299 sb->md_minor, sb->layout, sb->chunk_size);
1300 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1301 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1302 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1303 sb->failed_disks, sb->spare_disks,
1304 sb->sb_csum, (unsigned long)sb->events_lo);
1305
1306 printk(KERN_INFO);
1307 for (i = 0; i < MD_SB_DISKS; i++) {
1308 mdp_disk_t *desc;
1309
1310 desc = sb->disks + i;
1311 if (desc->number || desc->major || desc->minor ||
1312 desc->raid_disk || (desc->state && (desc->state != 4))) {
1313 printk(" D %2d: ", i);
1314 print_desc(desc);
1315 }
1316 }
1317 printk(KERN_INFO "md: THIS: ");
1318 print_desc(&sb->this_disk);
1319
1320}
1321
1322static void print_rdev(mdk_rdev_t *rdev)
1323{
1324 char b[BDEVNAME_SIZE];
1325 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1326 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1327 rdev->faulty, rdev->in_sync, rdev->desc_nr);
1328 if (rdev->sb_loaded) {
1329 printk(KERN_INFO "md: rdev superblock:\n");
1330 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1331 } else
1332 printk(KERN_INFO "md: no rdev superblock!\n");
1333}
1334
1335void md_print_devices(void)
1336{
1337 struct list_head *tmp, *tmp2;
1338 mdk_rdev_t *rdev;
1339 mddev_t *mddev;
1340 char b[BDEVNAME_SIZE];
1341
1342 printk("\n");
1343 printk("md: **********************************\n");
1344 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1345 printk("md: **********************************\n");
1346 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
NeilBrown32a76272005-06-21 17:17:14 -07001348 if (mddev->bitmap)
1349 bitmap_print_sb(mddev->bitmap);
1350 else
1351 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 ITERATE_RDEV(mddev,rdev,tmp2)
1353 printk("<%s>", bdevname(rdev->bdev,b));
1354 printk("\n");
1355
1356 ITERATE_RDEV(mddev,rdev,tmp2)
1357 print_rdev(rdev);
1358 }
1359 printk("md: **********************************\n");
1360 printk("\n");
1361}
1362
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364static void sync_sbs(mddev_t * mddev)
1365{
1366 mdk_rdev_t *rdev;
1367 struct list_head *tmp;
1368
1369 ITERATE_RDEV(mddev,rdev,tmp) {
1370 super_types[mddev->major_version].
1371 sync_super(mddev, rdev);
1372 rdev->sb_loaded = 1;
1373 }
1374}
1375
1376static void md_update_sb(mddev_t * mddev)
1377{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001378 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 struct list_head *tmp;
1380 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001381 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383repeat:
NeilBrown06d91a52005-06-21 17:17:12 -07001384 spin_lock(&mddev->write_lock);
1385 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 mddev->utime = get_seconds();
1387 mddev->events ++;
1388
1389 if (!mddev->events) {
1390 /*
1391 * oops, this 64-bit counter should never wrap.
1392 * Either we are in around ~1 trillion A.C., assuming
1393 * 1 reboot per second, or we have a bug:
1394 */
1395 MD_BUG();
1396 mddev->events --;
1397 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001398 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 sync_sbs(mddev);
1400
1401 /*
1402 * do not write anything to disk if using
1403 * nonpersistent superblocks
1404 */
NeilBrown06d91a52005-06-21 17:17:12 -07001405 if (!mddev->persistent) {
1406 mddev->sb_dirty = 0;
1407 spin_unlock(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001408 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001410 }
1411 spin_unlock(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 dprintk(KERN_INFO
1414 "md: updating %s RAID superblock on device (in sync %d)\n",
1415 mdname(mddev),mddev->in_sync);
1416
NeilBrown32a76272005-06-21 17:17:14 -07001417 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 ITERATE_RDEV(mddev,rdev,tmp) {
1419 char b[BDEVNAME_SIZE];
1420 dprintk(KERN_INFO "md: ");
1421 if (rdev->faulty)
1422 dprintk("(skipping faulty ");
1423
1424 dprintk("%s ", bdevname(rdev->bdev,b));
1425 if (!rdev->faulty) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001426 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001427 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001428 rdev->sb_page);
1429 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1430 bdevname(rdev->bdev,b),
1431 (unsigned long long)rdev->sb_offset);
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 } else
1434 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001435 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /* only need to write one superblock... */
1437 break;
1438 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001439 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
1440 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1441
NeilBrown06d91a52005-06-21 17:17:12 -07001442 spin_lock(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001443 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001444 /* have to write it out again */
1445 spin_unlock(&mddev->write_lock);
1446 goto repeat;
1447 }
1448 mddev->sb_dirty = 0;
1449 spin_unlock(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001450 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
NeilBrown86e6ffd2005-11-08 21:39:24 -08001454struct rdev_sysfs_entry {
1455 struct attribute attr;
1456 ssize_t (*show)(mdk_rdev_t *, char *);
1457 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1458};
1459
1460static ssize_t
1461rdev_show_state(mdk_rdev_t *rdev, char *page)
1462{
1463 char *sep = "";
1464 int len=0;
1465
1466 if (rdev->faulty) {
1467 len+= sprintf(page+len, "%sfaulty",sep);
1468 sep = ",";
1469 }
1470 if (rdev->in_sync) {
1471 len += sprintf(page+len, "%sin_sync",sep);
1472 sep = ",";
1473 }
1474 if (!rdev->faulty && !rdev->in_sync) {
1475 len += sprintf(page+len, "%sspare", sep);
1476 sep = ",";
1477 }
1478 return len+sprintf(page+len, "\n");
1479}
1480
1481static struct rdev_sysfs_entry rdev_state = {
1482 .attr = {.name = "state", .mode = S_IRUGO },
1483 .show = rdev_show_state,
1484};
1485
1486static ssize_t
1487rdev_show_super(mdk_rdev_t *rdev, char *page)
1488{
1489 if (rdev->sb_loaded && rdev->sb_size) {
1490 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1491 return rdev->sb_size;
1492 } else
1493 return 0;
1494}
1495static struct rdev_sysfs_entry rdev_super = {
1496 .attr = {.name = "super", .mode = S_IRUGO },
1497 .show = rdev_show_super,
1498};
1499static struct attribute *rdev_default_attrs[] = {
1500 &rdev_state.attr,
1501 &rdev_super.attr,
1502 NULL,
1503};
1504static ssize_t
1505rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1506{
1507 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1508 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1509
1510 if (!entry->show)
1511 return -EIO;
1512 return entry->show(rdev, page);
1513}
1514
1515static ssize_t
1516rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1517 const char *page, size_t length)
1518{
1519 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1520 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1521
1522 if (!entry->store)
1523 return -EIO;
1524 return entry->store(rdev, page, length);
1525}
1526
1527static void rdev_free(struct kobject *ko)
1528{
1529 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1530 kfree(rdev);
1531}
1532static struct sysfs_ops rdev_sysfs_ops = {
1533 .show = rdev_attr_show,
1534 .store = rdev_attr_store,
1535};
1536static struct kobj_type rdev_ktype = {
1537 .release = rdev_free,
1538 .sysfs_ops = &rdev_sysfs_ops,
1539 .default_attrs = rdev_default_attrs,
1540};
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542/*
1543 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1544 *
1545 * mark the device faulty if:
1546 *
1547 * - the device is nonexistent (zero size)
1548 * - the device has no valid superblock
1549 *
1550 * a faulty rdev _never_ has rdev->sb set.
1551 */
1552static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1553{
1554 char b[BDEVNAME_SIZE];
1555 int err;
1556 mdk_rdev_t *rdev;
1557 sector_t size;
1558
1559 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1560 if (!rdev) {
1561 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1562 return ERR_PTR(-ENOMEM);
1563 }
1564 memset(rdev, 0, sizeof(*rdev));
1565
1566 if ((err = alloc_disk_sb(rdev)))
1567 goto abort_free;
1568
1569 err = lock_rdev(rdev, newdev);
1570 if (err)
1571 goto abort_free;
1572
NeilBrown86e6ffd2005-11-08 21:39:24 -08001573 rdev->kobj.parent = NULL;
1574 rdev->kobj.ktype = &rdev_ktype;
1575 kobject_init(&rdev->kobj);
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 rdev->desc_nr = -1;
1578 rdev->faulty = 0;
1579 rdev->in_sync = 0;
1580 rdev->data_offset = 0;
1581 atomic_set(&rdev->nr_pending, 0);
1582
1583 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1584 if (!size) {
1585 printk(KERN_WARNING
1586 "md: %s has zero or unknown size, marking faulty!\n",
1587 bdevname(rdev->bdev,b));
1588 err = -EINVAL;
1589 goto abort_free;
1590 }
1591
1592 if (super_format >= 0) {
1593 err = super_types[super_format].
1594 load_super(rdev, NULL, super_minor);
1595 if (err == -EINVAL) {
1596 printk(KERN_WARNING
1597 "md: %s has invalid sb, not importing!\n",
1598 bdevname(rdev->bdev,b));
1599 goto abort_free;
1600 }
1601 if (err < 0) {
1602 printk(KERN_WARNING
1603 "md: could not read %s's sb, not importing!\n",
1604 bdevname(rdev->bdev,b));
1605 goto abort_free;
1606 }
1607 }
1608 INIT_LIST_HEAD(&rdev->same_set);
1609
1610 return rdev;
1611
1612abort_free:
1613 if (rdev->sb_page) {
1614 if (rdev->bdev)
1615 unlock_rdev(rdev);
1616 free_disk_sb(rdev);
1617 }
1618 kfree(rdev);
1619 return ERR_PTR(err);
1620}
1621
1622/*
1623 * Check a full RAID array for plausibility
1624 */
1625
1626
NeilBrowna757e642005-04-16 15:26:42 -07001627static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 int i;
1630 struct list_head *tmp;
1631 mdk_rdev_t *rdev, *freshest;
1632 char b[BDEVNAME_SIZE];
1633
1634 freshest = NULL;
1635 ITERATE_RDEV(mddev,rdev,tmp)
1636 switch (super_types[mddev->major_version].
1637 load_super(rdev, freshest, mddev->minor_version)) {
1638 case 1:
1639 freshest = rdev;
1640 break;
1641 case 0:
1642 break;
1643 default:
1644 printk( KERN_ERR \
1645 "md: fatal superblock inconsistency in %s"
1646 " -- removing from array\n",
1647 bdevname(rdev->bdev,b));
1648 kick_rdev_from_array(rdev);
1649 }
1650
1651
1652 super_types[mddev->major_version].
1653 validate_super(mddev, freshest);
1654
1655 i = 0;
1656 ITERATE_RDEV(mddev,rdev,tmp) {
1657 if (rdev != freshest)
1658 if (super_types[mddev->major_version].
1659 validate_super(mddev, rdev)) {
1660 printk(KERN_WARNING "md: kicking non-fresh %s"
1661 " from array!\n",
1662 bdevname(rdev->bdev,b));
1663 kick_rdev_from_array(rdev);
1664 continue;
1665 }
1666 if (mddev->level == LEVEL_MULTIPATH) {
1667 rdev->desc_nr = i++;
1668 rdev->raid_disk = rdev->desc_nr;
1669 rdev->in_sync = 1;
1670 }
1671 }
1672
1673
1674
1675 if (mddev->recovery_cp != MaxSector &&
1676 mddev->level >= 1)
1677 printk(KERN_ERR "md: %s: raid array is not clean"
1678 " -- starting background reconstruction\n",
1679 mdname(mddev));
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
NeilBrowneae17012005-11-08 21:39:23 -08001683struct md_sysfs_entry {
1684 struct attribute attr;
1685 ssize_t (*show)(mddev_t *, char *);
1686 ssize_t (*store)(mddev_t *, const char *, size_t);
1687};
1688
1689static ssize_t
1690md_show_level(mddev_t *mddev, char *page)
1691{
1692 mdk_personality_t *p = mddev->pers;
1693 if (p == NULL)
1694 return 0;
1695 if (mddev->level >= 0)
1696 return sprintf(page, "RAID-%d\n", mddev->level);
1697 else
1698 return sprintf(page, "%s\n", p->name);
1699}
1700
1701static struct md_sysfs_entry md_level = {
1702 .attr = {.name = "level", .mode = S_IRUGO },
1703 .show = md_show_level,
1704};
1705
1706static ssize_t
1707md_show_rdisks(mddev_t *mddev, char *page)
1708{
1709 return sprintf(page, "%d\n", mddev->raid_disks);
1710}
1711
1712static struct md_sysfs_entry md_raid_disks = {
1713 .attr = {.name = "raid_disks", .mode = S_IRUGO },
1714 .show = md_show_rdisks,
1715};
1716
NeilBrown24dd4692005-11-08 21:39:26 -08001717static ssize_t
1718md_show_scan(mddev_t *mddev, char *page)
1719{
1720 char *type = "none";
1721 if (mddev->recovery &
1722 ((1<<MD_RECOVERY_RUNNING) || (1<<MD_RECOVERY_NEEDED))) {
1723 if (mddev->recovery & (1<<MD_RECOVERY_SYNC)) {
1724 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1725 type = "resync";
1726 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1727 type = "check";
1728 else
1729 type = "repair";
1730 } else
1731 type = "recover";
1732 }
1733 return sprintf(page, "%s\n", type);
1734}
1735
1736static ssize_t
1737md_store_scan(mddev_t *mddev, const char *page, size_t len)
1738{
1739 int canscan=0;
1740 if (mddev->recovery &
1741 ((1<<MD_RECOVERY_RUNNING) || (1<<MD_RECOVERY_NEEDED)))
1742 return -EBUSY;
1743 down(&mddev->reconfig_sem);
1744 if (mddev->pers && mddev->pers->sync_request)
1745 canscan=1;
1746 up(&mddev->reconfig_sem);
1747 if (!canscan)
1748 return -EINVAL;
1749
1750 if (strcmp(page, "check")==0 || strcmp(page, "check\n")==0)
1751 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1752 else if (strcmp(page, "repair")!=0 && strcmp(page, "repair\n")!=0)
1753 return -EINVAL;
1754 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1755 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1756 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1757 md_wakeup_thread(mddev->thread);
1758 return len;
1759}
1760
NeilBrown9d888832005-11-08 21:39:26 -08001761static ssize_t
1762md_show_mismatch(mddev_t *mddev, char *page)
1763{
1764 return sprintf(page, "%llu\n",
1765 (unsigned long long) mddev->resync_mismatches);
1766}
1767
NeilBrown24dd4692005-11-08 21:39:26 -08001768static struct md_sysfs_entry md_scan_mode = {
1769 .attr = {.name = "scan_mode", .mode = S_IRUGO|S_IWUSR },
1770 .show = md_show_scan,
1771 .store = md_store_scan,
1772};
1773
NeilBrown9d888832005-11-08 21:39:26 -08001774static struct md_sysfs_entry md_mismatches = {
1775 .attr = {.name = "mismatch_cnt", .mode = S_IRUGO },
1776 .show = md_show_mismatch,
1777};
1778
NeilBrowneae17012005-11-08 21:39:23 -08001779static struct attribute *md_default_attrs[] = {
1780 &md_level.attr,
1781 &md_raid_disks.attr,
NeilBrown24dd4692005-11-08 21:39:26 -08001782 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08001783 &md_mismatches.attr,
NeilBrowneae17012005-11-08 21:39:23 -08001784 NULL,
1785};
1786
1787static ssize_t
1788md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1789{
1790 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1791 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1792
1793 if (!entry->show)
1794 return -EIO;
1795 return entry->show(mddev, page);
1796}
1797
1798static ssize_t
1799md_attr_store(struct kobject *kobj, struct attribute *attr,
1800 const char *page, size_t length)
1801{
1802 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1803 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1804
1805 if (!entry->store)
1806 return -EIO;
1807 return entry->store(mddev, page, length);
1808}
1809
1810static void md_free(struct kobject *ko)
1811{
1812 mddev_t *mddev = container_of(ko, mddev_t, kobj);
1813 kfree(mddev);
1814}
1815
1816static struct sysfs_ops md_sysfs_ops = {
1817 .show = md_attr_show,
1818 .store = md_attr_store,
1819};
1820static struct kobj_type md_ktype = {
1821 .release = md_free,
1822 .sysfs_ops = &md_sysfs_ops,
1823 .default_attrs = md_default_attrs,
1824};
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826int mdp_major = 0;
1827
1828static struct kobject *md_probe(dev_t dev, int *part, void *data)
1829{
1830 static DECLARE_MUTEX(disks_sem);
1831 mddev_t *mddev = mddev_find(dev);
1832 struct gendisk *disk;
1833 int partitioned = (MAJOR(dev) != MD_MAJOR);
1834 int shift = partitioned ? MdpMinorShift : 0;
1835 int unit = MINOR(dev) >> shift;
1836
1837 if (!mddev)
1838 return NULL;
1839
1840 down(&disks_sem);
1841 if (mddev->gendisk) {
1842 up(&disks_sem);
1843 mddev_put(mddev);
1844 return NULL;
1845 }
1846 disk = alloc_disk(1 << shift);
1847 if (!disk) {
1848 up(&disks_sem);
1849 mddev_put(mddev);
1850 return NULL;
1851 }
1852 disk->major = MAJOR(dev);
1853 disk->first_minor = unit << shift;
1854 if (partitioned) {
1855 sprintf(disk->disk_name, "md_d%d", unit);
1856 sprintf(disk->devfs_name, "md/d%d", unit);
1857 } else {
1858 sprintf(disk->disk_name, "md%d", unit);
1859 sprintf(disk->devfs_name, "md/%d", unit);
1860 }
1861 disk->fops = &md_fops;
1862 disk->private_data = mddev;
1863 disk->queue = mddev->queue;
1864 add_disk(disk);
1865 mddev->gendisk = disk;
1866 up(&disks_sem);
NeilBrowneae17012005-11-08 21:39:23 -08001867 mddev->kobj.parent = kobject_get(&disk->kobj);
1868 mddev->kobj.k_name = NULL;
1869 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
1870 mddev->kobj.ktype = &md_ktype;
1871 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return NULL;
1873}
1874
1875void md_wakeup_thread(mdk_thread_t *thread);
1876
1877static void md_safemode_timeout(unsigned long data)
1878{
1879 mddev_t *mddev = (mddev_t *) data;
1880
1881 mddev->safemode = 1;
1882 md_wakeup_thread(mddev->thread);
1883}
1884
1885
1886static int do_md_run(mddev_t * mddev)
1887{
1888 int pnum, err;
1889 int chunk_size;
1890 struct list_head *tmp;
1891 mdk_rdev_t *rdev;
1892 struct gendisk *disk;
1893 char b[BDEVNAME_SIZE];
1894
NeilBrowna757e642005-04-16 15:26:42 -07001895 if (list_empty(&mddev->disks))
1896 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 if (mddev->pers)
1900 return -EBUSY;
1901
1902 /*
1903 * Analyze all RAID superblock(s)
1904 */
NeilBrowna757e642005-04-16 15:26:42 -07001905 if (!mddev->raid_disks)
1906 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 chunk_size = mddev->chunk_size;
1909 pnum = level_to_pers(mddev->level);
1910
1911 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1912 if (!chunk_size) {
1913 /*
1914 * 'default chunksize' in the old md code used to
1915 * be PAGE_SIZE, baaad.
1916 * we abort here to be on the safe side. We don't
1917 * want to continue the bad practice.
1918 */
1919 printk(KERN_ERR
1920 "no chunksize specified, see 'man raidtab'\n");
1921 return -EINVAL;
1922 }
1923 if (chunk_size > MAX_CHUNK_SIZE) {
1924 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1925 chunk_size, MAX_CHUNK_SIZE);
1926 return -EINVAL;
1927 }
1928 /*
1929 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1930 */
1931 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07001932 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return -EINVAL;
1934 }
1935 if (chunk_size < PAGE_SIZE) {
1936 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1937 chunk_size, PAGE_SIZE);
1938 return -EINVAL;
1939 }
1940
1941 /* devices must have minimum size of one chunk */
1942 ITERATE_RDEV(mddev,rdev,tmp) {
1943 if (rdev->faulty)
1944 continue;
1945 if (rdev->size < chunk_size / 1024) {
1946 printk(KERN_WARNING
1947 "md: Dev %s smaller than chunk_size:"
1948 " %lluk < %dk\n",
1949 bdevname(rdev->bdev,b),
1950 (unsigned long long)rdev->size,
1951 chunk_size / 1024);
1952 return -EINVAL;
1953 }
1954 }
1955 }
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957#ifdef CONFIG_KMOD
1958 if (!pers[pnum])
1959 {
1960 request_module("md-personality-%d", pnum);
1961 }
1962#endif
1963
1964 /*
1965 * Drop all container device buffers, from now on
1966 * the only valid external interface is through the md
1967 * device.
1968 * Also find largest hardsector size
1969 */
1970 ITERATE_RDEV(mddev,rdev,tmp) {
1971 if (rdev->faulty)
1972 continue;
1973 sync_blockdev(rdev->bdev);
1974 invalidate_bdev(rdev->bdev, 0);
1975 }
1976
1977 md_probe(mddev->unit, NULL, NULL);
1978 disk = mddev->gendisk;
1979 if (!disk)
1980 return -ENOMEM;
1981
1982 spin_lock(&pers_lock);
1983 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
1984 spin_unlock(&pers_lock);
1985 printk(KERN_WARNING "md: personality %d is not loaded!\n",
1986 pnum);
1987 return -EINVAL;
1988 }
1989
1990 mddev->pers = pers[pnum];
1991 spin_unlock(&pers_lock);
1992
NeilBrown657390d2005-08-26 18:34:16 -07001993 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
1995
NeilBrown32a76272005-06-21 17:17:14 -07001996 /* before we start the array running, initialise the bitmap */
1997 err = bitmap_create(mddev);
1998 if (err)
1999 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2000 mdname(mddev), err);
2001 else
2002 err = mddev->pers->run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (err) {
2004 printk(KERN_ERR "md: pers->run() failed ...\n");
2005 module_put(mddev->pers->owner);
2006 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002007 bitmap_destroy(mddev);
2008 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010 atomic_set(&mddev->writes_pending,0);
2011 mddev->safemode = 0;
2012 mddev->safemode_timer.function = md_safemode_timeout;
2013 mddev->safemode_timer.data = (unsigned long) mddev;
2014 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2015 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002016
2017 ITERATE_RDEV(mddev,rdev,tmp)
2018 if (rdev->raid_disk >= 0) {
2019 char nm[20];
2020 sprintf(nm, "rd%d", rdev->raid_disk);
2021 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002025 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 if (mddev->sb_dirty)
2028 md_update_sb(mddev);
2029
2030 set_capacity(disk, mddev->array_size<<1);
2031
2032 /* If we call blk_queue_make_request here, it will
2033 * re-initialise max_sectors etc which may have been
2034 * refined inside -> run. So just set the bits we need to set.
2035 * Most initialisation happended when we called
2036 * blk_queue_make_request(..., md_fail_request)
2037 * earlier.
2038 */
2039 mddev->queue->queuedata = mddev;
2040 mddev->queue->make_request_fn = mddev->pers->make_request;
2041
2042 mddev->changed = 1;
2043 return 0;
2044}
2045
2046static int restart_array(mddev_t *mddev)
2047{
2048 struct gendisk *disk = mddev->gendisk;
2049 int err;
2050
2051 /*
2052 * Complain if it has no devices
2053 */
2054 err = -ENXIO;
2055 if (list_empty(&mddev->disks))
2056 goto out;
2057
2058 if (mddev->pers) {
2059 err = -EBUSY;
2060 if (!mddev->ro)
2061 goto out;
2062
2063 mddev->safemode = 0;
2064 mddev->ro = 0;
2065 set_disk_ro(disk, 0);
2066
2067 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2068 mdname(mddev));
2069 /*
2070 * Kick recovery or resync if necessary
2071 */
2072 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2073 md_wakeup_thread(mddev->thread);
2074 err = 0;
2075 } else {
2076 printk(KERN_ERR "md: %s has no personality assigned.\n",
2077 mdname(mddev));
2078 err = -EINVAL;
2079 }
2080
2081out:
2082 return err;
2083}
2084
2085static int do_md_stop(mddev_t * mddev, int ro)
2086{
2087 int err = 0;
2088 struct gendisk *disk = mddev->gendisk;
2089
2090 if (mddev->pers) {
2091 if (atomic_read(&mddev->active)>2) {
2092 printk("md: %s still in use.\n",mdname(mddev));
2093 return -EBUSY;
2094 }
2095
2096 if (mddev->sync_thread) {
2097 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2098 md_unregister_thread(mddev->sync_thread);
2099 mddev->sync_thread = NULL;
2100 }
2101
2102 del_timer_sync(&mddev->safemode_timer);
2103
2104 invalidate_partition(disk, 0);
2105
2106 if (ro) {
2107 err = -ENXIO;
2108 if (mddev->ro)
2109 goto out;
2110 mddev->ro = 1;
2111 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002112 bitmap_flush(mddev);
2113 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 if (mddev->ro)
2115 set_disk_ro(disk, 0);
2116 blk_queue_make_request(mddev->queue, md_fail_request);
2117 mddev->pers->stop(mddev);
2118 module_put(mddev->pers->owner);
2119 mddev->pers = NULL;
2120 if (mddev->ro)
2121 mddev->ro = 0;
2122 }
2123 if (!mddev->in_sync) {
2124 /* mark array as shutdown cleanly */
2125 mddev->in_sync = 1;
2126 md_update_sb(mddev);
2127 }
2128 if (ro)
2129 set_disk_ro(disk, 1);
2130 }
NeilBrown32a76272005-06-21 17:17:14 -07002131
2132 bitmap_destroy(mddev);
2133 if (mddev->bitmap_file) {
2134 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2135 fput(mddev->bitmap_file);
2136 mddev->bitmap_file = NULL;
2137 }
NeilBrown92232142005-08-18 11:24:16 -07002138 mddev->bitmap_offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /*
2141 * Free resources if final stop
2142 */
2143 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002144 mdk_rdev_t *rdev;
2145 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 struct gendisk *disk;
2147 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2148
NeilBrown86e6ffd2005-11-08 21:39:24 -08002149 ITERATE_RDEV(mddev,rdev,tmp)
2150 if (rdev->raid_disk >= 0) {
2151 char nm[20];
2152 sprintf(nm, "rd%d", rdev->raid_disk);
2153 sysfs_remove_link(&mddev->kobj, nm);
2154 }
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 export_array(mddev);
2157
2158 mddev->array_size = 0;
2159 disk = mddev->gendisk;
2160 if (disk)
2161 set_capacity(disk, 0);
2162 mddev->changed = 1;
2163 } else
2164 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2165 mdname(mddev));
2166 err = 0;
2167out:
2168 return err;
2169}
2170
2171static void autorun_array(mddev_t *mddev)
2172{
2173 mdk_rdev_t *rdev;
2174 struct list_head *tmp;
2175 int err;
2176
NeilBrowna757e642005-04-16 15:26:42 -07002177 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 printk(KERN_INFO "md: running: ");
2181
2182 ITERATE_RDEV(mddev,rdev,tmp) {
2183 char b[BDEVNAME_SIZE];
2184 printk("<%s>", bdevname(rdev->bdev,b));
2185 }
2186 printk("\n");
2187
2188 err = do_md_run (mddev);
2189 if (err) {
2190 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2191 do_md_stop (mddev, 0);
2192 }
2193}
2194
2195/*
2196 * lets try to run arrays based on all disks that have arrived
2197 * until now. (those are in pending_raid_disks)
2198 *
2199 * the method: pick the first pending disk, collect all disks with
2200 * the same UUID, remove all from the pending list and put them into
2201 * the 'same_array' list. Then order this list based on superblock
2202 * update time (freshest comes first), kick out 'old' disks and
2203 * compare superblocks. If everything's fine then run it.
2204 *
2205 * If "unit" is allocated, then bump its reference count
2206 */
2207static void autorun_devices(int part)
2208{
2209 struct list_head candidates;
2210 struct list_head *tmp;
2211 mdk_rdev_t *rdev0, *rdev;
2212 mddev_t *mddev;
2213 char b[BDEVNAME_SIZE];
2214
2215 printk(KERN_INFO "md: autorun ...\n");
2216 while (!list_empty(&pending_raid_disks)) {
2217 dev_t dev;
2218 rdev0 = list_entry(pending_raid_disks.next,
2219 mdk_rdev_t, same_set);
2220
2221 printk(KERN_INFO "md: considering %s ...\n",
2222 bdevname(rdev0->bdev,b));
2223 INIT_LIST_HEAD(&candidates);
2224 ITERATE_RDEV_PENDING(rdev,tmp)
2225 if (super_90_load(rdev, rdev0, 0) >= 0) {
2226 printk(KERN_INFO "md: adding %s ...\n",
2227 bdevname(rdev->bdev,b));
2228 list_move(&rdev->same_set, &candidates);
2229 }
2230 /*
2231 * now we have a set of devices, with all of them having
2232 * mostly sane superblocks. It's time to allocate the
2233 * mddev.
2234 */
2235 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2236 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2237 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2238 break;
2239 }
2240 if (part)
2241 dev = MKDEV(mdp_major,
2242 rdev0->preferred_minor << MdpMinorShift);
2243 else
2244 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2245
2246 md_probe(dev, NULL, NULL);
2247 mddev = mddev_find(dev);
2248 if (!mddev) {
2249 printk(KERN_ERR
2250 "md: cannot allocate memory for md drive.\n");
2251 break;
2252 }
2253 if (mddev_lock(mddev))
2254 printk(KERN_WARNING "md: %s locked, cannot run\n",
2255 mdname(mddev));
2256 else if (mddev->raid_disks || mddev->major_version
2257 || !list_empty(&mddev->disks)) {
2258 printk(KERN_WARNING
2259 "md: %s already running, cannot run %s\n",
2260 mdname(mddev), bdevname(rdev0->bdev,b));
2261 mddev_unlock(mddev);
2262 } else {
2263 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2264 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2265 list_del_init(&rdev->same_set);
2266 if (bind_rdev_to_array(rdev, mddev))
2267 export_rdev(rdev);
2268 }
2269 autorun_array(mddev);
2270 mddev_unlock(mddev);
2271 }
2272 /* on success, candidates will be empty, on error
2273 * it won't...
2274 */
2275 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2276 export_rdev(rdev);
2277 mddev_put(mddev);
2278 }
2279 printk(KERN_INFO "md: ... autorun DONE.\n");
2280}
2281
2282/*
2283 * import RAID devices based on one partition
2284 * if possible, the array gets run as well.
2285 */
2286
2287static int autostart_array(dev_t startdev)
2288{
2289 char b[BDEVNAME_SIZE];
2290 int err = -EINVAL, i;
2291 mdp_super_t *sb = NULL;
2292 mdk_rdev_t *start_rdev = NULL, *rdev;
2293
2294 start_rdev = md_import_device(startdev, 0, 0);
2295 if (IS_ERR(start_rdev))
2296 return err;
2297
2298
2299 /* NOTE: this can only work for 0.90.0 superblocks */
2300 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2301 if (sb->major_version != 0 ||
2302 sb->minor_version != 90 ) {
2303 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2304 export_rdev(start_rdev);
2305 return err;
2306 }
2307
2308 if (start_rdev->faulty) {
2309 printk(KERN_WARNING
2310 "md: can not autostart based on faulty %s!\n",
2311 bdevname(start_rdev->bdev,b));
2312 export_rdev(start_rdev);
2313 return err;
2314 }
2315 list_add(&start_rdev->same_set, &pending_raid_disks);
2316
2317 for (i = 0; i < MD_SB_DISKS; i++) {
2318 mdp_disk_t *desc = sb->disks + i;
2319 dev_t dev = MKDEV(desc->major, desc->minor);
2320
2321 if (!dev)
2322 continue;
2323 if (dev == startdev)
2324 continue;
2325 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2326 continue;
2327 rdev = md_import_device(dev, 0, 0);
2328 if (IS_ERR(rdev))
2329 continue;
2330
2331 list_add(&rdev->same_set, &pending_raid_disks);
2332 }
2333
2334 /*
2335 * possibly return codes
2336 */
2337 autorun_devices(0);
2338 return 0;
2339
2340}
2341
2342
2343static int get_version(void __user * arg)
2344{
2345 mdu_version_t ver;
2346
2347 ver.major = MD_MAJOR_VERSION;
2348 ver.minor = MD_MINOR_VERSION;
2349 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2350
2351 if (copy_to_user(arg, &ver, sizeof(ver)))
2352 return -EFAULT;
2353
2354 return 0;
2355}
2356
2357static int get_array_info(mddev_t * mddev, void __user * arg)
2358{
2359 mdu_array_info_t info;
2360 int nr,working,active,failed,spare;
2361 mdk_rdev_t *rdev;
2362 struct list_head *tmp;
2363
2364 nr=working=active=failed=spare=0;
2365 ITERATE_RDEV(mddev,rdev,tmp) {
2366 nr++;
2367 if (rdev->faulty)
2368 failed++;
2369 else {
2370 working++;
2371 if (rdev->in_sync)
2372 active++;
2373 else
2374 spare++;
2375 }
2376 }
2377
2378 info.major_version = mddev->major_version;
2379 info.minor_version = mddev->minor_version;
2380 info.patch_version = MD_PATCHLEVEL_VERSION;
2381 info.ctime = mddev->ctime;
2382 info.level = mddev->level;
2383 info.size = mddev->size;
2384 info.nr_disks = nr;
2385 info.raid_disks = mddev->raid_disks;
2386 info.md_minor = mddev->md_minor;
2387 info.not_persistent= !mddev->persistent;
2388
2389 info.utime = mddev->utime;
2390 info.state = 0;
2391 if (mddev->in_sync)
2392 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07002393 if (mddev->bitmap && mddev->bitmap_offset)
2394 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 info.active_disks = active;
2396 info.working_disks = working;
2397 info.failed_disks = failed;
2398 info.spare_disks = spare;
2399
2400 info.layout = mddev->layout;
2401 info.chunk_size = mddev->chunk_size;
2402
2403 if (copy_to_user(arg, &info, sizeof(info)))
2404 return -EFAULT;
2405
2406 return 0;
2407}
2408
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002409static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07002410{
2411 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2412 char *ptr, *buf = NULL;
2413 int err = -ENOMEM;
2414
2415 file = kmalloc(sizeof(*file), GFP_KERNEL);
2416 if (!file)
2417 goto out;
2418
2419 /* bitmap disabled, zero the first byte and copy out */
2420 if (!mddev->bitmap || !mddev->bitmap->file) {
2421 file->pathname[0] = '\0';
2422 goto copy_out;
2423 }
2424
2425 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2426 if (!buf)
2427 goto out;
2428
2429 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2430 if (!ptr)
2431 goto out;
2432
2433 strcpy(file->pathname, ptr);
2434
2435copy_out:
2436 err = 0;
2437 if (copy_to_user(arg, file, sizeof(*file)))
2438 err = -EFAULT;
2439out:
2440 kfree(buf);
2441 kfree(file);
2442 return err;
2443}
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445static int get_disk_info(mddev_t * mddev, void __user * arg)
2446{
2447 mdu_disk_info_t info;
2448 unsigned int nr;
2449 mdk_rdev_t *rdev;
2450
2451 if (copy_from_user(&info, arg, sizeof(info)))
2452 return -EFAULT;
2453
2454 nr = info.number;
2455
2456 rdev = find_rdev_nr(mddev, nr);
2457 if (rdev) {
2458 info.major = MAJOR(rdev->bdev->bd_dev);
2459 info.minor = MINOR(rdev->bdev->bd_dev);
2460 info.raid_disk = rdev->raid_disk;
2461 info.state = 0;
2462 if (rdev->faulty)
2463 info.state |= (1<<MD_DISK_FAULTY);
2464 else if (rdev->in_sync) {
2465 info.state |= (1<<MD_DISK_ACTIVE);
2466 info.state |= (1<<MD_DISK_SYNC);
2467 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002468 if (test_bit(WriteMostly, &rdev->flags))
2469 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 } else {
2471 info.major = info.minor = 0;
2472 info.raid_disk = -1;
2473 info.state = (1<<MD_DISK_REMOVED);
2474 }
2475
2476 if (copy_to_user(arg, &info, sizeof(info)))
2477 return -EFAULT;
2478
2479 return 0;
2480}
2481
2482static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2483{
2484 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2485 mdk_rdev_t *rdev;
2486 dev_t dev = MKDEV(info->major,info->minor);
2487
2488 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2489 return -EOVERFLOW;
2490
2491 if (!mddev->raid_disks) {
2492 int err;
2493 /* expecting a device which has a superblock */
2494 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2495 if (IS_ERR(rdev)) {
2496 printk(KERN_WARNING
2497 "md: md_import_device returned %ld\n",
2498 PTR_ERR(rdev));
2499 return PTR_ERR(rdev);
2500 }
2501 if (!list_empty(&mddev->disks)) {
2502 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2503 mdk_rdev_t, same_set);
2504 int err = super_types[mddev->major_version]
2505 .load_super(rdev, rdev0, mddev->minor_version);
2506 if (err < 0) {
2507 printk(KERN_WARNING
2508 "md: %s has different UUID to %s\n",
2509 bdevname(rdev->bdev,b),
2510 bdevname(rdev0->bdev,b2));
2511 export_rdev(rdev);
2512 return -EINVAL;
2513 }
2514 }
2515 err = bind_rdev_to_array(rdev, mddev);
2516 if (err)
2517 export_rdev(rdev);
2518 return err;
2519 }
2520
2521 /*
2522 * add_new_disk can be used once the array is assembled
2523 * to add "hot spares". They must already have a superblock
2524 * written
2525 */
2526 if (mddev->pers) {
2527 int err;
2528 if (!mddev->pers->hot_add_disk) {
2529 printk(KERN_WARNING
2530 "%s: personality does not support diskops!\n",
2531 mdname(mddev));
2532 return -EINVAL;
2533 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07002534 if (mddev->persistent)
2535 rdev = md_import_device(dev, mddev->major_version,
2536 mddev->minor_version);
2537 else
2538 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (IS_ERR(rdev)) {
2540 printk(KERN_WARNING
2541 "md: md_import_device returned %ld\n",
2542 PTR_ERR(rdev));
2543 return PTR_ERR(rdev);
2544 }
NeilBrown41158c72005-06-21 17:17:25 -07002545 /* set save_raid_disk if appropriate */
2546 if (!mddev->persistent) {
2547 if (info->state & (1<<MD_DISK_SYNC) &&
2548 info->raid_disk < mddev->raid_disks)
2549 rdev->raid_disk = info->raid_disk;
2550 else
2551 rdev->raid_disk = -1;
2552 } else
2553 super_types[mddev->major_version].
2554 validate_super(mddev, rdev);
2555 rdev->saved_raid_disk = rdev->raid_disk;
2556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 rdev->in_sync = 0; /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002558 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2559 set_bit(WriteMostly, &rdev->flags);
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 rdev->raid_disk = -1;
2562 err = bind_rdev_to_array(rdev, mddev);
2563 if (err)
2564 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07002565
2566 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002567 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return err;
2569 }
2570
2571 /* otherwise, add_new_disk is only allowed
2572 * for major_version==0 superblocks
2573 */
2574 if (mddev->major_version != 0) {
2575 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2576 mdname(mddev));
2577 return -EINVAL;
2578 }
2579
2580 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2581 int err;
2582 rdev = md_import_device (dev, -1, 0);
2583 if (IS_ERR(rdev)) {
2584 printk(KERN_WARNING
2585 "md: error, md_import_device() returned %ld\n",
2586 PTR_ERR(rdev));
2587 return PTR_ERR(rdev);
2588 }
2589 rdev->desc_nr = info->number;
2590 if (info->raid_disk < mddev->raid_disks)
2591 rdev->raid_disk = info->raid_disk;
2592 else
2593 rdev->raid_disk = -1;
2594
2595 rdev->faulty = 0;
2596 if (rdev->raid_disk < mddev->raid_disks)
2597 rdev->in_sync = (info->state & (1<<MD_DISK_SYNC));
2598 else
2599 rdev->in_sync = 0;
2600
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002601 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2602 set_bit(WriteMostly, &rdev->flags);
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 err = bind_rdev_to_array(rdev, mddev);
2605 if (err) {
2606 export_rdev(rdev);
2607 return err;
2608 }
2609
2610 if (!mddev->persistent) {
2611 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2612 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2613 } else
2614 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2615 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2616
2617 if (!mddev->size || (mddev->size > rdev->size))
2618 mddev->size = rdev->size;
2619 }
2620
2621 return 0;
2622}
2623
2624static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2625{
2626 char b[BDEVNAME_SIZE];
2627 mdk_rdev_t *rdev;
2628
2629 if (!mddev->pers)
2630 return -ENODEV;
2631
2632 rdev = find_rdev(mddev, dev);
2633 if (!rdev)
2634 return -ENXIO;
2635
2636 if (rdev->raid_disk >= 0)
2637 goto busy;
2638
2639 kick_rdev_from_array(rdev);
2640 md_update_sb(mddev);
2641
2642 return 0;
2643busy:
2644 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2645 bdevname(rdev->bdev,b), mdname(mddev));
2646 return -EBUSY;
2647}
2648
2649static int hot_add_disk(mddev_t * mddev, dev_t dev)
2650{
2651 char b[BDEVNAME_SIZE];
2652 int err;
2653 unsigned int size;
2654 mdk_rdev_t *rdev;
2655
2656 if (!mddev->pers)
2657 return -ENODEV;
2658
2659 if (mddev->major_version != 0) {
2660 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2661 " version-0 superblocks.\n",
2662 mdname(mddev));
2663 return -EINVAL;
2664 }
2665 if (!mddev->pers->hot_add_disk) {
2666 printk(KERN_WARNING
2667 "%s: personality does not support diskops!\n",
2668 mdname(mddev));
2669 return -EINVAL;
2670 }
2671
2672 rdev = md_import_device (dev, -1, 0);
2673 if (IS_ERR(rdev)) {
2674 printk(KERN_WARNING
2675 "md: error, md_import_device() returned %ld\n",
2676 PTR_ERR(rdev));
2677 return -EINVAL;
2678 }
2679
2680 if (mddev->persistent)
2681 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2682 else
2683 rdev->sb_offset =
2684 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2685
2686 size = calc_dev_size(rdev, mddev->chunk_size);
2687 rdev->size = size;
2688
2689 if (size < mddev->size) {
2690 printk(KERN_WARNING
2691 "%s: disk size %llu blocks < array size %llu\n",
2692 mdname(mddev), (unsigned long long)size,
2693 (unsigned long long)mddev->size);
2694 err = -ENOSPC;
2695 goto abort_export;
2696 }
2697
2698 if (rdev->faulty) {
2699 printk(KERN_WARNING
2700 "md: can not hot-add faulty %s disk to %s!\n",
2701 bdevname(rdev->bdev,b), mdname(mddev));
2702 err = -EINVAL;
2703 goto abort_export;
2704 }
2705 rdev->in_sync = 0;
2706 rdev->desc_nr = -1;
2707 bind_rdev_to_array(rdev, mddev);
2708
2709 /*
2710 * The rest should better be atomic, we can have disk failures
2711 * noticed in interrupt contexts ...
2712 */
2713
2714 if (rdev->desc_nr == mddev->max_disks) {
2715 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2716 mdname(mddev));
2717 err = -EBUSY;
2718 goto abort_unbind_export;
2719 }
2720
2721 rdev->raid_disk = -1;
2722
2723 md_update_sb(mddev);
2724
2725 /*
2726 * Kick recovery, maybe this spare has to be added to the
2727 * array immediately.
2728 */
2729 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2730 md_wakeup_thread(mddev->thread);
2731
2732 return 0;
2733
2734abort_unbind_export:
2735 unbind_rdev_from_array(rdev);
2736
2737abort_export:
2738 export_rdev(rdev);
2739 return err;
2740}
2741
NeilBrown32a76272005-06-21 17:17:14 -07002742/* similar to deny_write_access, but accounts for our holding a reference
2743 * to the file ourselves */
2744static int deny_bitmap_write_access(struct file * file)
2745{
2746 struct inode *inode = file->f_mapping->host;
2747
2748 spin_lock(&inode->i_lock);
2749 if (atomic_read(&inode->i_writecount) > 1) {
2750 spin_unlock(&inode->i_lock);
2751 return -ETXTBSY;
2752 }
2753 atomic_set(&inode->i_writecount, -1);
2754 spin_unlock(&inode->i_lock);
2755
2756 return 0;
2757}
2758
2759static int set_bitmap_file(mddev_t *mddev, int fd)
2760{
2761 int err;
2762
NeilBrown36fa3062005-09-09 16:23:45 -07002763 if (mddev->pers) {
2764 if (!mddev->pers->quiesce)
2765 return -EBUSY;
2766 if (mddev->recovery || mddev->sync_thread)
2767 return -EBUSY;
2768 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07002769 }
2770
NeilBrown36fa3062005-09-09 16:23:45 -07002771
2772 if (fd >= 0) {
2773 if (mddev->bitmap)
2774 return -EEXIST; /* cannot add when bitmap is present */
2775 mddev->bitmap_file = fget(fd);
2776
2777 if (mddev->bitmap_file == NULL) {
2778 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2779 mdname(mddev));
2780 return -EBADF;
2781 }
2782
2783 err = deny_bitmap_write_access(mddev->bitmap_file);
2784 if (err) {
2785 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2786 mdname(mddev));
2787 fput(mddev->bitmap_file);
2788 mddev->bitmap_file = NULL;
2789 return err;
2790 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002791 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07002792 } else if (mddev->bitmap == NULL)
2793 return -ENOENT; /* cannot remove what isn't there */
2794 err = 0;
2795 if (mddev->pers) {
2796 mddev->pers->quiesce(mddev, 1);
2797 if (fd >= 0)
2798 err = bitmap_create(mddev);
2799 if (fd < 0 || err)
2800 bitmap_destroy(mddev);
2801 mddev->pers->quiesce(mddev, 0);
2802 } else if (fd < 0) {
2803 if (mddev->bitmap_file)
2804 fput(mddev->bitmap_file);
2805 mddev->bitmap_file = NULL;
2806 }
2807
NeilBrown32a76272005-06-21 17:17:14 -07002808 return err;
2809}
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811/*
2812 * set_array_info is used two different ways
2813 * The original usage is when creating a new array.
2814 * In this usage, raid_disks is > 0 and it together with
2815 * level, size, not_persistent,layout,chunksize determine the
2816 * shape of the array.
2817 * This will always create an array with a type-0.90.0 superblock.
2818 * The newer usage is when assembling an array.
2819 * In this case raid_disks will be 0, and the major_version field is
2820 * use to determine which style super-blocks are to be found on the devices.
2821 * The minor and patch _version numbers are also kept incase the
2822 * super_block handler wishes to interpret them.
2823 */
2824static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2825{
2826
2827 if (info->raid_disks == 0) {
2828 /* just setting version number for superblock loading */
2829 if (info->major_version < 0 ||
2830 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2831 super_types[info->major_version].name == NULL) {
2832 /* maybe try to auto-load a module? */
2833 printk(KERN_INFO
2834 "md: superblock version %d not known\n",
2835 info->major_version);
2836 return -EINVAL;
2837 }
2838 mddev->major_version = info->major_version;
2839 mddev->minor_version = info->minor_version;
2840 mddev->patch_version = info->patch_version;
2841 return 0;
2842 }
2843 mddev->major_version = MD_MAJOR_VERSION;
2844 mddev->minor_version = MD_MINOR_VERSION;
2845 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2846 mddev->ctime = get_seconds();
2847
2848 mddev->level = info->level;
2849 mddev->size = info->size;
2850 mddev->raid_disks = info->raid_disks;
2851 /* don't set md_minor, it is determined by which /dev/md* was
2852 * openned
2853 */
2854 if (info->state & (1<<MD_SB_CLEAN))
2855 mddev->recovery_cp = MaxSector;
2856 else
2857 mddev->recovery_cp = 0;
2858 mddev->persistent = ! info->not_persistent;
2859
2860 mddev->layout = info->layout;
2861 mddev->chunk_size = info->chunk_size;
2862
2863 mddev->max_disks = MD_SB_DISKS;
2864
2865 mddev->sb_dirty = 1;
2866
2867 /*
2868 * Generate a 128 bit UUID
2869 */
2870 get_random_bytes(mddev->uuid, 16);
2871
2872 return 0;
2873}
2874
2875/*
2876 * update_array_info is used to change the configuration of an
2877 * on-line array.
2878 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2879 * fields in the info are checked against the array.
2880 * Any differences that cannot be handled will cause an error.
2881 * Normally, only one change can be managed at a time.
2882 */
2883static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2884{
2885 int rv = 0;
2886 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07002887 int state = 0;
2888
2889 /* calculate expected state,ignoring low bits */
2890 if (mddev->bitmap && mddev->bitmap_offset)
2891 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
2893 if (mddev->major_version != info->major_version ||
2894 mddev->minor_version != info->minor_version ||
2895/* mddev->patch_version != info->patch_version || */
2896 mddev->ctime != info->ctime ||
2897 mddev->level != info->level ||
2898/* mddev->layout != info->layout || */
2899 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07002900 mddev->chunk_size != info->chunk_size ||
2901 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2902 ((state^info->state) & 0xfffffe00)
2903 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return -EINVAL;
2905 /* Check there is only one change */
2906 if (mddev->size != info->size) cnt++;
2907 if (mddev->raid_disks != info->raid_disks) cnt++;
2908 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07002909 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 if (cnt == 0) return 0;
2911 if (cnt > 1) return -EINVAL;
2912
2913 if (mddev->layout != info->layout) {
2914 /* Change layout
2915 * we don't need to do anything at the md level, the
2916 * personality will take care of it all.
2917 */
2918 if (mddev->pers->reconfig == NULL)
2919 return -EINVAL;
2920 else
2921 return mddev->pers->reconfig(mddev, info->layout, -1);
2922 }
2923 if (mddev->size != info->size) {
2924 mdk_rdev_t * rdev;
2925 struct list_head *tmp;
2926 if (mddev->pers->resize == NULL)
2927 return -EINVAL;
2928 /* The "size" is the amount of each device that is used.
2929 * This can only make sense for arrays with redundancy.
2930 * linear and raid0 always use whatever space is available
2931 * We can only consider changing the size if no resync
2932 * or reconstruction is happening, and if the new size
2933 * is acceptable. It must fit before the sb_offset or,
2934 * if that is <data_offset, it must fit before the
2935 * size of each device.
2936 * If size is zero, we find the largest size that fits.
2937 */
2938 if (mddev->sync_thread)
2939 return -EBUSY;
2940 ITERATE_RDEV(mddev,rdev,tmp) {
2941 sector_t avail;
2942 int fit = (info->size == 0);
2943 if (rdev->sb_offset > rdev->data_offset)
2944 avail = (rdev->sb_offset*2) - rdev->data_offset;
2945 else
2946 avail = get_capacity(rdev->bdev->bd_disk)
2947 - rdev->data_offset;
2948 if (fit && (info->size == 0 || info->size > avail/2))
2949 info->size = avail/2;
2950 if (avail < ((sector_t)info->size << 1))
2951 return -ENOSPC;
2952 }
2953 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
2954 if (!rv) {
2955 struct block_device *bdev;
2956
2957 bdev = bdget_disk(mddev->gendisk, 0);
2958 if (bdev) {
2959 down(&bdev->bd_inode->i_sem);
2960 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2961 up(&bdev->bd_inode->i_sem);
2962 bdput(bdev);
2963 }
2964 }
2965 }
2966 if (mddev->raid_disks != info->raid_disks) {
2967 /* change the number of raid disks */
2968 if (mddev->pers->reshape == NULL)
2969 return -EINVAL;
2970 if (info->raid_disks <= 0 ||
2971 info->raid_disks >= mddev->max_disks)
2972 return -EINVAL;
2973 if (mddev->sync_thread)
2974 return -EBUSY;
2975 rv = mddev->pers->reshape(mddev, info->raid_disks);
2976 if (!rv) {
2977 struct block_device *bdev;
2978
2979 bdev = bdget_disk(mddev->gendisk, 0);
2980 if (bdev) {
2981 down(&bdev->bd_inode->i_sem);
2982 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2983 up(&bdev->bd_inode->i_sem);
2984 bdput(bdev);
2985 }
2986 }
2987 }
NeilBrown36fa3062005-09-09 16:23:45 -07002988 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
2989 if (mddev->pers->quiesce == NULL)
2990 return -EINVAL;
2991 if (mddev->recovery || mddev->sync_thread)
2992 return -EBUSY;
2993 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
2994 /* add the bitmap */
2995 if (mddev->bitmap)
2996 return -EEXIST;
2997 if (mddev->default_bitmap_offset == 0)
2998 return -EINVAL;
2999 mddev->bitmap_offset = mddev->default_bitmap_offset;
3000 mddev->pers->quiesce(mddev, 1);
3001 rv = bitmap_create(mddev);
3002 if (rv)
3003 bitmap_destroy(mddev);
3004 mddev->pers->quiesce(mddev, 0);
3005 } else {
3006 /* remove the bitmap */
3007 if (!mddev->bitmap)
3008 return -ENOENT;
3009 if (mddev->bitmap->file)
3010 return -EINVAL;
3011 mddev->pers->quiesce(mddev, 1);
3012 bitmap_destroy(mddev);
3013 mddev->pers->quiesce(mddev, 0);
3014 mddev->bitmap_offset = 0;
3015 }
3016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 md_update_sb(mddev);
3018 return rv;
3019}
3020
3021static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3022{
3023 mdk_rdev_t *rdev;
3024
3025 if (mddev->pers == NULL)
3026 return -ENODEV;
3027
3028 rdev = find_rdev(mddev, dev);
3029 if (!rdev)
3030 return -ENODEV;
3031
3032 md_error(mddev, rdev);
3033 return 0;
3034}
3035
3036static int md_ioctl(struct inode *inode, struct file *file,
3037 unsigned int cmd, unsigned long arg)
3038{
3039 int err = 0;
3040 void __user *argp = (void __user *)arg;
3041 struct hd_geometry __user *loc = argp;
3042 mddev_t *mddev = NULL;
3043
3044 if (!capable(CAP_SYS_ADMIN))
3045 return -EACCES;
3046
3047 /*
3048 * Commands dealing with the RAID driver but not any
3049 * particular array:
3050 */
3051 switch (cmd)
3052 {
3053 case RAID_VERSION:
3054 err = get_version(argp);
3055 goto done;
3056
3057 case PRINT_RAID_DEBUG:
3058 err = 0;
3059 md_print_devices();
3060 goto done;
3061
3062#ifndef MODULE
3063 case RAID_AUTORUN:
3064 err = 0;
3065 autostart_arrays(arg);
3066 goto done;
3067#endif
3068 default:;
3069 }
3070
3071 /*
3072 * Commands creating/starting a new array:
3073 */
3074
3075 mddev = inode->i_bdev->bd_disk->private_data;
3076
3077 if (!mddev) {
3078 BUG();
3079 goto abort;
3080 }
3081
3082
3083 if (cmd == START_ARRAY) {
3084 /* START_ARRAY doesn't need to lock the array as autostart_array
3085 * does the locking, and it could even be a different array
3086 */
3087 static int cnt = 3;
3088 if (cnt > 0 ) {
3089 printk(KERN_WARNING
3090 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
3091 "This will not be supported beyond 2.6\n",
3092 current->comm, current->pid);
3093 cnt--;
3094 }
3095 err = autostart_array(new_decode_dev(arg));
3096 if (err) {
3097 printk(KERN_WARNING "md: autostart failed!\n");
3098 goto abort;
3099 }
3100 goto done;
3101 }
3102
3103 err = mddev_lock(mddev);
3104 if (err) {
3105 printk(KERN_INFO
3106 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3107 err, cmd);
3108 goto abort;
3109 }
3110
3111 switch (cmd)
3112 {
3113 case SET_ARRAY_INFO:
3114 {
3115 mdu_array_info_t info;
3116 if (!arg)
3117 memset(&info, 0, sizeof(info));
3118 else if (copy_from_user(&info, argp, sizeof(info))) {
3119 err = -EFAULT;
3120 goto abort_unlock;
3121 }
3122 if (mddev->pers) {
3123 err = update_array_info(mddev, &info);
3124 if (err) {
3125 printk(KERN_WARNING "md: couldn't update"
3126 " array info. %d\n", err);
3127 goto abort_unlock;
3128 }
3129 goto done_unlock;
3130 }
3131 if (!list_empty(&mddev->disks)) {
3132 printk(KERN_WARNING
3133 "md: array %s already has disks!\n",
3134 mdname(mddev));
3135 err = -EBUSY;
3136 goto abort_unlock;
3137 }
3138 if (mddev->raid_disks) {
3139 printk(KERN_WARNING
3140 "md: array %s already initialised!\n",
3141 mdname(mddev));
3142 err = -EBUSY;
3143 goto abort_unlock;
3144 }
3145 err = set_array_info(mddev, &info);
3146 if (err) {
3147 printk(KERN_WARNING "md: couldn't set"
3148 " array info. %d\n", err);
3149 goto abort_unlock;
3150 }
3151 }
3152 goto done_unlock;
3153
3154 default:;
3155 }
3156
3157 /*
3158 * Commands querying/configuring an existing array:
3159 */
NeilBrown32a76272005-06-21 17:17:14 -07003160 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3161 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3162 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3163 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 err = -ENODEV;
3165 goto abort_unlock;
3166 }
3167
3168 /*
3169 * Commands even a read-only array can execute:
3170 */
3171 switch (cmd)
3172 {
3173 case GET_ARRAY_INFO:
3174 err = get_array_info(mddev, argp);
3175 goto done_unlock;
3176
NeilBrown32a76272005-06-21 17:17:14 -07003177 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003178 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003179 goto done_unlock;
3180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 case GET_DISK_INFO:
3182 err = get_disk_info(mddev, argp);
3183 goto done_unlock;
3184
3185 case RESTART_ARRAY_RW:
3186 err = restart_array(mddev);
3187 goto done_unlock;
3188
3189 case STOP_ARRAY:
3190 err = do_md_stop (mddev, 0);
3191 goto done_unlock;
3192
3193 case STOP_ARRAY_RO:
3194 err = do_md_stop (mddev, 1);
3195 goto done_unlock;
3196
3197 /*
3198 * We have a problem here : there is no easy way to give a CHS
3199 * virtual geometry. We currently pretend that we have a 2 heads
3200 * 4 sectors (with a BIG number of cylinders...). This drives
3201 * dosfs just mad... ;-)
3202 */
3203 case HDIO_GETGEO:
3204 if (!loc) {
3205 err = -EINVAL;
3206 goto abort_unlock;
3207 }
3208 err = put_user (2, (char __user *) &loc->heads);
3209 if (err)
3210 goto abort_unlock;
3211 err = put_user (4, (char __user *) &loc->sectors);
3212 if (err)
3213 goto abort_unlock;
3214 err = put_user(get_capacity(mddev->gendisk)/8,
3215 (short __user *) &loc->cylinders);
3216 if (err)
3217 goto abort_unlock;
3218 err = put_user (get_start_sect(inode->i_bdev),
3219 (long __user *) &loc->start);
3220 goto done_unlock;
3221 }
3222
3223 /*
3224 * The remaining ioctls are changing the state of the
3225 * superblock, so we do not allow read-only arrays
3226 * here:
3227 */
3228 if (mddev->ro) {
3229 err = -EROFS;
3230 goto abort_unlock;
3231 }
3232
3233 switch (cmd)
3234 {
3235 case ADD_NEW_DISK:
3236 {
3237 mdu_disk_info_t info;
3238 if (copy_from_user(&info, argp, sizeof(info)))
3239 err = -EFAULT;
3240 else
3241 err = add_new_disk(mddev, &info);
3242 goto done_unlock;
3243 }
3244
3245 case HOT_REMOVE_DISK:
3246 err = hot_remove_disk(mddev, new_decode_dev(arg));
3247 goto done_unlock;
3248
3249 case HOT_ADD_DISK:
3250 err = hot_add_disk(mddev, new_decode_dev(arg));
3251 goto done_unlock;
3252
3253 case SET_DISK_FAULTY:
3254 err = set_disk_faulty(mddev, new_decode_dev(arg));
3255 goto done_unlock;
3256
3257 case RUN_ARRAY:
3258 err = do_md_run (mddev);
3259 goto done_unlock;
3260
NeilBrown32a76272005-06-21 17:17:14 -07003261 case SET_BITMAP_FILE:
3262 err = set_bitmap_file(mddev, (int)arg);
3263 goto done_unlock;
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 default:
3266 if (_IOC_TYPE(cmd) == MD_MAJOR)
3267 printk(KERN_WARNING "md: %s(pid %d) used"
3268 " obsolete MD ioctl, upgrade your"
3269 " software to use new ictls.\n",
3270 current->comm, current->pid);
3271 err = -EINVAL;
3272 goto abort_unlock;
3273 }
3274
3275done_unlock:
3276abort_unlock:
3277 mddev_unlock(mddev);
3278
3279 return err;
3280done:
3281 if (err)
3282 MD_BUG();
3283abort:
3284 return err;
3285}
3286
3287static int md_open(struct inode *inode, struct file *file)
3288{
3289 /*
3290 * Succeed if we can lock the mddev, which confirms that
3291 * it isn't being stopped right now.
3292 */
3293 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3294 int err;
3295
3296 if ((err = mddev_lock(mddev)))
3297 goto out;
3298
3299 err = 0;
3300 mddev_get(mddev);
3301 mddev_unlock(mddev);
3302
3303 check_disk_change(inode->i_bdev);
3304 out:
3305 return err;
3306}
3307
3308static int md_release(struct inode *inode, struct file * file)
3309{
3310 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3311
3312 if (!mddev)
3313 BUG();
3314 mddev_put(mddev);
3315
3316 return 0;
3317}
3318
3319static int md_media_changed(struct gendisk *disk)
3320{
3321 mddev_t *mddev = disk->private_data;
3322
3323 return mddev->changed;
3324}
3325
3326static int md_revalidate(struct gendisk *disk)
3327{
3328 mddev_t *mddev = disk->private_data;
3329
3330 mddev->changed = 0;
3331 return 0;
3332}
3333static struct block_device_operations md_fops =
3334{
3335 .owner = THIS_MODULE,
3336 .open = md_open,
3337 .release = md_release,
3338 .ioctl = md_ioctl,
3339 .media_changed = md_media_changed,
3340 .revalidate_disk= md_revalidate,
3341};
3342
Adrian Bunk75c96f82005-05-05 16:16:09 -07003343static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
3345 mdk_thread_t *thread = arg;
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 /*
3348 * md_thread is a 'system-thread', it's priority should be very
3349 * high. We avoid resource deadlocks individually in each
3350 * raid personality. (RAID5 does preallocation) We also use RR and
3351 * the very same RT priority as kswapd, thus we will never get
3352 * into a priority inversion deadlock.
3353 *
3354 * we definitely have to have equal or higher priority than
3355 * bdflush, otherwise bdflush will deadlock if there are too
3356 * many dirty RAID5 blocks.
3357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
NeilBrown6985c432005-10-19 21:23:47 -07003359 allow_signal(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 complete(thread->event);
NeilBrowna6fb0932005-09-09 16:23:56 -07003361 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 void (*run)(mddev_t *);
3363
NeilBrown32a76272005-06-21 17:17:14 -07003364 wait_event_interruptible_timeout(thread->wqueue,
NeilBrowna6fb0932005-09-09 16:23:56 -07003365 test_bit(THREAD_WAKEUP, &thread->flags)
3366 || kthread_should_stop(),
NeilBrown32a76272005-06-21 17:17:14 -07003367 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003368 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
3370 clear_bit(THREAD_WAKEUP, &thread->flags);
3371
3372 run = thread->run;
3373 if (run)
3374 run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
NeilBrowna6fb0932005-09-09 16:23:56 -07003376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 return 0;
3378}
3379
3380void md_wakeup_thread(mdk_thread_t *thread)
3381{
3382 if (thread) {
3383 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3384 set_bit(THREAD_WAKEUP, &thread->flags);
3385 wake_up(&thread->wqueue);
3386 }
3387}
3388
3389mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3390 const char *name)
3391{
3392 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 struct completion event;
3394
NeilBrowna6fb0932005-09-09 16:23:56 -07003395 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 if (!thread)
3397 return NULL;
3398
3399 memset(thread, 0, sizeof(mdk_thread_t));
3400 init_waitqueue_head(&thread->wqueue);
3401
3402 init_completion(&event);
3403 thread->event = &event;
3404 thread->run = run;
3405 thread->mddev = mddev;
3406 thread->name = name;
NeilBrown32a76272005-06-21 17:17:14 -07003407 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07003408 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07003409 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 kfree(thread);
3411 return NULL;
3412 }
3413 wait_for_completion(&event);
3414 return thread;
3415}
3416
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417void md_unregister_thread(mdk_thread_t *thread)
3418{
NeilBrownd28446f2005-04-16 15:26:41 -07003419 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07003420
3421 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 kfree(thread);
3423}
3424
3425void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3426{
3427 if (!mddev) {
3428 MD_BUG();
3429 return;
3430 }
3431
3432 if (!rdev || rdev->faulty)
3433 return;
NeilBrown32a76272005-06-21 17:17:14 -07003434/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3436 mdname(mddev),
3437 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3438 __builtin_return_address(0),__builtin_return_address(1),
3439 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07003440*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 if (!mddev->pers->error_handler)
3442 return;
3443 mddev->pers->error_handler(mddev,rdev);
3444 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3445 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3446 md_wakeup_thread(mddev->thread);
3447}
3448
3449/* seq_file implementation /proc/mdstat */
3450
3451static void status_unused(struct seq_file *seq)
3452{
3453 int i = 0;
3454 mdk_rdev_t *rdev;
3455 struct list_head *tmp;
3456
3457 seq_printf(seq, "unused devices: ");
3458
3459 ITERATE_RDEV_PENDING(rdev,tmp) {
3460 char b[BDEVNAME_SIZE];
3461 i++;
3462 seq_printf(seq, "%s ",
3463 bdevname(rdev->bdev,b));
3464 }
3465 if (!i)
3466 seq_printf(seq, "<none>");
3467
3468 seq_printf(seq, "\n");
3469}
3470
3471
3472static void status_resync(struct seq_file *seq, mddev_t * mddev)
3473{
3474 unsigned long max_blocks, resync, res, dt, db, rt;
3475
3476 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3477
3478 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3479 max_blocks = mddev->resync_max_sectors >> 1;
3480 else
3481 max_blocks = mddev->size;
3482
3483 /*
3484 * Should not happen.
3485 */
3486 if (!max_blocks) {
3487 MD_BUG();
3488 return;
3489 }
3490 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3491 {
3492 int i, x = res/50, y = 20-x;
3493 seq_printf(seq, "[");
3494 for (i = 0; i < x; i++)
3495 seq_printf(seq, "=");
3496 seq_printf(seq, ">");
3497 for (i = 0; i < y; i++)
3498 seq_printf(seq, ".");
3499 seq_printf(seq, "] ");
3500 }
3501 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3502 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3503 "resync" : "recovery"),
3504 res/10, res % 10, resync, max_blocks);
3505
3506 /*
3507 * We do not want to overflow, so the order of operands and
3508 * the * 100 / 100 trick are important. We do a +1 to be
3509 * safe against division by zero. We only estimate anyway.
3510 *
3511 * dt: time from mark until now
3512 * db: blocks written from mark until now
3513 * rt: remaining time
3514 */
3515 dt = ((jiffies - mddev->resync_mark) / HZ);
3516 if (!dt) dt++;
3517 db = resync - (mddev->resync_mark_cnt/2);
3518 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3519
3520 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3521
3522 seq_printf(seq, " speed=%ldK/sec", db/dt);
3523}
3524
3525static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3526{
3527 struct list_head *tmp;
3528 loff_t l = *pos;
3529 mddev_t *mddev;
3530
3531 if (l >= 0x10000)
3532 return NULL;
3533 if (!l--)
3534 /* header */
3535 return (void*)1;
3536
3537 spin_lock(&all_mddevs_lock);
3538 list_for_each(tmp,&all_mddevs)
3539 if (!l--) {
3540 mddev = list_entry(tmp, mddev_t, all_mddevs);
3541 mddev_get(mddev);
3542 spin_unlock(&all_mddevs_lock);
3543 return mddev;
3544 }
3545 spin_unlock(&all_mddevs_lock);
3546 if (!l--)
3547 return (void*)2;/* tail */
3548 return NULL;
3549}
3550
3551static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3552{
3553 struct list_head *tmp;
3554 mddev_t *next_mddev, *mddev = v;
3555
3556 ++*pos;
3557 if (v == (void*)2)
3558 return NULL;
3559
3560 spin_lock(&all_mddevs_lock);
3561 if (v == (void*)1)
3562 tmp = all_mddevs.next;
3563 else
3564 tmp = mddev->all_mddevs.next;
3565 if (tmp != &all_mddevs)
3566 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3567 else {
3568 next_mddev = (void*)2;
3569 *pos = 0x10000;
3570 }
3571 spin_unlock(&all_mddevs_lock);
3572
3573 if (v != (void*)1)
3574 mddev_put(mddev);
3575 return next_mddev;
3576
3577}
3578
3579static void md_seq_stop(struct seq_file *seq, void *v)
3580{
3581 mddev_t *mddev = v;
3582
3583 if (mddev && v != (void*)1 && v != (void*)2)
3584 mddev_put(mddev);
3585}
3586
3587static int md_seq_show(struct seq_file *seq, void *v)
3588{
3589 mddev_t *mddev = v;
3590 sector_t size;
3591 struct list_head *tmp2;
3592 mdk_rdev_t *rdev;
3593 int i;
NeilBrown32a76272005-06-21 17:17:14 -07003594 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
3596 if (v == (void*)1) {
3597 seq_printf(seq, "Personalities : ");
3598 spin_lock(&pers_lock);
3599 for (i = 0; i < MAX_PERSONALITY; i++)
3600 if (pers[i])
3601 seq_printf(seq, "[%s] ", pers[i]->name);
3602
3603 spin_unlock(&pers_lock);
3604 seq_printf(seq, "\n");
3605 return 0;
3606 }
3607 if (v == (void*)2) {
3608 status_unused(seq);
3609 return 0;
3610 }
3611
3612 if (mddev_lock(mddev)!=0)
3613 return -EINTR;
3614 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3615 seq_printf(seq, "%s : %sactive", mdname(mddev),
3616 mddev->pers ? "" : "in");
3617 if (mddev->pers) {
3618 if (mddev->ro)
3619 seq_printf(seq, " (read-only)");
3620 seq_printf(seq, " %s", mddev->pers->name);
3621 }
3622
3623 size = 0;
3624 ITERATE_RDEV(mddev,rdev,tmp2) {
3625 char b[BDEVNAME_SIZE];
3626 seq_printf(seq, " %s[%d]",
3627 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003628 if (test_bit(WriteMostly, &rdev->flags))
3629 seq_printf(seq, "(W)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 if (rdev->faulty) {
3631 seq_printf(seq, "(F)");
3632 continue;
NeilBrownb325a322005-09-09 16:24:00 -07003633 } else if (rdev->raid_disk < 0)
3634 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 size += rdev->size;
3636 }
3637
3638 if (!list_empty(&mddev->disks)) {
3639 if (mddev->pers)
3640 seq_printf(seq, "\n %llu blocks",
3641 (unsigned long long)mddev->array_size);
3642 else
3643 seq_printf(seq, "\n %llu blocks",
3644 (unsigned long long)size);
3645 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07003646 if (mddev->persistent) {
3647 if (mddev->major_version != 0 ||
3648 mddev->minor_version != 90) {
3649 seq_printf(seq," super %d.%d",
3650 mddev->major_version,
3651 mddev->minor_version);
3652 }
3653 } else
3654 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
3656 if (mddev->pers) {
3657 mddev->pers->status (seq, mddev);
3658 seq_printf(seq, "\n ");
NeilBrown32a76272005-06-21 17:17:14 -07003659 if (mddev->curr_resync > 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 status_resync (seq, mddev);
NeilBrown32a76272005-06-21 17:17:14 -07003661 seq_printf(seq, "\n ");
3662 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3663 seq_printf(seq, " resync=DELAYED\n ");
3664 } else
3665 seq_printf(seq, "\n ");
3666
3667 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07003668 unsigned long chunk_kb;
3669 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07003670 spin_lock_irqsave(&bitmap->lock, flags);
3671 chunk_kb = bitmap->chunksize >> 10;
3672 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3673 "%lu%s chunk",
3674 bitmap->pages - bitmap->missing_pages,
3675 bitmap->pages,
3676 (bitmap->pages - bitmap->missing_pages)
3677 << (PAGE_SHIFT - 10),
3678 chunk_kb ? chunk_kb : bitmap->chunksize,
3679 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07003680 if (bitmap->file) {
3681 seq_printf(seq, ", file: ");
3682 seq_path(seq, bitmap->file->f_vfsmnt,
3683 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07003684 }
NeilBrown78d742d2005-06-21 17:17:15 -07003685
NeilBrown32a76272005-06-21 17:17:14 -07003686 seq_printf(seq, "\n");
3687 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 }
3689
3690 seq_printf(seq, "\n");
3691 }
3692 mddev_unlock(mddev);
3693
3694 return 0;
3695}
3696
3697static struct seq_operations md_seq_ops = {
3698 .start = md_seq_start,
3699 .next = md_seq_next,
3700 .stop = md_seq_stop,
3701 .show = md_seq_show,
3702};
3703
3704static int md_seq_open(struct inode *inode, struct file *file)
3705{
3706 int error;
3707
3708 error = seq_open(file, &md_seq_ops);
3709 return error;
3710}
3711
3712static struct file_operations md_seq_fops = {
3713 .open = md_seq_open,
3714 .read = seq_read,
3715 .llseek = seq_lseek,
3716 .release = seq_release,
3717};
3718
3719int register_md_personality(int pnum, mdk_personality_t *p)
3720{
3721 if (pnum >= MAX_PERSONALITY) {
3722 printk(KERN_ERR
3723 "md: tried to install personality %s as nr %d, but max is %lu\n",
3724 p->name, pnum, MAX_PERSONALITY-1);
3725 return -EINVAL;
3726 }
3727
3728 spin_lock(&pers_lock);
3729 if (pers[pnum]) {
3730 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 return -EBUSY;
3732 }
3733
3734 pers[pnum] = p;
3735 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3736 spin_unlock(&pers_lock);
3737 return 0;
3738}
3739
3740int unregister_md_personality(int pnum)
3741{
NeilBrowna757e642005-04-16 15:26:42 -07003742 if (pnum >= MAX_PERSONALITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
3745 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3746 spin_lock(&pers_lock);
3747 pers[pnum] = NULL;
3748 spin_unlock(&pers_lock);
3749 return 0;
3750}
3751
3752static int is_mddev_idle(mddev_t *mddev)
3753{
3754 mdk_rdev_t * rdev;
3755 struct list_head *tmp;
3756 int idle;
3757 unsigned long curr_events;
3758
3759 idle = 1;
3760 ITERATE_RDEV(mddev,rdev,tmp) {
3761 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01003762 curr_events = disk_stat_read(disk, sectors[0]) +
3763 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 atomic_read(&disk->sync_io);
3765 /* Allow some slack between valud of curr_events and last_events,
3766 * as there are some uninteresting races.
3767 * Note: the following is an unsigned comparison.
3768 */
3769 if ((curr_events - rdev->last_events + 32) > 64) {
3770 rdev->last_events = curr_events;
3771 idle = 0;
3772 }
3773 }
3774 return idle;
3775}
3776
3777void md_done_sync(mddev_t *mddev, int blocks, int ok)
3778{
3779 /* another "blocks" (512byte) blocks have been synced */
3780 atomic_sub(blocks, &mddev->recovery_active);
3781 wake_up(&mddev->recovery_wait);
3782 if (!ok) {
3783 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3784 md_wakeup_thread(mddev->thread);
3785 // stop recovery, signal do_sync ....
3786 }
3787}
3788
3789
NeilBrown06d91a52005-06-21 17:17:12 -07003790/* md_write_start(mddev, bi)
3791 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07003792 * in superblock) before writing, schedule a superblock update
3793 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07003794 */
NeilBrown3d310eb2005-06-21 17:17:26 -07003795void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
NeilBrown06d91a52005-06-21 17:17:12 -07003797 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07003798 return;
NeilBrown06d91a52005-06-21 17:17:12 -07003799
3800 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07003801 if (mddev->in_sync) {
NeilBrown3d310eb2005-06-21 17:17:26 -07003802 spin_lock(&mddev->write_lock);
3803 if (mddev->in_sync) {
3804 mddev->in_sync = 0;
3805 mddev->sb_dirty = 1;
3806 md_wakeup_thread(mddev->thread);
3807 }
3808 spin_unlock(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07003809 }
NeilBrown3d310eb2005-06-21 17:17:26 -07003810 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811}
3812
3813void md_write_end(mddev_t *mddev)
3814{
3815 if (atomic_dec_and_test(&mddev->writes_pending)) {
3816 if (mddev->safemode == 2)
3817 md_wakeup_thread(mddev->thread);
3818 else
3819 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3820 }
3821}
3822
Adrian Bunk75c96f82005-05-05 16:16:09 -07003823static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
3825#define SYNC_MARKS 10
3826#define SYNC_MARK_STEP (3*HZ)
3827static void md_do_sync(mddev_t *mddev)
3828{
3829 mddev_t *mddev2;
3830 unsigned int currspeed = 0,
3831 window;
NeilBrown57afd892005-06-21 17:17:13 -07003832 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 unsigned long mark[SYNC_MARKS];
3834 sector_t mark_cnt[SYNC_MARKS];
3835 int last_mark,m;
3836 struct list_head *tmp;
3837 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07003838 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
3840 /* just incase thread restarts... */
3841 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3842 return;
3843
3844 /* we overload curr_resync somewhat here.
3845 * 0 == not engaged in resync at all
3846 * 2 == checking that there is no conflict with another sync
3847 * 1 == like 2, but have yielded to allow conflicting resync to
3848 * commense
3849 * other == active in resync - this many blocks
3850 *
3851 * Before starting a resync we must have set curr_resync to
3852 * 2, and then checked that every "conflicting" array has curr_resync
3853 * less than ours. When we find one that is the same or higher
3854 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3855 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3856 * This will mean we have to start checking from the beginning again.
3857 *
3858 */
3859
3860 do {
3861 mddev->curr_resync = 2;
3862
3863 try_again:
NeilBrown8712e552005-10-26 01:58:58 -07003864 if (signal_pending(current) ||
3865 kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 flush_signals(current);
NeilBrown6985c432005-10-19 21:23:47 -07003867 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 goto skip;
3869 }
3870 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 if (mddev2 == mddev)
3872 continue;
3873 if (mddev2->curr_resync &&
3874 match_mddev_units(mddev,mddev2)) {
3875 DEFINE_WAIT(wq);
3876 if (mddev < mddev2 && mddev->curr_resync == 2) {
3877 /* arbitrarily yield */
3878 mddev->curr_resync = 1;
3879 wake_up(&resync_wait);
3880 }
3881 if (mddev > mddev2 && mddev->curr_resync == 1)
3882 /* no need to wait here, we can wait the next
3883 * time 'round when curr_resync == 2
3884 */
3885 continue;
3886 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown8712e552005-10-26 01:58:58 -07003887 if (!signal_pending(current) &&
3888 !kthread_should_stop() &&
3889 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 printk(KERN_INFO "md: delaying resync of %s"
3891 " until %s has finished resync (they"
3892 " share one or more physical units)\n",
3893 mdname(mddev), mdname(mddev2));
3894 mddev_put(mddev2);
3895 schedule();
3896 finish_wait(&resync_wait, &wq);
3897 goto try_again;
3898 }
3899 finish_wait(&resync_wait, &wq);
3900 }
3901 }
3902 } while (mddev->curr_resync < 2);
3903
NeilBrown9d888832005-11-08 21:39:26 -08003904 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07003906 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 */
3908 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08003909 mddev->resync_mismatches = 0;
3910 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 /* recovery follows the physical size of devices */
3912 max_sectors = mddev->size << 1;
3913
3914 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
3915 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
3916 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
Adrian Bunk338cec32005-09-10 00:26:54 -07003917 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 "(but not more than %d KB/sec) for reconstruction.\n",
3919 sysctl_speed_limit_max);
3920
3921 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07003922 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08003923 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
3924 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 j = mddev->recovery_cp;
3926 else
3927 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07003928 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 for (m = 0; m < SYNC_MARKS; m++) {
3930 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07003931 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 }
3933 last_mark = 0;
3934 mddev->resync_mark = mark[last_mark];
3935 mddev->resync_mark_cnt = mark_cnt[last_mark];
3936
3937 /*
3938 * Tune reconstruction:
3939 */
3940 window = 32*(PAGE_SIZE/512);
3941 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
3942 window/2,(unsigned long long) max_sectors/2);
3943
3944 atomic_set(&mddev->recovery_active, 0);
3945 init_waitqueue_head(&mddev->recovery_wait);
3946 last_check = 0;
3947
3948 if (j>2) {
3949 printk(KERN_INFO
3950 "md: resuming recovery of %s from checkpoint.\n",
3951 mdname(mddev));
3952 mddev->curr_resync = j;
3953 }
3954
3955 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07003956 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
NeilBrown57afd892005-06-21 17:17:13 -07003958 skipped = 0;
3959 sectors = mddev->pers->sync_request(mddev, j, &skipped,
3960 currspeed < sysctl_speed_limit_min);
3961 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3963 goto out;
3964 }
NeilBrown57afd892005-06-21 17:17:13 -07003965
3966 if (!skipped) { /* actual IO requested */
3967 io_sectors += sectors;
3968 atomic_add(sectors, &mddev->recovery_active);
3969 }
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 j += sectors;
3972 if (j>1) mddev->curr_resync = j;
3973
NeilBrown57afd892005-06-21 17:17:13 -07003974
3975 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 continue;
3977
NeilBrown57afd892005-06-21 17:17:13 -07003978 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
3980 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
3981 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
3982 break;
3983
3984 repeat:
3985 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
3986 /* step marks */
3987 int next = (last_mark+1) % SYNC_MARKS;
3988
3989 mddev->resync_mark = mark[next];
3990 mddev->resync_mark_cnt = mark_cnt[next];
3991 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07003992 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 last_mark = next;
3994 }
3995
3996
NeilBrown8712e552005-10-26 01:58:58 -07003997 if (signal_pending(current) || kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 /*
3999 * got a signal, exit.
4000 */
4001 printk(KERN_INFO
4002 "md: md_do_sync() got signal ... exiting\n");
4003 flush_signals(current);
4004 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4005 goto out;
4006 }
4007
4008 /*
4009 * this loop exits only if either when we are slower than
4010 * the 'hard' speed limit, or the system was IO-idle for
4011 * a jiffy.
4012 * the system might be non-idle CPU-wise, but we only care
4013 * about not overloading the IO subsystem. (things like an
4014 * e2fsck being done on the RAID array should execute fast)
4015 */
4016 mddev->queue->unplug_fn(mddev->queue);
4017 cond_resched();
4018
NeilBrown57afd892005-06-21 17:17:13 -07004019 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4020 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
4022 if (currspeed > sysctl_speed_limit_min) {
4023 if ((currspeed > sysctl_speed_limit_max) ||
4024 !is_mddev_idle(mddev)) {
4025 msleep_interruptible(250);
4026 goto repeat;
4027 }
4028 }
4029 }
4030 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4031 /*
4032 * this also signals 'finished resyncing' to md_stop
4033 */
4034 out:
4035 mddev->queue->unplug_fn(mddev->queue);
4036
4037 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4038
4039 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004040 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
4042 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4043 mddev->curr_resync > 2 &&
4044 mddev->curr_resync >= mddev->recovery_cp) {
4045 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4046 printk(KERN_INFO
4047 "md: checkpointing recovery of %s.\n",
4048 mdname(mddev));
4049 mddev->recovery_cp = mddev->curr_resync;
4050 } else
4051 mddev->recovery_cp = MaxSector;
4052 }
4053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 skip:
4055 mddev->curr_resync = 0;
4056 wake_up(&resync_wait);
4057 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4058 md_wakeup_thread(mddev->thread);
4059}
4060
4061
4062/*
4063 * This routine is regularly called by all per-raid-array threads to
4064 * deal with generic issues like resync and super-block update.
4065 * Raid personalities that don't have a thread (linear/raid0) do not
4066 * need this as they never do any recovery or update the superblock.
4067 *
4068 * It does not do any resync itself, but rather "forks" off other threads
4069 * to do that as needed.
4070 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4071 * "->recovery" and create a thread at ->sync_thread.
4072 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4073 * and wakeups up this thread which will reap the thread and finish up.
4074 * This thread also removes any faulty devices (with nr_pending == 0).
4075 *
4076 * The overall approach is:
4077 * 1/ if the superblock needs updating, update it.
4078 * 2/ If a recovery thread is running, don't do anything else.
4079 * 3/ If recovery has finished, clean up, possibly marking spares active.
4080 * 4/ If there are any faulty devices, remove them.
4081 * 5/ If array is degraded, try to add spares devices
4082 * 6/ If array has spares or is not in-sync, start a resync thread.
4083 */
4084void md_check_recovery(mddev_t *mddev)
4085{
4086 mdk_rdev_t *rdev;
4087 struct list_head *rtmp;
4088
4089
NeilBrown5f404022005-06-21 17:17:16 -07004090 if (mddev->bitmap)
4091 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
4093 if (mddev->ro)
4094 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004095
4096 if (signal_pending(current)) {
4097 if (mddev->pers->sync_request) {
4098 printk(KERN_INFO "md: %s in immediate safe mode\n",
4099 mdname(mddev));
4100 mddev->safemode = 2;
4101 }
4102 flush_signals(current);
4103 }
4104
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 if ( ! (
4106 mddev->sb_dirty ||
4107 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004108 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4109 (mddev->safemode == 1) ||
4110 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4111 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 ))
4113 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004114
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 if (mddev_trylock(mddev)==0) {
4116 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004117
NeilBrown06d91a52005-06-21 17:17:12 -07004118 spin_lock(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004119 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4120 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4121 mddev->in_sync = 1;
4122 mddev->sb_dirty = 1;
4123 }
4124 if (mddev->safemode == 1)
4125 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07004126 spin_unlock(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 if (mddev->sb_dirty)
4129 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004130
NeilBrown06d91a52005-06-21 17:17:12 -07004131
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4133 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4134 /* resync/recovery still happening */
4135 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4136 goto unlock;
4137 }
4138 if (mddev->sync_thread) {
4139 /* resync has finished, collect result */
4140 md_unregister_thread(mddev->sync_thread);
4141 mddev->sync_thread = NULL;
4142 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4143 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4144 /* success...*/
4145 /* activate any spares */
4146 mddev->pers->spare_active(mddev);
4147 }
4148 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004149
4150 /* if array is no-longer degraded, then any saved_raid_disk
4151 * information must be scrapped
4152 */
4153 if (!mddev->degraded)
4154 ITERATE_RDEV(mddev,rdev,rtmp)
4155 rdev->saved_raid_disk = -1;
4156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 mddev->recovery = 0;
4158 /* flag recovery needed just to double check */
4159 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4160 goto unlock;
4161 }
NeilBrown24dd4692005-11-08 21:39:26 -08004162 /* Clear some bits that don't mean anything, but
4163 * might be left set
4164 */
4165 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4166 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4167 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4168 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
4170 /* no recovery is running.
4171 * remove any failed drives, then
4172 * add spares if possible.
4173 * Spare are also removed and re-added, to allow
4174 * the personality to fail the re-add.
4175 */
4176 ITERATE_RDEV(mddev,rdev,rtmp)
4177 if (rdev->raid_disk >= 0 &&
4178 (rdev->faulty || ! rdev->in_sync) &&
4179 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004180 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4181 char nm[20];
4182 sprintf(nm,"rd%d", rdev->raid_disk);
4183 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 }
4187
4188 if (mddev->degraded) {
4189 ITERATE_RDEV(mddev,rdev,rtmp)
4190 if (rdev->raid_disk < 0
4191 && !rdev->faulty) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004192 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4193 char nm[20];
4194 sprintf(nm, "rd%d", rdev->raid_disk);
4195 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 spares++;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004197 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 break;
4199 }
4200 }
4201
NeilBrown24dd4692005-11-08 21:39:26 -08004202 if (spares) {
4203 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4204 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4205 } else if (mddev->recovery_cp < MaxSector) {
4206 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4207 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4208 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08004210
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 if (mddev->pers->sync_request) {
4212 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07004213 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4214 /* We are adding a device or devices to an array
4215 * which has the bitmap stored on all devices.
4216 * So make sure all bitmap pages get written
4217 */
4218 bitmap_write_all(mddev->bitmap);
4219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 mddev->sync_thread = md_register_thread(md_do_sync,
4221 mddev,
4222 "%s_resync");
4223 if (!mddev->sync_thread) {
4224 printk(KERN_ERR "%s: could not start resync"
4225 " thread...\n",
4226 mdname(mddev));
4227 /* leave the spares where they are, it shouldn't hurt */
4228 mddev->recovery = 0;
4229 } else {
4230 md_wakeup_thread(mddev->sync_thread);
4231 }
4232 }
4233 unlock:
4234 mddev_unlock(mddev);
4235 }
4236}
4237
Adrian Bunk75c96f82005-05-05 16:16:09 -07004238static int md_notify_reboot(struct notifier_block *this,
4239 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240{
4241 struct list_head *tmp;
4242 mddev_t *mddev;
4243
4244 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4245
4246 printk(KERN_INFO "md: stopping all md devices.\n");
4247
4248 ITERATE_MDDEV(mddev,tmp)
4249 if (mddev_trylock(mddev)==0)
4250 do_md_stop (mddev, 1);
4251 /*
4252 * certain more exotic SCSI devices are known to be
4253 * volatile wrt too early system reboots. While the
4254 * right place to handle this issue is the given
4255 * driver, we do want to have a safe RAID driver ...
4256 */
4257 mdelay(1000*1);
4258 }
4259 return NOTIFY_DONE;
4260}
4261
Adrian Bunk75c96f82005-05-05 16:16:09 -07004262static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 .notifier_call = md_notify_reboot,
4264 .next = NULL,
4265 .priority = INT_MAX, /* before any real devices */
4266};
4267
4268static void md_geninit(void)
4269{
4270 struct proc_dir_entry *p;
4271
4272 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4273
4274 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4275 if (p)
4276 p->proc_fops = &md_seq_fops;
4277}
4278
Adrian Bunk75c96f82005-05-05 16:16:09 -07004279static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
4281 int minor;
4282
4283 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4284 " MD_SB_DISKS=%d\n",
4285 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4286 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrown32a76272005-06-21 17:17:14 -07004287 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR,
4288 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
4290 if (register_blkdev(MAJOR_NR, "md"))
4291 return -1;
4292 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4293 unregister_blkdev(MAJOR_NR, "md");
4294 return -1;
4295 }
4296 devfs_mk_dir("md");
4297 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4298 md_probe, NULL, NULL);
4299 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4300 md_probe, NULL, NULL);
4301
4302 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4303 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4304 S_IFBLK|S_IRUSR|S_IWUSR,
4305 "md/%d", minor);
4306
4307 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4308 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4309 S_IFBLK|S_IRUSR|S_IWUSR,
4310 "md/mdp%d", minor);
4311
4312
4313 register_reboot_notifier(&md_notifier);
4314 raid_table_header = register_sysctl_table(raid_root_table, 1);
4315
4316 md_geninit();
4317 return (0);
4318}
4319
4320
4321#ifndef MODULE
4322
4323/*
4324 * Searches all registered partitions for autorun RAID arrays
4325 * at boot time.
4326 */
4327static dev_t detected_devices[128];
4328static int dev_cnt;
4329
4330void md_autodetect_dev(dev_t dev)
4331{
4332 if (dev_cnt >= 0 && dev_cnt < 127)
4333 detected_devices[dev_cnt++] = dev;
4334}
4335
4336
4337static void autostart_arrays(int part)
4338{
4339 mdk_rdev_t *rdev;
4340 int i;
4341
4342 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4343
4344 for (i = 0; i < dev_cnt; i++) {
4345 dev_t dev = detected_devices[i];
4346
4347 rdev = md_import_device(dev,0, 0);
4348 if (IS_ERR(rdev))
4349 continue;
4350
4351 if (rdev->faulty) {
4352 MD_BUG();
4353 continue;
4354 }
4355 list_add(&rdev->same_set, &pending_raid_disks);
4356 }
4357 dev_cnt = 0;
4358
4359 autorun_devices(part);
4360}
4361
4362#endif
4363
4364static __exit void md_exit(void)
4365{
4366 mddev_t *mddev;
4367 struct list_head *tmp;
4368 int i;
4369 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4370 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4371 for (i=0; i < MAX_MD_DEVS; i++)
4372 devfs_remove("md/%d", i);
4373 for (i=0; i < MAX_MD_DEVS; i++)
4374 devfs_remove("md/d%d", i);
4375
4376 devfs_remove("md");
4377
4378 unregister_blkdev(MAJOR_NR,"md");
4379 unregister_blkdev(mdp_major, "mdp");
4380 unregister_reboot_notifier(&md_notifier);
4381 unregister_sysctl_table(raid_table_header);
4382 remove_proc_entry("mdstat", NULL);
4383 ITERATE_MDDEV(mddev,tmp) {
4384 struct gendisk *disk = mddev->gendisk;
4385 if (!disk)
4386 continue;
4387 export_array(mddev);
4388 del_gendisk(disk);
4389 put_disk(disk);
4390 mddev->gendisk = NULL;
4391 mddev_put(mddev);
4392 }
4393}
4394
4395module_init(md_init)
4396module_exit(md_exit)
4397
4398EXPORT_SYMBOL(register_md_personality);
4399EXPORT_SYMBOL(unregister_md_personality);
4400EXPORT_SYMBOL(md_error);
4401EXPORT_SYMBOL(md_done_sync);
4402EXPORT_SYMBOL(md_write_start);
4403EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404EXPORT_SYMBOL(md_register_thread);
4405EXPORT_SYMBOL(md_unregister_thread);
4406EXPORT_SYMBOL(md_wakeup_thread);
4407EXPORT_SYMBOL(md_print_devices);
4408EXPORT_SYMBOL(md_check_recovery);
4409MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07004410MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07004411MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);