blob: cd12fca73b0d89fa0ee83e711f97c7d08e9cf63d [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)
1732 return sprintf(page, "RAID-%d\n", mddev->level);
1733 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
1940
1941static int do_md_run(mddev_t * mddev)
1942{
1943 int pnum, err;
1944 int chunk_size;
1945 struct list_head *tmp;
1946 mdk_rdev_t *rdev;
1947 struct gendisk *disk;
1948 char b[BDEVNAME_SIZE];
1949
NeilBrowna757e642005-04-16 15:26:42 -07001950 if (list_empty(&mddev->disks))
1951 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 if (mddev->pers)
1955 return -EBUSY;
1956
1957 /*
1958 * Analyze all RAID superblock(s)
1959 */
NeilBrowna757e642005-04-16 15:26:42 -07001960 if (!mddev->raid_disks)
1961 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 chunk_size = mddev->chunk_size;
1964 pnum = level_to_pers(mddev->level);
1965
1966 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1967 if (!chunk_size) {
1968 /*
1969 * 'default chunksize' in the old md code used to
1970 * be PAGE_SIZE, baaad.
1971 * we abort here to be on the safe side. We don't
1972 * want to continue the bad practice.
1973 */
1974 printk(KERN_ERR
1975 "no chunksize specified, see 'man raidtab'\n");
1976 return -EINVAL;
1977 }
1978 if (chunk_size > MAX_CHUNK_SIZE) {
1979 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1980 chunk_size, MAX_CHUNK_SIZE);
1981 return -EINVAL;
1982 }
1983 /*
1984 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1985 */
1986 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07001987 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return -EINVAL;
1989 }
1990 if (chunk_size < PAGE_SIZE) {
1991 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1992 chunk_size, PAGE_SIZE);
1993 return -EINVAL;
1994 }
1995
1996 /* devices must have minimum size of one chunk */
1997 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001998 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 continue;
2000 if (rdev->size < chunk_size / 1024) {
2001 printk(KERN_WARNING
2002 "md: Dev %s smaller than chunk_size:"
2003 " %lluk < %dk\n",
2004 bdevname(rdev->bdev,b),
2005 (unsigned long long)rdev->size,
2006 chunk_size / 1024);
2007 return -EINVAL;
2008 }
2009 }
2010 }
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012#ifdef CONFIG_KMOD
2013 if (!pers[pnum])
2014 {
2015 request_module("md-personality-%d", pnum);
2016 }
2017#endif
2018
2019 /*
2020 * Drop all container device buffers, from now on
2021 * the only valid external interface is through the md
2022 * device.
2023 * Also find largest hardsector size
2024 */
2025 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002026 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 continue;
2028 sync_blockdev(rdev->bdev);
2029 invalidate_bdev(rdev->bdev, 0);
2030 }
2031
2032 md_probe(mddev->unit, NULL, NULL);
2033 disk = mddev->gendisk;
2034 if (!disk)
2035 return -ENOMEM;
2036
2037 spin_lock(&pers_lock);
2038 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
2039 spin_unlock(&pers_lock);
2040 printk(KERN_WARNING "md: personality %d is not loaded!\n",
2041 pnum);
2042 return -EINVAL;
2043 }
2044
2045 mddev->pers = pers[pnum];
2046 spin_unlock(&pers_lock);
2047
NeilBrown657390d2005-08-26 18:34:16 -07002048 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08002050 mddev->barriers_work = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
NeilBrownf91de922005-11-08 21:39:36 -08002052 if (start_readonly)
2053 mddev->ro = 2; /* read-only, but switch on first write */
2054
NeilBrown32a76272005-06-21 17:17:14 -07002055 /* before we start the array running, initialise the bitmap */
2056 err = bitmap_create(mddev);
2057 if (err)
2058 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2059 mdname(mddev), err);
2060 else
2061 err = mddev->pers->run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (err) {
2063 printk(KERN_ERR "md: pers->run() failed ...\n");
2064 module_put(mddev->pers->owner);
2065 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002066 bitmap_destroy(mddev);
2067 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
NeilBrown411036f2005-11-08 21:39:40 -08002069 if (mddev->pers->sync_request)
2070 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08002071 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2072 mddev->ro = 0;
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 atomic_set(&mddev->writes_pending,0);
2075 mddev->safemode = 0;
2076 mddev->safemode_timer.function = md_safemode_timeout;
2077 mddev->safemode_timer.data = (unsigned long) mddev;
2078 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2079 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002080
2081 ITERATE_RDEV(mddev,rdev,tmp)
2082 if (rdev->raid_disk >= 0) {
2083 char nm[20];
2084 sprintf(nm, "rd%d", rdev->raid_disk);
2085 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002089 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 if (mddev->sb_dirty)
2092 md_update_sb(mddev);
2093
2094 set_capacity(disk, mddev->array_size<<1);
2095
2096 /* If we call blk_queue_make_request here, it will
2097 * re-initialise max_sectors etc which may have been
2098 * refined inside -> run. So just set the bits we need to set.
2099 * Most initialisation happended when we called
2100 * blk_queue_make_request(..., md_fail_request)
2101 * earlier.
2102 */
2103 mddev->queue->queuedata = mddev;
2104 mddev->queue->make_request_fn = mddev->pers->make_request;
2105
2106 mddev->changed = 1;
2107 return 0;
2108}
2109
2110static int restart_array(mddev_t *mddev)
2111{
2112 struct gendisk *disk = mddev->gendisk;
2113 int err;
2114
2115 /*
2116 * Complain if it has no devices
2117 */
2118 err = -ENXIO;
2119 if (list_empty(&mddev->disks))
2120 goto out;
2121
2122 if (mddev->pers) {
2123 err = -EBUSY;
2124 if (!mddev->ro)
2125 goto out;
2126
2127 mddev->safemode = 0;
2128 mddev->ro = 0;
2129 set_disk_ro(disk, 0);
2130
2131 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2132 mdname(mddev));
2133 /*
2134 * Kick recovery or resync if necessary
2135 */
2136 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2137 md_wakeup_thread(mddev->thread);
2138 err = 0;
2139 } else {
2140 printk(KERN_ERR "md: %s has no personality assigned.\n",
2141 mdname(mddev));
2142 err = -EINVAL;
2143 }
2144
2145out:
2146 return err;
2147}
2148
2149static int do_md_stop(mddev_t * mddev, int ro)
2150{
2151 int err = 0;
2152 struct gendisk *disk = mddev->gendisk;
2153
2154 if (mddev->pers) {
2155 if (atomic_read(&mddev->active)>2) {
2156 printk("md: %s still in use.\n",mdname(mddev));
2157 return -EBUSY;
2158 }
2159
2160 if (mddev->sync_thread) {
2161 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2162 md_unregister_thread(mddev->sync_thread);
2163 mddev->sync_thread = NULL;
2164 }
2165
2166 del_timer_sync(&mddev->safemode_timer);
2167
2168 invalidate_partition(disk, 0);
2169
2170 if (ro) {
2171 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08002172 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 goto out;
2174 mddev->ro = 1;
2175 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002176 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08002177 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (mddev->ro)
2179 set_disk_ro(disk, 0);
2180 blk_queue_make_request(mddev->queue, md_fail_request);
2181 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08002182 if (mddev->pers->sync_request)
2183 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 module_put(mddev->pers->owner);
2186 mddev->pers = NULL;
2187 if (mddev->ro)
2188 mddev->ro = 0;
2189 }
2190 if (!mddev->in_sync) {
2191 /* mark array as shutdown cleanly */
2192 mddev->in_sync = 1;
2193 md_update_sb(mddev);
2194 }
2195 if (ro)
2196 set_disk_ro(disk, 1);
2197 }
NeilBrown32a76272005-06-21 17:17:14 -07002198
2199 bitmap_destroy(mddev);
2200 if (mddev->bitmap_file) {
2201 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2202 fput(mddev->bitmap_file);
2203 mddev->bitmap_file = NULL;
2204 }
NeilBrown92232142005-08-18 11:24:16 -07002205 mddev->bitmap_offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07002206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 /*
2208 * Free resources if final stop
2209 */
2210 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002211 mdk_rdev_t *rdev;
2212 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 struct gendisk *disk;
2214 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2215
NeilBrown86e6ffd2005-11-08 21:39:24 -08002216 ITERATE_RDEV(mddev,rdev,tmp)
2217 if (rdev->raid_disk >= 0) {
2218 char nm[20];
2219 sprintf(nm, "rd%d", rdev->raid_disk);
2220 sysfs_remove_link(&mddev->kobj, nm);
2221 }
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 export_array(mddev);
2224
2225 mddev->array_size = 0;
2226 disk = mddev->gendisk;
2227 if (disk)
2228 set_capacity(disk, 0);
2229 mddev->changed = 1;
2230 } else
2231 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2232 mdname(mddev));
2233 err = 0;
2234out:
2235 return err;
2236}
2237
2238static void autorun_array(mddev_t *mddev)
2239{
2240 mdk_rdev_t *rdev;
2241 struct list_head *tmp;
2242 int err;
2243
NeilBrowna757e642005-04-16 15:26:42 -07002244 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 printk(KERN_INFO "md: running: ");
2248
2249 ITERATE_RDEV(mddev,rdev,tmp) {
2250 char b[BDEVNAME_SIZE];
2251 printk("<%s>", bdevname(rdev->bdev,b));
2252 }
2253 printk("\n");
2254
2255 err = do_md_run (mddev);
2256 if (err) {
2257 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2258 do_md_stop (mddev, 0);
2259 }
2260}
2261
2262/*
2263 * lets try to run arrays based on all disks that have arrived
2264 * until now. (those are in pending_raid_disks)
2265 *
2266 * the method: pick the first pending disk, collect all disks with
2267 * the same UUID, remove all from the pending list and put them into
2268 * the 'same_array' list. Then order this list based on superblock
2269 * update time (freshest comes first), kick out 'old' disks and
2270 * compare superblocks. If everything's fine then run it.
2271 *
2272 * If "unit" is allocated, then bump its reference count
2273 */
2274static void autorun_devices(int part)
2275{
2276 struct list_head candidates;
2277 struct list_head *tmp;
2278 mdk_rdev_t *rdev0, *rdev;
2279 mddev_t *mddev;
2280 char b[BDEVNAME_SIZE];
2281
2282 printk(KERN_INFO "md: autorun ...\n");
2283 while (!list_empty(&pending_raid_disks)) {
2284 dev_t dev;
2285 rdev0 = list_entry(pending_raid_disks.next,
2286 mdk_rdev_t, same_set);
2287
2288 printk(KERN_INFO "md: considering %s ...\n",
2289 bdevname(rdev0->bdev,b));
2290 INIT_LIST_HEAD(&candidates);
2291 ITERATE_RDEV_PENDING(rdev,tmp)
2292 if (super_90_load(rdev, rdev0, 0) >= 0) {
2293 printk(KERN_INFO "md: adding %s ...\n",
2294 bdevname(rdev->bdev,b));
2295 list_move(&rdev->same_set, &candidates);
2296 }
2297 /*
2298 * now we have a set of devices, with all of them having
2299 * mostly sane superblocks. It's time to allocate the
2300 * mddev.
2301 */
2302 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2303 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2304 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2305 break;
2306 }
2307 if (part)
2308 dev = MKDEV(mdp_major,
2309 rdev0->preferred_minor << MdpMinorShift);
2310 else
2311 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2312
2313 md_probe(dev, NULL, NULL);
2314 mddev = mddev_find(dev);
2315 if (!mddev) {
2316 printk(KERN_ERR
2317 "md: cannot allocate memory for md drive.\n");
2318 break;
2319 }
2320 if (mddev_lock(mddev))
2321 printk(KERN_WARNING "md: %s locked, cannot run\n",
2322 mdname(mddev));
2323 else if (mddev->raid_disks || mddev->major_version
2324 || !list_empty(&mddev->disks)) {
2325 printk(KERN_WARNING
2326 "md: %s already running, cannot run %s\n",
2327 mdname(mddev), bdevname(rdev0->bdev,b));
2328 mddev_unlock(mddev);
2329 } else {
2330 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2331 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2332 list_del_init(&rdev->same_set);
2333 if (bind_rdev_to_array(rdev, mddev))
2334 export_rdev(rdev);
2335 }
2336 autorun_array(mddev);
2337 mddev_unlock(mddev);
2338 }
2339 /* on success, candidates will be empty, on error
2340 * it won't...
2341 */
2342 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2343 export_rdev(rdev);
2344 mddev_put(mddev);
2345 }
2346 printk(KERN_INFO "md: ... autorun DONE.\n");
2347}
2348
2349/*
2350 * import RAID devices based on one partition
2351 * if possible, the array gets run as well.
2352 */
2353
2354static int autostart_array(dev_t startdev)
2355{
2356 char b[BDEVNAME_SIZE];
2357 int err = -EINVAL, i;
2358 mdp_super_t *sb = NULL;
2359 mdk_rdev_t *start_rdev = NULL, *rdev;
2360
2361 start_rdev = md_import_device(startdev, 0, 0);
2362 if (IS_ERR(start_rdev))
2363 return err;
2364
2365
2366 /* NOTE: this can only work for 0.90.0 superblocks */
2367 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2368 if (sb->major_version != 0 ||
2369 sb->minor_version != 90 ) {
2370 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2371 export_rdev(start_rdev);
2372 return err;
2373 }
2374
NeilBrownb2d444d2005-11-08 21:39:31 -08002375 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 printk(KERN_WARNING
2377 "md: can not autostart based on faulty %s!\n",
2378 bdevname(start_rdev->bdev,b));
2379 export_rdev(start_rdev);
2380 return err;
2381 }
2382 list_add(&start_rdev->same_set, &pending_raid_disks);
2383
2384 for (i = 0; i < MD_SB_DISKS; i++) {
2385 mdp_disk_t *desc = sb->disks + i;
2386 dev_t dev = MKDEV(desc->major, desc->minor);
2387
2388 if (!dev)
2389 continue;
2390 if (dev == startdev)
2391 continue;
2392 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2393 continue;
2394 rdev = md_import_device(dev, 0, 0);
2395 if (IS_ERR(rdev))
2396 continue;
2397
2398 list_add(&rdev->same_set, &pending_raid_disks);
2399 }
2400
2401 /*
2402 * possibly return codes
2403 */
2404 autorun_devices(0);
2405 return 0;
2406
2407}
2408
2409
2410static int get_version(void __user * arg)
2411{
2412 mdu_version_t ver;
2413
2414 ver.major = MD_MAJOR_VERSION;
2415 ver.minor = MD_MINOR_VERSION;
2416 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2417
2418 if (copy_to_user(arg, &ver, sizeof(ver)))
2419 return -EFAULT;
2420
2421 return 0;
2422}
2423
2424static int get_array_info(mddev_t * mddev, void __user * arg)
2425{
2426 mdu_array_info_t info;
2427 int nr,working,active,failed,spare;
2428 mdk_rdev_t *rdev;
2429 struct list_head *tmp;
2430
2431 nr=working=active=failed=spare=0;
2432 ITERATE_RDEV(mddev,rdev,tmp) {
2433 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002434 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 failed++;
2436 else {
2437 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002438 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 active++;
2440 else
2441 spare++;
2442 }
2443 }
2444
2445 info.major_version = mddev->major_version;
2446 info.minor_version = mddev->minor_version;
2447 info.patch_version = MD_PATCHLEVEL_VERSION;
2448 info.ctime = mddev->ctime;
2449 info.level = mddev->level;
2450 info.size = mddev->size;
2451 info.nr_disks = nr;
2452 info.raid_disks = mddev->raid_disks;
2453 info.md_minor = mddev->md_minor;
2454 info.not_persistent= !mddev->persistent;
2455
2456 info.utime = mddev->utime;
2457 info.state = 0;
2458 if (mddev->in_sync)
2459 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07002460 if (mddev->bitmap && mddev->bitmap_offset)
2461 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 info.active_disks = active;
2463 info.working_disks = working;
2464 info.failed_disks = failed;
2465 info.spare_disks = spare;
2466
2467 info.layout = mddev->layout;
2468 info.chunk_size = mddev->chunk_size;
2469
2470 if (copy_to_user(arg, &info, sizeof(info)))
2471 return -EFAULT;
2472
2473 return 0;
2474}
2475
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002476static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07002477{
2478 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2479 char *ptr, *buf = NULL;
2480 int err = -ENOMEM;
2481
2482 file = kmalloc(sizeof(*file), GFP_KERNEL);
2483 if (!file)
2484 goto out;
2485
2486 /* bitmap disabled, zero the first byte and copy out */
2487 if (!mddev->bitmap || !mddev->bitmap->file) {
2488 file->pathname[0] = '\0';
2489 goto copy_out;
2490 }
2491
2492 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2493 if (!buf)
2494 goto out;
2495
2496 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2497 if (!ptr)
2498 goto out;
2499
2500 strcpy(file->pathname, ptr);
2501
2502copy_out:
2503 err = 0;
2504 if (copy_to_user(arg, file, sizeof(*file)))
2505 err = -EFAULT;
2506out:
2507 kfree(buf);
2508 kfree(file);
2509 return err;
2510}
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512static int get_disk_info(mddev_t * mddev, void __user * arg)
2513{
2514 mdu_disk_info_t info;
2515 unsigned int nr;
2516 mdk_rdev_t *rdev;
2517
2518 if (copy_from_user(&info, arg, sizeof(info)))
2519 return -EFAULT;
2520
2521 nr = info.number;
2522
2523 rdev = find_rdev_nr(mddev, nr);
2524 if (rdev) {
2525 info.major = MAJOR(rdev->bdev->bd_dev);
2526 info.minor = MINOR(rdev->bdev->bd_dev);
2527 info.raid_disk = rdev->raid_disk;
2528 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08002529 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002531 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 info.state |= (1<<MD_DISK_ACTIVE);
2533 info.state |= (1<<MD_DISK_SYNC);
2534 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002535 if (test_bit(WriteMostly, &rdev->flags))
2536 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 } else {
2538 info.major = info.minor = 0;
2539 info.raid_disk = -1;
2540 info.state = (1<<MD_DISK_REMOVED);
2541 }
2542
2543 if (copy_to_user(arg, &info, sizeof(info)))
2544 return -EFAULT;
2545
2546 return 0;
2547}
2548
2549static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2550{
2551 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2552 mdk_rdev_t *rdev;
2553 dev_t dev = MKDEV(info->major,info->minor);
2554
2555 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2556 return -EOVERFLOW;
2557
2558 if (!mddev->raid_disks) {
2559 int err;
2560 /* expecting a device which has a superblock */
2561 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2562 if (IS_ERR(rdev)) {
2563 printk(KERN_WARNING
2564 "md: md_import_device returned %ld\n",
2565 PTR_ERR(rdev));
2566 return PTR_ERR(rdev);
2567 }
2568 if (!list_empty(&mddev->disks)) {
2569 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2570 mdk_rdev_t, same_set);
2571 int err = super_types[mddev->major_version]
2572 .load_super(rdev, rdev0, mddev->minor_version);
2573 if (err < 0) {
2574 printk(KERN_WARNING
2575 "md: %s has different UUID to %s\n",
2576 bdevname(rdev->bdev,b),
2577 bdevname(rdev0->bdev,b2));
2578 export_rdev(rdev);
2579 return -EINVAL;
2580 }
2581 }
2582 err = bind_rdev_to_array(rdev, mddev);
2583 if (err)
2584 export_rdev(rdev);
2585 return err;
2586 }
2587
2588 /*
2589 * add_new_disk can be used once the array is assembled
2590 * to add "hot spares". They must already have a superblock
2591 * written
2592 */
2593 if (mddev->pers) {
2594 int err;
2595 if (!mddev->pers->hot_add_disk) {
2596 printk(KERN_WARNING
2597 "%s: personality does not support diskops!\n",
2598 mdname(mddev));
2599 return -EINVAL;
2600 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07002601 if (mddev->persistent)
2602 rdev = md_import_device(dev, mddev->major_version,
2603 mddev->minor_version);
2604 else
2605 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (IS_ERR(rdev)) {
2607 printk(KERN_WARNING
2608 "md: md_import_device returned %ld\n",
2609 PTR_ERR(rdev));
2610 return PTR_ERR(rdev);
2611 }
NeilBrown41158c72005-06-21 17:17:25 -07002612 /* set save_raid_disk if appropriate */
2613 if (!mddev->persistent) {
2614 if (info->state & (1<<MD_DISK_SYNC) &&
2615 info->raid_disk < mddev->raid_disks)
2616 rdev->raid_disk = info->raid_disk;
2617 else
2618 rdev->raid_disk = -1;
2619 } else
2620 super_types[mddev->major_version].
2621 validate_super(mddev, rdev);
2622 rdev->saved_raid_disk = rdev->raid_disk;
2623
NeilBrownb2d444d2005-11-08 21:39:31 -08002624 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002625 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2626 set_bit(WriteMostly, &rdev->flags);
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 rdev->raid_disk = -1;
2629 err = bind_rdev_to_array(rdev, mddev);
2630 if (err)
2631 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07002632
2633 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002634 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return err;
2636 }
2637
2638 /* otherwise, add_new_disk is only allowed
2639 * for major_version==0 superblocks
2640 */
2641 if (mddev->major_version != 0) {
2642 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2643 mdname(mddev));
2644 return -EINVAL;
2645 }
2646
2647 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2648 int err;
2649 rdev = md_import_device (dev, -1, 0);
2650 if (IS_ERR(rdev)) {
2651 printk(KERN_WARNING
2652 "md: error, md_import_device() returned %ld\n",
2653 PTR_ERR(rdev));
2654 return PTR_ERR(rdev);
2655 }
2656 rdev->desc_nr = info->number;
2657 if (info->raid_disk < mddev->raid_disks)
2658 rdev->raid_disk = info->raid_disk;
2659 else
2660 rdev->raid_disk = -1;
2661
NeilBrownb2d444d2005-11-08 21:39:31 -08002662 rdev->flags = 0;
2663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08002665 if (info->state & (1<<MD_DISK_SYNC))
2666 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002668 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2669 set_bit(WriteMostly, &rdev->flags);
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 err = bind_rdev_to_array(rdev, mddev);
2672 if (err) {
2673 export_rdev(rdev);
2674 return err;
2675 }
2676
2677 if (!mddev->persistent) {
2678 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2679 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2680 } else
2681 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2682 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2683
2684 if (!mddev->size || (mddev->size > rdev->size))
2685 mddev->size = rdev->size;
2686 }
2687
2688 return 0;
2689}
2690
2691static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2692{
2693 char b[BDEVNAME_SIZE];
2694 mdk_rdev_t *rdev;
2695
2696 if (!mddev->pers)
2697 return -ENODEV;
2698
2699 rdev = find_rdev(mddev, dev);
2700 if (!rdev)
2701 return -ENXIO;
2702
2703 if (rdev->raid_disk >= 0)
2704 goto busy;
2705
2706 kick_rdev_from_array(rdev);
2707 md_update_sb(mddev);
2708
2709 return 0;
2710busy:
2711 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2712 bdevname(rdev->bdev,b), mdname(mddev));
2713 return -EBUSY;
2714}
2715
2716static int hot_add_disk(mddev_t * mddev, dev_t dev)
2717{
2718 char b[BDEVNAME_SIZE];
2719 int err;
2720 unsigned int size;
2721 mdk_rdev_t *rdev;
2722
2723 if (!mddev->pers)
2724 return -ENODEV;
2725
2726 if (mddev->major_version != 0) {
2727 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2728 " version-0 superblocks.\n",
2729 mdname(mddev));
2730 return -EINVAL;
2731 }
2732 if (!mddev->pers->hot_add_disk) {
2733 printk(KERN_WARNING
2734 "%s: personality does not support diskops!\n",
2735 mdname(mddev));
2736 return -EINVAL;
2737 }
2738
2739 rdev = md_import_device (dev, -1, 0);
2740 if (IS_ERR(rdev)) {
2741 printk(KERN_WARNING
2742 "md: error, md_import_device() returned %ld\n",
2743 PTR_ERR(rdev));
2744 return -EINVAL;
2745 }
2746
2747 if (mddev->persistent)
2748 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2749 else
2750 rdev->sb_offset =
2751 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2752
2753 size = calc_dev_size(rdev, mddev->chunk_size);
2754 rdev->size = size;
2755
2756 if (size < mddev->size) {
2757 printk(KERN_WARNING
2758 "%s: disk size %llu blocks < array size %llu\n",
2759 mdname(mddev), (unsigned long long)size,
2760 (unsigned long long)mddev->size);
2761 err = -ENOSPC;
2762 goto abort_export;
2763 }
2764
NeilBrownb2d444d2005-11-08 21:39:31 -08002765 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 printk(KERN_WARNING
2767 "md: can not hot-add faulty %s disk to %s!\n",
2768 bdevname(rdev->bdev,b), mdname(mddev));
2769 err = -EINVAL;
2770 goto abort_export;
2771 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002772 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 rdev->desc_nr = -1;
2774 bind_rdev_to_array(rdev, mddev);
2775
2776 /*
2777 * The rest should better be atomic, we can have disk failures
2778 * noticed in interrupt contexts ...
2779 */
2780
2781 if (rdev->desc_nr == mddev->max_disks) {
2782 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2783 mdname(mddev));
2784 err = -EBUSY;
2785 goto abort_unbind_export;
2786 }
2787
2788 rdev->raid_disk = -1;
2789
2790 md_update_sb(mddev);
2791
2792 /*
2793 * Kick recovery, maybe this spare has to be added to the
2794 * array immediately.
2795 */
2796 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2797 md_wakeup_thread(mddev->thread);
2798
2799 return 0;
2800
2801abort_unbind_export:
2802 unbind_rdev_from_array(rdev);
2803
2804abort_export:
2805 export_rdev(rdev);
2806 return err;
2807}
2808
NeilBrown32a76272005-06-21 17:17:14 -07002809/* similar to deny_write_access, but accounts for our holding a reference
2810 * to the file ourselves */
2811static int deny_bitmap_write_access(struct file * file)
2812{
2813 struct inode *inode = file->f_mapping->host;
2814
2815 spin_lock(&inode->i_lock);
2816 if (atomic_read(&inode->i_writecount) > 1) {
2817 spin_unlock(&inode->i_lock);
2818 return -ETXTBSY;
2819 }
2820 atomic_set(&inode->i_writecount, -1);
2821 spin_unlock(&inode->i_lock);
2822
2823 return 0;
2824}
2825
2826static int set_bitmap_file(mddev_t *mddev, int fd)
2827{
2828 int err;
2829
NeilBrown36fa3062005-09-09 16:23:45 -07002830 if (mddev->pers) {
2831 if (!mddev->pers->quiesce)
2832 return -EBUSY;
2833 if (mddev->recovery || mddev->sync_thread)
2834 return -EBUSY;
2835 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07002836 }
2837
NeilBrown36fa3062005-09-09 16:23:45 -07002838
2839 if (fd >= 0) {
2840 if (mddev->bitmap)
2841 return -EEXIST; /* cannot add when bitmap is present */
2842 mddev->bitmap_file = fget(fd);
2843
2844 if (mddev->bitmap_file == NULL) {
2845 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2846 mdname(mddev));
2847 return -EBADF;
2848 }
2849
2850 err = deny_bitmap_write_access(mddev->bitmap_file);
2851 if (err) {
2852 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2853 mdname(mddev));
2854 fput(mddev->bitmap_file);
2855 mddev->bitmap_file = NULL;
2856 return err;
2857 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002858 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07002859 } else if (mddev->bitmap == NULL)
2860 return -ENOENT; /* cannot remove what isn't there */
2861 err = 0;
2862 if (mddev->pers) {
2863 mddev->pers->quiesce(mddev, 1);
2864 if (fd >= 0)
2865 err = bitmap_create(mddev);
2866 if (fd < 0 || err)
2867 bitmap_destroy(mddev);
2868 mddev->pers->quiesce(mddev, 0);
2869 } else if (fd < 0) {
2870 if (mddev->bitmap_file)
2871 fput(mddev->bitmap_file);
2872 mddev->bitmap_file = NULL;
2873 }
2874
NeilBrown32a76272005-06-21 17:17:14 -07002875 return err;
2876}
2877
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878/*
2879 * set_array_info is used two different ways
2880 * The original usage is when creating a new array.
2881 * In this usage, raid_disks is > 0 and it together with
2882 * level, size, not_persistent,layout,chunksize determine the
2883 * shape of the array.
2884 * This will always create an array with a type-0.90.0 superblock.
2885 * The newer usage is when assembling an array.
2886 * In this case raid_disks will be 0, and the major_version field is
2887 * use to determine which style super-blocks are to be found on the devices.
2888 * The minor and patch _version numbers are also kept incase the
2889 * super_block handler wishes to interpret them.
2890 */
2891static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2892{
2893
2894 if (info->raid_disks == 0) {
2895 /* just setting version number for superblock loading */
2896 if (info->major_version < 0 ||
2897 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2898 super_types[info->major_version].name == NULL) {
2899 /* maybe try to auto-load a module? */
2900 printk(KERN_INFO
2901 "md: superblock version %d not known\n",
2902 info->major_version);
2903 return -EINVAL;
2904 }
2905 mddev->major_version = info->major_version;
2906 mddev->minor_version = info->minor_version;
2907 mddev->patch_version = info->patch_version;
2908 return 0;
2909 }
2910 mddev->major_version = MD_MAJOR_VERSION;
2911 mddev->minor_version = MD_MINOR_VERSION;
2912 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2913 mddev->ctime = get_seconds();
2914
2915 mddev->level = info->level;
2916 mddev->size = info->size;
2917 mddev->raid_disks = info->raid_disks;
2918 /* don't set md_minor, it is determined by which /dev/md* was
2919 * openned
2920 */
2921 if (info->state & (1<<MD_SB_CLEAN))
2922 mddev->recovery_cp = MaxSector;
2923 else
2924 mddev->recovery_cp = 0;
2925 mddev->persistent = ! info->not_persistent;
2926
2927 mddev->layout = info->layout;
2928 mddev->chunk_size = info->chunk_size;
2929
2930 mddev->max_disks = MD_SB_DISKS;
2931
2932 mddev->sb_dirty = 1;
2933
NeilBrownb2a27032005-11-28 13:44:12 -08002934 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
2935 mddev->bitmap_offset = 0;
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 /*
2938 * Generate a 128 bit UUID
2939 */
2940 get_random_bytes(mddev->uuid, 16);
2941
2942 return 0;
2943}
2944
2945/*
2946 * update_array_info is used to change the configuration of an
2947 * on-line array.
2948 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2949 * fields in the info are checked against the array.
2950 * Any differences that cannot be handled will cause an error.
2951 * Normally, only one change can be managed at a time.
2952 */
2953static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2954{
2955 int rv = 0;
2956 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07002957 int state = 0;
2958
2959 /* calculate expected state,ignoring low bits */
2960 if (mddev->bitmap && mddev->bitmap_offset)
2961 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 if (mddev->major_version != info->major_version ||
2964 mddev->minor_version != info->minor_version ||
2965/* mddev->patch_version != info->patch_version || */
2966 mddev->ctime != info->ctime ||
2967 mddev->level != info->level ||
2968/* mddev->layout != info->layout || */
2969 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07002970 mddev->chunk_size != info->chunk_size ||
2971 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2972 ((state^info->state) & 0xfffffe00)
2973 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return -EINVAL;
2975 /* Check there is only one change */
2976 if (mddev->size != info->size) cnt++;
2977 if (mddev->raid_disks != info->raid_disks) cnt++;
2978 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07002979 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if (cnt == 0) return 0;
2981 if (cnt > 1) return -EINVAL;
2982
2983 if (mddev->layout != info->layout) {
2984 /* Change layout
2985 * we don't need to do anything at the md level, the
2986 * personality will take care of it all.
2987 */
2988 if (mddev->pers->reconfig == NULL)
2989 return -EINVAL;
2990 else
2991 return mddev->pers->reconfig(mddev, info->layout, -1);
2992 }
2993 if (mddev->size != info->size) {
2994 mdk_rdev_t * rdev;
2995 struct list_head *tmp;
2996 if (mddev->pers->resize == NULL)
2997 return -EINVAL;
2998 /* The "size" is the amount of each device that is used.
2999 * This can only make sense for arrays with redundancy.
3000 * linear and raid0 always use whatever space is available
3001 * We can only consider changing the size if no resync
3002 * or reconstruction is happening, and if the new size
3003 * is acceptable. It must fit before the sb_offset or,
3004 * if that is <data_offset, it must fit before the
3005 * size of each device.
3006 * If size is zero, we find the largest size that fits.
3007 */
3008 if (mddev->sync_thread)
3009 return -EBUSY;
3010 ITERATE_RDEV(mddev,rdev,tmp) {
3011 sector_t avail;
3012 int fit = (info->size == 0);
3013 if (rdev->sb_offset > rdev->data_offset)
3014 avail = (rdev->sb_offset*2) - rdev->data_offset;
3015 else
3016 avail = get_capacity(rdev->bdev->bd_disk)
3017 - rdev->data_offset;
3018 if (fit && (info->size == 0 || info->size > avail/2))
3019 info->size = avail/2;
3020 if (avail < ((sector_t)info->size << 1))
3021 return -ENOSPC;
3022 }
3023 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
3024 if (!rv) {
3025 struct block_device *bdev;
3026
3027 bdev = bdget_disk(mddev->gendisk, 0);
3028 if (bdev) {
3029 down(&bdev->bd_inode->i_sem);
3030 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3031 up(&bdev->bd_inode->i_sem);
3032 bdput(bdev);
3033 }
3034 }
3035 }
3036 if (mddev->raid_disks != info->raid_disks) {
3037 /* change the number of raid disks */
3038 if (mddev->pers->reshape == NULL)
3039 return -EINVAL;
3040 if (info->raid_disks <= 0 ||
3041 info->raid_disks >= mddev->max_disks)
3042 return -EINVAL;
3043 if (mddev->sync_thread)
3044 return -EBUSY;
3045 rv = mddev->pers->reshape(mddev, info->raid_disks);
3046 if (!rv) {
3047 struct block_device *bdev;
3048
3049 bdev = bdget_disk(mddev->gendisk, 0);
3050 if (bdev) {
3051 down(&bdev->bd_inode->i_sem);
3052 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3053 up(&bdev->bd_inode->i_sem);
3054 bdput(bdev);
3055 }
3056 }
3057 }
NeilBrown36fa3062005-09-09 16:23:45 -07003058 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3059 if (mddev->pers->quiesce == NULL)
3060 return -EINVAL;
3061 if (mddev->recovery || mddev->sync_thread)
3062 return -EBUSY;
3063 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3064 /* add the bitmap */
3065 if (mddev->bitmap)
3066 return -EEXIST;
3067 if (mddev->default_bitmap_offset == 0)
3068 return -EINVAL;
3069 mddev->bitmap_offset = mddev->default_bitmap_offset;
3070 mddev->pers->quiesce(mddev, 1);
3071 rv = bitmap_create(mddev);
3072 if (rv)
3073 bitmap_destroy(mddev);
3074 mddev->pers->quiesce(mddev, 0);
3075 } else {
3076 /* remove the bitmap */
3077 if (!mddev->bitmap)
3078 return -ENOENT;
3079 if (mddev->bitmap->file)
3080 return -EINVAL;
3081 mddev->pers->quiesce(mddev, 1);
3082 bitmap_destroy(mddev);
3083 mddev->pers->quiesce(mddev, 0);
3084 mddev->bitmap_offset = 0;
3085 }
3086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 md_update_sb(mddev);
3088 return rv;
3089}
3090
3091static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3092{
3093 mdk_rdev_t *rdev;
3094
3095 if (mddev->pers == NULL)
3096 return -ENODEV;
3097
3098 rdev = find_rdev(mddev, dev);
3099 if (!rdev)
3100 return -ENODEV;
3101
3102 md_error(mddev, rdev);
3103 return 0;
3104}
3105
3106static int md_ioctl(struct inode *inode, struct file *file,
3107 unsigned int cmd, unsigned long arg)
3108{
3109 int err = 0;
3110 void __user *argp = (void __user *)arg;
3111 struct hd_geometry __user *loc = argp;
3112 mddev_t *mddev = NULL;
3113
3114 if (!capable(CAP_SYS_ADMIN))
3115 return -EACCES;
3116
3117 /*
3118 * Commands dealing with the RAID driver but not any
3119 * particular array:
3120 */
3121 switch (cmd)
3122 {
3123 case RAID_VERSION:
3124 err = get_version(argp);
3125 goto done;
3126
3127 case PRINT_RAID_DEBUG:
3128 err = 0;
3129 md_print_devices();
3130 goto done;
3131
3132#ifndef MODULE
3133 case RAID_AUTORUN:
3134 err = 0;
3135 autostart_arrays(arg);
3136 goto done;
3137#endif
3138 default:;
3139 }
3140
3141 /*
3142 * Commands creating/starting a new array:
3143 */
3144
3145 mddev = inode->i_bdev->bd_disk->private_data;
3146
3147 if (!mddev) {
3148 BUG();
3149 goto abort;
3150 }
3151
3152
3153 if (cmd == START_ARRAY) {
3154 /* START_ARRAY doesn't need to lock the array as autostart_array
3155 * does the locking, and it could even be a different array
3156 */
3157 static int cnt = 3;
3158 if (cnt > 0 ) {
3159 printk(KERN_WARNING
3160 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08003161 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 current->comm, current->pid);
3163 cnt--;
3164 }
3165 err = autostart_array(new_decode_dev(arg));
3166 if (err) {
3167 printk(KERN_WARNING "md: autostart failed!\n");
3168 goto abort;
3169 }
3170 goto done;
3171 }
3172
3173 err = mddev_lock(mddev);
3174 if (err) {
3175 printk(KERN_INFO
3176 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3177 err, cmd);
3178 goto abort;
3179 }
3180
3181 switch (cmd)
3182 {
3183 case SET_ARRAY_INFO:
3184 {
3185 mdu_array_info_t info;
3186 if (!arg)
3187 memset(&info, 0, sizeof(info));
3188 else if (copy_from_user(&info, argp, sizeof(info))) {
3189 err = -EFAULT;
3190 goto abort_unlock;
3191 }
3192 if (mddev->pers) {
3193 err = update_array_info(mddev, &info);
3194 if (err) {
3195 printk(KERN_WARNING "md: couldn't update"
3196 " array info. %d\n", err);
3197 goto abort_unlock;
3198 }
3199 goto done_unlock;
3200 }
3201 if (!list_empty(&mddev->disks)) {
3202 printk(KERN_WARNING
3203 "md: array %s already has disks!\n",
3204 mdname(mddev));
3205 err = -EBUSY;
3206 goto abort_unlock;
3207 }
3208 if (mddev->raid_disks) {
3209 printk(KERN_WARNING
3210 "md: array %s already initialised!\n",
3211 mdname(mddev));
3212 err = -EBUSY;
3213 goto abort_unlock;
3214 }
3215 err = set_array_info(mddev, &info);
3216 if (err) {
3217 printk(KERN_WARNING "md: couldn't set"
3218 " array info. %d\n", err);
3219 goto abort_unlock;
3220 }
3221 }
3222 goto done_unlock;
3223
3224 default:;
3225 }
3226
3227 /*
3228 * Commands querying/configuring an existing array:
3229 */
NeilBrown32a76272005-06-21 17:17:14 -07003230 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3231 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3232 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3233 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 err = -ENODEV;
3235 goto abort_unlock;
3236 }
3237
3238 /*
3239 * Commands even a read-only array can execute:
3240 */
3241 switch (cmd)
3242 {
3243 case GET_ARRAY_INFO:
3244 err = get_array_info(mddev, argp);
3245 goto done_unlock;
3246
NeilBrown32a76272005-06-21 17:17:14 -07003247 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003248 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003249 goto done_unlock;
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 case GET_DISK_INFO:
3252 err = get_disk_info(mddev, argp);
3253 goto done_unlock;
3254
3255 case RESTART_ARRAY_RW:
3256 err = restart_array(mddev);
3257 goto done_unlock;
3258
3259 case STOP_ARRAY:
3260 err = do_md_stop (mddev, 0);
3261 goto done_unlock;
3262
3263 case STOP_ARRAY_RO:
3264 err = do_md_stop (mddev, 1);
3265 goto done_unlock;
3266
3267 /*
3268 * We have a problem here : there is no easy way to give a CHS
3269 * virtual geometry. We currently pretend that we have a 2 heads
3270 * 4 sectors (with a BIG number of cylinders...). This drives
3271 * dosfs just mad... ;-)
3272 */
3273 case HDIO_GETGEO:
3274 if (!loc) {
3275 err = -EINVAL;
3276 goto abort_unlock;
3277 }
3278 err = put_user (2, (char __user *) &loc->heads);
3279 if (err)
3280 goto abort_unlock;
3281 err = put_user (4, (char __user *) &loc->sectors);
3282 if (err)
3283 goto abort_unlock;
3284 err = put_user(get_capacity(mddev->gendisk)/8,
3285 (short __user *) &loc->cylinders);
3286 if (err)
3287 goto abort_unlock;
3288 err = put_user (get_start_sect(inode->i_bdev),
3289 (long __user *) &loc->start);
3290 goto done_unlock;
3291 }
3292
3293 /*
3294 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08003295 * superblock, so we do not allow them on read-only arrays.
3296 * However non-MD ioctls (e.g. get-size) will still come through
3297 * here and hit the 'default' below, so only disallow
3298 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 */
NeilBrownf91de922005-11-08 21:39:36 -08003300 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3301 mddev->ro && mddev->pers) {
3302 if (mddev->ro == 2) {
3303 mddev->ro = 0;
3304 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3305 md_wakeup_thread(mddev->thread);
3306
3307 } else {
3308 err = -EROFS;
3309 goto abort_unlock;
3310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
3312
3313 switch (cmd)
3314 {
3315 case ADD_NEW_DISK:
3316 {
3317 mdu_disk_info_t info;
3318 if (copy_from_user(&info, argp, sizeof(info)))
3319 err = -EFAULT;
3320 else
3321 err = add_new_disk(mddev, &info);
3322 goto done_unlock;
3323 }
3324
3325 case HOT_REMOVE_DISK:
3326 err = hot_remove_disk(mddev, new_decode_dev(arg));
3327 goto done_unlock;
3328
3329 case HOT_ADD_DISK:
3330 err = hot_add_disk(mddev, new_decode_dev(arg));
3331 goto done_unlock;
3332
3333 case SET_DISK_FAULTY:
3334 err = set_disk_faulty(mddev, new_decode_dev(arg));
3335 goto done_unlock;
3336
3337 case RUN_ARRAY:
3338 err = do_md_run (mddev);
3339 goto done_unlock;
3340
NeilBrown32a76272005-06-21 17:17:14 -07003341 case SET_BITMAP_FILE:
3342 err = set_bitmap_file(mddev, (int)arg);
3343 goto done_unlock;
3344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 default:
3346 if (_IOC_TYPE(cmd) == MD_MAJOR)
3347 printk(KERN_WARNING "md: %s(pid %d) used"
3348 " obsolete MD ioctl, upgrade your"
3349 " software to use new ictls.\n",
3350 current->comm, current->pid);
3351 err = -EINVAL;
3352 goto abort_unlock;
3353 }
3354
3355done_unlock:
3356abort_unlock:
3357 mddev_unlock(mddev);
3358
3359 return err;
3360done:
3361 if (err)
3362 MD_BUG();
3363abort:
3364 return err;
3365}
3366
3367static int md_open(struct inode *inode, struct file *file)
3368{
3369 /*
3370 * Succeed if we can lock the mddev, which confirms that
3371 * it isn't being stopped right now.
3372 */
3373 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3374 int err;
3375
3376 if ((err = mddev_lock(mddev)))
3377 goto out;
3378
3379 err = 0;
3380 mddev_get(mddev);
3381 mddev_unlock(mddev);
3382
3383 check_disk_change(inode->i_bdev);
3384 out:
3385 return err;
3386}
3387
3388static int md_release(struct inode *inode, struct file * file)
3389{
3390 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3391
3392 if (!mddev)
3393 BUG();
3394 mddev_put(mddev);
3395
3396 return 0;
3397}
3398
3399static int md_media_changed(struct gendisk *disk)
3400{
3401 mddev_t *mddev = disk->private_data;
3402
3403 return mddev->changed;
3404}
3405
3406static int md_revalidate(struct gendisk *disk)
3407{
3408 mddev_t *mddev = disk->private_data;
3409
3410 mddev->changed = 0;
3411 return 0;
3412}
3413static struct block_device_operations md_fops =
3414{
3415 .owner = THIS_MODULE,
3416 .open = md_open,
3417 .release = md_release,
3418 .ioctl = md_ioctl,
3419 .media_changed = md_media_changed,
3420 .revalidate_disk= md_revalidate,
3421};
3422
Adrian Bunk75c96f82005-05-05 16:16:09 -07003423static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424{
3425 mdk_thread_t *thread = arg;
3426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 /*
3428 * md_thread is a 'system-thread', it's priority should be very
3429 * high. We avoid resource deadlocks individually in each
3430 * raid personality. (RAID5 does preallocation) We also use RR and
3431 * the very same RT priority as kswapd, thus we will never get
3432 * into a priority inversion deadlock.
3433 *
3434 * we definitely have to have equal or higher priority than
3435 * bdflush, otherwise bdflush will deadlock if there are too
3436 * many dirty RAID5 blocks.
3437 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
NeilBrown6985c432005-10-19 21:23:47 -07003439 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07003440 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
NeilBrown93588e22005-11-15 00:09:12 -08003442 /* We need to wait INTERRUPTIBLE so that
3443 * we don't add to the load-average.
3444 * That means we need to be sure no signals are
3445 * pending
3446 */
3447 if (signal_pending(current))
3448 flush_signals(current);
3449
3450 wait_event_interruptible_timeout
3451 (thread->wqueue,
3452 test_bit(THREAD_WAKEUP, &thread->flags)
3453 || kthread_should_stop(),
3454 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003455 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 clear_bit(THREAD_WAKEUP, &thread->flags);
3458
NeilBrown787453c2005-11-08 21:39:43 -08003459 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 }
NeilBrowna6fb0932005-09-09 16:23:56 -07003461
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 return 0;
3463}
3464
3465void md_wakeup_thread(mdk_thread_t *thread)
3466{
3467 if (thread) {
3468 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3469 set_bit(THREAD_WAKEUP, &thread->flags);
3470 wake_up(&thread->wqueue);
3471 }
3472}
3473
3474mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3475 const char *name)
3476{
3477 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
NeilBrowna6fb0932005-09-09 16:23:56 -07003479 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 if (!thread)
3481 return NULL;
3482
3483 memset(thread, 0, sizeof(mdk_thread_t));
3484 init_waitqueue_head(&thread->wqueue);
3485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 thread->run = run;
3487 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07003488 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07003489 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07003490 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 kfree(thread);
3492 return NULL;
3493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return thread;
3495}
3496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497void md_unregister_thread(mdk_thread_t *thread)
3498{
NeilBrownd28446f2005-04-16 15:26:41 -07003499 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07003500
3501 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 kfree(thread);
3503}
3504
3505void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3506{
3507 if (!mddev) {
3508 MD_BUG();
3509 return;
3510 }
3511
NeilBrownb2d444d2005-11-08 21:39:31 -08003512 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 return;
NeilBrown32a76272005-06-21 17:17:14 -07003514/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3516 mdname(mddev),
3517 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3518 __builtin_return_address(0),__builtin_return_address(1),
3519 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07003520*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 if (!mddev->pers->error_handler)
3522 return;
3523 mddev->pers->error_handler(mddev,rdev);
3524 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3525 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3526 md_wakeup_thread(mddev->thread);
3527}
3528
3529/* seq_file implementation /proc/mdstat */
3530
3531static void status_unused(struct seq_file *seq)
3532{
3533 int i = 0;
3534 mdk_rdev_t *rdev;
3535 struct list_head *tmp;
3536
3537 seq_printf(seq, "unused devices: ");
3538
3539 ITERATE_RDEV_PENDING(rdev,tmp) {
3540 char b[BDEVNAME_SIZE];
3541 i++;
3542 seq_printf(seq, "%s ",
3543 bdevname(rdev->bdev,b));
3544 }
3545 if (!i)
3546 seq_printf(seq, "<none>");
3547
3548 seq_printf(seq, "\n");
3549}
3550
3551
3552static void status_resync(struct seq_file *seq, mddev_t * mddev)
3553{
3554 unsigned long max_blocks, resync, res, dt, db, rt;
3555
3556 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3557
3558 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3559 max_blocks = mddev->resync_max_sectors >> 1;
3560 else
3561 max_blocks = mddev->size;
3562
3563 /*
3564 * Should not happen.
3565 */
3566 if (!max_blocks) {
3567 MD_BUG();
3568 return;
3569 }
3570 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3571 {
3572 int i, x = res/50, y = 20-x;
3573 seq_printf(seq, "[");
3574 for (i = 0; i < x; i++)
3575 seq_printf(seq, "=");
3576 seq_printf(seq, ">");
3577 for (i = 0; i < y; i++)
3578 seq_printf(seq, ".");
3579 seq_printf(seq, "] ");
3580 }
3581 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3582 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3583 "resync" : "recovery"),
3584 res/10, res % 10, resync, max_blocks);
3585
3586 /*
3587 * We do not want to overflow, so the order of operands and
3588 * the * 100 / 100 trick are important. We do a +1 to be
3589 * safe against division by zero. We only estimate anyway.
3590 *
3591 * dt: time from mark until now
3592 * db: blocks written from mark until now
3593 * rt: remaining time
3594 */
3595 dt = ((jiffies - mddev->resync_mark) / HZ);
3596 if (!dt) dt++;
3597 db = resync - (mddev->resync_mark_cnt/2);
3598 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3599
3600 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3601
3602 seq_printf(seq, " speed=%ldK/sec", db/dt);
3603}
3604
3605static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3606{
3607 struct list_head *tmp;
3608 loff_t l = *pos;
3609 mddev_t *mddev;
3610
3611 if (l >= 0x10000)
3612 return NULL;
3613 if (!l--)
3614 /* header */
3615 return (void*)1;
3616
3617 spin_lock(&all_mddevs_lock);
3618 list_for_each(tmp,&all_mddevs)
3619 if (!l--) {
3620 mddev = list_entry(tmp, mddev_t, all_mddevs);
3621 mddev_get(mddev);
3622 spin_unlock(&all_mddevs_lock);
3623 return mddev;
3624 }
3625 spin_unlock(&all_mddevs_lock);
3626 if (!l--)
3627 return (void*)2;/* tail */
3628 return NULL;
3629}
3630
3631static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3632{
3633 struct list_head *tmp;
3634 mddev_t *next_mddev, *mddev = v;
3635
3636 ++*pos;
3637 if (v == (void*)2)
3638 return NULL;
3639
3640 spin_lock(&all_mddevs_lock);
3641 if (v == (void*)1)
3642 tmp = all_mddevs.next;
3643 else
3644 tmp = mddev->all_mddevs.next;
3645 if (tmp != &all_mddevs)
3646 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3647 else {
3648 next_mddev = (void*)2;
3649 *pos = 0x10000;
3650 }
3651 spin_unlock(&all_mddevs_lock);
3652
3653 if (v != (void*)1)
3654 mddev_put(mddev);
3655 return next_mddev;
3656
3657}
3658
3659static void md_seq_stop(struct seq_file *seq, void *v)
3660{
3661 mddev_t *mddev = v;
3662
3663 if (mddev && v != (void*)1 && v != (void*)2)
3664 mddev_put(mddev);
3665}
3666
3667static int md_seq_show(struct seq_file *seq, void *v)
3668{
3669 mddev_t *mddev = v;
3670 sector_t size;
3671 struct list_head *tmp2;
3672 mdk_rdev_t *rdev;
3673 int i;
NeilBrown32a76272005-06-21 17:17:14 -07003674 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
3676 if (v == (void*)1) {
3677 seq_printf(seq, "Personalities : ");
3678 spin_lock(&pers_lock);
3679 for (i = 0; i < MAX_PERSONALITY; i++)
3680 if (pers[i])
3681 seq_printf(seq, "[%s] ", pers[i]->name);
3682
3683 spin_unlock(&pers_lock);
3684 seq_printf(seq, "\n");
3685 return 0;
3686 }
3687 if (v == (void*)2) {
3688 status_unused(seq);
3689 return 0;
3690 }
3691
3692 if (mddev_lock(mddev)!=0)
3693 return -EINTR;
3694 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3695 seq_printf(seq, "%s : %sactive", mdname(mddev),
3696 mddev->pers ? "" : "in");
3697 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08003698 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08003700 if (mddev->ro==2)
3701 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 seq_printf(seq, " %s", mddev->pers->name);
3703 }
3704
3705 size = 0;
3706 ITERATE_RDEV(mddev,rdev,tmp2) {
3707 char b[BDEVNAME_SIZE];
3708 seq_printf(seq, " %s[%d]",
3709 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003710 if (test_bit(WriteMostly, &rdev->flags))
3711 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08003712 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 seq_printf(seq, "(F)");
3714 continue;
NeilBrownb325a322005-09-09 16:24:00 -07003715 } else if (rdev->raid_disk < 0)
3716 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 size += rdev->size;
3718 }
3719
3720 if (!list_empty(&mddev->disks)) {
3721 if (mddev->pers)
3722 seq_printf(seq, "\n %llu blocks",
3723 (unsigned long long)mddev->array_size);
3724 else
3725 seq_printf(seq, "\n %llu blocks",
3726 (unsigned long long)size);
3727 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07003728 if (mddev->persistent) {
3729 if (mddev->major_version != 0 ||
3730 mddev->minor_version != 90) {
3731 seq_printf(seq," super %d.%d",
3732 mddev->major_version,
3733 mddev->minor_version);
3734 }
3735 } else
3736 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
3738 if (mddev->pers) {
3739 mddev->pers->status (seq, mddev);
3740 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08003741 if (mddev->pers->sync_request) {
3742 if (mddev->curr_resync > 2) {
3743 status_resync (seq, mddev);
3744 seq_printf(seq, "\n ");
3745 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3746 seq_printf(seq, "\tresync=DELAYED\n ");
3747 else if (mddev->recovery_cp < MaxSector)
3748 seq_printf(seq, "\tresync=PENDING\n ");
3749 }
NeilBrown32a76272005-06-21 17:17:14 -07003750 } else
3751 seq_printf(seq, "\n ");
3752
3753 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07003754 unsigned long chunk_kb;
3755 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07003756 spin_lock_irqsave(&bitmap->lock, flags);
3757 chunk_kb = bitmap->chunksize >> 10;
3758 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3759 "%lu%s chunk",
3760 bitmap->pages - bitmap->missing_pages,
3761 bitmap->pages,
3762 (bitmap->pages - bitmap->missing_pages)
3763 << (PAGE_SHIFT - 10),
3764 chunk_kb ? chunk_kb : bitmap->chunksize,
3765 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07003766 if (bitmap->file) {
3767 seq_printf(seq, ", file: ");
3768 seq_path(seq, bitmap->file->f_vfsmnt,
3769 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07003770 }
NeilBrown78d742d2005-06-21 17:17:15 -07003771
NeilBrown32a76272005-06-21 17:17:14 -07003772 seq_printf(seq, "\n");
3773 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 }
3775
3776 seq_printf(seq, "\n");
3777 }
3778 mddev_unlock(mddev);
3779
3780 return 0;
3781}
3782
3783static struct seq_operations md_seq_ops = {
3784 .start = md_seq_start,
3785 .next = md_seq_next,
3786 .stop = md_seq_stop,
3787 .show = md_seq_show,
3788};
3789
3790static int md_seq_open(struct inode *inode, struct file *file)
3791{
3792 int error;
3793
3794 error = seq_open(file, &md_seq_ops);
3795 return error;
3796}
3797
3798static struct file_operations md_seq_fops = {
3799 .open = md_seq_open,
3800 .read = seq_read,
3801 .llseek = seq_lseek,
3802 .release = seq_release,
3803};
3804
3805int register_md_personality(int pnum, mdk_personality_t *p)
3806{
3807 if (pnum >= MAX_PERSONALITY) {
3808 printk(KERN_ERR
3809 "md: tried to install personality %s as nr %d, but max is %lu\n",
3810 p->name, pnum, MAX_PERSONALITY-1);
3811 return -EINVAL;
3812 }
3813
3814 spin_lock(&pers_lock);
3815 if (pers[pnum]) {
3816 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 return -EBUSY;
3818 }
3819
3820 pers[pnum] = p;
3821 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3822 spin_unlock(&pers_lock);
3823 return 0;
3824}
3825
3826int unregister_md_personality(int pnum)
3827{
NeilBrowna757e642005-04-16 15:26:42 -07003828 if (pnum >= MAX_PERSONALITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
3831 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3832 spin_lock(&pers_lock);
3833 pers[pnum] = NULL;
3834 spin_unlock(&pers_lock);
3835 return 0;
3836}
3837
3838static int is_mddev_idle(mddev_t *mddev)
3839{
3840 mdk_rdev_t * rdev;
3841 struct list_head *tmp;
3842 int idle;
3843 unsigned long curr_events;
3844
3845 idle = 1;
3846 ITERATE_RDEV(mddev,rdev,tmp) {
3847 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01003848 curr_events = disk_stat_read(disk, sectors[0]) +
3849 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08003851 /* The difference between curr_events and last_events
3852 * will be affected by any new non-sync IO (making
3853 * curr_events bigger) and any difference in the amount of
3854 * in-flight syncio (making current_events bigger or smaller)
3855 * The amount in-flight is currently limited to
3856 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
3857 * which is at most 4096 sectors.
3858 * These numbers are fairly fragile and should be made
3859 * more robust, probably by enforcing the
3860 * 'window size' that md_do_sync sort-of uses.
3861 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 * Note: the following is an unsigned comparison.
3863 */
NeilBrownc0e48522005-11-18 01:11:01 -08003864 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 rdev->last_events = curr_events;
3866 idle = 0;
3867 }
3868 }
3869 return idle;
3870}
3871
3872void md_done_sync(mddev_t *mddev, int blocks, int ok)
3873{
3874 /* another "blocks" (512byte) blocks have been synced */
3875 atomic_sub(blocks, &mddev->recovery_active);
3876 wake_up(&mddev->recovery_wait);
3877 if (!ok) {
3878 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3879 md_wakeup_thread(mddev->thread);
3880 // stop recovery, signal do_sync ....
3881 }
3882}
3883
3884
NeilBrown06d91a52005-06-21 17:17:12 -07003885/* md_write_start(mddev, bi)
3886 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07003887 * in superblock) before writing, schedule a superblock update
3888 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07003889 */
NeilBrown3d310eb2005-06-21 17:17:26 -07003890void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
NeilBrown06d91a52005-06-21 17:17:12 -07003892 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07003893 return;
NeilBrown06d91a52005-06-21 17:17:12 -07003894
NeilBrownf91de922005-11-08 21:39:36 -08003895 BUG_ON(mddev->ro == 1);
3896 if (mddev->ro == 2) {
3897 /* need to switch to read/write */
3898 mddev->ro = 0;
3899 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3900 md_wakeup_thread(mddev->thread);
3901 }
NeilBrown06d91a52005-06-21 17:17:12 -07003902 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07003903 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08003904 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07003905 if (mddev->in_sync) {
3906 mddev->in_sync = 0;
3907 mddev->sb_dirty = 1;
3908 md_wakeup_thread(mddev->thread);
3909 }
NeilBrowna9701a32005-11-08 21:39:34 -08003910 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07003911 }
NeilBrown3d310eb2005-06-21 17:17:26 -07003912 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913}
3914
3915void md_write_end(mddev_t *mddev)
3916{
3917 if (atomic_dec_and_test(&mddev->writes_pending)) {
3918 if (mddev->safemode == 2)
3919 md_wakeup_thread(mddev->thread);
3920 else
3921 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3922 }
3923}
3924
Adrian Bunk75c96f82005-05-05 16:16:09 -07003925static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
3927#define SYNC_MARKS 10
3928#define SYNC_MARK_STEP (3*HZ)
3929static void md_do_sync(mddev_t *mddev)
3930{
3931 mddev_t *mddev2;
3932 unsigned int currspeed = 0,
3933 window;
NeilBrown57afd892005-06-21 17:17:13 -07003934 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 unsigned long mark[SYNC_MARKS];
3936 sector_t mark_cnt[SYNC_MARKS];
3937 int last_mark,m;
3938 struct list_head *tmp;
3939 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07003940 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
3942 /* just incase thread restarts... */
3943 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3944 return;
3945
3946 /* we overload curr_resync somewhat here.
3947 * 0 == not engaged in resync at all
3948 * 2 == checking that there is no conflict with another sync
3949 * 1 == like 2, but have yielded to allow conflicting resync to
3950 * commense
3951 * other == active in resync - this many blocks
3952 *
3953 * Before starting a resync we must have set curr_resync to
3954 * 2, and then checked that every "conflicting" array has curr_resync
3955 * less than ours. When we find one that is the same or higher
3956 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3957 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3958 * This will mean we have to start checking from the beginning again.
3959 *
3960 */
3961
3962 do {
3963 mddev->curr_resync = 2;
3964
3965 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08003966 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07003967 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 goto skip;
3969 }
3970 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 if (mddev2 == mddev)
3972 continue;
3973 if (mddev2->curr_resync &&
3974 match_mddev_units(mddev,mddev2)) {
3975 DEFINE_WAIT(wq);
3976 if (mddev < mddev2 && mddev->curr_resync == 2) {
3977 /* arbitrarily yield */
3978 mddev->curr_resync = 1;
3979 wake_up(&resync_wait);
3980 }
3981 if (mddev > mddev2 && mddev->curr_resync == 1)
3982 /* no need to wait here, we can wait the next
3983 * time 'round when curr_resync == 2
3984 */
3985 continue;
NeilBrown787453c2005-11-08 21:39:43 -08003986 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
3987 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07003988 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 printk(KERN_INFO "md: delaying resync of %s"
3990 " until %s has finished resync (they"
3991 " share one or more physical units)\n",
3992 mdname(mddev), mdname(mddev2));
3993 mddev_put(mddev2);
3994 schedule();
3995 finish_wait(&resync_wait, &wq);
3996 goto try_again;
3997 }
3998 finish_wait(&resync_wait, &wq);
3999 }
4000 }
4001 } while (mddev->curr_resync < 2);
4002
NeilBrown9d888832005-11-08 21:39:26 -08004003 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07004005 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 */
4007 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08004008 mddev->resync_mismatches = 0;
4009 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 /* recovery follows the physical size of devices */
4011 max_sectors = mddev->size << 1;
4012
4013 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4014 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
4015 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
Adrian Bunk338cec32005-09-10 00:26:54 -07004016 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 "(but not more than %d KB/sec) for reconstruction.\n",
4018 sysctl_speed_limit_max);
4019
4020 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07004021 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08004022 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4023 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 j = mddev->recovery_cp;
4025 else
4026 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07004027 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 for (m = 0; m < SYNC_MARKS; m++) {
4029 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004030 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 }
4032 last_mark = 0;
4033 mddev->resync_mark = mark[last_mark];
4034 mddev->resync_mark_cnt = mark_cnt[last_mark];
4035
4036 /*
4037 * Tune reconstruction:
4038 */
4039 window = 32*(PAGE_SIZE/512);
4040 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4041 window/2,(unsigned long long) max_sectors/2);
4042
4043 atomic_set(&mddev->recovery_active, 0);
4044 init_waitqueue_head(&mddev->recovery_wait);
4045 last_check = 0;
4046
4047 if (j>2) {
4048 printk(KERN_INFO
4049 "md: resuming recovery of %s from checkpoint.\n",
4050 mdname(mddev));
4051 mddev->curr_resync = j;
4052 }
4053
4054 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07004055 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
NeilBrown57afd892005-06-21 17:17:13 -07004057 skipped = 0;
4058 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4059 currspeed < sysctl_speed_limit_min);
4060 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4062 goto out;
4063 }
NeilBrown57afd892005-06-21 17:17:13 -07004064
4065 if (!skipped) { /* actual IO requested */
4066 io_sectors += sectors;
4067 atomic_add(sectors, &mddev->recovery_active);
4068 }
4069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 j += sectors;
4071 if (j>1) mddev->curr_resync = j;
4072
NeilBrown57afd892005-06-21 17:17:13 -07004073
4074 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 continue;
4076
NeilBrown57afd892005-06-21 17:17:13 -07004077 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
4079 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4080 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4081 break;
4082
4083 repeat:
4084 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4085 /* step marks */
4086 int next = (last_mark+1) % SYNC_MARKS;
4087
4088 mddev->resync_mark = mark[next];
4089 mddev->resync_mark_cnt = mark_cnt[next];
4090 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004091 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 last_mark = next;
4093 }
4094
4095
NeilBrown787453c2005-11-08 21:39:43 -08004096 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 /*
4098 * got a signal, exit.
4099 */
4100 printk(KERN_INFO
4101 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4103 goto out;
4104 }
4105
4106 /*
4107 * this loop exits only if either when we are slower than
4108 * the 'hard' speed limit, or the system was IO-idle for
4109 * a jiffy.
4110 * the system might be non-idle CPU-wise, but we only care
4111 * about not overloading the IO subsystem. (things like an
4112 * e2fsck being done on the RAID array should execute fast)
4113 */
4114 mddev->queue->unplug_fn(mddev->queue);
4115 cond_resched();
4116
NeilBrown57afd892005-06-21 17:17:13 -07004117 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4118 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
4120 if (currspeed > sysctl_speed_limit_min) {
4121 if ((currspeed > sysctl_speed_limit_max) ||
4122 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08004123 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 goto repeat;
4125 }
4126 }
4127 }
4128 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4129 /*
4130 * this also signals 'finished resyncing' to md_stop
4131 */
4132 out:
4133 mddev->queue->unplug_fn(mddev->queue);
4134
4135 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4136
4137 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004138 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
4140 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4141 mddev->curr_resync > 2 &&
4142 mddev->curr_resync >= mddev->recovery_cp) {
4143 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4144 printk(KERN_INFO
4145 "md: checkpointing recovery of %s.\n",
4146 mdname(mddev));
4147 mddev->recovery_cp = mddev->curr_resync;
4148 } else
4149 mddev->recovery_cp = MaxSector;
4150 }
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 skip:
4153 mddev->curr_resync = 0;
4154 wake_up(&resync_wait);
4155 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4156 md_wakeup_thread(mddev->thread);
4157}
4158
4159
4160/*
4161 * This routine is regularly called by all per-raid-array threads to
4162 * deal with generic issues like resync and super-block update.
4163 * Raid personalities that don't have a thread (linear/raid0) do not
4164 * need this as they never do any recovery or update the superblock.
4165 *
4166 * It does not do any resync itself, but rather "forks" off other threads
4167 * to do that as needed.
4168 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4169 * "->recovery" and create a thread at ->sync_thread.
4170 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4171 * and wakeups up this thread which will reap the thread and finish up.
4172 * This thread also removes any faulty devices (with nr_pending == 0).
4173 *
4174 * The overall approach is:
4175 * 1/ if the superblock needs updating, update it.
4176 * 2/ If a recovery thread is running, don't do anything else.
4177 * 3/ If recovery has finished, clean up, possibly marking spares active.
4178 * 4/ If there are any faulty devices, remove them.
4179 * 5/ If array is degraded, try to add spares devices
4180 * 6/ If array has spares or is not in-sync, start a resync thread.
4181 */
4182void md_check_recovery(mddev_t *mddev)
4183{
4184 mdk_rdev_t *rdev;
4185 struct list_head *rtmp;
4186
4187
NeilBrown5f404022005-06-21 17:17:16 -07004188 if (mddev->bitmap)
4189 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
4191 if (mddev->ro)
4192 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004193
4194 if (signal_pending(current)) {
4195 if (mddev->pers->sync_request) {
4196 printk(KERN_INFO "md: %s in immediate safe mode\n",
4197 mdname(mddev));
4198 mddev->safemode = 2;
4199 }
4200 flush_signals(current);
4201 }
4202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 if ( ! (
4204 mddev->sb_dirty ||
4205 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004206 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4207 (mddev->safemode == 1) ||
4208 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4209 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 ))
4211 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004212
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 if (mddev_trylock(mddev)==0) {
4214 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004215
NeilBrowna9701a32005-11-08 21:39:34 -08004216 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004217 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4218 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4219 mddev->in_sync = 1;
4220 mddev->sb_dirty = 1;
4221 }
4222 if (mddev->safemode == 1)
4223 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08004224 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004225
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 if (mddev->sb_dirty)
4227 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004228
NeilBrown06d91a52005-06-21 17:17:12 -07004229
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4231 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4232 /* resync/recovery still happening */
4233 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4234 goto unlock;
4235 }
4236 if (mddev->sync_thread) {
4237 /* resync has finished, collect result */
4238 md_unregister_thread(mddev->sync_thread);
4239 mddev->sync_thread = NULL;
4240 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4241 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4242 /* success...*/
4243 /* activate any spares */
4244 mddev->pers->spare_active(mddev);
4245 }
4246 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004247
4248 /* if array is no-longer degraded, then any saved_raid_disk
4249 * information must be scrapped
4250 */
4251 if (!mddev->degraded)
4252 ITERATE_RDEV(mddev,rdev,rtmp)
4253 rdev->saved_raid_disk = -1;
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 mddev->recovery = 0;
4256 /* flag recovery needed just to double check */
4257 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4258 goto unlock;
4259 }
NeilBrown24dd4692005-11-08 21:39:26 -08004260 /* Clear some bits that don't mean anything, but
4261 * might be left set
4262 */
4263 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4264 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4265 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4266 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
4268 /* no recovery is running.
4269 * remove any failed drives, then
4270 * add spares if possible.
4271 * Spare are also removed and re-added, to allow
4272 * the personality to fail the re-add.
4273 */
4274 ITERATE_RDEV(mddev,rdev,rtmp)
4275 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004276 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004278 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4279 char nm[20];
4280 sprintf(nm,"rd%d", rdev->raid_disk);
4281 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 }
4285
4286 if (mddev->degraded) {
4287 ITERATE_RDEV(mddev,rdev,rtmp)
4288 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08004289 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004290 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4291 char nm[20];
4292 sprintf(nm, "rd%d", rdev->raid_disk);
4293 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 spares++;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004295 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 break;
4297 }
4298 }
4299
NeilBrown24dd4692005-11-08 21:39:26 -08004300 if (spares) {
4301 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4302 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4303 } else if (mddev->recovery_cp < MaxSector) {
4304 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4305 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4306 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08004308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 if (mddev->pers->sync_request) {
4310 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07004311 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4312 /* We are adding a device or devices to an array
4313 * which has the bitmap stored on all devices.
4314 * So make sure all bitmap pages get written
4315 */
4316 bitmap_write_all(mddev->bitmap);
4317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 mddev->sync_thread = md_register_thread(md_do_sync,
4319 mddev,
4320 "%s_resync");
4321 if (!mddev->sync_thread) {
4322 printk(KERN_ERR "%s: could not start resync"
4323 " thread...\n",
4324 mdname(mddev));
4325 /* leave the spares where they are, it shouldn't hurt */
4326 mddev->recovery = 0;
4327 } else {
4328 md_wakeup_thread(mddev->sync_thread);
4329 }
4330 }
4331 unlock:
4332 mddev_unlock(mddev);
4333 }
4334}
4335
Adrian Bunk75c96f82005-05-05 16:16:09 -07004336static int md_notify_reboot(struct notifier_block *this,
4337 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338{
4339 struct list_head *tmp;
4340 mddev_t *mddev;
4341
4342 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4343
4344 printk(KERN_INFO "md: stopping all md devices.\n");
4345
4346 ITERATE_MDDEV(mddev,tmp)
4347 if (mddev_trylock(mddev)==0)
4348 do_md_stop (mddev, 1);
4349 /*
4350 * certain more exotic SCSI devices are known to be
4351 * volatile wrt too early system reboots. While the
4352 * right place to handle this issue is the given
4353 * driver, we do want to have a safe RAID driver ...
4354 */
4355 mdelay(1000*1);
4356 }
4357 return NOTIFY_DONE;
4358}
4359
Adrian Bunk75c96f82005-05-05 16:16:09 -07004360static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 .notifier_call = md_notify_reboot,
4362 .next = NULL,
4363 .priority = INT_MAX, /* before any real devices */
4364};
4365
4366static void md_geninit(void)
4367{
4368 struct proc_dir_entry *p;
4369
4370 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4371
4372 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4373 if (p)
4374 p->proc_fops = &md_seq_fops;
4375}
4376
Adrian Bunk75c96f82005-05-05 16:16:09 -07004377static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378{
4379 int minor;
4380
4381 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4382 " MD_SB_DISKS=%d\n",
4383 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4384 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08004385 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07004386 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
4388 if (register_blkdev(MAJOR_NR, "md"))
4389 return -1;
4390 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4391 unregister_blkdev(MAJOR_NR, "md");
4392 return -1;
4393 }
4394 devfs_mk_dir("md");
4395 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4396 md_probe, NULL, NULL);
4397 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4398 md_probe, NULL, NULL);
4399
4400 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4401 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4402 S_IFBLK|S_IRUSR|S_IWUSR,
4403 "md/%d", minor);
4404
4405 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4406 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4407 S_IFBLK|S_IRUSR|S_IWUSR,
4408 "md/mdp%d", minor);
4409
4410
4411 register_reboot_notifier(&md_notifier);
4412 raid_table_header = register_sysctl_table(raid_root_table, 1);
4413
4414 md_geninit();
4415 return (0);
4416}
4417
4418
4419#ifndef MODULE
4420
4421/*
4422 * Searches all registered partitions for autorun RAID arrays
4423 * at boot time.
4424 */
4425static dev_t detected_devices[128];
4426static int dev_cnt;
4427
4428void md_autodetect_dev(dev_t dev)
4429{
4430 if (dev_cnt >= 0 && dev_cnt < 127)
4431 detected_devices[dev_cnt++] = dev;
4432}
4433
4434
4435static void autostart_arrays(int part)
4436{
4437 mdk_rdev_t *rdev;
4438 int i;
4439
4440 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4441
4442 for (i = 0; i < dev_cnt; i++) {
4443 dev_t dev = detected_devices[i];
4444
4445 rdev = md_import_device(dev,0, 0);
4446 if (IS_ERR(rdev))
4447 continue;
4448
NeilBrownb2d444d2005-11-08 21:39:31 -08004449 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 MD_BUG();
4451 continue;
4452 }
4453 list_add(&rdev->same_set, &pending_raid_disks);
4454 }
4455 dev_cnt = 0;
4456
4457 autorun_devices(part);
4458}
4459
4460#endif
4461
4462static __exit void md_exit(void)
4463{
4464 mddev_t *mddev;
4465 struct list_head *tmp;
4466 int i;
4467 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4468 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4469 for (i=0; i < MAX_MD_DEVS; i++)
4470 devfs_remove("md/%d", i);
4471 for (i=0; i < MAX_MD_DEVS; i++)
4472 devfs_remove("md/d%d", i);
4473
4474 devfs_remove("md");
4475
4476 unregister_blkdev(MAJOR_NR,"md");
4477 unregister_blkdev(mdp_major, "mdp");
4478 unregister_reboot_notifier(&md_notifier);
4479 unregister_sysctl_table(raid_table_header);
4480 remove_proc_entry("mdstat", NULL);
4481 ITERATE_MDDEV(mddev,tmp) {
4482 struct gendisk *disk = mddev->gendisk;
4483 if (!disk)
4484 continue;
4485 export_array(mddev);
4486 del_gendisk(disk);
4487 put_disk(disk);
4488 mddev->gendisk = NULL;
4489 mddev_put(mddev);
4490 }
4491}
4492
4493module_init(md_init)
4494module_exit(md_exit)
4495
NeilBrownf91de922005-11-08 21:39:36 -08004496static int get_ro(char *buffer, struct kernel_param *kp)
4497{
4498 return sprintf(buffer, "%d", start_readonly);
4499}
4500static int set_ro(const char *val, struct kernel_param *kp)
4501{
4502 char *e;
4503 int num = simple_strtoul(val, &e, 10);
4504 if (*val && (*e == '\0' || *e == '\n')) {
4505 start_readonly = num;
4506 return 0;;
4507 }
4508 return -EINVAL;
4509}
4510
4511module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
4512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513EXPORT_SYMBOL(register_md_personality);
4514EXPORT_SYMBOL(unregister_md_personality);
4515EXPORT_SYMBOL(md_error);
4516EXPORT_SYMBOL(md_done_sync);
4517EXPORT_SYMBOL(md_write_start);
4518EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519EXPORT_SYMBOL(md_register_thread);
4520EXPORT_SYMBOL(md_unregister_thread);
4521EXPORT_SYMBOL(md_wakeup_thread);
4522EXPORT_SYMBOL(md_print_devices);
4523EXPORT_SYMBOL(md_check_recovery);
4524MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07004525MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07004526MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);