blob: ee199d4625208e6c675a92cf2fc752a3d79f3260 [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
NeilBrownb15c2e52006-01-06 00:20:16 -08002057 err = mddev->pers->run(mddev);
2058 if (!err && mddev->pers->sync_request) {
2059 err = bitmap_create(mddev);
2060 if (err) {
2061 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2062 mdname(mddev), err);
2063 mddev->pers->stop(mddev);
2064 }
2065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (err) {
2067 printk(KERN_ERR "md: pers->run() failed ...\n");
2068 module_put(mddev->pers->owner);
2069 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002070 bitmap_destroy(mddev);
2071 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
NeilBrown411036f2005-11-08 21:39:40 -08002073 if (mddev->pers->sync_request)
2074 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08002075 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2076 mddev->ro = 0;
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 atomic_set(&mddev->writes_pending,0);
2079 mddev->safemode = 0;
2080 mddev->safemode_timer.function = md_safemode_timeout;
2081 mddev->safemode_timer.data = (unsigned long) mddev;
2082 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2083 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002084
2085 ITERATE_RDEV(mddev,rdev,tmp)
2086 if (rdev->raid_disk >= 0) {
2087 char nm[20];
2088 sprintf(nm, "rd%d", rdev->raid_disk);
2089 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002093 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 if (mddev->sb_dirty)
2096 md_update_sb(mddev);
2097
2098 set_capacity(disk, mddev->array_size<<1);
2099
2100 /* If we call blk_queue_make_request here, it will
2101 * re-initialise max_sectors etc which may have been
2102 * refined inside -> run. So just set the bits we need to set.
2103 * Most initialisation happended when we called
2104 * blk_queue_make_request(..., md_fail_request)
2105 * earlier.
2106 */
2107 mddev->queue->queuedata = mddev;
2108 mddev->queue->make_request_fn = mddev->pers->make_request;
2109
2110 mddev->changed = 1;
2111 return 0;
2112}
2113
2114static int restart_array(mddev_t *mddev)
2115{
2116 struct gendisk *disk = mddev->gendisk;
2117 int err;
2118
2119 /*
2120 * Complain if it has no devices
2121 */
2122 err = -ENXIO;
2123 if (list_empty(&mddev->disks))
2124 goto out;
2125
2126 if (mddev->pers) {
2127 err = -EBUSY;
2128 if (!mddev->ro)
2129 goto out;
2130
2131 mddev->safemode = 0;
2132 mddev->ro = 0;
2133 set_disk_ro(disk, 0);
2134
2135 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2136 mdname(mddev));
2137 /*
2138 * Kick recovery or resync if necessary
2139 */
2140 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2141 md_wakeup_thread(mddev->thread);
2142 err = 0;
2143 } else {
2144 printk(KERN_ERR "md: %s has no personality assigned.\n",
2145 mdname(mddev));
2146 err = -EINVAL;
2147 }
2148
2149out:
2150 return err;
2151}
2152
2153static int do_md_stop(mddev_t * mddev, int ro)
2154{
2155 int err = 0;
2156 struct gendisk *disk = mddev->gendisk;
2157
2158 if (mddev->pers) {
2159 if (atomic_read(&mddev->active)>2) {
2160 printk("md: %s still in use.\n",mdname(mddev));
2161 return -EBUSY;
2162 }
2163
2164 if (mddev->sync_thread) {
2165 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2166 md_unregister_thread(mddev->sync_thread);
2167 mddev->sync_thread = NULL;
2168 }
2169
2170 del_timer_sync(&mddev->safemode_timer);
2171
2172 invalidate_partition(disk, 0);
2173
2174 if (ro) {
2175 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08002176 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 goto out;
2178 mddev->ro = 1;
2179 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002180 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08002181 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if (mddev->ro)
2183 set_disk_ro(disk, 0);
2184 blk_queue_make_request(mddev->queue, md_fail_request);
2185 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08002186 if (mddev->pers->sync_request)
2187 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 module_put(mddev->pers->owner);
2190 mddev->pers = NULL;
2191 if (mddev->ro)
2192 mddev->ro = 0;
2193 }
2194 if (!mddev->in_sync) {
2195 /* mark array as shutdown cleanly */
2196 mddev->in_sync = 1;
2197 md_update_sb(mddev);
2198 }
2199 if (ro)
2200 set_disk_ro(disk, 1);
2201 }
NeilBrown32a76272005-06-21 17:17:14 -07002202
2203 bitmap_destroy(mddev);
2204 if (mddev->bitmap_file) {
2205 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2206 fput(mddev->bitmap_file);
2207 mddev->bitmap_file = NULL;
2208 }
NeilBrown92232142005-08-18 11:24:16 -07002209 mddev->bitmap_offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07002210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 /*
2212 * Free resources if final stop
2213 */
2214 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002215 mdk_rdev_t *rdev;
2216 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 struct gendisk *disk;
2218 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2219
NeilBrown86e6ffd2005-11-08 21:39:24 -08002220 ITERATE_RDEV(mddev,rdev,tmp)
2221 if (rdev->raid_disk >= 0) {
2222 char nm[20];
2223 sprintf(nm, "rd%d", rdev->raid_disk);
2224 sysfs_remove_link(&mddev->kobj, nm);
2225 }
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 export_array(mddev);
2228
2229 mddev->array_size = 0;
2230 disk = mddev->gendisk;
2231 if (disk)
2232 set_capacity(disk, 0);
2233 mddev->changed = 1;
2234 } else
2235 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2236 mdname(mddev));
2237 err = 0;
2238out:
2239 return err;
2240}
2241
2242static void autorun_array(mddev_t *mddev)
2243{
2244 mdk_rdev_t *rdev;
2245 struct list_head *tmp;
2246 int err;
2247
NeilBrowna757e642005-04-16 15:26:42 -07002248 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 printk(KERN_INFO "md: running: ");
2252
2253 ITERATE_RDEV(mddev,rdev,tmp) {
2254 char b[BDEVNAME_SIZE];
2255 printk("<%s>", bdevname(rdev->bdev,b));
2256 }
2257 printk("\n");
2258
2259 err = do_md_run (mddev);
2260 if (err) {
2261 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2262 do_md_stop (mddev, 0);
2263 }
2264}
2265
2266/*
2267 * lets try to run arrays based on all disks that have arrived
2268 * until now. (those are in pending_raid_disks)
2269 *
2270 * the method: pick the first pending disk, collect all disks with
2271 * the same UUID, remove all from the pending list and put them into
2272 * the 'same_array' list. Then order this list based on superblock
2273 * update time (freshest comes first), kick out 'old' disks and
2274 * compare superblocks. If everything's fine then run it.
2275 *
2276 * If "unit" is allocated, then bump its reference count
2277 */
2278static void autorun_devices(int part)
2279{
2280 struct list_head candidates;
2281 struct list_head *tmp;
2282 mdk_rdev_t *rdev0, *rdev;
2283 mddev_t *mddev;
2284 char b[BDEVNAME_SIZE];
2285
2286 printk(KERN_INFO "md: autorun ...\n");
2287 while (!list_empty(&pending_raid_disks)) {
2288 dev_t dev;
2289 rdev0 = list_entry(pending_raid_disks.next,
2290 mdk_rdev_t, same_set);
2291
2292 printk(KERN_INFO "md: considering %s ...\n",
2293 bdevname(rdev0->bdev,b));
2294 INIT_LIST_HEAD(&candidates);
2295 ITERATE_RDEV_PENDING(rdev,tmp)
2296 if (super_90_load(rdev, rdev0, 0) >= 0) {
2297 printk(KERN_INFO "md: adding %s ...\n",
2298 bdevname(rdev->bdev,b));
2299 list_move(&rdev->same_set, &candidates);
2300 }
2301 /*
2302 * now we have a set of devices, with all of them having
2303 * mostly sane superblocks. It's time to allocate the
2304 * mddev.
2305 */
2306 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2307 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2308 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2309 break;
2310 }
2311 if (part)
2312 dev = MKDEV(mdp_major,
2313 rdev0->preferred_minor << MdpMinorShift);
2314 else
2315 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2316
2317 md_probe(dev, NULL, NULL);
2318 mddev = mddev_find(dev);
2319 if (!mddev) {
2320 printk(KERN_ERR
2321 "md: cannot allocate memory for md drive.\n");
2322 break;
2323 }
2324 if (mddev_lock(mddev))
2325 printk(KERN_WARNING "md: %s locked, cannot run\n",
2326 mdname(mddev));
2327 else if (mddev->raid_disks || mddev->major_version
2328 || !list_empty(&mddev->disks)) {
2329 printk(KERN_WARNING
2330 "md: %s already running, cannot run %s\n",
2331 mdname(mddev), bdevname(rdev0->bdev,b));
2332 mddev_unlock(mddev);
2333 } else {
2334 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2335 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2336 list_del_init(&rdev->same_set);
2337 if (bind_rdev_to_array(rdev, mddev))
2338 export_rdev(rdev);
2339 }
2340 autorun_array(mddev);
2341 mddev_unlock(mddev);
2342 }
2343 /* on success, candidates will be empty, on error
2344 * it won't...
2345 */
2346 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2347 export_rdev(rdev);
2348 mddev_put(mddev);
2349 }
2350 printk(KERN_INFO "md: ... autorun DONE.\n");
2351}
2352
2353/*
2354 * import RAID devices based on one partition
2355 * if possible, the array gets run as well.
2356 */
2357
2358static int autostart_array(dev_t startdev)
2359{
2360 char b[BDEVNAME_SIZE];
2361 int err = -EINVAL, i;
2362 mdp_super_t *sb = NULL;
2363 mdk_rdev_t *start_rdev = NULL, *rdev;
2364
2365 start_rdev = md_import_device(startdev, 0, 0);
2366 if (IS_ERR(start_rdev))
2367 return err;
2368
2369
2370 /* NOTE: this can only work for 0.90.0 superblocks */
2371 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2372 if (sb->major_version != 0 ||
2373 sb->minor_version != 90 ) {
2374 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2375 export_rdev(start_rdev);
2376 return err;
2377 }
2378
NeilBrownb2d444d2005-11-08 21:39:31 -08002379 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 printk(KERN_WARNING
2381 "md: can not autostart based on faulty %s!\n",
2382 bdevname(start_rdev->bdev,b));
2383 export_rdev(start_rdev);
2384 return err;
2385 }
2386 list_add(&start_rdev->same_set, &pending_raid_disks);
2387
2388 for (i = 0; i < MD_SB_DISKS; i++) {
2389 mdp_disk_t *desc = sb->disks + i;
2390 dev_t dev = MKDEV(desc->major, desc->minor);
2391
2392 if (!dev)
2393 continue;
2394 if (dev == startdev)
2395 continue;
2396 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2397 continue;
2398 rdev = md_import_device(dev, 0, 0);
2399 if (IS_ERR(rdev))
2400 continue;
2401
2402 list_add(&rdev->same_set, &pending_raid_disks);
2403 }
2404
2405 /*
2406 * possibly return codes
2407 */
2408 autorun_devices(0);
2409 return 0;
2410
2411}
2412
2413
2414static int get_version(void __user * arg)
2415{
2416 mdu_version_t ver;
2417
2418 ver.major = MD_MAJOR_VERSION;
2419 ver.minor = MD_MINOR_VERSION;
2420 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2421
2422 if (copy_to_user(arg, &ver, sizeof(ver)))
2423 return -EFAULT;
2424
2425 return 0;
2426}
2427
2428static int get_array_info(mddev_t * mddev, void __user * arg)
2429{
2430 mdu_array_info_t info;
2431 int nr,working,active,failed,spare;
2432 mdk_rdev_t *rdev;
2433 struct list_head *tmp;
2434
2435 nr=working=active=failed=spare=0;
2436 ITERATE_RDEV(mddev,rdev,tmp) {
2437 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002438 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 failed++;
2440 else {
2441 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002442 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 active++;
2444 else
2445 spare++;
2446 }
2447 }
2448
2449 info.major_version = mddev->major_version;
2450 info.minor_version = mddev->minor_version;
2451 info.patch_version = MD_PATCHLEVEL_VERSION;
2452 info.ctime = mddev->ctime;
2453 info.level = mddev->level;
2454 info.size = mddev->size;
2455 info.nr_disks = nr;
2456 info.raid_disks = mddev->raid_disks;
2457 info.md_minor = mddev->md_minor;
2458 info.not_persistent= !mddev->persistent;
2459
2460 info.utime = mddev->utime;
2461 info.state = 0;
2462 if (mddev->in_sync)
2463 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07002464 if (mddev->bitmap && mddev->bitmap_offset)
2465 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 info.active_disks = active;
2467 info.working_disks = working;
2468 info.failed_disks = failed;
2469 info.spare_disks = spare;
2470
2471 info.layout = mddev->layout;
2472 info.chunk_size = mddev->chunk_size;
2473
2474 if (copy_to_user(arg, &info, sizeof(info)))
2475 return -EFAULT;
2476
2477 return 0;
2478}
2479
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002480static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07002481{
2482 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2483 char *ptr, *buf = NULL;
2484 int err = -ENOMEM;
2485
2486 file = kmalloc(sizeof(*file), GFP_KERNEL);
2487 if (!file)
2488 goto out;
2489
2490 /* bitmap disabled, zero the first byte and copy out */
2491 if (!mddev->bitmap || !mddev->bitmap->file) {
2492 file->pathname[0] = '\0';
2493 goto copy_out;
2494 }
2495
2496 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2497 if (!buf)
2498 goto out;
2499
2500 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2501 if (!ptr)
2502 goto out;
2503
2504 strcpy(file->pathname, ptr);
2505
2506copy_out:
2507 err = 0;
2508 if (copy_to_user(arg, file, sizeof(*file)))
2509 err = -EFAULT;
2510out:
2511 kfree(buf);
2512 kfree(file);
2513 return err;
2514}
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516static int get_disk_info(mddev_t * mddev, void __user * arg)
2517{
2518 mdu_disk_info_t info;
2519 unsigned int nr;
2520 mdk_rdev_t *rdev;
2521
2522 if (copy_from_user(&info, arg, sizeof(info)))
2523 return -EFAULT;
2524
2525 nr = info.number;
2526
2527 rdev = find_rdev_nr(mddev, nr);
2528 if (rdev) {
2529 info.major = MAJOR(rdev->bdev->bd_dev);
2530 info.minor = MINOR(rdev->bdev->bd_dev);
2531 info.raid_disk = rdev->raid_disk;
2532 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08002533 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002535 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 info.state |= (1<<MD_DISK_ACTIVE);
2537 info.state |= (1<<MD_DISK_SYNC);
2538 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002539 if (test_bit(WriteMostly, &rdev->flags))
2540 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 } else {
2542 info.major = info.minor = 0;
2543 info.raid_disk = -1;
2544 info.state = (1<<MD_DISK_REMOVED);
2545 }
2546
2547 if (copy_to_user(arg, &info, sizeof(info)))
2548 return -EFAULT;
2549
2550 return 0;
2551}
2552
2553static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2554{
2555 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2556 mdk_rdev_t *rdev;
2557 dev_t dev = MKDEV(info->major,info->minor);
2558
2559 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2560 return -EOVERFLOW;
2561
2562 if (!mddev->raid_disks) {
2563 int err;
2564 /* expecting a device which has a superblock */
2565 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2566 if (IS_ERR(rdev)) {
2567 printk(KERN_WARNING
2568 "md: md_import_device returned %ld\n",
2569 PTR_ERR(rdev));
2570 return PTR_ERR(rdev);
2571 }
2572 if (!list_empty(&mddev->disks)) {
2573 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2574 mdk_rdev_t, same_set);
2575 int err = super_types[mddev->major_version]
2576 .load_super(rdev, rdev0, mddev->minor_version);
2577 if (err < 0) {
2578 printk(KERN_WARNING
2579 "md: %s has different UUID to %s\n",
2580 bdevname(rdev->bdev,b),
2581 bdevname(rdev0->bdev,b2));
2582 export_rdev(rdev);
2583 return -EINVAL;
2584 }
2585 }
2586 err = bind_rdev_to_array(rdev, mddev);
2587 if (err)
2588 export_rdev(rdev);
2589 return err;
2590 }
2591
2592 /*
2593 * add_new_disk can be used once the array is assembled
2594 * to add "hot spares". They must already have a superblock
2595 * written
2596 */
2597 if (mddev->pers) {
2598 int err;
2599 if (!mddev->pers->hot_add_disk) {
2600 printk(KERN_WARNING
2601 "%s: personality does not support diskops!\n",
2602 mdname(mddev));
2603 return -EINVAL;
2604 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07002605 if (mddev->persistent)
2606 rdev = md_import_device(dev, mddev->major_version,
2607 mddev->minor_version);
2608 else
2609 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 if (IS_ERR(rdev)) {
2611 printk(KERN_WARNING
2612 "md: md_import_device returned %ld\n",
2613 PTR_ERR(rdev));
2614 return PTR_ERR(rdev);
2615 }
NeilBrown41158c72005-06-21 17:17:25 -07002616 /* set save_raid_disk if appropriate */
2617 if (!mddev->persistent) {
2618 if (info->state & (1<<MD_DISK_SYNC) &&
2619 info->raid_disk < mddev->raid_disks)
2620 rdev->raid_disk = info->raid_disk;
2621 else
2622 rdev->raid_disk = -1;
2623 } else
2624 super_types[mddev->major_version].
2625 validate_super(mddev, rdev);
2626 rdev->saved_raid_disk = rdev->raid_disk;
2627
NeilBrownb2d444d2005-11-08 21:39:31 -08002628 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002629 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2630 set_bit(WriteMostly, &rdev->flags);
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 rdev->raid_disk = -1;
2633 err = bind_rdev_to_array(rdev, mddev);
2634 if (err)
2635 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07002636
2637 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002638 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return err;
2640 }
2641
2642 /* otherwise, add_new_disk is only allowed
2643 * for major_version==0 superblocks
2644 */
2645 if (mddev->major_version != 0) {
2646 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2647 mdname(mddev));
2648 return -EINVAL;
2649 }
2650
2651 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2652 int err;
2653 rdev = md_import_device (dev, -1, 0);
2654 if (IS_ERR(rdev)) {
2655 printk(KERN_WARNING
2656 "md: error, md_import_device() returned %ld\n",
2657 PTR_ERR(rdev));
2658 return PTR_ERR(rdev);
2659 }
2660 rdev->desc_nr = info->number;
2661 if (info->raid_disk < mddev->raid_disks)
2662 rdev->raid_disk = info->raid_disk;
2663 else
2664 rdev->raid_disk = -1;
2665
NeilBrownb2d444d2005-11-08 21:39:31 -08002666 rdev->flags = 0;
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08002669 if (info->state & (1<<MD_DISK_SYNC))
2670 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002672 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2673 set_bit(WriteMostly, &rdev->flags);
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 err = bind_rdev_to_array(rdev, mddev);
2676 if (err) {
2677 export_rdev(rdev);
2678 return err;
2679 }
2680
2681 if (!mddev->persistent) {
2682 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2683 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2684 } else
2685 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2686 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2687
2688 if (!mddev->size || (mddev->size > rdev->size))
2689 mddev->size = rdev->size;
2690 }
2691
2692 return 0;
2693}
2694
2695static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2696{
2697 char b[BDEVNAME_SIZE];
2698 mdk_rdev_t *rdev;
2699
2700 if (!mddev->pers)
2701 return -ENODEV;
2702
2703 rdev = find_rdev(mddev, dev);
2704 if (!rdev)
2705 return -ENXIO;
2706
2707 if (rdev->raid_disk >= 0)
2708 goto busy;
2709
2710 kick_rdev_from_array(rdev);
2711 md_update_sb(mddev);
2712
2713 return 0;
2714busy:
2715 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2716 bdevname(rdev->bdev,b), mdname(mddev));
2717 return -EBUSY;
2718}
2719
2720static int hot_add_disk(mddev_t * mddev, dev_t dev)
2721{
2722 char b[BDEVNAME_SIZE];
2723 int err;
2724 unsigned int size;
2725 mdk_rdev_t *rdev;
2726
2727 if (!mddev->pers)
2728 return -ENODEV;
2729
2730 if (mddev->major_version != 0) {
2731 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2732 " version-0 superblocks.\n",
2733 mdname(mddev));
2734 return -EINVAL;
2735 }
2736 if (!mddev->pers->hot_add_disk) {
2737 printk(KERN_WARNING
2738 "%s: personality does not support diskops!\n",
2739 mdname(mddev));
2740 return -EINVAL;
2741 }
2742
2743 rdev = md_import_device (dev, -1, 0);
2744 if (IS_ERR(rdev)) {
2745 printk(KERN_WARNING
2746 "md: error, md_import_device() returned %ld\n",
2747 PTR_ERR(rdev));
2748 return -EINVAL;
2749 }
2750
2751 if (mddev->persistent)
2752 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2753 else
2754 rdev->sb_offset =
2755 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2756
2757 size = calc_dev_size(rdev, mddev->chunk_size);
2758 rdev->size = size;
2759
2760 if (size < mddev->size) {
2761 printk(KERN_WARNING
2762 "%s: disk size %llu blocks < array size %llu\n",
2763 mdname(mddev), (unsigned long long)size,
2764 (unsigned long long)mddev->size);
2765 err = -ENOSPC;
2766 goto abort_export;
2767 }
2768
NeilBrownb2d444d2005-11-08 21:39:31 -08002769 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 printk(KERN_WARNING
2771 "md: can not hot-add faulty %s disk to %s!\n",
2772 bdevname(rdev->bdev,b), mdname(mddev));
2773 err = -EINVAL;
2774 goto abort_export;
2775 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002776 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 rdev->desc_nr = -1;
2778 bind_rdev_to_array(rdev, mddev);
2779
2780 /*
2781 * The rest should better be atomic, we can have disk failures
2782 * noticed in interrupt contexts ...
2783 */
2784
2785 if (rdev->desc_nr == mddev->max_disks) {
2786 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2787 mdname(mddev));
2788 err = -EBUSY;
2789 goto abort_unbind_export;
2790 }
2791
2792 rdev->raid_disk = -1;
2793
2794 md_update_sb(mddev);
2795
2796 /*
2797 * Kick recovery, maybe this spare has to be added to the
2798 * array immediately.
2799 */
2800 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2801 md_wakeup_thread(mddev->thread);
2802
2803 return 0;
2804
2805abort_unbind_export:
2806 unbind_rdev_from_array(rdev);
2807
2808abort_export:
2809 export_rdev(rdev);
2810 return err;
2811}
2812
NeilBrown32a76272005-06-21 17:17:14 -07002813/* similar to deny_write_access, but accounts for our holding a reference
2814 * to the file ourselves */
2815static int deny_bitmap_write_access(struct file * file)
2816{
2817 struct inode *inode = file->f_mapping->host;
2818
2819 spin_lock(&inode->i_lock);
2820 if (atomic_read(&inode->i_writecount) > 1) {
2821 spin_unlock(&inode->i_lock);
2822 return -ETXTBSY;
2823 }
2824 atomic_set(&inode->i_writecount, -1);
2825 spin_unlock(&inode->i_lock);
2826
2827 return 0;
2828}
2829
2830static int set_bitmap_file(mddev_t *mddev, int fd)
2831{
2832 int err;
2833
NeilBrown36fa3062005-09-09 16:23:45 -07002834 if (mddev->pers) {
2835 if (!mddev->pers->quiesce)
2836 return -EBUSY;
2837 if (mddev->recovery || mddev->sync_thread)
2838 return -EBUSY;
2839 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07002840 }
2841
NeilBrown36fa3062005-09-09 16:23:45 -07002842
2843 if (fd >= 0) {
2844 if (mddev->bitmap)
2845 return -EEXIST; /* cannot add when bitmap is present */
2846 mddev->bitmap_file = fget(fd);
2847
2848 if (mddev->bitmap_file == NULL) {
2849 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2850 mdname(mddev));
2851 return -EBADF;
2852 }
2853
2854 err = deny_bitmap_write_access(mddev->bitmap_file);
2855 if (err) {
2856 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2857 mdname(mddev));
2858 fput(mddev->bitmap_file);
2859 mddev->bitmap_file = NULL;
2860 return err;
2861 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002862 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07002863 } else if (mddev->bitmap == NULL)
2864 return -ENOENT; /* cannot remove what isn't there */
2865 err = 0;
2866 if (mddev->pers) {
2867 mddev->pers->quiesce(mddev, 1);
2868 if (fd >= 0)
2869 err = bitmap_create(mddev);
2870 if (fd < 0 || err)
2871 bitmap_destroy(mddev);
2872 mddev->pers->quiesce(mddev, 0);
2873 } else if (fd < 0) {
2874 if (mddev->bitmap_file)
2875 fput(mddev->bitmap_file);
2876 mddev->bitmap_file = NULL;
2877 }
2878
NeilBrown32a76272005-06-21 17:17:14 -07002879 return err;
2880}
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882/*
2883 * set_array_info is used two different ways
2884 * The original usage is when creating a new array.
2885 * In this usage, raid_disks is > 0 and it together with
2886 * level, size, not_persistent,layout,chunksize determine the
2887 * shape of the array.
2888 * This will always create an array with a type-0.90.0 superblock.
2889 * The newer usage is when assembling an array.
2890 * In this case raid_disks will be 0, and the major_version field is
2891 * use to determine which style super-blocks are to be found on the devices.
2892 * The minor and patch _version numbers are also kept incase the
2893 * super_block handler wishes to interpret them.
2894 */
2895static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2896{
2897
2898 if (info->raid_disks == 0) {
2899 /* just setting version number for superblock loading */
2900 if (info->major_version < 0 ||
2901 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2902 super_types[info->major_version].name == NULL) {
2903 /* maybe try to auto-load a module? */
2904 printk(KERN_INFO
2905 "md: superblock version %d not known\n",
2906 info->major_version);
2907 return -EINVAL;
2908 }
2909 mddev->major_version = info->major_version;
2910 mddev->minor_version = info->minor_version;
2911 mddev->patch_version = info->patch_version;
2912 return 0;
2913 }
2914 mddev->major_version = MD_MAJOR_VERSION;
2915 mddev->minor_version = MD_MINOR_VERSION;
2916 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2917 mddev->ctime = get_seconds();
2918
2919 mddev->level = info->level;
2920 mddev->size = info->size;
2921 mddev->raid_disks = info->raid_disks;
2922 /* don't set md_minor, it is determined by which /dev/md* was
2923 * openned
2924 */
2925 if (info->state & (1<<MD_SB_CLEAN))
2926 mddev->recovery_cp = MaxSector;
2927 else
2928 mddev->recovery_cp = 0;
2929 mddev->persistent = ! info->not_persistent;
2930
2931 mddev->layout = info->layout;
2932 mddev->chunk_size = info->chunk_size;
2933
2934 mddev->max_disks = MD_SB_DISKS;
2935
2936 mddev->sb_dirty = 1;
2937
NeilBrownb2a27032005-11-28 13:44:12 -08002938 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
2939 mddev->bitmap_offset = 0;
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 /*
2942 * Generate a 128 bit UUID
2943 */
2944 get_random_bytes(mddev->uuid, 16);
2945
2946 return 0;
2947}
2948
2949/*
2950 * update_array_info is used to change the configuration of an
2951 * on-line array.
2952 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2953 * fields in the info are checked against the array.
2954 * Any differences that cannot be handled will cause an error.
2955 * Normally, only one change can be managed at a time.
2956 */
2957static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2958{
2959 int rv = 0;
2960 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07002961 int state = 0;
2962
2963 /* calculate expected state,ignoring low bits */
2964 if (mddev->bitmap && mddev->bitmap_offset)
2965 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
2967 if (mddev->major_version != info->major_version ||
2968 mddev->minor_version != info->minor_version ||
2969/* mddev->patch_version != info->patch_version || */
2970 mddev->ctime != info->ctime ||
2971 mddev->level != info->level ||
2972/* mddev->layout != info->layout || */
2973 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07002974 mddev->chunk_size != info->chunk_size ||
2975 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2976 ((state^info->state) & 0xfffffe00)
2977 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 return -EINVAL;
2979 /* Check there is only one change */
2980 if (mddev->size != info->size) cnt++;
2981 if (mddev->raid_disks != info->raid_disks) cnt++;
2982 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07002983 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (cnt == 0) return 0;
2985 if (cnt > 1) return -EINVAL;
2986
2987 if (mddev->layout != info->layout) {
2988 /* Change layout
2989 * we don't need to do anything at the md level, the
2990 * personality will take care of it all.
2991 */
2992 if (mddev->pers->reconfig == NULL)
2993 return -EINVAL;
2994 else
2995 return mddev->pers->reconfig(mddev, info->layout, -1);
2996 }
2997 if (mddev->size != info->size) {
2998 mdk_rdev_t * rdev;
2999 struct list_head *tmp;
3000 if (mddev->pers->resize == NULL)
3001 return -EINVAL;
3002 /* The "size" is the amount of each device that is used.
3003 * This can only make sense for arrays with redundancy.
3004 * linear and raid0 always use whatever space is available
3005 * We can only consider changing the size if no resync
3006 * or reconstruction is happening, and if the new size
3007 * is acceptable. It must fit before the sb_offset or,
3008 * if that is <data_offset, it must fit before the
3009 * size of each device.
3010 * If size is zero, we find the largest size that fits.
3011 */
3012 if (mddev->sync_thread)
3013 return -EBUSY;
3014 ITERATE_RDEV(mddev,rdev,tmp) {
3015 sector_t avail;
3016 int fit = (info->size == 0);
3017 if (rdev->sb_offset > rdev->data_offset)
3018 avail = (rdev->sb_offset*2) - rdev->data_offset;
3019 else
3020 avail = get_capacity(rdev->bdev->bd_disk)
3021 - rdev->data_offset;
3022 if (fit && (info->size == 0 || info->size > avail/2))
3023 info->size = avail/2;
3024 if (avail < ((sector_t)info->size << 1))
3025 return -ENOSPC;
3026 }
3027 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
3028 if (!rv) {
3029 struct block_device *bdev;
3030
3031 bdev = bdget_disk(mddev->gendisk, 0);
3032 if (bdev) {
3033 down(&bdev->bd_inode->i_sem);
3034 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3035 up(&bdev->bd_inode->i_sem);
3036 bdput(bdev);
3037 }
3038 }
3039 }
3040 if (mddev->raid_disks != info->raid_disks) {
3041 /* change the number of raid disks */
3042 if (mddev->pers->reshape == NULL)
3043 return -EINVAL;
3044 if (info->raid_disks <= 0 ||
3045 info->raid_disks >= mddev->max_disks)
3046 return -EINVAL;
3047 if (mddev->sync_thread)
3048 return -EBUSY;
3049 rv = mddev->pers->reshape(mddev, info->raid_disks);
3050 if (!rv) {
3051 struct block_device *bdev;
3052
3053 bdev = bdget_disk(mddev->gendisk, 0);
3054 if (bdev) {
3055 down(&bdev->bd_inode->i_sem);
3056 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3057 up(&bdev->bd_inode->i_sem);
3058 bdput(bdev);
3059 }
3060 }
3061 }
NeilBrown36fa3062005-09-09 16:23:45 -07003062 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3063 if (mddev->pers->quiesce == NULL)
3064 return -EINVAL;
3065 if (mddev->recovery || mddev->sync_thread)
3066 return -EBUSY;
3067 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3068 /* add the bitmap */
3069 if (mddev->bitmap)
3070 return -EEXIST;
3071 if (mddev->default_bitmap_offset == 0)
3072 return -EINVAL;
3073 mddev->bitmap_offset = mddev->default_bitmap_offset;
3074 mddev->pers->quiesce(mddev, 1);
3075 rv = bitmap_create(mddev);
3076 if (rv)
3077 bitmap_destroy(mddev);
3078 mddev->pers->quiesce(mddev, 0);
3079 } else {
3080 /* remove the bitmap */
3081 if (!mddev->bitmap)
3082 return -ENOENT;
3083 if (mddev->bitmap->file)
3084 return -EINVAL;
3085 mddev->pers->quiesce(mddev, 1);
3086 bitmap_destroy(mddev);
3087 mddev->pers->quiesce(mddev, 0);
3088 mddev->bitmap_offset = 0;
3089 }
3090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 md_update_sb(mddev);
3092 return rv;
3093}
3094
3095static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3096{
3097 mdk_rdev_t *rdev;
3098
3099 if (mddev->pers == NULL)
3100 return -ENODEV;
3101
3102 rdev = find_rdev(mddev, dev);
3103 if (!rdev)
3104 return -ENODEV;
3105
3106 md_error(mddev, rdev);
3107 return 0;
3108}
3109
3110static int md_ioctl(struct inode *inode, struct file *file,
3111 unsigned int cmd, unsigned long arg)
3112{
3113 int err = 0;
3114 void __user *argp = (void __user *)arg;
3115 struct hd_geometry __user *loc = argp;
3116 mddev_t *mddev = NULL;
3117
3118 if (!capable(CAP_SYS_ADMIN))
3119 return -EACCES;
3120
3121 /*
3122 * Commands dealing with the RAID driver but not any
3123 * particular array:
3124 */
3125 switch (cmd)
3126 {
3127 case RAID_VERSION:
3128 err = get_version(argp);
3129 goto done;
3130
3131 case PRINT_RAID_DEBUG:
3132 err = 0;
3133 md_print_devices();
3134 goto done;
3135
3136#ifndef MODULE
3137 case RAID_AUTORUN:
3138 err = 0;
3139 autostart_arrays(arg);
3140 goto done;
3141#endif
3142 default:;
3143 }
3144
3145 /*
3146 * Commands creating/starting a new array:
3147 */
3148
3149 mddev = inode->i_bdev->bd_disk->private_data;
3150
3151 if (!mddev) {
3152 BUG();
3153 goto abort;
3154 }
3155
3156
3157 if (cmd == START_ARRAY) {
3158 /* START_ARRAY doesn't need to lock the array as autostart_array
3159 * does the locking, and it could even be a different array
3160 */
3161 static int cnt = 3;
3162 if (cnt > 0 ) {
3163 printk(KERN_WARNING
3164 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08003165 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 current->comm, current->pid);
3167 cnt--;
3168 }
3169 err = autostart_array(new_decode_dev(arg));
3170 if (err) {
3171 printk(KERN_WARNING "md: autostart failed!\n");
3172 goto abort;
3173 }
3174 goto done;
3175 }
3176
3177 err = mddev_lock(mddev);
3178 if (err) {
3179 printk(KERN_INFO
3180 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3181 err, cmd);
3182 goto abort;
3183 }
3184
3185 switch (cmd)
3186 {
3187 case SET_ARRAY_INFO:
3188 {
3189 mdu_array_info_t info;
3190 if (!arg)
3191 memset(&info, 0, sizeof(info));
3192 else if (copy_from_user(&info, argp, sizeof(info))) {
3193 err = -EFAULT;
3194 goto abort_unlock;
3195 }
3196 if (mddev->pers) {
3197 err = update_array_info(mddev, &info);
3198 if (err) {
3199 printk(KERN_WARNING "md: couldn't update"
3200 " array info. %d\n", err);
3201 goto abort_unlock;
3202 }
3203 goto done_unlock;
3204 }
3205 if (!list_empty(&mddev->disks)) {
3206 printk(KERN_WARNING
3207 "md: array %s already has disks!\n",
3208 mdname(mddev));
3209 err = -EBUSY;
3210 goto abort_unlock;
3211 }
3212 if (mddev->raid_disks) {
3213 printk(KERN_WARNING
3214 "md: array %s already initialised!\n",
3215 mdname(mddev));
3216 err = -EBUSY;
3217 goto abort_unlock;
3218 }
3219 err = set_array_info(mddev, &info);
3220 if (err) {
3221 printk(KERN_WARNING "md: couldn't set"
3222 " array info. %d\n", err);
3223 goto abort_unlock;
3224 }
3225 }
3226 goto done_unlock;
3227
3228 default:;
3229 }
3230
3231 /*
3232 * Commands querying/configuring an existing array:
3233 */
NeilBrown32a76272005-06-21 17:17:14 -07003234 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3235 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3236 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3237 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 err = -ENODEV;
3239 goto abort_unlock;
3240 }
3241
3242 /*
3243 * Commands even a read-only array can execute:
3244 */
3245 switch (cmd)
3246 {
3247 case GET_ARRAY_INFO:
3248 err = get_array_info(mddev, argp);
3249 goto done_unlock;
3250
NeilBrown32a76272005-06-21 17:17:14 -07003251 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003252 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003253 goto done_unlock;
3254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 case GET_DISK_INFO:
3256 err = get_disk_info(mddev, argp);
3257 goto done_unlock;
3258
3259 case RESTART_ARRAY_RW:
3260 err = restart_array(mddev);
3261 goto done_unlock;
3262
3263 case STOP_ARRAY:
3264 err = do_md_stop (mddev, 0);
3265 goto done_unlock;
3266
3267 case STOP_ARRAY_RO:
3268 err = do_md_stop (mddev, 1);
3269 goto done_unlock;
3270
3271 /*
3272 * We have a problem here : there is no easy way to give a CHS
3273 * virtual geometry. We currently pretend that we have a 2 heads
3274 * 4 sectors (with a BIG number of cylinders...). This drives
3275 * dosfs just mad... ;-)
3276 */
3277 case HDIO_GETGEO:
3278 if (!loc) {
3279 err = -EINVAL;
3280 goto abort_unlock;
3281 }
3282 err = put_user (2, (char __user *) &loc->heads);
3283 if (err)
3284 goto abort_unlock;
3285 err = put_user (4, (char __user *) &loc->sectors);
3286 if (err)
3287 goto abort_unlock;
3288 err = put_user(get_capacity(mddev->gendisk)/8,
3289 (short __user *) &loc->cylinders);
3290 if (err)
3291 goto abort_unlock;
3292 err = put_user (get_start_sect(inode->i_bdev),
3293 (long __user *) &loc->start);
3294 goto done_unlock;
3295 }
3296
3297 /*
3298 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08003299 * superblock, so we do not allow them on read-only arrays.
3300 * However non-MD ioctls (e.g. get-size) will still come through
3301 * here and hit the 'default' below, so only disallow
3302 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 */
NeilBrownf91de922005-11-08 21:39:36 -08003304 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3305 mddev->ro && mddev->pers) {
3306 if (mddev->ro == 2) {
3307 mddev->ro = 0;
3308 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3309 md_wakeup_thread(mddev->thread);
3310
3311 } else {
3312 err = -EROFS;
3313 goto abort_unlock;
3314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
3316
3317 switch (cmd)
3318 {
3319 case ADD_NEW_DISK:
3320 {
3321 mdu_disk_info_t info;
3322 if (copy_from_user(&info, argp, sizeof(info)))
3323 err = -EFAULT;
3324 else
3325 err = add_new_disk(mddev, &info);
3326 goto done_unlock;
3327 }
3328
3329 case HOT_REMOVE_DISK:
3330 err = hot_remove_disk(mddev, new_decode_dev(arg));
3331 goto done_unlock;
3332
3333 case HOT_ADD_DISK:
3334 err = hot_add_disk(mddev, new_decode_dev(arg));
3335 goto done_unlock;
3336
3337 case SET_DISK_FAULTY:
3338 err = set_disk_faulty(mddev, new_decode_dev(arg));
3339 goto done_unlock;
3340
3341 case RUN_ARRAY:
3342 err = do_md_run (mddev);
3343 goto done_unlock;
3344
NeilBrown32a76272005-06-21 17:17:14 -07003345 case SET_BITMAP_FILE:
3346 err = set_bitmap_file(mddev, (int)arg);
3347 goto done_unlock;
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 default:
3350 if (_IOC_TYPE(cmd) == MD_MAJOR)
3351 printk(KERN_WARNING "md: %s(pid %d) used"
3352 " obsolete MD ioctl, upgrade your"
3353 " software to use new ictls.\n",
3354 current->comm, current->pid);
3355 err = -EINVAL;
3356 goto abort_unlock;
3357 }
3358
3359done_unlock:
3360abort_unlock:
3361 mddev_unlock(mddev);
3362
3363 return err;
3364done:
3365 if (err)
3366 MD_BUG();
3367abort:
3368 return err;
3369}
3370
3371static int md_open(struct inode *inode, struct file *file)
3372{
3373 /*
3374 * Succeed if we can lock the mddev, which confirms that
3375 * it isn't being stopped right now.
3376 */
3377 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3378 int err;
3379
3380 if ((err = mddev_lock(mddev)))
3381 goto out;
3382
3383 err = 0;
3384 mddev_get(mddev);
3385 mddev_unlock(mddev);
3386
3387 check_disk_change(inode->i_bdev);
3388 out:
3389 return err;
3390}
3391
3392static int md_release(struct inode *inode, struct file * file)
3393{
3394 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3395
3396 if (!mddev)
3397 BUG();
3398 mddev_put(mddev);
3399
3400 return 0;
3401}
3402
3403static int md_media_changed(struct gendisk *disk)
3404{
3405 mddev_t *mddev = disk->private_data;
3406
3407 return mddev->changed;
3408}
3409
3410static int md_revalidate(struct gendisk *disk)
3411{
3412 mddev_t *mddev = disk->private_data;
3413
3414 mddev->changed = 0;
3415 return 0;
3416}
3417static struct block_device_operations md_fops =
3418{
3419 .owner = THIS_MODULE,
3420 .open = md_open,
3421 .release = md_release,
3422 .ioctl = md_ioctl,
3423 .media_changed = md_media_changed,
3424 .revalidate_disk= md_revalidate,
3425};
3426
Adrian Bunk75c96f82005-05-05 16:16:09 -07003427static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428{
3429 mdk_thread_t *thread = arg;
3430
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 /*
3432 * md_thread is a 'system-thread', it's priority should be very
3433 * high. We avoid resource deadlocks individually in each
3434 * raid personality. (RAID5 does preallocation) We also use RR and
3435 * the very same RT priority as kswapd, thus we will never get
3436 * into a priority inversion deadlock.
3437 *
3438 * we definitely have to have equal or higher priority than
3439 * bdflush, otherwise bdflush will deadlock if there are too
3440 * many dirty RAID5 blocks.
3441 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
NeilBrown6985c432005-10-19 21:23:47 -07003443 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07003444 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
NeilBrown93588e22005-11-15 00:09:12 -08003446 /* We need to wait INTERRUPTIBLE so that
3447 * we don't add to the load-average.
3448 * That means we need to be sure no signals are
3449 * pending
3450 */
3451 if (signal_pending(current))
3452 flush_signals(current);
3453
3454 wait_event_interruptible_timeout
3455 (thread->wqueue,
3456 test_bit(THREAD_WAKEUP, &thread->flags)
3457 || kthread_should_stop(),
3458 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003459 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 clear_bit(THREAD_WAKEUP, &thread->flags);
3462
NeilBrown787453c2005-11-08 21:39:43 -08003463 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
NeilBrowna6fb0932005-09-09 16:23:56 -07003465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return 0;
3467}
3468
3469void md_wakeup_thread(mdk_thread_t *thread)
3470{
3471 if (thread) {
3472 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3473 set_bit(THREAD_WAKEUP, &thread->flags);
3474 wake_up(&thread->wqueue);
3475 }
3476}
3477
3478mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3479 const char *name)
3480{
3481 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
NeilBrowna6fb0932005-09-09 16:23:56 -07003483 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 if (!thread)
3485 return NULL;
3486
3487 memset(thread, 0, sizeof(mdk_thread_t));
3488 init_waitqueue_head(&thread->wqueue);
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 thread->run = run;
3491 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07003492 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07003493 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07003494 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 kfree(thread);
3496 return NULL;
3497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 return thread;
3499}
3500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501void md_unregister_thread(mdk_thread_t *thread)
3502{
NeilBrownd28446f2005-04-16 15:26:41 -07003503 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07003504
3505 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 kfree(thread);
3507}
3508
3509void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3510{
3511 if (!mddev) {
3512 MD_BUG();
3513 return;
3514 }
3515
NeilBrownb2d444d2005-11-08 21:39:31 -08003516 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 return;
NeilBrown32a76272005-06-21 17:17:14 -07003518/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3520 mdname(mddev),
3521 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3522 __builtin_return_address(0),__builtin_return_address(1),
3523 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07003524*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 if (!mddev->pers->error_handler)
3526 return;
3527 mddev->pers->error_handler(mddev,rdev);
3528 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3529 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3530 md_wakeup_thread(mddev->thread);
3531}
3532
3533/* seq_file implementation /proc/mdstat */
3534
3535static void status_unused(struct seq_file *seq)
3536{
3537 int i = 0;
3538 mdk_rdev_t *rdev;
3539 struct list_head *tmp;
3540
3541 seq_printf(seq, "unused devices: ");
3542
3543 ITERATE_RDEV_PENDING(rdev,tmp) {
3544 char b[BDEVNAME_SIZE];
3545 i++;
3546 seq_printf(seq, "%s ",
3547 bdevname(rdev->bdev,b));
3548 }
3549 if (!i)
3550 seq_printf(seq, "<none>");
3551
3552 seq_printf(seq, "\n");
3553}
3554
3555
3556static void status_resync(struct seq_file *seq, mddev_t * mddev)
3557{
3558 unsigned long max_blocks, resync, res, dt, db, rt;
3559
3560 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3561
3562 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3563 max_blocks = mddev->resync_max_sectors >> 1;
3564 else
3565 max_blocks = mddev->size;
3566
3567 /*
3568 * Should not happen.
3569 */
3570 if (!max_blocks) {
3571 MD_BUG();
3572 return;
3573 }
3574 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3575 {
3576 int i, x = res/50, y = 20-x;
3577 seq_printf(seq, "[");
3578 for (i = 0; i < x; i++)
3579 seq_printf(seq, "=");
3580 seq_printf(seq, ">");
3581 for (i = 0; i < y; i++)
3582 seq_printf(seq, ".");
3583 seq_printf(seq, "] ");
3584 }
3585 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3586 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3587 "resync" : "recovery"),
3588 res/10, res % 10, resync, max_blocks);
3589
3590 /*
3591 * We do not want to overflow, so the order of operands and
3592 * the * 100 / 100 trick are important. We do a +1 to be
3593 * safe against division by zero. We only estimate anyway.
3594 *
3595 * dt: time from mark until now
3596 * db: blocks written from mark until now
3597 * rt: remaining time
3598 */
3599 dt = ((jiffies - mddev->resync_mark) / HZ);
3600 if (!dt) dt++;
3601 db = resync - (mddev->resync_mark_cnt/2);
3602 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3603
3604 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3605
3606 seq_printf(seq, " speed=%ldK/sec", db/dt);
3607}
3608
3609static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3610{
3611 struct list_head *tmp;
3612 loff_t l = *pos;
3613 mddev_t *mddev;
3614
3615 if (l >= 0x10000)
3616 return NULL;
3617 if (!l--)
3618 /* header */
3619 return (void*)1;
3620
3621 spin_lock(&all_mddevs_lock);
3622 list_for_each(tmp,&all_mddevs)
3623 if (!l--) {
3624 mddev = list_entry(tmp, mddev_t, all_mddevs);
3625 mddev_get(mddev);
3626 spin_unlock(&all_mddevs_lock);
3627 return mddev;
3628 }
3629 spin_unlock(&all_mddevs_lock);
3630 if (!l--)
3631 return (void*)2;/* tail */
3632 return NULL;
3633}
3634
3635static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3636{
3637 struct list_head *tmp;
3638 mddev_t *next_mddev, *mddev = v;
3639
3640 ++*pos;
3641 if (v == (void*)2)
3642 return NULL;
3643
3644 spin_lock(&all_mddevs_lock);
3645 if (v == (void*)1)
3646 tmp = all_mddevs.next;
3647 else
3648 tmp = mddev->all_mddevs.next;
3649 if (tmp != &all_mddevs)
3650 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3651 else {
3652 next_mddev = (void*)2;
3653 *pos = 0x10000;
3654 }
3655 spin_unlock(&all_mddevs_lock);
3656
3657 if (v != (void*)1)
3658 mddev_put(mddev);
3659 return next_mddev;
3660
3661}
3662
3663static void md_seq_stop(struct seq_file *seq, void *v)
3664{
3665 mddev_t *mddev = v;
3666
3667 if (mddev && v != (void*)1 && v != (void*)2)
3668 mddev_put(mddev);
3669}
3670
3671static int md_seq_show(struct seq_file *seq, void *v)
3672{
3673 mddev_t *mddev = v;
3674 sector_t size;
3675 struct list_head *tmp2;
3676 mdk_rdev_t *rdev;
3677 int i;
NeilBrown32a76272005-06-21 17:17:14 -07003678 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
3680 if (v == (void*)1) {
3681 seq_printf(seq, "Personalities : ");
3682 spin_lock(&pers_lock);
3683 for (i = 0; i < MAX_PERSONALITY; i++)
3684 if (pers[i])
3685 seq_printf(seq, "[%s] ", pers[i]->name);
3686
3687 spin_unlock(&pers_lock);
3688 seq_printf(seq, "\n");
3689 return 0;
3690 }
3691 if (v == (void*)2) {
3692 status_unused(seq);
3693 return 0;
3694 }
3695
3696 if (mddev_lock(mddev)!=0)
3697 return -EINTR;
3698 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3699 seq_printf(seq, "%s : %sactive", mdname(mddev),
3700 mddev->pers ? "" : "in");
3701 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08003702 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08003704 if (mddev->ro==2)
3705 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 seq_printf(seq, " %s", mddev->pers->name);
3707 }
3708
3709 size = 0;
3710 ITERATE_RDEV(mddev,rdev,tmp2) {
3711 char b[BDEVNAME_SIZE];
3712 seq_printf(seq, " %s[%d]",
3713 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003714 if (test_bit(WriteMostly, &rdev->flags))
3715 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08003716 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 seq_printf(seq, "(F)");
3718 continue;
NeilBrownb325a322005-09-09 16:24:00 -07003719 } else if (rdev->raid_disk < 0)
3720 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 size += rdev->size;
3722 }
3723
3724 if (!list_empty(&mddev->disks)) {
3725 if (mddev->pers)
3726 seq_printf(seq, "\n %llu blocks",
3727 (unsigned long long)mddev->array_size);
3728 else
3729 seq_printf(seq, "\n %llu blocks",
3730 (unsigned long long)size);
3731 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07003732 if (mddev->persistent) {
3733 if (mddev->major_version != 0 ||
3734 mddev->minor_version != 90) {
3735 seq_printf(seq," super %d.%d",
3736 mddev->major_version,
3737 mddev->minor_version);
3738 }
3739 } else
3740 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
3742 if (mddev->pers) {
3743 mddev->pers->status (seq, mddev);
3744 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08003745 if (mddev->pers->sync_request) {
3746 if (mddev->curr_resync > 2) {
3747 status_resync (seq, mddev);
3748 seq_printf(seq, "\n ");
3749 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3750 seq_printf(seq, "\tresync=DELAYED\n ");
3751 else if (mddev->recovery_cp < MaxSector)
3752 seq_printf(seq, "\tresync=PENDING\n ");
3753 }
NeilBrown32a76272005-06-21 17:17:14 -07003754 } else
3755 seq_printf(seq, "\n ");
3756
3757 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07003758 unsigned long chunk_kb;
3759 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07003760 spin_lock_irqsave(&bitmap->lock, flags);
3761 chunk_kb = bitmap->chunksize >> 10;
3762 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3763 "%lu%s chunk",
3764 bitmap->pages - bitmap->missing_pages,
3765 bitmap->pages,
3766 (bitmap->pages - bitmap->missing_pages)
3767 << (PAGE_SHIFT - 10),
3768 chunk_kb ? chunk_kb : bitmap->chunksize,
3769 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07003770 if (bitmap->file) {
3771 seq_printf(seq, ", file: ");
3772 seq_path(seq, bitmap->file->f_vfsmnt,
3773 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07003774 }
NeilBrown78d742d2005-06-21 17:17:15 -07003775
NeilBrown32a76272005-06-21 17:17:14 -07003776 seq_printf(seq, "\n");
3777 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
3779
3780 seq_printf(seq, "\n");
3781 }
3782 mddev_unlock(mddev);
3783
3784 return 0;
3785}
3786
3787static struct seq_operations md_seq_ops = {
3788 .start = md_seq_start,
3789 .next = md_seq_next,
3790 .stop = md_seq_stop,
3791 .show = md_seq_show,
3792};
3793
3794static int md_seq_open(struct inode *inode, struct file *file)
3795{
3796 int error;
3797
3798 error = seq_open(file, &md_seq_ops);
3799 return error;
3800}
3801
3802static struct file_operations md_seq_fops = {
3803 .open = md_seq_open,
3804 .read = seq_read,
3805 .llseek = seq_lseek,
3806 .release = seq_release,
3807};
3808
3809int register_md_personality(int pnum, mdk_personality_t *p)
3810{
3811 if (pnum >= MAX_PERSONALITY) {
3812 printk(KERN_ERR
3813 "md: tried to install personality %s as nr %d, but max is %lu\n",
3814 p->name, pnum, MAX_PERSONALITY-1);
3815 return -EINVAL;
3816 }
3817
3818 spin_lock(&pers_lock);
3819 if (pers[pnum]) {
3820 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 return -EBUSY;
3822 }
3823
3824 pers[pnum] = p;
3825 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3826 spin_unlock(&pers_lock);
3827 return 0;
3828}
3829
3830int unregister_md_personality(int pnum)
3831{
NeilBrowna757e642005-04-16 15:26:42 -07003832 if (pnum >= MAX_PERSONALITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
3835 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3836 spin_lock(&pers_lock);
3837 pers[pnum] = NULL;
3838 spin_unlock(&pers_lock);
3839 return 0;
3840}
3841
3842static int is_mddev_idle(mddev_t *mddev)
3843{
3844 mdk_rdev_t * rdev;
3845 struct list_head *tmp;
3846 int idle;
3847 unsigned long curr_events;
3848
3849 idle = 1;
3850 ITERATE_RDEV(mddev,rdev,tmp) {
3851 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01003852 curr_events = disk_stat_read(disk, sectors[0]) +
3853 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08003855 /* The difference between curr_events and last_events
3856 * will be affected by any new non-sync IO (making
3857 * curr_events bigger) and any difference in the amount of
3858 * in-flight syncio (making current_events bigger or smaller)
3859 * The amount in-flight is currently limited to
3860 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
3861 * which is at most 4096 sectors.
3862 * These numbers are fairly fragile and should be made
3863 * more robust, probably by enforcing the
3864 * 'window size' that md_do_sync sort-of uses.
3865 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 * Note: the following is an unsigned comparison.
3867 */
NeilBrownc0e48522005-11-18 01:11:01 -08003868 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 rdev->last_events = curr_events;
3870 idle = 0;
3871 }
3872 }
3873 return idle;
3874}
3875
3876void md_done_sync(mddev_t *mddev, int blocks, int ok)
3877{
3878 /* another "blocks" (512byte) blocks have been synced */
3879 atomic_sub(blocks, &mddev->recovery_active);
3880 wake_up(&mddev->recovery_wait);
3881 if (!ok) {
3882 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3883 md_wakeup_thread(mddev->thread);
3884 // stop recovery, signal do_sync ....
3885 }
3886}
3887
3888
NeilBrown06d91a52005-06-21 17:17:12 -07003889/* md_write_start(mddev, bi)
3890 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07003891 * in superblock) before writing, schedule a superblock update
3892 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07003893 */
NeilBrown3d310eb2005-06-21 17:17:26 -07003894void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895{
NeilBrown06d91a52005-06-21 17:17:12 -07003896 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07003897 return;
NeilBrown06d91a52005-06-21 17:17:12 -07003898
NeilBrownf91de922005-11-08 21:39:36 -08003899 BUG_ON(mddev->ro == 1);
3900 if (mddev->ro == 2) {
3901 /* need to switch to read/write */
3902 mddev->ro = 0;
3903 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3904 md_wakeup_thread(mddev->thread);
3905 }
NeilBrown06d91a52005-06-21 17:17:12 -07003906 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07003907 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08003908 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07003909 if (mddev->in_sync) {
3910 mddev->in_sync = 0;
3911 mddev->sb_dirty = 1;
3912 md_wakeup_thread(mddev->thread);
3913 }
NeilBrowna9701a32005-11-08 21:39:34 -08003914 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07003915 }
NeilBrown3d310eb2005-06-21 17:17:26 -07003916 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917}
3918
3919void md_write_end(mddev_t *mddev)
3920{
3921 if (atomic_dec_and_test(&mddev->writes_pending)) {
3922 if (mddev->safemode == 2)
3923 md_wakeup_thread(mddev->thread);
3924 else
3925 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3926 }
3927}
3928
Adrian Bunk75c96f82005-05-05 16:16:09 -07003929static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931#define SYNC_MARKS 10
3932#define SYNC_MARK_STEP (3*HZ)
3933static void md_do_sync(mddev_t *mddev)
3934{
3935 mddev_t *mddev2;
3936 unsigned int currspeed = 0,
3937 window;
NeilBrown57afd892005-06-21 17:17:13 -07003938 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 unsigned long mark[SYNC_MARKS];
3940 sector_t mark_cnt[SYNC_MARKS];
3941 int last_mark,m;
3942 struct list_head *tmp;
3943 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07003944 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
3946 /* just incase thread restarts... */
3947 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3948 return;
3949
3950 /* we overload curr_resync somewhat here.
3951 * 0 == not engaged in resync at all
3952 * 2 == checking that there is no conflict with another sync
3953 * 1 == like 2, but have yielded to allow conflicting resync to
3954 * commense
3955 * other == active in resync - this many blocks
3956 *
3957 * Before starting a resync we must have set curr_resync to
3958 * 2, and then checked that every "conflicting" array has curr_resync
3959 * less than ours. When we find one that is the same or higher
3960 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3961 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3962 * This will mean we have to start checking from the beginning again.
3963 *
3964 */
3965
3966 do {
3967 mddev->curr_resync = 2;
3968
3969 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08003970 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07003971 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 goto skip;
3973 }
3974 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 if (mddev2 == mddev)
3976 continue;
3977 if (mddev2->curr_resync &&
3978 match_mddev_units(mddev,mddev2)) {
3979 DEFINE_WAIT(wq);
3980 if (mddev < mddev2 && mddev->curr_resync == 2) {
3981 /* arbitrarily yield */
3982 mddev->curr_resync = 1;
3983 wake_up(&resync_wait);
3984 }
3985 if (mddev > mddev2 && mddev->curr_resync == 1)
3986 /* no need to wait here, we can wait the next
3987 * time 'round when curr_resync == 2
3988 */
3989 continue;
NeilBrown787453c2005-11-08 21:39:43 -08003990 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
3991 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07003992 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 printk(KERN_INFO "md: delaying resync of %s"
3994 " until %s has finished resync (they"
3995 " share one or more physical units)\n",
3996 mdname(mddev), mdname(mddev2));
3997 mddev_put(mddev2);
3998 schedule();
3999 finish_wait(&resync_wait, &wq);
4000 goto try_again;
4001 }
4002 finish_wait(&resync_wait, &wq);
4003 }
4004 }
4005 } while (mddev->curr_resync < 2);
4006
NeilBrown9d888832005-11-08 21:39:26 -08004007 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07004009 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 */
4011 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08004012 mddev->resync_mismatches = 0;
4013 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 /* recovery follows the physical size of devices */
4015 max_sectors = mddev->size << 1;
4016
4017 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4018 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
4019 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
Adrian Bunk338cec32005-09-10 00:26:54 -07004020 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 "(but not more than %d KB/sec) for reconstruction.\n",
4022 sysctl_speed_limit_max);
4023
4024 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07004025 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08004026 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4027 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 j = mddev->recovery_cp;
4029 else
4030 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07004031 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 for (m = 0; m < SYNC_MARKS; m++) {
4033 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004034 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 }
4036 last_mark = 0;
4037 mddev->resync_mark = mark[last_mark];
4038 mddev->resync_mark_cnt = mark_cnt[last_mark];
4039
4040 /*
4041 * Tune reconstruction:
4042 */
4043 window = 32*(PAGE_SIZE/512);
4044 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4045 window/2,(unsigned long long) max_sectors/2);
4046
4047 atomic_set(&mddev->recovery_active, 0);
4048 init_waitqueue_head(&mddev->recovery_wait);
4049 last_check = 0;
4050
4051 if (j>2) {
4052 printk(KERN_INFO
4053 "md: resuming recovery of %s from checkpoint.\n",
4054 mdname(mddev));
4055 mddev->curr_resync = j;
4056 }
4057
4058 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07004059 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
NeilBrown57afd892005-06-21 17:17:13 -07004061 skipped = 0;
4062 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4063 currspeed < sysctl_speed_limit_min);
4064 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4066 goto out;
4067 }
NeilBrown57afd892005-06-21 17:17:13 -07004068
4069 if (!skipped) { /* actual IO requested */
4070 io_sectors += sectors;
4071 atomic_add(sectors, &mddev->recovery_active);
4072 }
4073
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 j += sectors;
4075 if (j>1) mddev->curr_resync = j;
4076
NeilBrown57afd892005-06-21 17:17:13 -07004077
4078 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 continue;
4080
NeilBrown57afd892005-06-21 17:17:13 -07004081 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
4083 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4084 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4085 break;
4086
4087 repeat:
4088 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4089 /* step marks */
4090 int next = (last_mark+1) % SYNC_MARKS;
4091
4092 mddev->resync_mark = mark[next];
4093 mddev->resync_mark_cnt = mark_cnt[next];
4094 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004095 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 last_mark = next;
4097 }
4098
4099
NeilBrown787453c2005-11-08 21:39:43 -08004100 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 /*
4102 * got a signal, exit.
4103 */
4104 printk(KERN_INFO
4105 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4107 goto out;
4108 }
4109
4110 /*
4111 * this loop exits only if either when we are slower than
4112 * the 'hard' speed limit, or the system was IO-idle for
4113 * a jiffy.
4114 * the system might be non-idle CPU-wise, but we only care
4115 * about not overloading the IO subsystem. (things like an
4116 * e2fsck being done on the RAID array should execute fast)
4117 */
4118 mddev->queue->unplug_fn(mddev->queue);
4119 cond_resched();
4120
NeilBrown57afd892005-06-21 17:17:13 -07004121 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4122 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
4124 if (currspeed > sysctl_speed_limit_min) {
4125 if ((currspeed > sysctl_speed_limit_max) ||
4126 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08004127 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 goto repeat;
4129 }
4130 }
4131 }
4132 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4133 /*
4134 * this also signals 'finished resyncing' to md_stop
4135 */
4136 out:
4137 mddev->queue->unplug_fn(mddev->queue);
4138
4139 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4140
4141 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004142 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
4144 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4145 mddev->curr_resync > 2 &&
4146 mddev->curr_resync >= mddev->recovery_cp) {
4147 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4148 printk(KERN_INFO
4149 "md: checkpointing recovery of %s.\n",
4150 mdname(mddev));
4151 mddev->recovery_cp = mddev->curr_resync;
4152 } else
4153 mddev->recovery_cp = MaxSector;
4154 }
4155
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 skip:
4157 mddev->curr_resync = 0;
4158 wake_up(&resync_wait);
4159 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4160 md_wakeup_thread(mddev->thread);
4161}
4162
4163
4164/*
4165 * This routine is regularly called by all per-raid-array threads to
4166 * deal with generic issues like resync and super-block update.
4167 * Raid personalities that don't have a thread (linear/raid0) do not
4168 * need this as they never do any recovery or update the superblock.
4169 *
4170 * It does not do any resync itself, but rather "forks" off other threads
4171 * to do that as needed.
4172 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4173 * "->recovery" and create a thread at ->sync_thread.
4174 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4175 * and wakeups up this thread which will reap the thread and finish up.
4176 * This thread also removes any faulty devices (with nr_pending == 0).
4177 *
4178 * The overall approach is:
4179 * 1/ if the superblock needs updating, update it.
4180 * 2/ If a recovery thread is running, don't do anything else.
4181 * 3/ If recovery has finished, clean up, possibly marking spares active.
4182 * 4/ If there are any faulty devices, remove them.
4183 * 5/ If array is degraded, try to add spares devices
4184 * 6/ If array has spares or is not in-sync, start a resync thread.
4185 */
4186void md_check_recovery(mddev_t *mddev)
4187{
4188 mdk_rdev_t *rdev;
4189 struct list_head *rtmp;
4190
4191
NeilBrown5f404022005-06-21 17:17:16 -07004192 if (mddev->bitmap)
4193 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
4195 if (mddev->ro)
4196 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004197
4198 if (signal_pending(current)) {
4199 if (mddev->pers->sync_request) {
4200 printk(KERN_INFO "md: %s in immediate safe mode\n",
4201 mdname(mddev));
4202 mddev->safemode = 2;
4203 }
4204 flush_signals(current);
4205 }
4206
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 if ( ! (
4208 mddev->sb_dirty ||
4209 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004210 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4211 (mddev->safemode == 1) ||
4212 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4213 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 ))
4215 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004216
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 if (mddev_trylock(mddev)==0) {
4218 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004219
NeilBrowna9701a32005-11-08 21:39:34 -08004220 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004221 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4222 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4223 mddev->in_sync = 1;
4224 mddev->sb_dirty = 1;
4225 }
4226 if (mddev->safemode == 1)
4227 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08004228 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004229
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 if (mddev->sb_dirty)
4231 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004232
NeilBrown06d91a52005-06-21 17:17:12 -07004233
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4235 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4236 /* resync/recovery still happening */
4237 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4238 goto unlock;
4239 }
4240 if (mddev->sync_thread) {
4241 /* resync has finished, collect result */
4242 md_unregister_thread(mddev->sync_thread);
4243 mddev->sync_thread = NULL;
4244 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4245 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4246 /* success...*/
4247 /* activate any spares */
4248 mddev->pers->spare_active(mddev);
4249 }
4250 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004251
4252 /* if array is no-longer degraded, then any saved_raid_disk
4253 * information must be scrapped
4254 */
4255 if (!mddev->degraded)
4256 ITERATE_RDEV(mddev,rdev,rtmp)
4257 rdev->saved_raid_disk = -1;
4258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 mddev->recovery = 0;
4260 /* flag recovery needed just to double check */
4261 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4262 goto unlock;
4263 }
NeilBrown24dd4692005-11-08 21:39:26 -08004264 /* Clear some bits that don't mean anything, but
4265 * might be left set
4266 */
4267 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4268 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4269 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4270 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
4272 /* no recovery is running.
4273 * remove any failed drives, then
4274 * add spares if possible.
4275 * Spare are also removed and re-added, to allow
4276 * the personality to fail the re-add.
4277 */
4278 ITERATE_RDEV(mddev,rdev,rtmp)
4279 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004280 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004282 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4283 char nm[20];
4284 sprintf(nm,"rd%d", rdev->raid_disk);
4285 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 }
4289
4290 if (mddev->degraded) {
4291 ITERATE_RDEV(mddev,rdev,rtmp)
4292 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08004293 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004294 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4295 char nm[20];
4296 sprintf(nm, "rd%d", rdev->raid_disk);
4297 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 spares++;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004299 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 break;
4301 }
4302 }
4303
NeilBrown24dd4692005-11-08 21:39:26 -08004304 if (spares) {
4305 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4306 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4307 } else if (mddev->recovery_cp < MaxSector) {
4308 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4309 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4310 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08004312
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 if (mddev->pers->sync_request) {
4314 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07004315 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4316 /* We are adding a device or devices to an array
4317 * which has the bitmap stored on all devices.
4318 * So make sure all bitmap pages get written
4319 */
4320 bitmap_write_all(mddev->bitmap);
4321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 mddev->sync_thread = md_register_thread(md_do_sync,
4323 mddev,
4324 "%s_resync");
4325 if (!mddev->sync_thread) {
4326 printk(KERN_ERR "%s: could not start resync"
4327 " thread...\n",
4328 mdname(mddev));
4329 /* leave the spares where they are, it shouldn't hurt */
4330 mddev->recovery = 0;
4331 } else {
4332 md_wakeup_thread(mddev->sync_thread);
4333 }
4334 }
4335 unlock:
4336 mddev_unlock(mddev);
4337 }
4338}
4339
Adrian Bunk75c96f82005-05-05 16:16:09 -07004340static int md_notify_reboot(struct notifier_block *this,
4341 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
4343 struct list_head *tmp;
4344 mddev_t *mddev;
4345
4346 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4347
4348 printk(KERN_INFO "md: stopping all md devices.\n");
4349
4350 ITERATE_MDDEV(mddev,tmp)
4351 if (mddev_trylock(mddev)==0)
4352 do_md_stop (mddev, 1);
4353 /*
4354 * certain more exotic SCSI devices are known to be
4355 * volatile wrt too early system reboots. While the
4356 * right place to handle this issue is the given
4357 * driver, we do want to have a safe RAID driver ...
4358 */
4359 mdelay(1000*1);
4360 }
4361 return NOTIFY_DONE;
4362}
4363
Adrian Bunk75c96f82005-05-05 16:16:09 -07004364static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 .notifier_call = md_notify_reboot,
4366 .next = NULL,
4367 .priority = INT_MAX, /* before any real devices */
4368};
4369
4370static void md_geninit(void)
4371{
4372 struct proc_dir_entry *p;
4373
4374 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4375
4376 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4377 if (p)
4378 p->proc_fops = &md_seq_fops;
4379}
4380
Adrian Bunk75c96f82005-05-05 16:16:09 -07004381static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382{
4383 int minor;
4384
4385 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4386 " MD_SB_DISKS=%d\n",
4387 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4388 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08004389 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07004390 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
4392 if (register_blkdev(MAJOR_NR, "md"))
4393 return -1;
4394 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4395 unregister_blkdev(MAJOR_NR, "md");
4396 return -1;
4397 }
4398 devfs_mk_dir("md");
4399 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4400 md_probe, NULL, NULL);
4401 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4402 md_probe, NULL, NULL);
4403
4404 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4405 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4406 S_IFBLK|S_IRUSR|S_IWUSR,
4407 "md/%d", minor);
4408
4409 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4410 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4411 S_IFBLK|S_IRUSR|S_IWUSR,
4412 "md/mdp%d", minor);
4413
4414
4415 register_reboot_notifier(&md_notifier);
4416 raid_table_header = register_sysctl_table(raid_root_table, 1);
4417
4418 md_geninit();
4419 return (0);
4420}
4421
4422
4423#ifndef MODULE
4424
4425/*
4426 * Searches all registered partitions for autorun RAID arrays
4427 * at boot time.
4428 */
4429static dev_t detected_devices[128];
4430static int dev_cnt;
4431
4432void md_autodetect_dev(dev_t dev)
4433{
4434 if (dev_cnt >= 0 && dev_cnt < 127)
4435 detected_devices[dev_cnt++] = dev;
4436}
4437
4438
4439static void autostart_arrays(int part)
4440{
4441 mdk_rdev_t *rdev;
4442 int i;
4443
4444 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4445
4446 for (i = 0; i < dev_cnt; i++) {
4447 dev_t dev = detected_devices[i];
4448
4449 rdev = md_import_device(dev,0, 0);
4450 if (IS_ERR(rdev))
4451 continue;
4452
NeilBrownb2d444d2005-11-08 21:39:31 -08004453 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 MD_BUG();
4455 continue;
4456 }
4457 list_add(&rdev->same_set, &pending_raid_disks);
4458 }
4459 dev_cnt = 0;
4460
4461 autorun_devices(part);
4462}
4463
4464#endif
4465
4466static __exit void md_exit(void)
4467{
4468 mddev_t *mddev;
4469 struct list_head *tmp;
4470 int i;
4471 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4472 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4473 for (i=0; i < MAX_MD_DEVS; i++)
4474 devfs_remove("md/%d", i);
4475 for (i=0; i < MAX_MD_DEVS; i++)
4476 devfs_remove("md/d%d", i);
4477
4478 devfs_remove("md");
4479
4480 unregister_blkdev(MAJOR_NR,"md");
4481 unregister_blkdev(mdp_major, "mdp");
4482 unregister_reboot_notifier(&md_notifier);
4483 unregister_sysctl_table(raid_table_header);
4484 remove_proc_entry("mdstat", NULL);
4485 ITERATE_MDDEV(mddev,tmp) {
4486 struct gendisk *disk = mddev->gendisk;
4487 if (!disk)
4488 continue;
4489 export_array(mddev);
4490 del_gendisk(disk);
4491 put_disk(disk);
4492 mddev->gendisk = NULL;
4493 mddev_put(mddev);
4494 }
4495}
4496
4497module_init(md_init)
4498module_exit(md_exit)
4499
NeilBrownf91de922005-11-08 21:39:36 -08004500static int get_ro(char *buffer, struct kernel_param *kp)
4501{
4502 return sprintf(buffer, "%d", start_readonly);
4503}
4504static int set_ro(const char *val, struct kernel_param *kp)
4505{
4506 char *e;
4507 int num = simple_strtoul(val, &e, 10);
4508 if (*val && (*e == '\0' || *e == '\n')) {
4509 start_readonly = num;
4510 return 0;;
4511 }
4512 return -EINVAL;
4513}
4514
4515module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004516module_param(start_dirty_degraded, int, 0644);
4517
NeilBrownf91de922005-11-08 21:39:36 -08004518
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519EXPORT_SYMBOL(register_md_personality);
4520EXPORT_SYMBOL(unregister_md_personality);
4521EXPORT_SYMBOL(md_error);
4522EXPORT_SYMBOL(md_done_sync);
4523EXPORT_SYMBOL(md_write_start);
4524EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525EXPORT_SYMBOL(md_register_thread);
4526EXPORT_SYMBOL(md_unregister_thread);
4527EXPORT_SYMBOL(md_wakeup_thread);
4528EXPORT_SYMBOL(md_print_devices);
4529EXPORT_SYMBOL(md_check_recovery);
4530MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07004531MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07004532MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);