blob: b4fb7247b3ed03907b66cc22d1201f88740c9710 [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
NeilBrownf91de922005-11-08 21:39:36 -0800134static int start_readonly;
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
137 * Enables to iterate over all existing md arrays
138 * all_mddevs_lock protects this list.
139 */
140static LIST_HEAD(all_mddevs);
141static DEFINE_SPINLOCK(all_mddevs_lock);
142
143
144/*
145 * iterates through all used mddevs in the system.
146 * We take care to grab the all_mddevs_lock whenever navigating
147 * the list, and to always hold a refcount when unlocked.
148 * Any code which breaks out of this loop while own
149 * a reference to the current mddev and must mddev_put it.
150 */
151#define ITERATE_MDDEV(mddev,tmp) \
152 \
153 for (({ spin_lock(&all_mddevs_lock); \
154 tmp = all_mddevs.next; \
155 mddev = NULL;}); \
156 ({ if (tmp != &all_mddevs) \
157 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
158 spin_unlock(&all_mddevs_lock); \
159 if (mddev) mddev_put(mddev); \
160 mddev = list_entry(tmp, mddev_t, all_mddevs); \
161 tmp != &all_mddevs;}); \
162 ({ spin_lock(&all_mddevs_lock); \
163 tmp = tmp->next;}) \
164 )
165
166
167static int md_fail_request (request_queue_t *q, struct bio *bio)
168{
169 bio_io_error(bio, bio->bi_size);
170 return 0;
171}
172
173static inline mddev_t *mddev_get(mddev_t *mddev)
174{
175 atomic_inc(&mddev->active);
176 return mddev;
177}
178
179static void mddev_put(mddev_t *mddev)
180{
181 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
182 return;
183 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
184 list_del(&mddev->all_mddevs);
185 blk_put_queue(mddev->queue);
NeilBrowneae17012005-11-08 21:39:23 -0800186 kobject_unregister(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 spin_unlock(&all_mddevs_lock);
189}
190
191static mddev_t * mddev_find(dev_t unit)
192{
193 mddev_t *mddev, *new = NULL;
194
195 retry:
196 spin_lock(&all_mddevs_lock);
197 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
198 if (mddev->unit == unit) {
199 mddev_get(mddev);
200 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700201 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return mddev;
203 }
204
205 if (new) {
206 list_add(&new->all_mddevs, &all_mddevs);
207 spin_unlock(&all_mddevs_lock);
208 return new;
209 }
210 spin_unlock(&all_mddevs_lock);
211
212 new = (mddev_t *) kmalloc(sizeof(*new), GFP_KERNEL);
213 if (!new)
214 return NULL;
215
216 memset(new, 0, sizeof(*new));
217
218 new->unit = unit;
219 if (MAJOR(unit) == MD_MAJOR)
220 new->md_minor = MINOR(unit);
221 else
222 new->md_minor = MINOR(unit) >> MdpMinorShift;
223
224 init_MUTEX(&new->reconfig_sem);
225 INIT_LIST_HEAD(&new->disks);
226 INIT_LIST_HEAD(&new->all_mddevs);
227 init_timer(&new->safemode_timer);
228 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700229 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700230 init_waitqueue_head(&new->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 new->queue = blk_alloc_queue(GFP_KERNEL);
233 if (!new->queue) {
234 kfree(new);
235 return NULL;
236 }
237
238 blk_queue_make_request(new->queue, md_fail_request);
239
240 goto retry;
241}
242
243static inline int mddev_lock(mddev_t * mddev)
244{
245 return down_interruptible(&mddev->reconfig_sem);
246}
247
248static inline void mddev_lock_uninterruptible(mddev_t * mddev)
249{
250 down(&mddev->reconfig_sem);
251}
252
253static inline int mddev_trylock(mddev_t * mddev)
254{
255 return down_trylock(&mddev->reconfig_sem);
256}
257
258static inline void mddev_unlock(mddev_t * mddev)
259{
260 up(&mddev->reconfig_sem);
261
NeilBrown005eca52005-08-22 13:11:08 -0700262 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
266{
267 mdk_rdev_t * rdev;
268 struct list_head *tmp;
269
270 ITERATE_RDEV(mddev,rdev,tmp) {
271 if (rdev->desc_nr == nr)
272 return rdev;
273 }
274 return NULL;
275}
276
277static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
278{
279 struct list_head *tmp;
280 mdk_rdev_t *rdev;
281
282 ITERATE_RDEV(mddev,rdev,tmp) {
283 if (rdev->bdev->bd_dev == dev)
284 return rdev;
285 }
286 return NULL;
287}
288
Jesper Juhl77933d72005-07-27 11:46:09 -0700289static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
292 return MD_NEW_SIZE_BLOCKS(size);
293}
294
295static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
296{
297 sector_t size;
298
299 size = rdev->sb_offset;
300
301 if (chunk_size)
302 size &= ~((sector_t)chunk_size/1024 - 1);
303 return size;
304}
305
306static int alloc_disk_sb(mdk_rdev_t * rdev)
307{
308 if (rdev->sb_page)
309 MD_BUG();
310
311 rdev->sb_page = alloc_page(GFP_KERNEL);
312 if (!rdev->sb_page) {
313 printk(KERN_ALERT "md: out of memory.\n");
314 return -EINVAL;
315 }
316
317 return 0;
318}
319
320static void free_disk_sb(mdk_rdev_t * rdev)
321{
322 if (rdev->sb_page) {
323 page_cache_release(rdev->sb_page);
324 rdev->sb_loaded = 0;
325 rdev->sb_page = NULL;
326 rdev->sb_offset = 0;
327 rdev->size = 0;
328 }
329}
330
331
NeilBrown7bfa19f2005-06-21 17:17:28 -0700332static int super_written(struct bio *bio, unsigned int bytes_done, int error)
333{
334 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800335 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700336 if (bio->bi_size)
337 return 1;
338
339 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
NeilBrowna9701a32005-11-08 21:39:34 -0800340 md_error(mddev, rdev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700341
NeilBrowna9701a32005-11-08 21:39:34 -0800342 if (atomic_dec_and_test(&mddev->pending_writes))
343 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700344 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700345 return 0;
346}
347
NeilBrowna9701a32005-11-08 21:39:34 -0800348static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
349{
350 struct bio *bio2 = bio->bi_private;
351 mdk_rdev_t *rdev = bio2->bi_private;
352 mddev_t *mddev = rdev->mddev;
353 if (bio->bi_size)
354 return 1;
355
356 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
357 error == -EOPNOTSUPP) {
358 unsigned long flags;
359 /* barriers don't appear to be supported :-( */
360 set_bit(BarriersNotsupp, &rdev->flags);
361 mddev->barriers_work = 0;
362 spin_lock_irqsave(&mddev->write_lock, flags);
363 bio2->bi_next = mddev->biolist;
364 mddev->biolist = bio2;
365 spin_unlock_irqrestore(&mddev->write_lock, flags);
366 wake_up(&mddev->sb_wait);
367 bio_put(bio);
368 return 0;
369 }
370 bio_put(bio2);
371 bio->bi_private = rdev;
372 return super_written(bio, bytes_done, error);
373}
374
NeilBrown7bfa19f2005-06-21 17:17:28 -0700375void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
376 sector_t sector, int size, struct page *page)
377{
378 /* write first size bytes of page to sector of rdev
379 * Increment mddev->pending_writes before returning
380 * and decrement it on completion, waking up sb_wait
381 * if zero is reached.
382 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800383 *
384 * As we might need to resubmit the request if BIO_RW_BARRIER
385 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700386 */
387 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800388 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700389
390 bio->bi_bdev = rdev->bdev;
391 bio->bi_sector = sector;
392 bio_add_page(bio, page, size, 0);
393 bio->bi_private = rdev;
394 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800395 bio->bi_rw = rw;
396
NeilBrown7bfa19f2005-06-21 17:17:28 -0700397 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800398 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
399 struct bio *rbio;
400 rw |= (1<<BIO_RW_BARRIER);
401 rbio = bio_clone(bio, GFP_NOIO);
402 rbio->bi_private = bio;
403 rbio->bi_end_io = super_written_barrier;
404 submit_bio(rw, rbio);
405 } else
406 submit_bio(rw, bio);
407}
408
409void md_super_wait(mddev_t *mddev)
410{
411 /* wait for all superblock writes that were scheduled to complete.
412 * if any had to be retried (due to BARRIER problems), retry them
413 */
414 DEFINE_WAIT(wq);
415 for(;;) {
416 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
417 if (atomic_read(&mddev->pending_writes)==0)
418 break;
419 while (mddev->biolist) {
420 struct bio *bio;
421 spin_lock_irq(&mddev->write_lock);
422 bio = mddev->biolist;
423 mddev->biolist = bio->bi_next ;
424 bio->bi_next = NULL;
425 spin_unlock_irq(&mddev->write_lock);
426 submit_bio(bio->bi_rw, bio);
427 }
428 schedule();
429 }
430 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
434{
435 if (bio->bi_size)
436 return 1;
437
438 complete((struct completion*)bio->bi_private);
439 return 0;
440}
441
NeilBrowna654b9d82005-06-21 17:17:27 -0700442int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct page *page, int rw)
444{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700445 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct completion event;
447 int ret;
448
449 rw |= (1 << BIO_RW_SYNC);
450
451 bio->bi_bdev = bdev;
452 bio->bi_sector = sector;
453 bio_add_page(bio, page, size, 0);
454 init_completion(&event);
455 bio->bi_private = &event;
456 bio->bi_end_io = bi_complete;
457 submit_bio(rw, bio);
458 wait_for_completion(&event);
459
460 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
461 bio_put(bio);
462 return ret;
463}
464
NeilBrown0002b272005-09-09 16:23:53 -0700465static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 char b[BDEVNAME_SIZE];
468 if (!rdev->sb_page) {
469 MD_BUG();
470 return -EINVAL;
471 }
472 if (rdev->sb_loaded)
473 return 0;
474
475
NeilBrown0002b272005-09-09 16:23:53 -0700476 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto fail;
478 rdev->sb_loaded = 1;
479 return 0;
480
481fail:
482 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
483 bdevname(rdev->bdev,b));
484 return -EINVAL;
485}
486
487static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
488{
489 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
490 (sb1->set_uuid1 == sb2->set_uuid1) &&
491 (sb1->set_uuid2 == sb2->set_uuid2) &&
492 (sb1->set_uuid3 == sb2->set_uuid3))
493
494 return 1;
495
496 return 0;
497}
498
499
500static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
501{
502 int ret;
503 mdp_super_t *tmp1, *tmp2;
504
505 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
506 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
507
508 if (!tmp1 || !tmp2) {
509 ret = 0;
510 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
511 goto abort;
512 }
513
514 *tmp1 = *sb1;
515 *tmp2 = *sb2;
516
517 /*
518 * nr_disks is not constant
519 */
520 tmp1->nr_disks = 0;
521 tmp2->nr_disks = 0;
522
523 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
524 ret = 0;
525 else
526 ret = 1;
527
528abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700529 kfree(tmp1);
530 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return ret;
532}
533
534static unsigned int calc_sb_csum(mdp_super_t * sb)
535{
536 unsigned int disk_csum, csum;
537
538 disk_csum = sb->sb_csum;
539 sb->sb_csum = 0;
540 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
541 sb->sb_csum = disk_csum;
542 return csum;
543}
544
545
546/*
547 * Handle superblock details.
548 * We want to be able to handle multiple superblock formats
549 * so we have a common interface to them all, and an array of
550 * different handlers.
551 * We rely on user-space to write the initial superblock, and support
552 * reading and updating of superblocks.
553 * Interface methods are:
554 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
555 * loads and validates a superblock on dev.
556 * if refdev != NULL, compare superblocks on both devices
557 * Return:
558 * 0 - dev has a superblock that is compatible with refdev
559 * 1 - dev has a superblock that is compatible and newer than refdev
560 * so dev should be used as the refdev in future
561 * -EINVAL superblock incompatible or invalid
562 * -othererror e.g. -EIO
563 *
564 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
565 * Verify that dev is acceptable into mddev.
566 * The first time, mddev->raid_disks will be 0, and data from
567 * dev should be merged in. Subsequent calls check that dev
568 * is new enough. Return 0 or -EINVAL
569 *
570 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
571 * Update the superblock for rdev with data in mddev
572 * This does not write to disc.
573 *
574 */
575
576struct super_type {
577 char *name;
578 struct module *owner;
579 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
580 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
581 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
582};
583
584/*
585 * load_super for 0.90.0
586 */
587static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
588{
589 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
590 mdp_super_t *sb;
591 int ret;
592 sector_t sb_offset;
593
594 /*
595 * Calculate the position of the superblock,
596 * it's at the end of the disk.
597 *
598 * It also happens to be a multiple of 4Kb.
599 */
600 sb_offset = calc_dev_sboffset(rdev->bdev);
601 rdev->sb_offset = sb_offset;
602
NeilBrown0002b272005-09-09 16:23:53 -0700603 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (ret) return ret;
605
606 ret = -EINVAL;
607
608 bdevname(rdev->bdev, b);
609 sb = (mdp_super_t*)page_address(rdev->sb_page);
610
611 if (sb->md_magic != MD_SB_MAGIC) {
612 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
613 b);
614 goto abort;
615 }
616
617 if (sb->major_version != 0 ||
618 sb->minor_version != 90) {
619 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
620 sb->major_version, sb->minor_version,
621 b);
622 goto abort;
623 }
624
625 if (sb->raid_disks <= 0)
626 goto abort;
627
628 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
629 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
630 b);
631 goto abort;
632 }
633
634 rdev->preferred_minor = sb->md_minor;
635 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700636 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (sb->level == LEVEL_MULTIPATH)
639 rdev->desc_nr = -1;
640 else
641 rdev->desc_nr = sb->this_disk.number;
642
643 if (refdev == 0)
644 ret = 1;
645 else {
646 __u64 ev1, ev2;
647 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
648 if (!uuid_equal(refsb, sb)) {
649 printk(KERN_WARNING "md: %s has different UUID to %s\n",
650 b, bdevname(refdev->bdev,b2));
651 goto abort;
652 }
653 if (!sb_equal(refsb, sb)) {
654 printk(KERN_WARNING "md: %s has same UUID"
655 " but different superblock to %s\n",
656 b, bdevname(refdev->bdev, b2));
657 goto abort;
658 }
659 ev1 = md_event(sb);
660 ev2 = md_event(refsb);
661 if (ev1 > ev2)
662 ret = 1;
663 else
664 ret = 0;
665 }
666 rdev->size = calc_dev_size(rdev, sb->chunk_size);
667
668 abort:
669 return ret;
670}
671
672/*
673 * validate_super for 0.90.0
674 */
675static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
676{
677 mdp_disk_t *desc;
678 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
679
NeilBrown41158c72005-06-21 17:17:25 -0700680 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800681 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (mddev->raid_disks == 0) {
683 mddev->major_version = 0;
684 mddev->minor_version = sb->minor_version;
685 mddev->patch_version = sb->patch_version;
686 mddev->persistent = ! sb->not_persistent;
687 mddev->chunk_size = sb->chunk_size;
688 mddev->ctime = sb->ctime;
689 mddev->utime = sb->utime;
690 mddev->level = sb->level;
691 mddev->layout = sb->layout;
692 mddev->raid_disks = sb->raid_disks;
693 mddev->size = sb->size;
694 mddev->events = md_event(sb);
NeilBrown92232142005-08-18 11:24:16 -0700695 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700696 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 if (sb->state & (1<<MD_SB_CLEAN))
699 mddev->recovery_cp = MaxSector;
700 else {
701 if (sb->events_hi == sb->cp_events_hi &&
702 sb->events_lo == sb->cp_events_lo) {
703 mddev->recovery_cp = sb->recovery_cp;
704 } else
705 mddev->recovery_cp = 0;
706 }
707
708 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
709 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
710 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
711 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
712
713 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700714
715 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
716 mddev->bitmap_file == NULL) {
NeilBrown934ce7c2005-09-09 16:23:55 -0700717 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700718 /* FIXME use a better test */
719 printk(KERN_WARNING "md: bitmaps only support for raid1\n");
720 return -EINVAL;
721 }
NeilBrown36fa3062005-09-09 16:23:45 -0700722 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700723 }
724
NeilBrown41158c72005-06-21 17:17:25 -0700725 } else if (mddev->pers == NULL) {
726 /* Insist on good event counter while assembling */
727 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 ++ev1;
729 if (ev1 < mddev->events)
730 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700731 } else if (mddev->bitmap) {
732 /* if adding to array with a bitmap, then we can accept an
733 * older device ... but not too old.
734 */
735 __u64 ev1 = md_event(sb);
736 if (ev1 < mddev->bitmap->events_cleared)
737 return 0;
738 } else /* just a hot-add of a new device, leave raid_disk at -1 */
739 return 0;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 desc = sb->disks + rdev->desc_nr;
743
744 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800745 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 else if (desc->state & (1<<MD_DISK_SYNC) &&
747 desc->raid_disk < mddev->raid_disks) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800748 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 rdev->raid_disk = desc->raid_disk;
750 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700751 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
752 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700753 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800754 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
756}
757
758/*
759 * sync_super for 0.90.0
760 */
761static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
762{
763 mdp_super_t *sb;
764 struct list_head *tmp;
765 mdk_rdev_t *rdev2;
766 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /* make rdev->sb match mddev data..
770 *
771 * 1/ zero out disks
772 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
773 * 3/ any empty disks < next_spare become removed
774 *
775 * disks[0] gets initialised to REMOVED because
776 * we cannot be sure from other fields if it has
777 * been initialised or not.
778 */
779 int i;
780 int active=0, working=0,failed=0,spare=0,nr_disks=0;
781
NeilBrown61181562005-09-09 16:24:02 -0700782 rdev->sb_size = MD_SB_BYTES;
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 sb = (mdp_super_t*)page_address(rdev->sb_page);
785
786 memset(sb, 0, sizeof(*sb));
787
788 sb->md_magic = MD_SB_MAGIC;
789 sb->major_version = mddev->major_version;
790 sb->minor_version = mddev->minor_version;
791 sb->patch_version = mddev->patch_version;
792 sb->gvalid_words = 0; /* ignored */
793 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
794 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
795 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
796 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
797
798 sb->ctime = mddev->ctime;
799 sb->level = mddev->level;
800 sb->size = mddev->size;
801 sb->raid_disks = mddev->raid_disks;
802 sb->md_minor = mddev->md_minor;
803 sb->not_persistent = !mddev->persistent;
804 sb->utime = mddev->utime;
805 sb->state = 0;
806 sb->events_hi = (mddev->events>>32);
807 sb->events_lo = (u32)mddev->events;
808
809 if (mddev->in_sync)
810 {
811 sb->recovery_cp = mddev->recovery_cp;
812 sb->cp_events_hi = (mddev->events>>32);
813 sb->cp_events_lo = (u32)mddev->events;
814 if (mddev->recovery_cp == MaxSector)
815 sb->state = (1<< MD_SB_CLEAN);
816 } else
817 sb->recovery_cp = 0;
818
819 sb->layout = mddev->layout;
820 sb->chunk_size = mddev->chunk_size;
821
NeilBrowna654b9d82005-06-21 17:17:27 -0700822 if (mddev->bitmap && mddev->bitmap_file == NULL)
823 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 sb->disks[0].state = (1<<MD_DISK_REMOVED);
826 ITERATE_RDEV(mddev,rdev2,tmp) {
827 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800828 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800829 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
830 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800831 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800833 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800834 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 d = &sb->disks[rdev2->desc_nr];
836 nr_disks++;
837 d->number = rdev2->desc_nr;
838 d->major = MAJOR(rdev2->bdev->bd_dev);
839 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800840 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
841 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 d->raid_disk = rdev2->raid_disk;
843 else
844 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrownb2d444d2005-11-08 21:39:31 -0800845 if (test_bit(Faulty, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 d->state = (1<<MD_DISK_FAULTY);
847 failed++;
NeilBrownb2d444d2005-11-08 21:39:31 -0800848 } else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 d->state = (1<<MD_DISK_ACTIVE);
850 d->state |= (1<<MD_DISK_SYNC);
851 active++;
852 working++;
853 } else {
854 d->state = 0;
855 spare++;
856 working++;
857 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700858 if (test_bit(WriteMostly, &rdev2->flags))
859 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* now set the "removed" and "faulty" bits on any missing devices */
862 for (i=0 ; i < mddev->raid_disks ; i++) {
863 mdp_disk_t *d = &sb->disks[i];
864 if (d->state == 0 && d->number == 0) {
865 d->number = i;
866 d->raid_disk = i;
867 d->state = (1<<MD_DISK_REMOVED);
868 d->state |= (1<<MD_DISK_FAULTY);
869 failed++;
870 }
871 }
872 sb->nr_disks = nr_disks;
873 sb->active_disks = active;
874 sb->working_disks = working;
875 sb->failed_disks = failed;
876 sb->spare_disks = spare;
877
878 sb->this_disk = sb->disks[rdev->desc_nr];
879 sb->sb_csum = calc_sb_csum(sb);
880}
881
882/*
883 * version 1 superblock
884 */
885
886static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
887{
888 unsigned int disk_csum, csum;
889 unsigned long long newcsum;
890 int size = 256 + le32_to_cpu(sb->max_dev)*2;
891 unsigned int *isuper = (unsigned int*)sb;
892 int i;
893
894 disk_csum = sb->sb_csum;
895 sb->sb_csum = 0;
896 newcsum = 0;
897 for (i=0; size>=4; size -= 4 )
898 newcsum += le32_to_cpu(*isuper++);
899
900 if (size == 2)
901 newcsum += le16_to_cpu(*(unsigned short*) isuper);
902
903 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
904 sb->sb_csum = disk_csum;
905 return cpu_to_le32(csum);
906}
907
908static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
909{
910 struct mdp_superblock_1 *sb;
911 int ret;
912 sector_t sb_offset;
913 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -0700914 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 /*
917 * Calculate the position of the superblock.
918 * It is always aligned to a 4K boundary and
919 * depeding on minor_version, it can be:
920 * 0: At least 8K, but less than 12K, from end of device
921 * 1: At start of device
922 * 2: 4K from start of device.
923 */
924 switch(minor_version) {
925 case 0:
926 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
927 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -0700928 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* convert from sectors to K */
930 sb_offset /= 2;
931 break;
932 case 1:
933 sb_offset = 0;
934 break;
935 case 2:
936 sb_offset = 4;
937 break;
938 default:
939 return -EINVAL;
940 }
941 rdev->sb_offset = sb_offset;
942
NeilBrown0002b272005-09-09 16:23:53 -0700943 /* superblock is rarely larger than 1K, but it can be larger,
944 * and it is safe to read 4k, so we do that
945 */
946 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (ret) return ret;
948
949
950 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
951
952 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
953 sb->major_version != cpu_to_le32(1) ||
954 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
955 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -0700956 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return -EINVAL;
958
959 if (calc_sb_1_csum(sb) != sb->sb_csum) {
960 printk("md: invalid superblock checksum on %s\n",
961 bdevname(rdev->bdev,b));
962 return -EINVAL;
963 }
964 if (le64_to_cpu(sb->data_size) < 10) {
965 printk("md: data_size too small on %s\n",
966 bdevname(rdev->bdev,b));
967 return -EINVAL;
968 }
969 rdev->preferred_minor = 0xffff;
970 rdev->data_offset = le64_to_cpu(sb->data_offset);
971
NeilBrown0002b272005-09-09 16:23:53 -0700972 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -0700973 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -0700974 if (rdev->sb_size & bmask)
975 rdev-> sb_size = (rdev->sb_size | bmask)+1;
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (refdev == 0)
978 return 1;
979 else {
980 __u64 ev1, ev2;
981 struct mdp_superblock_1 *refsb =
982 (struct mdp_superblock_1*)page_address(refdev->sb_page);
983
984 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
985 sb->level != refsb->level ||
986 sb->layout != refsb->layout ||
987 sb->chunksize != refsb->chunksize) {
988 printk(KERN_WARNING "md: %s has strangely different"
989 " superblock to %s\n",
990 bdevname(rdev->bdev,b),
991 bdevname(refdev->bdev,b2));
992 return -EINVAL;
993 }
994 ev1 = le64_to_cpu(sb->events);
995 ev2 = le64_to_cpu(refsb->events);
996
997 if (ev1 > ev2)
998 return 1;
999 }
1000 if (minor_version)
1001 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1002 else
1003 rdev->size = rdev->sb_offset;
1004 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1005 return -EINVAL;
1006 rdev->size = le64_to_cpu(sb->data_size)/2;
1007 if (le32_to_cpu(sb->chunksize))
1008 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1009 return 0;
1010}
1011
1012static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1013{
1014 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1015
NeilBrown41158c72005-06-21 17:17:25 -07001016 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001017 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (mddev->raid_disks == 0) {
1019 mddev->major_version = 1;
1020 mddev->patch_version = 0;
1021 mddev->persistent = 1;
1022 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1023 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1024 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1025 mddev->level = le32_to_cpu(sb->level);
1026 mddev->layout = le32_to_cpu(sb->layout);
1027 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1028 mddev->size = le64_to_cpu(sb->size)/2;
1029 mddev->events = le64_to_cpu(sb->events);
NeilBrown92232142005-08-18 11:24:16 -07001030 mddev->bitmap_offset = 0;
NeilBrown53e87fb2005-09-09 16:23:58 -07001031 mddev->default_bitmap_offset = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1034 memcpy(mddev->uuid, sb->set_uuid, 16);
1035
1036 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001037
NeilBrown71c08052005-09-09 16:23:51 -07001038 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -07001039 mddev->bitmap_file == NULL ) {
1040 if (mddev->level != 1) {
1041 printk(KERN_WARNING "md: bitmaps only supported for raid1\n");
1042 return -EINVAL;
1043 }
1044 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1045 }
NeilBrown41158c72005-06-21 17:17:25 -07001046 } else if (mddev->pers == NULL) {
1047 /* Insist of good event counter while assembling */
1048 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ++ev1;
1050 if (ev1 < mddev->events)
1051 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001052 } else if (mddev->bitmap) {
1053 /* If adding to array with a bitmap, then we can accept an
1054 * older device, but not too old.
1055 */
1056 __u64 ev1 = le64_to_cpu(sb->events);
1057 if (ev1 < mddev->bitmap->events_cleared)
1058 return 0;
1059 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1060 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 if (mddev->level != LEVEL_MULTIPATH) {
1063 int role;
1064 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1065 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1066 switch(role) {
1067 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 break;
1069 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001070 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 break;
1072 default:
NeilBrownb2d444d2005-11-08 21:39:31 -08001073 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 rdev->raid_disk = role;
1075 break;
1076 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001077 if (sb->devflags & WriteMostly1)
1078 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001079 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001080 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return 0;
1083}
1084
1085static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1086{
1087 struct mdp_superblock_1 *sb;
1088 struct list_head *tmp;
1089 mdk_rdev_t *rdev2;
1090 int max_dev, i;
1091 /* make rdev->sb match mddev and rdev data. */
1092
1093 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1094
1095 sb->feature_map = 0;
1096 sb->pad0 = 0;
1097 memset(sb->pad1, 0, sizeof(sb->pad1));
1098 memset(sb->pad2, 0, sizeof(sb->pad2));
1099 memset(sb->pad3, 0, sizeof(sb->pad3));
1100
1101 sb->utime = cpu_to_le64((__u64)mddev->utime);
1102 sb->events = cpu_to_le64(mddev->events);
1103 if (mddev->in_sync)
1104 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1105 else
1106 sb->resync_offset = cpu_to_le64(0);
1107
NeilBrowna654b9d82005-06-21 17:17:27 -07001108 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1109 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001110 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001111 }
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 max_dev = 0;
1114 ITERATE_RDEV(mddev,rdev2,tmp)
1115 if (rdev2->desc_nr+1 > max_dev)
1116 max_dev = rdev2->desc_nr+1;
1117
1118 sb->max_dev = cpu_to_le32(max_dev);
1119 for (i=0; i<max_dev;i++)
1120 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1121
1122 ITERATE_RDEV(mddev,rdev2,tmp) {
1123 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001124 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001126 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1128 else
1129 sb->dev_roles[i] = cpu_to_le16(0xffff);
1130 }
1131
1132 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1133 sb->sb_csum = calc_sb_1_csum(sb);
1134}
1135
1136
Adrian Bunk75c96f82005-05-05 16:16:09 -07001137static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 [0] = {
1139 .name = "0.90.0",
1140 .owner = THIS_MODULE,
1141 .load_super = super_90_load,
1142 .validate_super = super_90_validate,
1143 .sync_super = super_90_sync,
1144 },
1145 [1] = {
1146 .name = "md-1",
1147 .owner = THIS_MODULE,
1148 .load_super = super_1_load,
1149 .validate_super = super_1_validate,
1150 .sync_super = super_1_sync,
1151 },
1152};
1153
1154static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1155{
1156 struct list_head *tmp;
1157 mdk_rdev_t *rdev;
1158
1159 ITERATE_RDEV(mddev,rdev,tmp)
1160 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1161 return rdev;
1162
1163 return NULL;
1164}
1165
1166static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1167{
1168 struct list_head *tmp;
1169 mdk_rdev_t *rdev;
1170
1171 ITERATE_RDEV(mddev1,rdev,tmp)
1172 if (match_dev_unit(mddev2, rdev))
1173 return 1;
1174
1175 return 0;
1176}
1177
1178static LIST_HEAD(pending_raid_disks);
1179
1180static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1181{
1182 mdk_rdev_t *same_pdev;
1183 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001184 struct kobject *ko;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 if (rdev->mddev) {
1187 MD_BUG();
1188 return -EINVAL;
1189 }
1190 same_pdev = match_dev_unit(mddev, rdev);
1191 if (same_pdev)
1192 printk(KERN_WARNING
1193 "%s: WARNING: %s appears to be on the same physical"
1194 " disk as %s. True\n protection against single-disk"
1195 " failure might be compromised.\n",
1196 mdname(mddev), bdevname(rdev->bdev,b),
1197 bdevname(same_pdev->bdev,b2));
1198
1199 /* Verify rdev->desc_nr is unique.
1200 * If it is -1, assign a free number, else
1201 * check number is not in use
1202 */
1203 if (rdev->desc_nr < 0) {
1204 int choice = 0;
1205 if (mddev->pers) choice = mddev->raid_disks;
1206 while (find_rdev_nr(mddev, choice))
1207 choice++;
1208 rdev->desc_nr = choice;
1209 } else {
1210 if (find_rdev_nr(mddev, rdev->desc_nr))
1211 return -EBUSY;
1212 }
NeilBrown19133a42005-11-08 21:39:35 -08001213 bdevname(rdev->bdev,b);
1214 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1215 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 list_add(&rdev->same_set, &mddev->disks);
1218 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001219 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001220
NeilBrown9c791972005-11-08 21:39:28 -08001221 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001222 kobject_add(&rdev->kobj);
1223
NeilBrownf637b9f2005-11-08 21:39:37 -08001224 if (rdev->bdev->bd_part)
1225 ko = &rdev->bdev->bd_part->kobj;
1226 else
1227 ko = &rdev->bdev->bd_disk->kobj;
1228 sysfs_create_link(&rdev->kobj, ko, "block");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return 0;
1230}
1231
1232static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1233{
1234 char b[BDEVNAME_SIZE];
1235 if (!rdev->mddev) {
1236 MD_BUG();
1237 return;
1238 }
1239 list_del_init(&rdev->same_set);
1240 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1241 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001242 sysfs_remove_link(&rdev->kobj, "block");
1243 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246/*
1247 * prevent the device from being mounted, repartitioned or
1248 * otherwise reused by a RAID array (or any other kernel
1249 * subsystem), by bd_claiming the device.
1250 */
1251static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1252{
1253 int err = 0;
1254 struct block_device *bdev;
1255 char b[BDEVNAME_SIZE];
1256
1257 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1258 if (IS_ERR(bdev)) {
1259 printk(KERN_ERR "md: could not open %s.\n",
1260 __bdevname(dev, b));
1261 return PTR_ERR(bdev);
1262 }
1263 err = bd_claim(bdev, rdev);
1264 if (err) {
1265 printk(KERN_ERR "md: could not bd_claim %s.\n",
1266 bdevname(bdev, b));
1267 blkdev_put(bdev);
1268 return err;
1269 }
1270 rdev->bdev = bdev;
1271 return err;
1272}
1273
1274static void unlock_rdev(mdk_rdev_t *rdev)
1275{
1276 struct block_device *bdev = rdev->bdev;
1277 rdev->bdev = NULL;
1278 if (!bdev)
1279 MD_BUG();
1280 bd_release(bdev);
1281 blkdev_put(bdev);
1282}
1283
1284void md_autodetect_dev(dev_t dev);
1285
1286static void export_rdev(mdk_rdev_t * rdev)
1287{
1288 char b[BDEVNAME_SIZE];
1289 printk(KERN_INFO "md: export_rdev(%s)\n",
1290 bdevname(rdev->bdev,b));
1291 if (rdev->mddev)
1292 MD_BUG();
1293 free_disk_sb(rdev);
1294 list_del_init(&rdev->same_set);
1295#ifndef MODULE
1296 md_autodetect_dev(rdev->bdev->bd_dev);
1297#endif
1298 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001299 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302static void kick_rdev_from_array(mdk_rdev_t * rdev)
1303{
1304 unbind_rdev_from_array(rdev);
1305 export_rdev(rdev);
1306}
1307
1308static void export_array(mddev_t *mddev)
1309{
1310 struct list_head *tmp;
1311 mdk_rdev_t *rdev;
1312
1313 ITERATE_RDEV(mddev,rdev,tmp) {
1314 if (!rdev->mddev) {
1315 MD_BUG();
1316 continue;
1317 }
1318 kick_rdev_from_array(rdev);
1319 }
1320 if (!list_empty(&mddev->disks))
1321 MD_BUG();
1322 mddev->raid_disks = 0;
1323 mddev->major_version = 0;
1324}
1325
1326static void print_desc(mdp_disk_t *desc)
1327{
1328 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1329 desc->major,desc->minor,desc->raid_disk,desc->state);
1330}
1331
1332static void print_sb(mdp_super_t *sb)
1333{
1334 int i;
1335
1336 printk(KERN_INFO
1337 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1338 sb->major_version, sb->minor_version, sb->patch_version,
1339 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1340 sb->ctime);
1341 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1342 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1343 sb->md_minor, sb->layout, sb->chunk_size);
1344 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1345 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1346 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1347 sb->failed_disks, sb->spare_disks,
1348 sb->sb_csum, (unsigned long)sb->events_lo);
1349
1350 printk(KERN_INFO);
1351 for (i = 0; i < MD_SB_DISKS; i++) {
1352 mdp_disk_t *desc;
1353
1354 desc = sb->disks + i;
1355 if (desc->number || desc->major || desc->minor ||
1356 desc->raid_disk || (desc->state && (desc->state != 4))) {
1357 printk(" D %2d: ", i);
1358 print_desc(desc);
1359 }
1360 }
1361 printk(KERN_INFO "md: THIS: ");
1362 print_desc(&sb->this_disk);
1363
1364}
1365
1366static void print_rdev(mdk_rdev_t *rdev)
1367{
1368 char b[BDEVNAME_SIZE];
1369 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1370 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001371 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1372 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (rdev->sb_loaded) {
1374 printk(KERN_INFO "md: rdev superblock:\n");
1375 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1376 } else
1377 printk(KERN_INFO "md: no rdev superblock!\n");
1378}
1379
1380void md_print_devices(void)
1381{
1382 struct list_head *tmp, *tmp2;
1383 mdk_rdev_t *rdev;
1384 mddev_t *mddev;
1385 char b[BDEVNAME_SIZE];
1386
1387 printk("\n");
1388 printk("md: **********************************\n");
1389 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1390 printk("md: **********************************\n");
1391 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
NeilBrown32a76272005-06-21 17:17:14 -07001393 if (mddev->bitmap)
1394 bitmap_print_sb(mddev->bitmap);
1395 else
1396 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 ITERATE_RDEV(mddev,rdev,tmp2)
1398 printk("<%s>", bdevname(rdev->bdev,b));
1399 printk("\n");
1400
1401 ITERATE_RDEV(mddev,rdev,tmp2)
1402 print_rdev(rdev);
1403 }
1404 printk("md: **********************************\n");
1405 printk("\n");
1406}
1407
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409static void sync_sbs(mddev_t * mddev)
1410{
1411 mdk_rdev_t *rdev;
1412 struct list_head *tmp;
1413
1414 ITERATE_RDEV(mddev,rdev,tmp) {
1415 super_types[mddev->major_version].
1416 sync_super(mddev, rdev);
1417 rdev->sb_loaded = 1;
1418 }
1419}
1420
1421static void md_update_sb(mddev_t * mddev)
1422{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001423 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 struct list_head *tmp;
1425 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001426 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001429 spin_lock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001430 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 mddev->utime = get_seconds();
1432 mddev->events ++;
1433
1434 if (!mddev->events) {
1435 /*
1436 * oops, this 64-bit counter should never wrap.
1437 * Either we are in around ~1 trillion A.C., assuming
1438 * 1 reboot per second, or we have a bug:
1439 */
1440 MD_BUG();
1441 mddev->events --;
1442 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001443 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 sync_sbs(mddev);
1445
1446 /*
1447 * do not write anything to disk if using
1448 * nonpersistent superblocks
1449 */
NeilBrown06d91a52005-06-21 17:17:12 -07001450 if (!mddev->persistent) {
1451 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001452 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001453 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001455 }
NeilBrowna9701a32005-11-08 21:39:34 -08001456 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 dprintk(KERN_INFO
1459 "md: updating %s RAID superblock on device (in sync %d)\n",
1460 mdname(mddev),mddev->in_sync);
1461
NeilBrown32a76272005-06-21 17:17:14 -07001462 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 ITERATE_RDEV(mddev,rdev,tmp) {
1464 char b[BDEVNAME_SIZE];
1465 dprintk(KERN_INFO "md: ");
NeilBrownb2d444d2005-11-08 21:39:31 -08001466 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 dprintk("(skipping faulty ");
1468
1469 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001470 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001471 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001472 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001473 rdev->sb_page);
1474 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1475 bdevname(rdev->bdev,b),
1476 (unsigned long long)rdev->sb_offset);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 } else
1479 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001480 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /* only need to write one superblock... */
1482 break;
1483 }
NeilBrowna9701a32005-11-08 21:39:34 -08001484 md_super_wait(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001485 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1486
NeilBrowna9701a32005-11-08 21:39:34 -08001487 spin_lock_irq(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001488 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001489 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001490 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001491 goto repeat;
1492 }
1493 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001494 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001495 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
NeilBrown86e6ffd2005-11-08 21:39:24 -08001499struct rdev_sysfs_entry {
1500 struct attribute attr;
1501 ssize_t (*show)(mdk_rdev_t *, char *);
1502 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1503};
1504
1505static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001506state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001507{
1508 char *sep = "";
1509 int len=0;
1510
NeilBrownb2d444d2005-11-08 21:39:31 -08001511 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001512 len+= sprintf(page+len, "%sfaulty",sep);
1513 sep = ",";
1514 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001515 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001516 len += sprintf(page+len, "%sin_sync",sep);
1517 sep = ",";
1518 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001519 if (!test_bit(Faulty, &rdev->flags) &&
1520 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001521 len += sprintf(page+len, "%sspare", sep);
1522 sep = ",";
1523 }
1524 return len+sprintf(page+len, "\n");
1525}
1526
NeilBrown96de1e62005-11-08 21:39:39 -08001527static struct rdev_sysfs_entry
1528rdev_state = __ATTR_RO(state);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001529
1530static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001531super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001532{
1533 if (rdev->sb_loaded && rdev->sb_size) {
1534 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1535 return rdev->sb_size;
1536 } else
1537 return 0;
1538}
NeilBrown96de1e62005-11-08 21:39:39 -08001539static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1540
NeilBrown86e6ffd2005-11-08 21:39:24 -08001541static struct attribute *rdev_default_attrs[] = {
1542 &rdev_state.attr,
1543 &rdev_super.attr,
1544 NULL,
1545};
1546static ssize_t
1547rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1548{
1549 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1550 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1551
1552 if (!entry->show)
1553 return -EIO;
1554 return entry->show(rdev, page);
1555}
1556
1557static ssize_t
1558rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1559 const char *page, size_t length)
1560{
1561 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1562 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1563
1564 if (!entry->store)
1565 return -EIO;
1566 return entry->store(rdev, page, length);
1567}
1568
1569static void rdev_free(struct kobject *ko)
1570{
1571 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1572 kfree(rdev);
1573}
1574static struct sysfs_ops rdev_sysfs_ops = {
1575 .show = rdev_attr_show,
1576 .store = rdev_attr_store,
1577};
1578static struct kobj_type rdev_ktype = {
1579 .release = rdev_free,
1580 .sysfs_ops = &rdev_sysfs_ops,
1581 .default_attrs = rdev_default_attrs,
1582};
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584/*
1585 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1586 *
1587 * mark the device faulty if:
1588 *
1589 * - the device is nonexistent (zero size)
1590 * - the device has no valid superblock
1591 *
1592 * a faulty rdev _never_ has rdev->sb set.
1593 */
1594static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1595{
1596 char b[BDEVNAME_SIZE];
1597 int err;
1598 mdk_rdev_t *rdev;
1599 sector_t size;
1600
1601 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1602 if (!rdev) {
1603 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1604 return ERR_PTR(-ENOMEM);
1605 }
1606 memset(rdev, 0, sizeof(*rdev));
1607
1608 if ((err = alloc_disk_sb(rdev)))
1609 goto abort_free;
1610
1611 err = lock_rdev(rdev, newdev);
1612 if (err)
1613 goto abort_free;
1614
NeilBrown86e6ffd2005-11-08 21:39:24 -08001615 rdev->kobj.parent = NULL;
1616 rdev->kobj.ktype = &rdev_ktype;
1617 kobject_init(&rdev->kobj);
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 rdev->desc_nr = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001620 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 rdev->data_offset = 0;
1622 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001623 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1626 if (!size) {
1627 printk(KERN_WARNING
1628 "md: %s has zero or unknown size, marking faulty!\n",
1629 bdevname(rdev->bdev,b));
1630 err = -EINVAL;
1631 goto abort_free;
1632 }
1633
1634 if (super_format >= 0) {
1635 err = super_types[super_format].
1636 load_super(rdev, NULL, super_minor);
1637 if (err == -EINVAL) {
1638 printk(KERN_WARNING
1639 "md: %s has invalid sb, not importing!\n",
1640 bdevname(rdev->bdev,b));
1641 goto abort_free;
1642 }
1643 if (err < 0) {
1644 printk(KERN_WARNING
1645 "md: could not read %s's sb, not importing!\n",
1646 bdevname(rdev->bdev,b));
1647 goto abort_free;
1648 }
1649 }
1650 INIT_LIST_HEAD(&rdev->same_set);
1651
1652 return rdev;
1653
1654abort_free:
1655 if (rdev->sb_page) {
1656 if (rdev->bdev)
1657 unlock_rdev(rdev);
1658 free_disk_sb(rdev);
1659 }
1660 kfree(rdev);
1661 return ERR_PTR(err);
1662}
1663
1664/*
1665 * Check a full RAID array for plausibility
1666 */
1667
1668
NeilBrowna757e642005-04-16 15:26:42 -07001669static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 int i;
1672 struct list_head *tmp;
1673 mdk_rdev_t *rdev, *freshest;
1674 char b[BDEVNAME_SIZE];
1675
1676 freshest = NULL;
1677 ITERATE_RDEV(mddev,rdev,tmp)
1678 switch (super_types[mddev->major_version].
1679 load_super(rdev, freshest, mddev->minor_version)) {
1680 case 1:
1681 freshest = rdev;
1682 break;
1683 case 0:
1684 break;
1685 default:
1686 printk( KERN_ERR \
1687 "md: fatal superblock inconsistency in %s"
1688 " -- removing from array\n",
1689 bdevname(rdev->bdev,b));
1690 kick_rdev_from_array(rdev);
1691 }
1692
1693
1694 super_types[mddev->major_version].
1695 validate_super(mddev, freshest);
1696
1697 i = 0;
1698 ITERATE_RDEV(mddev,rdev,tmp) {
1699 if (rdev != freshest)
1700 if (super_types[mddev->major_version].
1701 validate_super(mddev, rdev)) {
1702 printk(KERN_WARNING "md: kicking non-fresh %s"
1703 " from array!\n",
1704 bdevname(rdev->bdev,b));
1705 kick_rdev_from_array(rdev);
1706 continue;
1707 }
1708 if (mddev->level == LEVEL_MULTIPATH) {
1709 rdev->desc_nr = i++;
1710 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001711 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713 }
1714
1715
1716
1717 if (mddev->recovery_cp != MaxSector &&
1718 mddev->level >= 1)
1719 printk(KERN_ERR "md: %s: raid array is not clean"
1720 " -- starting background reconstruction\n",
1721 mdname(mddev));
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
NeilBrowneae17012005-11-08 21:39:23 -08001725static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001726level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08001727{
1728 mdk_personality_t *p = mddev->pers;
NeilBrownbb636542005-11-08 21:39:45 -08001729 if (p == NULL && mddev->raid_disks == 0)
NeilBrowneae17012005-11-08 21:39:23 -08001730 return 0;
1731 if (mddev->level >= 0)
Neil Brownbcb97942005-12-20 11:07:00 +11001732 return sprintf(page, "raid%d\n", mddev->level);
NeilBrowneae17012005-11-08 21:39:23 -08001733 else
1734 return sprintf(page, "%s\n", p->name);
1735}
1736
NeilBrown96de1e62005-11-08 21:39:39 -08001737static struct md_sysfs_entry md_level = __ATTR_RO(level);
NeilBrowneae17012005-11-08 21:39:23 -08001738
1739static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001740raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08001741{
NeilBrownbb636542005-11-08 21:39:45 -08001742 if (mddev->raid_disks == 0)
1743 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08001744 return sprintf(page, "%d\n", mddev->raid_disks);
1745}
1746
NeilBrown96de1e62005-11-08 21:39:39 -08001747static struct md_sysfs_entry md_raid_disks = __ATTR_RO(raid_disks);
NeilBrowneae17012005-11-08 21:39:23 -08001748
NeilBrown24dd4692005-11-08 21:39:26 -08001749static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08001750action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08001751{
NeilBrown7eec3142005-11-08 21:39:44 -08001752 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08001753 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1754 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
1755 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08001756 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1757 type = "resync";
1758 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1759 type = "check";
1760 else
1761 type = "repair";
1762 } else
1763 type = "recover";
1764 }
1765 return sprintf(page, "%s\n", type);
1766}
1767
1768static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08001769action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08001770{
NeilBrown7eec3142005-11-08 21:39:44 -08001771 if (!mddev->pers || !mddev->pers->sync_request)
1772 return -EINVAL;
1773
1774 if (strcmp(page, "idle")==0 || strcmp(page, "idle\n")==0) {
1775 if (mddev->sync_thread) {
1776 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1777 md_unregister_thread(mddev->sync_thread);
1778 mddev->sync_thread = NULL;
1779 mddev->recovery = 0;
1780 }
1781 return len;
1782 }
NeilBrown31399d92005-11-08 21:39:29 -08001783
1784 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1785 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08001786 return -EBUSY;
NeilBrown7eec3142005-11-08 21:39:44 -08001787 if (strcmp(page, "resync")==0 || strcmp(page, "resync\n")==0 ||
1788 strcmp(page, "recover")==0 || strcmp(page, "recover\n")==0)
1789 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1790 else {
1791 if (strcmp(page, "check")==0 || strcmp(page, "check\n")==0)
1792 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1793 else if (strcmp(page, "repair")!=0 && strcmp(page, "repair\n")!=0)
1794 return -EINVAL;
1795 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1796 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1797 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1798 }
NeilBrown24dd4692005-11-08 21:39:26 -08001799 md_wakeup_thread(mddev->thread);
1800 return len;
1801}
1802
NeilBrown9d888832005-11-08 21:39:26 -08001803static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001804mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08001805{
1806 return sprintf(page, "%llu\n",
1807 (unsigned long long) mddev->resync_mismatches);
1808}
1809
NeilBrown96de1e62005-11-08 21:39:39 -08001810static struct md_sysfs_entry
NeilBrown7eec3142005-11-08 21:39:44 -08001811md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08001812
NeilBrown96de1e62005-11-08 21:39:39 -08001813
1814static struct md_sysfs_entry
1815md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08001816
NeilBrowneae17012005-11-08 21:39:23 -08001817static struct attribute *md_default_attrs[] = {
1818 &md_level.attr,
1819 &md_raid_disks.attr,
NeilBrown411036f2005-11-08 21:39:40 -08001820 NULL,
1821};
1822
1823static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08001824 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08001825 &md_mismatches.attr,
NeilBrowneae17012005-11-08 21:39:23 -08001826 NULL,
1827};
NeilBrown411036f2005-11-08 21:39:40 -08001828static struct attribute_group md_redundancy_group = {
1829 .name = NULL,
1830 .attrs = md_redundancy_attrs,
1831};
1832
NeilBrowneae17012005-11-08 21:39:23 -08001833
1834static ssize_t
1835md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1836{
1837 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1838 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08001839 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08001840
1841 if (!entry->show)
1842 return -EIO;
NeilBrown96de1e62005-11-08 21:39:39 -08001843 mddev_lock(mddev);
1844 rv = entry->show(mddev, page);
1845 mddev_unlock(mddev);
1846 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08001847}
1848
1849static ssize_t
1850md_attr_store(struct kobject *kobj, struct attribute *attr,
1851 const char *page, size_t length)
1852{
1853 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1854 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08001855 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08001856
1857 if (!entry->store)
1858 return -EIO;
NeilBrown96de1e62005-11-08 21:39:39 -08001859 mddev_lock(mddev);
1860 rv = entry->store(mddev, page, length);
1861 mddev_unlock(mddev);
1862 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08001863}
1864
1865static void md_free(struct kobject *ko)
1866{
1867 mddev_t *mddev = container_of(ko, mddev_t, kobj);
1868 kfree(mddev);
1869}
1870
1871static struct sysfs_ops md_sysfs_ops = {
1872 .show = md_attr_show,
1873 .store = md_attr_store,
1874};
1875static struct kobj_type md_ktype = {
1876 .release = md_free,
1877 .sysfs_ops = &md_sysfs_ops,
1878 .default_attrs = md_default_attrs,
1879};
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881int mdp_major = 0;
1882
1883static struct kobject *md_probe(dev_t dev, int *part, void *data)
1884{
1885 static DECLARE_MUTEX(disks_sem);
1886 mddev_t *mddev = mddev_find(dev);
1887 struct gendisk *disk;
1888 int partitioned = (MAJOR(dev) != MD_MAJOR);
1889 int shift = partitioned ? MdpMinorShift : 0;
1890 int unit = MINOR(dev) >> shift;
1891
1892 if (!mddev)
1893 return NULL;
1894
1895 down(&disks_sem);
1896 if (mddev->gendisk) {
1897 up(&disks_sem);
1898 mddev_put(mddev);
1899 return NULL;
1900 }
1901 disk = alloc_disk(1 << shift);
1902 if (!disk) {
1903 up(&disks_sem);
1904 mddev_put(mddev);
1905 return NULL;
1906 }
1907 disk->major = MAJOR(dev);
1908 disk->first_minor = unit << shift;
1909 if (partitioned) {
1910 sprintf(disk->disk_name, "md_d%d", unit);
1911 sprintf(disk->devfs_name, "md/d%d", unit);
1912 } else {
1913 sprintf(disk->disk_name, "md%d", unit);
1914 sprintf(disk->devfs_name, "md/%d", unit);
1915 }
1916 disk->fops = &md_fops;
1917 disk->private_data = mddev;
1918 disk->queue = mddev->queue;
1919 add_disk(disk);
1920 mddev->gendisk = disk;
1921 up(&disks_sem);
NeilBrown9c791972005-11-08 21:39:28 -08001922 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08001923 mddev->kobj.k_name = NULL;
1924 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
1925 mddev->kobj.ktype = &md_ktype;
1926 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return NULL;
1928}
1929
1930void md_wakeup_thread(mdk_thread_t *thread);
1931
1932static void md_safemode_timeout(unsigned long data)
1933{
1934 mddev_t *mddev = (mddev_t *) data;
1935
1936 mddev->safemode = 1;
1937 md_wakeup_thread(mddev->thread);
1938}
1939
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08001940static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942static int do_md_run(mddev_t * mddev)
1943{
1944 int pnum, err;
1945 int chunk_size;
1946 struct list_head *tmp;
1947 mdk_rdev_t *rdev;
1948 struct gendisk *disk;
1949 char b[BDEVNAME_SIZE];
1950
NeilBrowna757e642005-04-16 15:26:42 -07001951 if (list_empty(&mddev->disks))
1952 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 if (mddev->pers)
1956 return -EBUSY;
1957
1958 /*
1959 * Analyze all RAID superblock(s)
1960 */
NeilBrowna757e642005-04-16 15:26:42 -07001961 if (!mddev->raid_disks)
1962 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 chunk_size = mddev->chunk_size;
1965 pnum = level_to_pers(mddev->level);
1966
1967 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1968 if (!chunk_size) {
1969 /*
1970 * 'default chunksize' in the old md code used to
1971 * be PAGE_SIZE, baaad.
1972 * we abort here to be on the safe side. We don't
1973 * want to continue the bad practice.
1974 */
1975 printk(KERN_ERR
1976 "no chunksize specified, see 'man raidtab'\n");
1977 return -EINVAL;
1978 }
1979 if (chunk_size > MAX_CHUNK_SIZE) {
1980 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1981 chunk_size, MAX_CHUNK_SIZE);
1982 return -EINVAL;
1983 }
1984 /*
1985 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1986 */
1987 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07001988 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return -EINVAL;
1990 }
1991 if (chunk_size < PAGE_SIZE) {
1992 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1993 chunk_size, PAGE_SIZE);
1994 return -EINVAL;
1995 }
1996
1997 /* devices must have minimum size of one chunk */
1998 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001999 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 continue;
2001 if (rdev->size < chunk_size / 1024) {
2002 printk(KERN_WARNING
2003 "md: Dev %s smaller than chunk_size:"
2004 " %lluk < %dk\n",
2005 bdevname(rdev->bdev,b),
2006 (unsigned long long)rdev->size,
2007 chunk_size / 1024);
2008 return -EINVAL;
2009 }
2010 }
2011 }
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013#ifdef CONFIG_KMOD
2014 if (!pers[pnum])
2015 {
2016 request_module("md-personality-%d", pnum);
2017 }
2018#endif
2019
2020 /*
2021 * Drop all container device buffers, from now on
2022 * the only valid external interface is through the md
2023 * device.
2024 * Also find largest hardsector size
2025 */
2026 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002027 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 continue;
2029 sync_blockdev(rdev->bdev);
2030 invalidate_bdev(rdev->bdev, 0);
2031 }
2032
2033 md_probe(mddev->unit, NULL, NULL);
2034 disk = mddev->gendisk;
2035 if (!disk)
2036 return -ENOMEM;
2037
2038 spin_lock(&pers_lock);
2039 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
2040 spin_unlock(&pers_lock);
2041 printk(KERN_WARNING "md: personality %d is not loaded!\n",
2042 pnum);
2043 return -EINVAL;
2044 }
2045
2046 mddev->pers = pers[pnum];
2047 spin_unlock(&pers_lock);
2048
NeilBrown657390d2005-08-26 18:34:16 -07002049 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08002051 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002052 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
NeilBrownf91de922005-11-08 21:39:36 -08002054 if (start_readonly)
2055 mddev->ro = 2; /* read-only, but switch on first write */
2056
NeilBrown32a76272005-06-21 17:17:14 -07002057 /* before we start the array running, initialise the bitmap */
2058 err = bitmap_create(mddev);
2059 if (err)
2060 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2061 mdname(mddev), err);
2062 else
2063 err = mddev->pers->run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (err) {
2065 printk(KERN_ERR "md: pers->run() failed ...\n");
2066 module_put(mddev->pers->owner);
2067 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002068 bitmap_destroy(mddev);
2069 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
NeilBrown411036f2005-11-08 21:39:40 -08002071 if (mddev->pers->sync_request)
2072 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08002073 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2074 mddev->ro = 0;
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 atomic_set(&mddev->writes_pending,0);
2077 mddev->safemode = 0;
2078 mddev->safemode_timer.function = md_safemode_timeout;
2079 mddev->safemode_timer.data = (unsigned long) mddev;
2080 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2081 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002082
2083 ITERATE_RDEV(mddev,rdev,tmp)
2084 if (rdev->raid_disk >= 0) {
2085 char nm[20];
2086 sprintf(nm, "rd%d", rdev->raid_disk);
2087 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002091 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 if (mddev->sb_dirty)
2094 md_update_sb(mddev);
2095
2096 set_capacity(disk, mddev->array_size<<1);
2097
2098 /* If we call blk_queue_make_request here, it will
2099 * re-initialise max_sectors etc which may have been
2100 * refined inside -> run. So just set the bits we need to set.
2101 * Most initialisation happended when we called
2102 * blk_queue_make_request(..., md_fail_request)
2103 * earlier.
2104 */
2105 mddev->queue->queuedata = mddev;
2106 mddev->queue->make_request_fn = mddev->pers->make_request;
2107
2108 mddev->changed = 1;
2109 return 0;
2110}
2111
2112static int restart_array(mddev_t *mddev)
2113{
2114 struct gendisk *disk = mddev->gendisk;
2115 int err;
2116
2117 /*
2118 * Complain if it has no devices
2119 */
2120 err = -ENXIO;
2121 if (list_empty(&mddev->disks))
2122 goto out;
2123
2124 if (mddev->pers) {
2125 err = -EBUSY;
2126 if (!mddev->ro)
2127 goto out;
2128
2129 mddev->safemode = 0;
2130 mddev->ro = 0;
2131 set_disk_ro(disk, 0);
2132
2133 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2134 mdname(mddev));
2135 /*
2136 * Kick recovery or resync if necessary
2137 */
2138 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2139 md_wakeup_thread(mddev->thread);
2140 err = 0;
2141 } else {
2142 printk(KERN_ERR "md: %s has no personality assigned.\n",
2143 mdname(mddev));
2144 err = -EINVAL;
2145 }
2146
2147out:
2148 return err;
2149}
2150
2151static int do_md_stop(mddev_t * mddev, int ro)
2152{
2153 int err = 0;
2154 struct gendisk *disk = mddev->gendisk;
2155
2156 if (mddev->pers) {
2157 if (atomic_read(&mddev->active)>2) {
2158 printk("md: %s still in use.\n",mdname(mddev));
2159 return -EBUSY;
2160 }
2161
2162 if (mddev->sync_thread) {
2163 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2164 md_unregister_thread(mddev->sync_thread);
2165 mddev->sync_thread = NULL;
2166 }
2167
2168 del_timer_sync(&mddev->safemode_timer);
2169
2170 invalidate_partition(disk, 0);
2171
2172 if (ro) {
2173 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08002174 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 goto out;
2176 mddev->ro = 1;
2177 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002178 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08002179 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (mddev->ro)
2181 set_disk_ro(disk, 0);
2182 blk_queue_make_request(mddev->queue, md_fail_request);
2183 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08002184 if (mddev->pers->sync_request)
2185 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 module_put(mddev->pers->owner);
2188 mddev->pers = NULL;
2189 if (mddev->ro)
2190 mddev->ro = 0;
2191 }
2192 if (!mddev->in_sync) {
2193 /* mark array as shutdown cleanly */
2194 mddev->in_sync = 1;
2195 md_update_sb(mddev);
2196 }
2197 if (ro)
2198 set_disk_ro(disk, 1);
2199 }
NeilBrown32a76272005-06-21 17:17:14 -07002200
2201 bitmap_destroy(mddev);
2202 if (mddev->bitmap_file) {
2203 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2204 fput(mddev->bitmap_file);
2205 mddev->bitmap_file = NULL;
2206 }
NeilBrown92232142005-08-18 11:24:16 -07002207 mddev->bitmap_offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /*
2210 * Free resources if final stop
2211 */
2212 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002213 mdk_rdev_t *rdev;
2214 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 struct gendisk *disk;
2216 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2217
NeilBrown86e6ffd2005-11-08 21:39:24 -08002218 ITERATE_RDEV(mddev,rdev,tmp)
2219 if (rdev->raid_disk >= 0) {
2220 char nm[20];
2221 sprintf(nm, "rd%d", rdev->raid_disk);
2222 sysfs_remove_link(&mddev->kobj, nm);
2223 }
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 export_array(mddev);
2226
2227 mddev->array_size = 0;
2228 disk = mddev->gendisk;
2229 if (disk)
2230 set_capacity(disk, 0);
2231 mddev->changed = 1;
2232 } else
2233 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2234 mdname(mddev));
2235 err = 0;
2236out:
2237 return err;
2238}
2239
2240static void autorun_array(mddev_t *mddev)
2241{
2242 mdk_rdev_t *rdev;
2243 struct list_head *tmp;
2244 int err;
2245
NeilBrowna757e642005-04-16 15:26:42 -07002246 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249 printk(KERN_INFO "md: running: ");
2250
2251 ITERATE_RDEV(mddev,rdev,tmp) {
2252 char b[BDEVNAME_SIZE];
2253 printk("<%s>", bdevname(rdev->bdev,b));
2254 }
2255 printk("\n");
2256
2257 err = do_md_run (mddev);
2258 if (err) {
2259 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2260 do_md_stop (mddev, 0);
2261 }
2262}
2263
2264/*
2265 * lets try to run arrays based on all disks that have arrived
2266 * until now. (those are in pending_raid_disks)
2267 *
2268 * the method: pick the first pending disk, collect all disks with
2269 * the same UUID, remove all from the pending list and put them into
2270 * the 'same_array' list. Then order this list based on superblock
2271 * update time (freshest comes first), kick out 'old' disks and
2272 * compare superblocks. If everything's fine then run it.
2273 *
2274 * If "unit" is allocated, then bump its reference count
2275 */
2276static void autorun_devices(int part)
2277{
2278 struct list_head candidates;
2279 struct list_head *tmp;
2280 mdk_rdev_t *rdev0, *rdev;
2281 mddev_t *mddev;
2282 char b[BDEVNAME_SIZE];
2283
2284 printk(KERN_INFO "md: autorun ...\n");
2285 while (!list_empty(&pending_raid_disks)) {
2286 dev_t dev;
2287 rdev0 = list_entry(pending_raid_disks.next,
2288 mdk_rdev_t, same_set);
2289
2290 printk(KERN_INFO "md: considering %s ...\n",
2291 bdevname(rdev0->bdev,b));
2292 INIT_LIST_HEAD(&candidates);
2293 ITERATE_RDEV_PENDING(rdev,tmp)
2294 if (super_90_load(rdev, rdev0, 0) >= 0) {
2295 printk(KERN_INFO "md: adding %s ...\n",
2296 bdevname(rdev->bdev,b));
2297 list_move(&rdev->same_set, &candidates);
2298 }
2299 /*
2300 * now we have a set of devices, with all of them having
2301 * mostly sane superblocks. It's time to allocate the
2302 * mddev.
2303 */
2304 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2305 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2306 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2307 break;
2308 }
2309 if (part)
2310 dev = MKDEV(mdp_major,
2311 rdev0->preferred_minor << MdpMinorShift);
2312 else
2313 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2314
2315 md_probe(dev, NULL, NULL);
2316 mddev = mddev_find(dev);
2317 if (!mddev) {
2318 printk(KERN_ERR
2319 "md: cannot allocate memory for md drive.\n");
2320 break;
2321 }
2322 if (mddev_lock(mddev))
2323 printk(KERN_WARNING "md: %s locked, cannot run\n",
2324 mdname(mddev));
2325 else if (mddev->raid_disks || mddev->major_version
2326 || !list_empty(&mddev->disks)) {
2327 printk(KERN_WARNING
2328 "md: %s already running, cannot run %s\n",
2329 mdname(mddev), bdevname(rdev0->bdev,b));
2330 mddev_unlock(mddev);
2331 } else {
2332 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2333 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2334 list_del_init(&rdev->same_set);
2335 if (bind_rdev_to_array(rdev, mddev))
2336 export_rdev(rdev);
2337 }
2338 autorun_array(mddev);
2339 mddev_unlock(mddev);
2340 }
2341 /* on success, candidates will be empty, on error
2342 * it won't...
2343 */
2344 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2345 export_rdev(rdev);
2346 mddev_put(mddev);
2347 }
2348 printk(KERN_INFO "md: ... autorun DONE.\n");
2349}
2350
2351/*
2352 * import RAID devices based on one partition
2353 * if possible, the array gets run as well.
2354 */
2355
2356static int autostart_array(dev_t startdev)
2357{
2358 char b[BDEVNAME_SIZE];
2359 int err = -EINVAL, i;
2360 mdp_super_t *sb = NULL;
2361 mdk_rdev_t *start_rdev = NULL, *rdev;
2362
2363 start_rdev = md_import_device(startdev, 0, 0);
2364 if (IS_ERR(start_rdev))
2365 return err;
2366
2367
2368 /* NOTE: this can only work for 0.90.0 superblocks */
2369 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2370 if (sb->major_version != 0 ||
2371 sb->minor_version != 90 ) {
2372 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2373 export_rdev(start_rdev);
2374 return err;
2375 }
2376
NeilBrownb2d444d2005-11-08 21:39:31 -08002377 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 printk(KERN_WARNING
2379 "md: can not autostart based on faulty %s!\n",
2380 bdevname(start_rdev->bdev,b));
2381 export_rdev(start_rdev);
2382 return err;
2383 }
2384 list_add(&start_rdev->same_set, &pending_raid_disks);
2385
2386 for (i = 0; i < MD_SB_DISKS; i++) {
2387 mdp_disk_t *desc = sb->disks + i;
2388 dev_t dev = MKDEV(desc->major, desc->minor);
2389
2390 if (!dev)
2391 continue;
2392 if (dev == startdev)
2393 continue;
2394 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2395 continue;
2396 rdev = md_import_device(dev, 0, 0);
2397 if (IS_ERR(rdev))
2398 continue;
2399
2400 list_add(&rdev->same_set, &pending_raid_disks);
2401 }
2402
2403 /*
2404 * possibly return codes
2405 */
2406 autorun_devices(0);
2407 return 0;
2408
2409}
2410
2411
2412static int get_version(void __user * arg)
2413{
2414 mdu_version_t ver;
2415
2416 ver.major = MD_MAJOR_VERSION;
2417 ver.minor = MD_MINOR_VERSION;
2418 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2419
2420 if (copy_to_user(arg, &ver, sizeof(ver)))
2421 return -EFAULT;
2422
2423 return 0;
2424}
2425
2426static int get_array_info(mddev_t * mddev, void __user * arg)
2427{
2428 mdu_array_info_t info;
2429 int nr,working,active,failed,spare;
2430 mdk_rdev_t *rdev;
2431 struct list_head *tmp;
2432
2433 nr=working=active=failed=spare=0;
2434 ITERATE_RDEV(mddev,rdev,tmp) {
2435 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002436 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 failed++;
2438 else {
2439 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002440 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 active++;
2442 else
2443 spare++;
2444 }
2445 }
2446
2447 info.major_version = mddev->major_version;
2448 info.minor_version = mddev->minor_version;
2449 info.patch_version = MD_PATCHLEVEL_VERSION;
2450 info.ctime = mddev->ctime;
2451 info.level = mddev->level;
2452 info.size = mddev->size;
2453 info.nr_disks = nr;
2454 info.raid_disks = mddev->raid_disks;
2455 info.md_minor = mddev->md_minor;
2456 info.not_persistent= !mddev->persistent;
2457
2458 info.utime = mddev->utime;
2459 info.state = 0;
2460 if (mddev->in_sync)
2461 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07002462 if (mddev->bitmap && mddev->bitmap_offset)
2463 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 info.active_disks = active;
2465 info.working_disks = working;
2466 info.failed_disks = failed;
2467 info.spare_disks = spare;
2468
2469 info.layout = mddev->layout;
2470 info.chunk_size = mddev->chunk_size;
2471
2472 if (copy_to_user(arg, &info, sizeof(info)))
2473 return -EFAULT;
2474
2475 return 0;
2476}
2477
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002478static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07002479{
2480 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2481 char *ptr, *buf = NULL;
2482 int err = -ENOMEM;
2483
2484 file = kmalloc(sizeof(*file), GFP_KERNEL);
2485 if (!file)
2486 goto out;
2487
2488 /* bitmap disabled, zero the first byte and copy out */
2489 if (!mddev->bitmap || !mddev->bitmap->file) {
2490 file->pathname[0] = '\0';
2491 goto copy_out;
2492 }
2493
2494 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2495 if (!buf)
2496 goto out;
2497
2498 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2499 if (!ptr)
2500 goto out;
2501
2502 strcpy(file->pathname, ptr);
2503
2504copy_out:
2505 err = 0;
2506 if (copy_to_user(arg, file, sizeof(*file)))
2507 err = -EFAULT;
2508out:
2509 kfree(buf);
2510 kfree(file);
2511 return err;
2512}
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514static int get_disk_info(mddev_t * mddev, void __user * arg)
2515{
2516 mdu_disk_info_t info;
2517 unsigned int nr;
2518 mdk_rdev_t *rdev;
2519
2520 if (copy_from_user(&info, arg, sizeof(info)))
2521 return -EFAULT;
2522
2523 nr = info.number;
2524
2525 rdev = find_rdev_nr(mddev, nr);
2526 if (rdev) {
2527 info.major = MAJOR(rdev->bdev->bd_dev);
2528 info.minor = MINOR(rdev->bdev->bd_dev);
2529 info.raid_disk = rdev->raid_disk;
2530 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08002531 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002533 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 info.state |= (1<<MD_DISK_ACTIVE);
2535 info.state |= (1<<MD_DISK_SYNC);
2536 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002537 if (test_bit(WriteMostly, &rdev->flags))
2538 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 } else {
2540 info.major = info.minor = 0;
2541 info.raid_disk = -1;
2542 info.state = (1<<MD_DISK_REMOVED);
2543 }
2544
2545 if (copy_to_user(arg, &info, sizeof(info)))
2546 return -EFAULT;
2547
2548 return 0;
2549}
2550
2551static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2552{
2553 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2554 mdk_rdev_t *rdev;
2555 dev_t dev = MKDEV(info->major,info->minor);
2556
2557 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2558 return -EOVERFLOW;
2559
2560 if (!mddev->raid_disks) {
2561 int err;
2562 /* expecting a device which has a superblock */
2563 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2564 if (IS_ERR(rdev)) {
2565 printk(KERN_WARNING
2566 "md: md_import_device returned %ld\n",
2567 PTR_ERR(rdev));
2568 return PTR_ERR(rdev);
2569 }
2570 if (!list_empty(&mddev->disks)) {
2571 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2572 mdk_rdev_t, same_set);
2573 int err = super_types[mddev->major_version]
2574 .load_super(rdev, rdev0, mddev->minor_version);
2575 if (err < 0) {
2576 printk(KERN_WARNING
2577 "md: %s has different UUID to %s\n",
2578 bdevname(rdev->bdev,b),
2579 bdevname(rdev0->bdev,b2));
2580 export_rdev(rdev);
2581 return -EINVAL;
2582 }
2583 }
2584 err = bind_rdev_to_array(rdev, mddev);
2585 if (err)
2586 export_rdev(rdev);
2587 return err;
2588 }
2589
2590 /*
2591 * add_new_disk can be used once the array is assembled
2592 * to add "hot spares". They must already have a superblock
2593 * written
2594 */
2595 if (mddev->pers) {
2596 int err;
2597 if (!mddev->pers->hot_add_disk) {
2598 printk(KERN_WARNING
2599 "%s: personality does not support diskops!\n",
2600 mdname(mddev));
2601 return -EINVAL;
2602 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07002603 if (mddev->persistent)
2604 rdev = md_import_device(dev, mddev->major_version,
2605 mddev->minor_version);
2606 else
2607 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if (IS_ERR(rdev)) {
2609 printk(KERN_WARNING
2610 "md: md_import_device returned %ld\n",
2611 PTR_ERR(rdev));
2612 return PTR_ERR(rdev);
2613 }
NeilBrown41158c72005-06-21 17:17:25 -07002614 /* set save_raid_disk if appropriate */
2615 if (!mddev->persistent) {
2616 if (info->state & (1<<MD_DISK_SYNC) &&
2617 info->raid_disk < mddev->raid_disks)
2618 rdev->raid_disk = info->raid_disk;
2619 else
2620 rdev->raid_disk = -1;
2621 } else
2622 super_types[mddev->major_version].
2623 validate_super(mddev, rdev);
2624 rdev->saved_raid_disk = rdev->raid_disk;
2625
NeilBrownb2d444d2005-11-08 21:39:31 -08002626 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002627 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2628 set_bit(WriteMostly, &rdev->flags);
2629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 rdev->raid_disk = -1;
2631 err = bind_rdev_to_array(rdev, mddev);
2632 if (err)
2633 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07002634
2635 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002636 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return err;
2638 }
2639
2640 /* otherwise, add_new_disk is only allowed
2641 * for major_version==0 superblocks
2642 */
2643 if (mddev->major_version != 0) {
2644 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2645 mdname(mddev));
2646 return -EINVAL;
2647 }
2648
2649 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2650 int err;
2651 rdev = md_import_device (dev, -1, 0);
2652 if (IS_ERR(rdev)) {
2653 printk(KERN_WARNING
2654 "md: error, md_import_device() returned %ld\n",
2655 PTR_ERR(rdev));
2656 return PTR_ERR(rdev);
2657 }
2658 rdev->desc_nr = info->number;
2659 if (info->raid_disk < mddev->raid_disks)
2660 rdev->raid_disk = info->raid_disk;
2661 else
2662 rdev->raid_disk = -1;
2663
NeilBrownb2d444d2005-11-08 21:39:31 -08002664 rdev->flags = 0;
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08002667 if (info->state & (1<<MD_DISK_SYNC))
2668 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002670 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2671 set_bit(WriteMostly, &rdev->flags);
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 err = bind_rdev_to_array(rdev, mddev);
2674 if (err) {
2675 export_rdev(rdev);
2676 return err;
2677 }
2678
2679 if (!mddev->persistent) {
2680 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2681 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2682 } else
2683 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2684 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2685
2686 if (!mddev->size || (mddev->size > rdev->size))
2687 mddev->size = rdev->size;
2688 }
2689
2690 return 0;
2691}
2692
2693static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2694{
2695 char b[BDEVNAME_SIZE];
2696 mdk_rdev_t *rdev;
2697
2698 if (!mddev->pers)
2699 return -ENODEV;
2700
2701 rdev = find_rdev(mddev, dev);
2702 if (!rdev)
2703 return -ENXIO;
2704
2705 if (rdev->raid_disk >= 0)
2706 goto busy;
2707
2708 kick_rdev_from_array(rdev);
2709 md_update_sb(mddev);
2710
2711 return 0;
2712busy:
2713 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2714 bdevname(rdev->bdev,b), mdname(mddev));
2715 return -EBUSY;
2716}
2717
2718static int hot_add_disk(mddev_t * mddev, dev_t dev)
2719{
2720 char b[BDEVNAME_SIZE];
2721 int err;
2722 unsigned int size;
2723 mdk_rdev_t *rdev;
2724
2725 if (!mddev->pers)
2726 return -ENODEV;
2727
2728 if (mddev->major_version != 0) {
2729 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2730 " version-0 superblocks.\n",
2731 mdname(mddev));
2732 return -EINVAL;
2733 }
2734 if (!mddev->pers->hot_add_disk) {
2735 printk(KERN_WARNING
2736 "%s: personality does not support diskops!\n",
2737 mdname(mddev));
2738 return -EINVAL;
2739 }
2740
2741 rdev = md_import_device (dev, -1, 0);
2742 if (IS_ERR(rdev)) {
2743 printk(KERN_WARNING
2744 "md: error, md_import_device() returned %ld\n",
2745 PTR_ERR(rdev));
2746 return -EINVAL;
2747 }
2748
2749 if (mddev->persistent)
2750 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2751 else
2752 rdev->sb_offset =
2753 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2754
2755 size = calc_dev_size(rdev, mddev->chunk_size);
2756 rdev->size = size;
2757
2758 if (size < mddev->size) {
2759 printk(KERN_WARNING
2760 "%s: disk size %llu blocks < array size %llu\n",
2761 mdname(mddev), (unsigned long long)size,
2762 (unsigned long long)mddev->size);
2763 err = -ENOSPC;
2764 goto abort_export;
2765 }
2766
NeilBrownb2d444d2005-11-08 21:39:31 -08002767 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 printk(KERN_WARNING
2769 "md: can not hot-add faulty %s disk to %s!\n",
2770 bdevname(rdev->bdev,b), mdname(mddev));
2771 err = -EINVAL;
2772 goto abort_export;
2773 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002774 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 rdev->desc_nr = -1;
2776 bind_rdev_to_array(rdev, mddev);
2777
2778 /*
2779 * The rest should better be atomic, we can have disk failures
2780 * noticed in interrupt contexts ...
2781 */
2782
2783 if (rdev->desc_nr == mddev->max_disks) {
2784 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2785 mdname(mddev));
2786 err = -EBUSY;
2787 goto abort_unbind_export;
2788 }
2789
2790 rdev->raid_disk = -1;
2791
2792 md_update_sb(mddev);
2793
2794 /*
2795 * Kick recovery, maybe this spare has to be added to the
2796 * array immediately.
2797 */
2798 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2799 md_wakeup_thread(mddev->thread);
2800
2801 return 0;
2802
2803abort_unbind_export:
2804 unbind_rdev_from_array(rdev);
2805
2806abort_export:
2807 export_rdev(rdev);
2808 return err;
2809}
2810
NeilBrown32a76272005-06-21 17:17:14 -07002811/* similar to deny_write_access, but accounts for our holding a reference
2812 * to the file ourselves */
2813static int deny_bitmap_write_access(struct file * file)
2814{
2815 struct inode *inode = file->f_mapping->host;
2816
2817 spin_lock(&inode->i_lock);
2818 if (atomic_read(&inode->i_writecount) > 1) {
2819 spin_unlock(&inode->i_lock);
2820 return -ETXTBSY;
2821 }
2822 atomic_set(&inode->i_writecount, -1);
2823 spin_unlock(&inode->i_lock);
2824
2825 return 0;
2826}
2827
2828static int set_bitmap_file(mddev_t *mddev, int fd)
2829{
2830 int err;
2831
NeilBrown36fa3062005-09-09 16:23:45 -07002832 if (mddev->pers) {
2833 if (!mddev->pers->quiesce)
2834 return -EBUSY;
2835 if (mddev->recovery || mddev->sync_thread)
2836 return -EBUSY;
2837 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07002838 }
2839
NeilBrown36fa3062005-09-09 16:23:45 -07002840
2841 if (fd >= 0) {
2842 if (mddev->bitmap)
2843 return -EEXIST; /* cannot add when bitmap is present */
2844 mddev->bitmap_file = fget(fd);
2845
2846 if (mddev->bitmap_file == NULL) {
2847 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2848 mdname(mddev));
2849 return -EBADF;
2850 }
2851
2852 err = deny_bitmap_write_access(mddev->bitmap_file);
2853 if (err) {
2854 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2855 mdname(mddev));
2856 fput(mddev->bitmap_file);
2857 mddev->bitmap_file = NULL;
2858 return err;
2859 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002860 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07002861 } else if (mddev->bitmap == NULL)
2862 return -ENOENT; /* cannot remove what isn't there */
2863 err = 0;
2864 if (mddev->pers) {
2865 mddev->pers->quiesce(mddev, 1);
2866 if (fd >= 0)
2867 err = bitmap_create(mddev);
2868 if (fd < 0 || err)
2869 bitmap_destroy(mddev);
2870 mddev->pers->quiesce(mddev, 0);
2871 } else if (fd < 0) {
2872 if (mddev->bitmap_file)
2873 fput(mddev->bitmap_file);
2874 mddev->bitmap_file = NULL;
2875 }
2876
NeilBrown32a76272005-06-21 17:17:14 -07002877 return err;
2878}
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880/*
2881 * set_array_info is used two different ways
2882 * The original usage is when creating a new array.
2883 * In this usage, raid_disks is > 0 and it together with
2884 * level, size, not_persistent,layout,chunksize determine the
2885 * shape of the array.
2886 * This will always create an array with a type-0.90.0 superblock.
2887 * The newer usage is when assembling an array.
2888 * In this case raid_disks will be 0, and the major_version field is
2889 * use to determine which style super-blocks are to be found on the devices.
2890 * The minor and patch _version numbers are also kept incase the
2891 * super_block handler wishes to interpret them.
2892 */
2893static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2894{
2895
2896 if (info->raid_disks == 0) {
2897 /* just setting version number for superblock loading */
2898 if (info->major_version < 0 ||
2899 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2900 super_types[info->major_version].name == NULL) {
2901 /* maybe try to auto-load a module? */
2902 printk(KERN_INFO
2903 "md: superblock version %d not known\n",
2904 info->major_version);
2905 return -EINVAL;
2906 }
2907 mddev->major_version = info->major_version;
2908 mddev->minor_version = info->minor_version;
2909 mddev->patch_version = info->patch_version;
2910 return 0;
2911 }
2912 mddev->major_version = MD_MAJOR_VERSION;
2913 mddev->minor_version = MD_MINOR_VERSION;
2914 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2915 mddev->ctime = get_seconds();
2916
2917 mddev->level = info->level;
2918 mddev->size = info->size;
2919 mddev->raid_disks = info->raid_disks;
2920 /* don't set md_minor, it is determined by which /dev/md* was
2921 * openned
2922 */
2923 if (info->state & (1<<MD_SB_CLEAN))
2924 mddev->recovery_cp = MaxSector;
2925 else
2926 mddev->recovery_cp = 0;
2927 mddev->persistent = ! info->not_persistent;
2928
2929 mddev->layout = info->layout;
2930 mddev->chunk_size = info->chunk_size;
2931
2932 mddev->max_disks = MD_SB_DISKS;
2933
2934 mddev->sb_dirty = 1;
2935
NeilBrownb2a27032005-11-28 13:44:12 -08002936 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
2937 mddev->bitmap_offset = 0;
2938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 /*
2940 * Generate a 128 bit UUID
2941 */
2942 get_random_bytes(mddev->uuid, 16);
2943
2944 return 0;
2945}
2946
2947/*
2948 * update_array_info is used to change the configuration of an
2949 * on-line array.
2950 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2951 * fields in the info are checked against the array.
2952 * Any differences that cannot be handled will cause an error.
2953 * Normally, only one change can be managed at a time.
2954 */
2955static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2956{
2957 int rv = 0;
2958 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07002959 int state = 0;
2960
2961 /* calculate expected state,ignoring low bits */
2962 if (mddev->bitmap && mddev->bitmap_offset)
2963 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
2965 if (mddev->major_version != info->major_version ||
2966 mddev->minor_version != info->minor_version ||
2967/* mddev->patch_version != info->patch_version || */
2968 mddev->ctime != info->ctime ||
2969 mddev->level != info->level ||
2970/* mddev->layout != info->layout || */
2971 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07002972 mddev->chunk_size != info->chunk_size ||
2973 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2974 ((state^info->state) & 0xfffffe00)
2975 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return -EINVAL;
2977 /* Check there is only one change */
2978 if (mddev->size != info->size) cnt++;
2979 if (mddev->raid_disks != info->raid_disks) cnt++;
2980 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07002981 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 if (cnt == 0) return 0;
2983 if (cnt > 1) return -EINVAL;
2984
2985 if (mddev->layout != info->layout) {
2986 /* Change layout
2987 * we don't need to do anything at the md level, the
2988 * personality will take care of it all.
2989 */
2990 if (mddev->pers->reconfig == NULL)
2991 return -EINVAL;
2992 else
2993 return mddev->pers->reconfig(mddev, info->layout, -1);
2994 }
2995 if (mddev->size != info->size) {
2996 mdk_rdev_t * rdev;
2997 struct list_head *tmp;
2998 if (mddev->pers->resize == NULL)
2999 return -EINVAL;
3000 /* The "size" is the amount of each device that is used.
3001 * This can only make sense for arrays with redundancy.
3002 * linear and raid0 always use whatever space is available
3003 * We can only consider changing the size if no resync
3004 * or reconstruction is happening, and if the new size
3005 * is acceptable. It must fit before the sb_offset or,
3006 * if that is <data_offset, it must fit before the
3007 * size of each device.
3008 * If size is zero, we find the largest size that fits.
3009 */
3010 if (mddev->sync_thread)
3011 return -EBUSY;
3012 ITERATE_RDEV(mddev,rdev,tmp) {
3013 sector_t avail;
3014 int fit = (info->size == 0);
3015 if (rdev->sb_offset > rdev->data_offset)
3016 avail = (rdev->sb_offset*2) - rdev->data_offset;
3017 else
3018 avail = get_capacity(rdev->bdev->bd_disk)
3019 - rdev->data_offset;
3020 if (fit && (info->size == 0 || info->size > avail/2))
3021 info->size = avail/2;
3022 if (avail < ((sector_t)info->size << 1))
3023 return -ENOSPC;
3024 }
3025 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
3026 if (!rv) {
3027 struct block_device *bdev;
3028
3029 bdev = bdget_disk(mddev->gendisk, 0);
3030 if (bdev) {
3031 down(&bdev->bd_inode->i_sem);
3032 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3033 up(&bdev->bd_inode->i_sem);
3034 bdput(bdev);
3035 }
3036 }
3037 }
3038 if (mddev->raid_disks != info->raid_disks) {
3039 /* change the number of raid disks */
3040 if (mddev->pers->reshape == NULL)
3041 return -EINVAL;
3042 if (info->raid_disks <= 0 ||
3043 info->raid_disks >= mddev->max_disks)
3044 return -EINVAL;
3045 if (mddev->sync_thread)
3046 return -EBUSY;
3047 rv = mddev->pers->reshape(mddev, info->raid_disks);
3048 if (!rv) {
3049 struct block_device *bdev;
3050
3051 bdev = bdget_disk(mddev->gendisk, 0);
3052 if (bdev) {
3053 down(&bdev->bd_inode->i_sem);
3054 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3055 up(&bdev->bd_inode->i_sem);
3056 bdput(bdev);
3057 }
3058 }
3059 }
NeilBrown36fa3062005-09-09 16:23:45 -07003060 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3061 if (mddev->pers->quiesce == NULL)
3062 return -EINVAL;
3063 if (mddev->recovery || mddev->sync_thread)
3064 return -EBUSY;
3065 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3066 /* add the bitmap */
3067 if (mddev->bitmap)
3068 return -EEXIST;
3069 if (mddev->default_bitmap_offset == 0)
3070 return -EINVAL;
3071 mddev->bitmap_offset = mddev->default_bitmap_offset;
3072 mddev->pers->quiesce(mddev, 1);
3073 rv = bitmap_create(mddev);
3074 if (rv)
3075 bitmap_destroy(mddev);
3076 mddev->pers->quiesce(mddev, 0);
3077 } else {
3078 /* remove the bitmap */
3079 if (!mddev->bitmap)
3080 return -ENOENT;
3081 if (mddev->bitmap->file)
3082 return -EINVAL;
3083 mddev->pers->quiesce(mddev, 1);
3084 bitmap_destroy(mddev);
3085 mddev->pers->quiesce(mddev, 0);
3086 mddev->bitmap_offset = 0;
3087 }
3088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 md_update_sb(mddev);
3090 return rv;
3091}
3092
3093static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3094{
3095 mdk_rdev_t *rdev;
3096
3097 if (mddev->pers == NULL)
3098 return -ENODEV;
3099
3100 rdev = find_rdev(mddev, dev);
3101 if (!rdev)
3102 return -ENODEV;
3103
3104 md_error(mddev, rdev);
3105 return 0;
3106}
3107
3108static int md_ioctl(struct inode *inode, struct file *file,
3109 unsigned int cmd, unsigned long arg)
3110{
3111 int err = 0;
3112 void __user *argp = (void __user *)arg;
3113 struct hd_geometry __user *loc = argp;
3114 mddev_t *mddev = NULL;
3115
3116 if (!capable(CAP_SYS_ADMIN))
3117 return -EACCES;
3118
3119 /*
3120 * Commands dealing with the RAID driver but not any
3121 * particular array:
3122 */
3123 switch (cmd)
3124 {
3125 case RAID_VERSION:
3126 err = get_version(argp);
3127 goto done;
3128
3129 case PRINT_RAID_DEBUG:
3130 err = 0;
3131 md_print_devices();
3132 goto done;
3133
3134#ifndef MODULE
3135 case RAID_AUTORUN:
3136 err = 0;
3137 autostart_arrays(arg);
3138 goto done;
3139#endif
3140 default:;
3141 }
3142
3143 /*
3144 * Commands creating/starting a new array:
3145 */
3146
3147 mddev = inode->i_bdev->bd_disk->private_data;
3148
3149 if (!mddev) {
3150 BUG();
3151 goto abort;
3152 }
3153
3154
3155 if (cmd == START_ARRAY) {
3156 /* START_ARRAY doesn't need to lock the array as autostart_array
3157 * does the locking, and it could even be a different array
3158 */
3159 static int cnt = 3;
3160 if (cnt > 0 ) {
3161 printk(KERN_WARNING
3162 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08003163 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 current->comm, current->pid);
3165 cnt--;
3166 }
3167 err = autostart_array(new_decode_dev(arg));
3168 if (err) {
3169 printk(KERN_WARNING "md: autostart failed!\n");
3170 goto abort;
3171 }
3172 goto done;
3173 }
3174
3175 err = mddev_lock(mddev);
3176 if (err) {
3177 printk(KERN_INFO
3178 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3179 err, cmd);
3180 goto abort;
3181 }
3182
3183 switch (cmd)
3184 {
3185 case SET_ARRAY_INFO:
3186 {
3187 mdu_array_info_t info;
3188 if (!arg)
3189 memset(&info, 0, sizeof(info));
3190 else if (copy_from_user(&info, argp, sizeof(info))) {
3191 err = -EFAULT;
3192 goto abort_unlock;
3193 }
3194 if (mddev->pers) {
3195 err = update_array_info(mddev, &info);
3196 if (err) {
3197 printk(KERN_WARNING "md: couldn't update"
3198 " array info. %d\n", err);
3199 goto abort_unlock;
3200 }
3201 goto done_unlock;
3202 }
3203 if (!list_empty(&mddev->disks)) {
3204 printk(KERN_WARNING
3205 "md: array %s already has disks!\n",
3206 mdname(mddev));
3207 err = -EBUSY;
3208 goto abort_unlock;
3209 }
3210 if (mddev->raid_disks) {
3211 printk(KERN_WARNING
3212 "md: array %s already initialised!\n",
3213 mdname(mddev));
3214 err = -EBUSY;
3215 goto abort_unlock;
3216 }
3217 err = set_array_info(mddev, &info);
3218 if (err) {
3219 printk(KERN_WARNING "md: couldn't set"
3220 " array info. %d\n", err);
3221 goto abort_unlock;
3222 }
3223 }
3224 goto done_unlock;
3225
3226 default:;
3227 }
3228
3229 /*
3230 * Commands querying/configuring an existing array:
3231 */
NeilBrown32a76272005-06-21 17:17:14 -07003232 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3233 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3234 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3235 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 err = -ENODEV;
3237 goto abort_unlock;
3238 }
3239
3240 /*
3241 * Commands even a read-only array can execute:
3242 */
3243 switch (cmd)
3244 {
3245 case GET_ARRAY_INFO:
3246 err = get_array_info(mddev, argp);
3247 goto done_unlock;
3248
NeilBrown32a76272005-06-21 17:17:14 -07003249 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003250 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003251 goto done_unlock;
3252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 case GET_DISK_INFO:
3254 err = get_disk_info(mddev, argp);
3255 goto done_unlock;
3256
3257 case RESTART_ARRAY_RW:
3258 err = restart_array(mddev);
3259 goto done_unlock;
3260
3261 case STOP_ARRAY:
3262 err = do_md_stop (mddev, 0);
3263 goto done_unlock;
3264
3265 case STOP_ARRAY_RO:
3266 err = do_md_stop (mddev, 1);
3267 goto done_unlock;
3268
3269 /*
3270 * We have a problem here : there is no easy way to give a CHS
3271 * virtual geometry. We currently pretend that we have a 2 heads
3272 * 4 sectors (with a BIG number of cylinders...). This drives
3273 * dosfs just mad... ;-)
3274 */
3275 case HDIO_GETGEO:
3276 if (!loc) {
3277 err = -EINVAL;
3278 goto abort_unlock;
3279 }
3280 err = put_user (2, (char __user *) &loc->heads);
3281 if (err)
3282 goto abort_unlock;
3283 err = put_user (4, (char __user *) &loc->sectors);
3284 if (err)
3285 goto abort_unlock;
3286 err = put_user(get_capacity(mddev->gendisk)/8,
3287 (short __user *) &loc->cylinders);
3288 if (err)
3289 goto abort_unlock;
3290 err = put_user (get_start_sect(inode->i_bdev),
3291 (long __user *) &loc->start);
3292 goto done_unlock;
3293 }
3294
3295 /*
3296 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08003297 * superblock, so we do not allow them on read-only arrays.
3298 * However non-MD ioctls (e.g. get-size) will still come through
3299 * here and hit the 'default' below, so only disallow
3300 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 */
NeilBrownf91de922005-11-08 21:39:36 -08003302 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3303 mddev->ro && mddev->pers) {
3304 if (mddev->ro == 2) {
3305 mddev->ro = 0;
3306 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3307 md_wakeup_thread(mddev->thread);
3308
3309 } else {
3310 err = -EROFS;
3311 goto abort_unlock;
3312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
3314
3315 switch (cmd)
3316 {
3317 case ADD_NEW_DISK:
3318 {
3319 mdu_disk_info_t info;
3320 if (copy_from_user(&info, argp, sizeof(info)))
3321 err = -EFAULT;
3322 else
3323 err = add_new_disk(mddev, &info);
3324 goto done_unlock;
3325 }
3326
3327 case HOT_REMOVE_DISK:
3328 err = hot_remove_disk(mddev, new_decode_dev(arg));
3329 goto done_unlock;
3330
3331 case HOT_ADD_DISK:
3332 err = hot_add_disk(mddev, new_decode_dev(arg));
3333 goto done_unlock;
3334
3335 case SET_DISK_FAULTY:
3336 err = set_disk_faulty(mddev, new_decode_dev(arg));
3337 goto done_unlock;
3338
3339 case RUN_ARRAY:
3340 err = do_md_run (mddev);
3341 goto done_unlock;
3342
NeilBrown32a76272005-06-21 17:17:14 -07003343 case SET_BITMAP_FILE:
3344 err = set_bitmap_file(mddev, (int)arg);
3345 goto done_unlock;
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 default:
3348 if (_IOC_TYPE(cmd) == MD_MAJOR)
3349 printk(KERN_WARNING "md: %s(pid %d) used"
3350 " obsolete MD ioctl, upgrade your"
3351 " software to use new ictls.\n",
3352 current->comm, current->pid);
3353 err = -EINVAL;
3354 goto abort_unlock;
3355 }
3356
3357done_unlock:
3358abort_unlock:
3359 mddev_unlock(mddev);
3360
3361 return err;
3362done:
3363 if (err)
3364 MD_BUG();
3365abort:
3366 return err;
3367}
3368
3369static int md_open(struct inode *inode, struct file *file)
3370{
3371 /*
3372 * Succeed if we can lock the mddev, which confirms that
3373 * it isn't being stopped right now.
3374 */
3375 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3376 int err;
3377
3378 if ((err = mddev_lock(mddev)))
3379 goto out;
3380
3381 err = 0;
3382 mddev_get(mddev);
3383 mddev_unlock(mddev);
3384
3385 check_disk_change(inode->i_bdev);
3386 out:
3387 return err;
3388}
3389
3390static int md_release(struct inode *inode, struct file * file)
3391{
3392 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3393
3394 if (!mddev)
3395 BUG();
3396 mddev_put(mddev);
3397
3398 return 0;
3399}
3400
3401static int md_media_changed(struct gendisk *disk)
3402{
3403 mddev_t *mddev = disk->private_data;
3404
3405 return mddev->changed;
3406}
3407
3408static int md_revalidate(struct gendisk *disk)
3409{
3410 mddev_t *mddev = disk->private_data;
3411
3412 mddev->changed = 0;
3413 return 0;
3414}
3415static struct block_device_operations md_fops =
3416{
3417 .owner = THIS_MODULE,
3418 .open = md_open,
3419 .release = md_release,
3420 .ioctl = md_ioctl,
3421 .media_changed = md_media_changed,
3422 .revalidate_disk= md_revalidate,
3423};
3424
Adrian Bunk75c96f82005-05-05 16:16:09 -07003425static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
3427 mdk_thread_t *thread = arg;
3428
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 /*
3430 * md_thread is a 'system-thread', it's priority should be very
3431 * high. We avoid resource deadlocks individually in each
3432 * raid personality. (RAID5 does preallocation) We also use RR and
3433 * the very same RT priority as kswapd, thus we will never get
3434 * into a priority inversion deadlock.
3435 *
3436 * we definitely have to have equal or higher priority than
3437 * bdflush, otherwise bdflush will deadlock if there are too
3438 * many dirty RAID5 blocks.
3439 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
NeilBrown6985c432005-10-19 21:23:47 -07003441 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07003442 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
NeilBrown93588e22005-11-15 00:09:12 -08003444 /* We need to wait INTERRUPTIBLE so that
3445 * we don't add to the load-average.
3446 * That means we need to be sure no signals are
3447 * pending
3448 */
3449 if (signal_pending(current))
3450 flush_signals(current);
3451
3452 wait_event_interruptible_timeout
3453 (thread->wqueue,
3454 test_bit(THREAD_WAKEUP, &thread->flags)
3455 || kthread_should_stop(),
3456 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003457 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
3459 clear_bit(THREAD_WAKEUP, &thread->flags);
3460
NeilBrown787453c2005-11-08 21:39:43 -08003461 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 }
NeilBrowna6fb0932005-09-09 16:23:56 -07003463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 return 0;
3465}
3466
3467void md_wakeup_thread(mdk_thread_t *thread)
3468{
3469 if (thread) {
3470 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3471 set_bit(THREAD_WAKEUP, &thread->flags);
3472 wake_up(&thread->wqueue);
3473 }
3474}
3475
3476mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3477 const char *name)
3478{
3479 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
NeilBrowna6fb0932005-09-09 16:23:56 -07003481 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 if (!thread)
3483 return NULL;
3484
3485 memset(thread, 0, sizeof(mdk_thread_t));
3486 init_waitqueue_head(&thread->wqueue);
3487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 thread->run = run;
3489 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07003490 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07003491 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07003492 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 kfree(thread);
3494 return NULL;
3495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 return thread;
3497}
3498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499void md_unregister_thread(mdk_thread_t *thread)
3500{
NeilBrownd28446f2005-04-16 15:26:41 -07003501 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07003502
3503 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 kfree(thread);
3505}
3506
3507void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3508{
3509 if (!mddev) {
3510 MD_BUG();
3511 return;
3512 }
3513
NeilBrownb2d444d2005-11-08 21:39:31 -08003514 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 return;
NeilBrown32a76272005-06-21 17:17:14 -07003516/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3518 mdname(mddev),
3519 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3520 __builtin_return_address(0),__builtin_return_address(1),
3521 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07003522*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 if (!mddev->pers->error_handler)
3524 return;
3525 mddev->pers->error_handler(mddev,rdev);
3526 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3527 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3528 md_wakeup_thread(mddev->thread);
3529}
3530
3531/* seq_file implementation /proc/mdstat */
3532
3533static void status_unused(struct seq_file *seq)
3534{
3535 int i = 0;
3536 mdk_rdev_t *rdev;
3537 struct list_head *tmp;
3538
3539 seq_printf(seq, "unused devices: ");
3540
3541 ITERATE_RDEV_PENDING(rdev,tmp) {
3542 char b[BDEVNAME_SIZE];
3543 i++;
3544 seq_printf(seq, "%s ",
3545 bdevname(rdev->bdev,b));
3546 }
3547 if (!i)
3548 seq_printf(seq, "<none>");
3549
3550 seq_printf(seq, "\n");
3551}
3552
3553
3554static void status_resync(struct seq_file *seq, mddev_t * mddev)
3555{
3556 unsigned long max_blocks, resync, res, dt, db, rt;
3557
3558 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3559
3560 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3561 max_blocks = mddev->resync_max_sectors >> 1;
3562 else
3563 max_blocks = mddev->size;
3564
3565 /*
3566 * Should not happen.
3567 */
3568 if (!max_blocks) {
3569 MD_BUG();
3570 return;
3571 }
3572 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3573 {
3574 int i, x = res/50, y = 20-x;
3575 seq_printf(seq, "[");
3576 for (i = 0; i < x; i++)
3577 seq_printf(seq, "=");
3578 seq_printf(seq, ">");
3579 for (i = 0; i < y; i++)
3580 seq_printf(seq, ".");
3581 seq_printf(seq, "] ");
3582 }
3583 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3584 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3585 "resync" : "recovery"),
3586 res/10, res % 10, resync, max_blocks);
3587
3588 /*
3589 * We do not want to overflow, so the order of operands and
3590 * the * 100 / 100 trick are important. We do a +1 to be
3591 * safe against division by zero. We only estimate anyway.
3592 *
3593 * dt: time from mark until now
3594 * db: blocks written from mark until now
3595 * rt: remaining time
3596 */
3597 dt = ((jiffies - mddev->resync_mark) / HZ);
3598 if (!dt) dt++;
3599 db = resync - (mddev->resync_mark_cnt/2);
3600 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3601
3602 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3603
3604 seq_printf(seq, " speed=%ldK/sec", db/dt);
3605}
3606
3607static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3608{
3609 struct list_head *tmp;
3610 loff_t l = *pos;
3611 mddev_t *mddev;
3612
3613 if (l >= 0x10000)
3614 return NULL;
3615 if (!l--)
3616 /* header */
3617 return (void*)1;
3618
3619 spin_lock(&all_mddevs_lock);
3620 list_for_each(tmp,&all_mddevs)
3621 if (!l--) {
3622 mddev = list_entry(tmp, mddev_t, all_mddevs);
3623 mddev_get(mddev);
3624 spin_unlock(&all_mddevs_lock);
3625 return mddev;
3626 }
3627 spin_unlock(&all_mddevs_lock);
3628 if (!l--)
3629 return (void*)2;/* tail */
3630 return NULL;
3631}
3632
3633static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3634{
3635 struct list_head *tmp;
3636 mddev_t *next_mddev, *mddev = v;
3637
3638 ++*pos;
3639 if (v == (void*)2)
3640 return NULL;
3641
3642 spin_lock(&all_mddevs_lock);
3643 if (v == (void*)1)
3644 tmp = all_mddevs.next;
3645 else
3646 tmp = mddev->all_mddevs.next;
3647 if (tmp != &all_mddevs)
3648 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3649 else {
3650 next_mddev = (void*)2;
3651 *pos = 0x10000;
3652 }
3653 spin_unlock(&all_mddevs_lock);
3654
3655 if (v != (void*)1)
3656 mddev_put(mddev);
3657 return next_mddev;
3658
3659}
3660
3661static void md_seq_stop(struct seq_file *seq, void *v)
3662{
3663 mddev_t *mddev = v;
3664
3665 if (mddev && v != (void*)1 && v != (void*)2)
3666 mddev_put(mddev);
3667}
3668
3669static int md_seq_show(struct seq_file *seq, void *v)
3670{
3671 mddev_t *mddev = v;
3672 sector_t size;
3673 struct list_head *tmp2;
3674 mdk_rdev_t *rdev;
3675 int i;
NeilBrown32a76272005-06-21 17:17:14 -07003676 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
3678 if (v == (void*)1) {
3679 seq_printf(seq, "Personalities : ");
3680 spin_lock(&pers_lock);
3681 for (i = 0; i < MAX_PERSONALITY; i++)
3682 if (pers[i])
3683 seq_printf(seq, "[%s] ", pers[i]->name);
3684
3685 spin_unlock(&pers_lock);
3686 seq_printf(seq, "\n");
3687 return 0;
3688 }
3689 if (v == (void*)2) {
3690 status_unused(seq);
3691 return 0;
3692 }
3693
3694 if (mddev_lock(mddev)!=0)
3695 return -EINTR;
3696 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3697 seq_printf(seq, "%s : %sactive", mdname(mddev),
3698 mddev->pers ? "" : "in");
3699 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08003700 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08003702 if (mddev->ro==2)
3703 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 seq_printf(seq, " %s", mddev->pers->name);
3705 }
3706
3707 size = 0;
3708 ITERATE_RDEV(mddev,rdev,tmp2) {
3709 char b[BDEVNAME_SIZE];
3710 seq_printf(seq, " %s[%d]",
3711 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003712 if (test_bit(WriteMostly, &rdev->flags))
3713 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08003714 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 seq_printf(seq, "(F)");
3716 continue;
NeilBrownb325a322005-09-09 16:24:00 -07003717 } else if (rdev->raid_disk < 0)
3718 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 size += rdev->size;
3720 }
3721
3722 if (!list_empty(&mddev->disks)) {
3723 if (mddev->pers)
3724 seq_printf(seq, "\n %llu blocks",
3725 (unsigned long long)mddev->array_size);
3726 else
3727 seq_printf(seq, "\n %llu blocks",
3728 (unsigned long long)size);
3729 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07003730 if (mddev->persistent) {
3731 if (mddev->major_version != 0 ||
3732 mddev->minor_version != 90) {
3733 seq_printf(seq," super %d.%d",
3734 mddev->major_version,
3735 mddev->minor_version);
3736 }
3737 } else
3738 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
3740 if (mddev->pers) {
3741 mddev->pers->status (seq, mddev);
3742 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08003743 if (mddev->pers->sync_request) {
3744 if (mddev->curr_resync > 2) {
3745 status_resync (seq, mddev);
3746 seq_printf(seq, "\n ");
3747 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3748 seq_printf(seq, "\tresync=DELAYED\n ");
3749 else if (mddev->recovery_cp < MaxSector)
3750 seq_printf(seq, "\tresync=PENDING\n ");
3751 }
NeilBrown32a76272005-06-21 17:17:14 -07003752 } else
3753 seq_printf(seq, "\n ");
3754
3755 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07003756 unsigned long chunk_kb;
3757 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07003758 spin_lock_irqsave(&bitmap->lock, flags);
3759 chunk_kb = bitmap->chunksize >> 10;
3760 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3761 "%lu%s chunk",
3762 bitmap->pages - bitmap->missing_pages,
3763 bitmap->pages,
3764 (bitmap->pages - bitmap->missing_pages)
3765 << (PAGE_SHIFT - 10),
3766 chunk_kb ? chunk_kb : bitmap->chunksize,
3767 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07003768 if (bitmap->file) {
3769 seq_printf(seq, ", file: ");
3770 seq_path(seq, bitmap->file->f_vfsmnt,
3771 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07003772 }
NeilBrown78d742d2005-06-21 17:17:15 -07003773
NeilBrown32a76272005-06-21 17:17:14 -07003774 seq_printf(seq, "\n");
3775 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 }
3777
3778 seq_printf(seq, "\n");
3779 }
3780 mddev_unlock(mddev);
3781
3782 return 0;
3783}
3784
3785static struct seq_operations md_seq_ops = {
3786 .start = md_seq_start,
3787 .next = md_seq_next,
3788 .stop = md_seq_stop,
3789 .show = md_seq_show,
3790};
3791
3792static int md_seq_open(struct inode *inode, struct file *file)
3793{
3794 int error;
3795
3796 error = seq_open(file, &md_seq_ops);
3797 return error;
3798}
3799
3800static struct file_operations md_seq_fops = {
3801 .open = md_seq_open,
3802 .read = seq_read,
3803 .llseek = seq_lseek,
3804 .release = seq_release,
3805};
3806
3807int register_md_personality(int pnum, mdk_personality_t *p)
3808{
3809 if (pnum >= MAX_PERSONALITY) {
3810 printk(KERN_ERR
3811 "md: tried to install personality %s as nr %d, but max is %lu\n",
3812 p->name, pnum, MAX_PERSONALITY-1);
3813 return -EINVAL;
3814 }
3815
3816 spin_lock(&pers_lock);
3817 if (pers[pnum]) {
3818 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 return -EBUSY;
3820 }
3821
3822 pers[pnum] = p;
3823 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3824 spin_unlock(&pers_lock);
3825 return 0;
3826}
3827
3828int unregister_md_personality(int pnum)
3829{
NeilBrowna757e642005-04-16 15:26:42 -07003830 if (pnum >= MAX_PERSONALITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3834 spin_lock(&pers_lock);
3835 pers[pnum] = NULL;
3836 spin_unlock(&pers_lock);
3837 return 0;
3838}
3839
3840static int is_mddev_idle(mddev_t *mddev)
3841{
3842 mdk_rdev_t * rdev;
3843 struct list_head *tmp;
3844 int idle;
3845 unsigned long curr_events;
3846
3847 idle = 1;
3848 ITERATE_RDEV(mddev,rdev,tmp) {
3849 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01003850 curr_events = disk_stat_read(disk, sectors[0]) +
3851 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08003853 /* The difference between curr_events and last_events
3854 * will be affected by any new non-sync IO (making
3855 * curr_events bigger) and any difference in the amount of
3856 * in-flight syncio (making current_events bigger or smaller)
3857 * The amount in-flight is currently limited to
3858 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
3859 * which is at most 4096 sectors.
3860 * These numbers are fairly fragile and should be made
3861 * more robust, probably by enforcing the
3862 * 'window size' that md_do_sync sort-of uses.
3863 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 * Note: the following is an unsigned comparison.
3865 */
NeilBrownc0e48522005-11-18 01:11:01 -08003866 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 rdev->last_events = curr_events;
3868 idle = 0;
3869 }
3870 }
3871 return idle;
3872}
3873
3874void md_done_sync(mddev_t *mddev, int blocks, int ok)
3875{
3876 /* another "blocks" (512byte) blocks have been synced */
3877 atomic_sub(blocks, &mddev->recovery_active);
3878 wake_up(&mddev->recovery_wait);
3879 if (!ok) {
3880 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3881 md_wakeup_thread(mddev->thread);
3882 // stop recovery, signal do_sync ....
3883 }
3884}
3885
3886
NeilBrown06d91a52005-06-21 17:17:12 -07003887/* md_write_start(mddev, bi)
3888 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07003889 * in superblock) before writing, schedule a superblock update
3890 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07003891 */
NeilBrown3d310eb2005-06-21 17:17:26 -07003892void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893{
NeilBrown06d91a52005-06-21 17:17:12 -07003894 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07003895 return;
NeilBrown06d91a52005-06-21 17:17:12 -07003896
NeilBrownf91de922005-11-08 21:39:36 -08003897 BUG_ON(mddev->ro == 1);
3898 if (mddev->ro == 2) {
3899 /* need to switch to read/write */
3900 mddev->ro = 0;
3901 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3902 md_wakeup_thread(mddev->thread);
3903 }
NeilBrown06d91a52005-06-21 17:17:12 -07003904 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07003905 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08003906 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07003907 if (mddev->in_sync) {
3908 mddev->in_sync = 0;
3909 mddev->sb_dirty = 1;
3910 md_wakeup_thread(mddev->thread);
3911 }
NeilBrowna9701a32005-11-08 21:39:34 -08003912 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07003913 }
NeilBrown3d310eb2005-06-21 17:17:26 -07003914 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915}
3916
3917void md_write_end(mddev_t *mddev)
3918{
3919 if (atomic_dec_and_test(&mddev->writes_pending)) {
3920 if (mddev->safemode == 2)
3921 md_wakeup_thread(mddev->thread);
3922 else
3923 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3924 }
3925}
3926
Adrian Bunk75c96f82005-05-05 16:16:09 -07003927static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
3929#define SYNC_MARKS 10
3930#define SYNC_MARK_STEP (3*HZ)
3931static void md_do_sync(mddev_t *mddev)
3932{
3933 mddev_t *mddev2;
3934 unsigned int currspeed = 0,
3935 window;
NeilBrown57afd892005-06-21 17:17:13 -07003936 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 unsigned long mark[SYNC_MARKS];
3938 sector_t mark_cnt[SYNC_MARKS];
3939 int last_mark,m;
3940 struct list_head *tmp;
3941 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07003942 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
3944 /* just incase thread restarts... */
3945 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3946 return;
3947
3948 /* we overload curr_resync somewhat here.
3949 * 0 == not engaged in resync at all
3950 * 2 == checking that there is no conflict with another sync
3951 * 1 == like 2, but have yielded to allow conflicting resync to
3952 * commense
3953 * other == active in resync - this many blocks
3954 *
3955 * Before starting a resync we must have set curr_resync to
3956 * 2, and then checked that every "conflicting" array has curr_resync
3957 * less than ours. When we find one that is the same or higher
3958 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3959 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3960 * This will mean we have to start checking from the beginning again.
3961 *
3962 */
3963
3964 do {
3965 mddev->curr_resync = 2;
3966
3967 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08003968 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07003969 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 goto skip;
3971 }
3972 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 if (mddev2 == mddev)
3974 continue;
3975 if (mddev2->curr_resync &&
3976 match_mddev_units(mddev,mddev2)) {
3977 DEFINE_WAIT(wq);
3978 if (mddev < mddev2 && mddev->curr_resync == 2) {
3979 /* arbitrarily yield */
3980 mddev->curr_resync = 1;
3981 wake_up(&resync_wait);
3982 }
3983 if (mddev > mddev2 && mddev->curr_resync == 1)
3984 /* no need to wait here, we can wait the next
3985 * time 'round when curr_resync == 2
3986 */
3987 continue;
NeilBrown787453c2005-11-08 21:39:43 -08003988 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
3989 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07003990 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 printk(KERN_INFO "md: delaying resync of %s"
3992 " until %s has finished resync (they"
3993 " share one or more physical units)\n",
3994 mdname(mddev), mdname(mddev2));
3995 mddev_put(mddev2);
3996 schedule();
3997 finish_wait(&resync_wait, &wq);
3998 goto try_again;
3999 }
4000 finish_wait(&resync_wait, &wq);
4001 }
4002 }
4003 } while (mddev->curr_resync < 2);
4004
NeilBrown9d888832005-11-08 21:39:26 -08004005 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07004007 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 */
4009 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08004010 mddev->resync_mismatches = 0;
4011 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 /* recovery follows the physical size of devices */
4013 max_sectors = mddev->size << 1;
4014
4015 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4016 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
4017 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
Adrian Bunk338cec32005-09-10 00:26:54 -07004018 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 "(but not more than %d KB/sec) for reconstruction.\n",
4020 sysctl_speed_limit_max);
4021
4022 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07004023 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08004024 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4025 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 j = mddev->recovery_cp;
4027 else
4028 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07004029 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 for (m = 0; m < SYNC_MARKS; m++) {
4031 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004032 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 }
4034 last_mark = 0;
4035 mddev->resync_mark = mark[last_mark];
4036 mddev->resync_mark_cnt = mark_cnt[last_mark];
4037
4038 /*
4039 * Tune reconstruction:
4040 */
4041 window = 32*(PAGE_SIZE/512);
4042 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4043 window/2,(unsigned long long) max_sectors/2);
4044
4045 atomic_set(&mddev->recovery_active, 0);
4046 init_waitqueue_head(&mddev->recovery_wait);
4047 last_check = 0;
4048
4049 if (j>2) {
4050 printk(KERN_INFO
4051 "md: resuming recovery of %s from checkpoint.\n",
4052 mdname(mddev));
4053 mddev->curr_resync = j;
4054 }
4055
4056 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07004057 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
NeilBrown57afd892005-06-21 17:17:13 -07004059 skipped = 0;
4060 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4061 currspeed < sysctl_speed_limit_min);
4062 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4064 goto out;
4065 }
NeilBrown57afd892005-06-21 17:17:13 -07004066
4067 if (!skipped) { /* actual IO requested */
4068 io_sectors += sectors;
4069 atomic_add(sectors, &mddev->recovery_active);
4070 }
4071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 j += sectors;
4073 if (j>1) mddev->curr_resync = j;
4074
NeilBrown57afd892005-06-21 17:17:13 -07004075
4076 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 continue;
4078
NeilBrown57afd892005-06-21 17:17:13 -07004079 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
4081 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4082 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4083 break;
4084
4085 repeat:
4086 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4087 /* step marks */
4088 int next = (last_mark+1) % SYNC_MARKS;
4089
4090 mddev->resync_mark = mark[next];
4091 mddev->resync_mark_cnt = mark_cnt[next];
4092 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004093 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 last_mark = next;
4095 }
4096
4097
NeilBrown787453c2005-11-08 21:39:43 -08004098 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 /*
4100 * got a signal, exit.
4101 */
4102 printk(KERN_INFO
4103 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4105 goto out;
4106 }
4107
4108 /*
4109 * this loop exits only if either when we are slower than
4110 * the 'hard' speed limit, or the system was IO-idle for
4111 * a jiffy.
4112 * the system might be non-idle CPU-wise, but we only care
4113 * about not overloading the IO subsystem. (things like an
4114 * e2fsck being done on the RAID array should execute fast)
4115 */
4116 mddev->queue->unplug_fn(mddev->queue);
4117 cond_resched();
4118
NeilBrown57afd892005-06-21 17:17:13 -07004119 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4120 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
4122 if (currspeed > sysctl_speed_limit_min) {
4123 if ((currspeed > sysctl_speed_limit_max) ||
4124 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08004125 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 goto repeat;
4127 }
4128 }
4129 }
4130 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4131 /*
4132 * this also signals 'finished resyncing' to md_stop
4133 */
4134 out:
4135 mddev->queue->unplug_fn(mddev->queue);
4136
4137 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4138
4139 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004140 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
4142 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4143 mddev->curr_resync > 2 &&
4144 mddev->curr_resync >= mddev->recovery_cp) {
4145 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4146 printk(KERN_INFO
4147 "md: checkpointing recovery of %s.\n",
4148 mdname(mddev));
4149 mddev->recovery_cp = mddev->curr_resync;
4150 } else
4151 mddev->recovery_cp = MaxSector;
4152 }
4153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 skip:
4155 mddev->curr_resync = 0;
4156 wake_up(&resync_wait);
4157 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4158 md_wakeup_thread(mddev->thread);
4159}
4160
4161
4162/*
4163 * This routine is regularly called by all per-raid-array threads to
4164 * deal with generic issues like resync and super-block update.
4165 * Raid personalities that don't have a thread (linear/raid0) do not
4166 * need this as they never do any recovery or update the superblock.
4167 *
4168 * It does not do any resync itself, but rather "forks" off other threads
4169 * to do that as needed.
4170 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4171 * "->recovery" and create a thread at ->sync_thread.
4172 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4173 * and wakeups up this thread which will reap the thread and finish up.
4174 * This thread also removes any faulty devices (with nr_pending == 0).
4175 *
4176 * The overall approach is:
4177 * 1/ if the superblock needs updating, update it.
4178 * 2/ If a recovery thread is running, don't do anything else.
4179 * 3/ If recovery has finished, clean up, possibly marking spares active.
4180 * 4/ If there are any faulty devices, remove them.
4181 * 5/ If array is degraded, try to add spares devices
4182 * 6/ If array has spares or is not in-sync, start a resync thread.
4183 */
4184void md_check_recovery(mddev_t *mddev)
4185{
4186 mdk_rdev_t *rdev;
4187 struct list_head *rtmp;
4188
4189
NeilBrown5f404022005-06-21 17:17:16 -07004190 if (mddev->bitmap)
4191 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
4193 if (mddev->ro)
4194 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004195
4196 if (signal_pending(current)) {
4197 if (mddev->pers->sync_request) {
4198 printk(KERN_INFO "md: %s in immediate safe mode\n",
4199 mdname(mddev));
4200 mddev->safemode = 2;
4201 }
4202 flush_signals(current);
4203 }
4204
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 if ( ! (
4206 mddev->sb_dirty ||
4207 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004208 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4209 (mddev->safemode == 1) ||
4210 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4211 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 ))
4213 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 if (mddev_trylock(mddev)==0) {
4216 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004217
NeilBrowna9701a32005-11-08 21:39:34 -08004218 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004219 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4220 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4221 mddev->in_sync = 1;
4222 mddev->sb_dirty = 1;
4223 }
4224 if (mddev->safemode == 1)
4225 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08004226 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004227
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 if (mddev->sb_dirty)
4229 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004230
NeilBrown06d91a52005-06-21 17:17:12 -07004231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4233 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4234 /* resync/recovery still happening */
4235 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4236 goto unlock;
4237 }
4238 if (mddev->sync_thread) {
4239 /* resync has finished, collect result */
4240 md_unregister_thread(mddev->sync_thread);
4241 mddev->sync_thread = NULL;
4242 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4243 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4244 /* success...*/
4245 /* activate any spares */
4246 mddev->pers->spare_active(mddev);
4247 }
4248 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004249
4250 /* if array is no-longer degraded, then any saved_raid_disk
4251 * information must be scrapped
4252 */
4253 if (!mddev->degraded)
4254 ITERATE_RDEV(mddev,rdev,rtmp)
4255 rdev->saved_raid_disk = -1;
4256
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 mddev->recovery = 0;
4258 /* flag recovery needed just to double check */
4259 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4260 goto unlock;
4261 }
NeilBrown24dd4692005-11-08 21:39:26 -08004262 /* Clear some bits that don't mean anything, but
4263 * might be left set
4264 */
4265 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4266 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4267 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4268 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
4270 /* no recovery is running.
4271 * remove any failed drives, then
4272 * add spares if possible.
4273 * Spare are also removed and re-added, to allow
4274 * the personality to fail the re-add.
4275 */
4276 ITERATE_RDEV(mddev,rdev,rtmp)
4277 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004278 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004280 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4281 char nm[20];
4282 sprintf(nm,"rd%d", rdev->raid_disk);
4283 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 }
4287
4288 if (mddev->degraded) {
4289 ITERATE_RDEV(mddev,rdev,rtmp)
4290 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08004291 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004292 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4293 char nm[20];
4294 sprintf(nm, "rd%d", rdev->raid_disk);
4295 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 spares++;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004297 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 break;
4299 }
4300 }
4301
NeilBrown24dd4692005-11-08 21:39:26 -08004302 if (spares) {
4303 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4304 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4305 } else if (mddev->recovery_cp < MaxSector) {
4306 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4307 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4308 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08004310
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 if (mddev->pers->sync_request) {
4312 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07004313 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4314 /* We are adding a device or devices to an array
4315 * which has the bitmap stored on all devices.
4316 * So make sure all bitmap pages get written
4317 */
4318 bitmap_write_all(mddev->bitmap);
4319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 mddev->sync_thread = md_register_thread(md_do_sync,
4321 mddev,
4322 "%s_resync");
4323 if (!mddev->sync_thread) {
4324 printk(KERN_ERR "%s: could not start resync"
4325 " thread...\n",
4326 mdname(mddev));
4327 /* leave the spares where they are, it shouldn't hurt */
4328 mddev->recovery = 0;
4329 } else {
4330 md_wakeup_thread(mddev->sync_thread);
4331 }
4332 }
4333 unlock:
4334 mddev_unlock(mddev);
4335 }
4336}
4337
Adrian Bunk75c96f82005-05-05 16:16:09 -07004338static int md_notify_reboot(struct notifier_block *this,
4339 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340{
4341 struct list_head *tmp;
4342 mddev_t *mddev;
4343
4344 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4345
4346 printk(KERN_INFO "md: stopping all md devices.\n");
4347
4348 ITERATE_MDDEV(mddev,tmp)
4349 if (mddev_trylock(mddev)==0)
4350 do_md_stop (mddev, 1);
4351 /*
4352 * certain more exotic SCSI devices are known to be
4353 * volatile wrt too early system reboots. While the
4354 * right place to handle this issue is the given
4355 * driver, we do want to have a safe RAID driver ...
4356 */
4357 mdelay(1000*1);
4358 }
4359 return NOTIFY_DONE;
4360}
4361
Adrian Bunk75c96f82005-05-05 16:16:09 -07004362static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 .notifier_call = md_notify_reboot,
4364 .next = NULL,
4365 .priority = INT_MAX, /* before any real devices */
4366};
4367
4368static void md_geninit(void)
4369{
4370 struct proc_dir_entry *p;
4371
4372 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4373
4374 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4375 if (p)
4376 p->proc_fops = &md_seq_fops;
4377}
4378
Adrian Bunk75c96f82005-05-05 16:16:09 -07004379static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380{
4381 int minor;
4382
4383 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4384 " MD_SB_DISKS=%d\n",
4385 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4386 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08004387 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07004388 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
4390 if (register_blkdev(MAJOR_NR, "md"))
4391 return -1;
4392 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4393 unregister_blkdev(MAJOR_NR, "md");
4394 return -1;
4395 }
4396 devfs_mk_dir("md");
4397 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4398 md_probe, NULL, NULL);
4399 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4400 md_probe, NULL, NULL);
4401
4402 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4403 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4404 S_IFBLK|S_IRUSR|S_IWUSR,
4405 "md/%d", minor);
4406
4407 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4408 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4409 S_IFBLK|S_IRUSR|S_IWUSR,
4410 "md/mdp%d", minor);
4411
4412
4413 register_reboot_notifier(&md_notifier);
4414 raid_table_header = register_sysctl_table(raid_root_table, 1);
4415
4416 md_geninit();
4417 return (0);
4418}
4419
4420
4421#ifndef MODULE
4422
4423/*
4424 * Searches all registered partitions for autorun RAID arrays
4425 * at boot time.
4426 */
4427static dev_t detected_devices[128];
4428static int dev_cnt;
4429
4430void md_autodetect_dev(dev_t dev)
4431{
4432 if (dev_cnt >= 0 && dev_cnt < 127)
4433 detected_devices[dev_cnt++] = dev;
4434}
4435
4436
4437static void autostart_arrays(int part)
4438{
4439 mdk_rdev_t *rdev;
4440 int i;
4441
4442 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4443
4444 for (i = 0; i < dev_cnt; i++) {
4445 dev_t dev = detected_devices[i];
4446
4447 rdev = md_import_device(dev,0, 0);
4448 if (IS_ERR(rdev))
4449 continue;
4450
NeilBrownb2d444d2005-11-08 21:39:31 -08004451 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 MD_BUG();
4453 continue;
4454 }
4455 list_add(&rdev->same_set, &pending_raid_disks);
4456 }
4457 dev_cnt = 0;
4458
4459 autorun_devices(part);
4460}
4461
4462#endif
4463
4464static __exit void md_exit(void)
4465{
4466 mddev_t *mddev;
4467 struct list_head *tmp;
4468 int i;
4469 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4470 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4471 for (i=0; i < MAX_MD_DEVS; i++)
4472 devfs_remove("md/%d", i);
4473 for (i=0; i < MAX_MD_DEVS; i++)
4474 devfs_remove("md/d%d", i);
4475
4476 devfs_remove("md");
4477
4478 unregister_blkdev(MAJOR_NR,"md");
4479 unregister_blkdev(mdp_major, "mdp");
4480 unregister_reboot_notifier(&md_notifier);
4481 unregister_sysctl_table(raid_table_header);
4482 remove_proc_entry("mdstat", NULL);
4483 ITERATE_MDDEV(mddev,tmp) {
4484 struct gendisk *disk = mddev->gendisk;
4485 if (!disk)
4486 continue;
4487 export_array(mddev);
4488 del_gendisk(disk);
4489 put_disk(disk);
4490 mddev->gendisk = NULL;
4491 mddev_put(mddev);
4492 }
4493}
4494
4495module_init(md_init)
4496module_exit(md_exit)
4497
NeilBrownf91de922005-11-08 21:39:36 -08004498static int get_ro(char *buffer, struct kernel_param *kp)
4499{
4500 return sprintf(buffer, "%d", start_readonly);
4501}
4502static int set_ro(const char *val, struct kernel_param *kp)
4503{
4504 char *e;
4505 int num = simple_strtoul(val, &e, 10);
4506 if (*val && (*e == '\0' || *e == '\n')) {
4507 start_readonly = num;
4508 return 0;;
4509 }
4510 return -EINVAL;
4511}
4512
4513module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004514module_param(start_dirty_degraded, int, 0644);
4515
NeilBrownf91de922005-11-08 21:39:36 -08004516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517EXPORT_SYMBOL(register_md_personality);
4518EXPORT_SYMBOL(unregister_md_personality);
4519EXPORT_SYMBOL(md_error);
4520EXPORT_SYMBOL(md_done_sync);
4521EXPORT_SYMBOL(md_write_start);
4522EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523EXPORT_SYMBOL(md_register_thread);
4524EXPORT_SYMBOL(md_unregister_thread);
4525EXPORT_SYMBOL(md_wakeup_thread);
4526EXPORT_SYMBOL(md_print_devices);
4527EXPORT_SYMBOL(md_check_recovery);
4528MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07004529MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07004530MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);