blob: 64e7da3701a5a7c642c3e40845a2989215a99391 [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) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800717 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
718 && mddev->level != 10) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700719 /* FIXME use a better test */
NeilBrown6cce3b22006-01-06 00:20:16 -0800720 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -0700721 return -EINVAL;
722 }
NeilBrown36fa3062005-09-09 16:23:45 -0700723 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700724 }
725
NeilBrown41158c72005-06-21 17:17:25 -0700726 } else if (mddev->pers == NULL) {
727 /* Insist on good event counter while assembling */
728 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 ++ev1;
730 if (ev1 < mddev->events)
731 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700732 } else if (mddev->bitmap) {
733 /* if adding to array with a bitmap, then we can accept an
734 * older device ... but not too old.
735 */
736 __u64 ev1 = md_event(sb);
737 if (ev1 < mddev->bitmap->events_cleared)
738 return 0;
739 } else /* just a hot-add of a new device, leave raid_disk at -1 */
740 return 0;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 desc = sb->disks + rdev->desc_nr;
744
745 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800746 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 else if (desc->state & (1<<MD_DISK_SYNC) &&
748 desc->raid_disk < mddev->raid_disks) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800749 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 rdev->raid_disk = desc->raid_disk;
751 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700752 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
753 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700754 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800755 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return 0;
757}
758
759/*
760 * sync_super for 0.90.0
761 */
762static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
763{
764 mdp_super_t *sb;
765 struct list_head *tmp;
766 mdk_rdev_t *rdev2;
767 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /* make rdev->sb match mddev data..
771 *
772 * 1/ zero out disks
773 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
774 * 3/ any empty disks < next_spare become removed
775 *
776 * disks[0] gets initialised to REMOVED because
777 * we cannot be sure from other fields if it has
778 * been initialised or not.
779 */
780 int i;
781 int active=0, working=0,failed=0,spare=0,nr_disks=0;
782
NeilBrown61181562005-09-09 16:24:02 -0700783 rdev->sb_size = MD_SB_BYTES;
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 sb = (mdp_super_t*)page_address(rdev->sb_page);
786
787 memset(sb, 0, sizeof(*sb));
788
789 sb->md_magic = MD_SB_MAGIC;
790 sb->major_version = mddev->major_version;
791 sb->minor_version = mddev->minor_version;
792 sb->patch_version = mddev->patch_version;
793 sb->gvalid_words = 0; /* ignored */
794 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
795 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
796 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
797 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
798
799 sb->ctime = mddev->ctime;
800 sb->level = mddev->level;
801 sb->size = mddev->size;
802 sb->raid_disks = mddev->raid_disks;
803 sb->md_minor = mddev->md_minor;
804 sb->not_persistent = !mddev->persistent;
805 sb->utime = mddev->utime;
806 sb->state = 0;
807 sb->events_hi = (mddev->events>>32);
808 sb->events_lo = (u32)mddev->events;
809
810 if (mddev->in_sync)
811 {
812 sb->recovery_cp = mddev->recovery_cp;
813 sb->cp_events_hi = (mddev->events>>32);
814 sb->cp_events_lo = (u32)mddev->events;
815 if (mddev->recovery_cp == MaxSector)
816 sb->state = (1<< MD_SB_CLEAN);
817 } else
818 sb->recovery_cp = 0;
819
820 sb->layout = mddev->layout;
821 sb->chunk_size = mddev->chunk_size;
822
NeilBrowna654b9d82005-06-21 17:17:27 -0700823 if (mddev->bitmap && mddev->bitmap_file == NULL)
824 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 sb->disks[0].state = (1<<MD_DISK_REMOVED);
827 ITERATE_RDEV(mddev,rdev2,tmp) {
828 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800829 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800830 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
831 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800832 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800834 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800835 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 d = &sb->disks[rdev2->desc_nr];
837 nr_disks++;
838 d->number = rdev2->desc_nr;
839 d->major = MAJOR(rdev2->bdev->bd_dev);
840 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800841 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
842 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 d->raid_disk = rdev2->raid_disk;
844 else
845 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrownb2d444d2005-11-08 21:39:31 -0800846 if (test_bit(Faulty, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 d->state = (1<<MD_DISK_FAULTY);
848 failed++;
NeilBrownb2d444d2005-11-08 21:39:31 -0800849 } else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 d->state = (1<<MD_DISK_ACTIVE);
851 d->state |= (1<<MD_DISK_SYNC);
852 active++;
853 working++;
854 } else {
855 d->state = 0;
856 spare++;
857 working++;
858 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700859 if (test_bit(WriteMostly, &rdev2->flags))
860 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* now set the "removed" and "faulty" bits on any missing devices */
863 for (i=0 ; i < mddev->raid_disks ; i++) {
864 mdp_disk_t *d = &sb->disks[i];
865 if (d->state == 0 && d->number == 0) {
866 d->number = i;
867 d->raid_disk = i;
868 d->state = (1<<MD_DISK_REMOVED);
869 d->state |= (1<<MD_DISK_FAULTY);
870 failed++;
871 }
872 }
873 sb->nr_disks = nr_disks;
874 sb->active_disks = active;
875 sb->working_disks = working;
876 sb->failed_disks = failed;
877 sb->spare_disks = spare;
878
879 sb->this_disk = sb->disks[rdev->desc_nr];
880 sb->sb_csum = calc_sb_csum(sb);
881}
882
883/*
884 * version 1 superblock
885 */
886
887static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
888{
889 unsigned int disk_csum, csum;
890 unsigned long long newcsum;
891 int size = 256 + le32_to_cpu(sb->max_dev)*2;
892 unsigned int *isuper = (unsigned int*)sb;
893 int i;
894
895 disk_csum = sb->sb_csum;
896 sb->sb_csum = 0;
897 newcsum = 0;
898 for (i=0; size>=4; size -= 4 )
899 newcsum += le32_to_cpu(*isuper++);
900
901 if (size == 2)
902 newcsum += le16_to_cpu(*(unsigned short*) isuper);
903
904 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
905 sb->sb_csum = disk_csum;
906 return cpu_to_le32(csum);
907}
908
909static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
910{
911 struct mdp_superblock_1 *sb;
912 int ret;
913 sector_t sb_offset;
914 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -0700915 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 /*
918 * Calculate the position of the superblock.
919 * It is always aligned to a 4K boundary and
920 * depeding on minor_version, it can be:
921 * 0: At least 8K, but less than 12K, from end of device
922 * 1: At start of device
923 * 2: 4K from start of device.
924 */
925 switch(minor_version) {
926 case 0:
927 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
928 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -0700929 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /* convert from sectors to K */
931 sb_offset /= 2;
932 break;
933 case 1:
934 sb_offset = 0;
935 break;
936 case 2:
937 sb_offset = 4;
938 break;
939 default:
940 return -EINVAL;
941 }
942 rdev->sb_offset = sb_offset;
943
NeilBrown0002b272005-09-09 16:23:53 -0700944 /* superblock is rarely larger than 1K, but it can be larger,
945 * and it is safe to read 4k, so we do that
946 */
947 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (ret) return ret;
949
950
951 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
952
953 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
954 sb->major_version != cpu_to_le32(1) ||
955 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
956 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -0700957 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return -EINVAL;
959
960 if (calc_sb_1_csum(sb) != sb->sb_csum) {
961 printk("md: invalid superblock checksum on %s\n",
962 bdevname(rdev->bdev,b));
963 return -EINVAL;
964 }
965 if (le64_to_cpu(sb->data_size) < 10) {
966 printk("md: data_size too small on %s\n",
967 bdevname(rdev->bdev,b));
968 return -EINVAL;
969 }
970 rdev->preferred_minor = 0xffff;
971 rdev->data_offset = le64_to_cpu(sb->data_offset);
972
NeilBrown0002b272005-09-09 16:23:53 -0700973 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -0700974 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -0700975 if (rdev->sb_size & bmask)
976 rdev-> sb_size = (rdev->sb_size | bmask)+1;
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (refdev == 0)
979 return 1;
980 else {
981 __u64 ev1, ev2;
982 struct mdp_superblock_1 *refsb =
983 (struct mdp_superblock_1*)page_address(refdev->sb_page);
984
985 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
986 sb->level != refsb->level ||
987 sb->layout != refsb->layout ||
988 sb->chunksize != refsb->chunksize) {
989 printk(KERN_WARNING "md: %s has strangely different"
990 " superblock to %s\n",
991 bdevname(rdev->bdev,b),
992 bdevname(refdev->bdev,b2));
993 return -EINVAL;
994 }
995 ev1 = le64_to_cpu(sb->events);
996 ev2 = le64_to_cpu(refsb->events);
997
998 if (ev1 > ev2)
999 return 1;
1000 }
1001 if (minor_version)
1002 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1003 else
1004 rdev->size = rdev->sb_offset;
1005 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1006 return -EINVAL;
1007 rdev->size = le64_to_cpu(sb->data_size)/2;
1008 if (le32_to_cpu(sb->chunksize))
1009 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1010 return 0;
1011}
1012
1013static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1014{
1015 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1016
NeilBrown41158c72005-06-21 17:17:25 -07001017 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001018 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (mddev->raid_disks == 0) {
1020 mddev->major_version = 1;
1021 mddev->patch_version = 0;
1022 mddev->persistent = 1;
1023 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1024 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1025 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1026 mddev->level = le32_to_cpu(sb->level);
1027 mddev->layout = le32_to_cpu(sb->layout);
1028 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1029 mddev->size = le64_to_cpu(sb->size)/2;
1030 mddev->events = le64_to_cpu(sb->events);
NeilBrown92232142005-08-18 11:24:16 -07001031 mddev->bitmap_offset = 0;
NeilBrown53e87fb2005-09-09 16:23:58 -07001032 mddev->default_bitmap_offset = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1035 memcpy(mddev->uuid, sb->set_uuid, 16);
1036
1037 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001038
NeilBrown71c08052005-09-09 16:23:51 -07001039 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -07001040 mddev->bitmap_file == NULL ) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001041 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1042 && mddev->level != 10) {
1043 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -07001044 return -EINVAL;
1045 }
1046 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1047 }
NeilBrown41158c72005-06-21 17:17:25 -07001048 } else if (mddev->pers == NULL) {
1049 /* Insist of good event counter while assembling */
1050 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 ++ev1;
1052 if (ev1 < mddev->events)
1053 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001054 } else if (mddev->bitmap) {
1055 /* If adding to array with a bitmap, then we can accept an
1056 * older device, but not too old.
1057 */
1058 __u64 ev1 = le64_to_cpu(sb->events);
1059 if (ev1 < mddev->bitmap->events_cleared)
1060 return 0;
1061 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 if (mddev->level != LEVEL_MULTIPATH) {
1065 int role;
1066 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1067 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1068 switch(role) {
1069 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 break;
1071 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001072 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 break;
1074 default:
NeilBrownb2d444d2005-11-08 21:39:31 -08001075 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 rdev->raid_disk = role;
1077 break;
1078 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001079 if (sb->devflags & WriteMostly1)
1080 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001081 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001082 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return 0;
1085}
1086
1087static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1088{
1089 struct mdp_superblock_1 *sb;
1090 struct list_head *tmp;
1091 mdk_rdev_t *rdev2;
1092 int max_dev, i;
1093 /* make rdev->sb match mddev and rdev data. */
1094
1095 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1096
1097 sb->feature_map = 0;
1098 sb->pad0 = 0;
1099 memset(sb->pad1, 0, sizeof(sb->pad1));
1100 memset(sb->pad2, 0, sizeof(sb->pad2));
1101 memset(sb->pad3, 0, sizeof(sb->pad3));
1102
1103 sb->utime = cpu_to_le64((__u64)mddev->utime);
1104 sb->events = cpu_to_le64(mddev->events);
1105 if (mddev->in_sync)
1106 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1107 else
1108 sb->resync_offset = cpu_to_le64(0);
1109
NeilBrowna654b9d82005-06-21 17:17:27 -07001110 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1111 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001112 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001113 }
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 max_dev = 0;
1116 ITERATE_RDEV(mddev,rdev2,tmp)
1117 if (rdev2->desc_nr+1 > max_dev)
1118 max_dev = rdev2->desc_nr+1;
1119
1120 sb->max_dev = cpu_to_le32(max_dev);
1121 for (i=0; i<max_dev;i++)
1122 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1123
1124 ITERATE_RDEV(mddev,rdev2,tmp) {
1125 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001126 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001128 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1130 else
1131 sb->dev_roles[i] = cpu_to_le16(0xffff);
1132 }
1133
1134 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1135 sb->sb_csum = calc_sb_1_csum(sb);
1136}
1137
1138
Adrian Bunk75c96f82005-05-05 16:16:09 -07001139static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 [0] = {
1141 .name = "0.90.0",
1142 .owner = THIS_MODULE,
1143 .load_super = super_90_load,
1144 .validate_super = super_90_validate,
1145 .sync_super = super_90_sync,
1146 },
1147 [1] = {
1148 .name = "md-1",
1149 .owner = THIS_MODULE,
1150 .load_super = super_1_load,
1151 .validate_super = super_1_validate,
1152 .sync_super = super_1_sync,
1153 },
1154};
1155
1156static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1157{
1158 struct list_head *tmp;
1159 mdk_rdev_t *rdev;
1160
1161 ITERATE_RDEV(mddev,rdev,tmp)
1162 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1163 return rdev;
1164
1165 return NULL;
1166}
1167
1168static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1169{
1170 struct list_head *tmp;
1171 mdk_rdev_t *rdev;
1172
1173 ITERATE_RDEV(mddev1,rdev,tmp)
1174 if (match_dev_unit(mddev2, rdev))
1175 return 1;
1176
1177 return 0;
1178}
1179
1180static LIST_HEAD(pending_raid_disks);
1181
1182static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1183{
1184 mdk_rdev_t *same_pdev;
1185 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001186 struct kobject *ko;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 if (rdev->mddev) {
1189 MD_BUG();
1190 return -EINVAL;
1191 }
1192 same_pdev = match_dev_unit(mddev, rdev);
1193 if (same_pdev)
1194 printk(KERN_WARNING
1195 "%s: WARNING: %s appears to be on the same physical"
1196 " disk as %s. True\n protection against single-disk"
1197 " failure might be compromised.\n",
1198 mdname(mddev), bdevname(rdev->bdev,b),
1199 bdevname(same_pdev->bdev,b2));
1200
1201 /* Verify rdev->desc_nr is unique.
1202 * If it is -1, assign a free number, else
1203 * check number is not in use
1204 */
1205 if (rdev->desc_nr < 0) {
1206 int choice = 0;
1207 if (mddev->pers) choice = mddev->raid_disks;
1208 while (find_rdev_nr(mddev, choice))
1209 choice++;
1210 rdev->desc_nr = choice;
1211 } else {
1212 if (find_rdev_nr(mddev, rdev->desc_nr))
1213 return -EBUSY;
1214 }
NeilBrown19133a42005-11-08 21:39:35 -08001215 bdevname(rdev->bdev,b);
1216 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1217 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 list_add(&rdev->same_set, &mddev->disks);
1220 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001221 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001222
NeilBrown9c791972005-11-08 21:39:28 -08001223 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001224 kobject_add(&rdev->kobj);
1225
NeilBrownf637b9f2005-11-08 21:39:37 -08001226 if (rdev->bdev->bd_part)
1227 ko = &rdev->bdev->bd_part->kobj;
1228 else
1229 ko = &rdev->bdev->bd_disk->kobj;
1230 sysfs_create_link(&rdev->kobj, ko, "block");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return 0;
1232}
1233
1234static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1235{
1236 char b[BDEVNAME_SIZE];
1237 if (!rdev->mddev) {
1238 MD_BUG();
1239 return;
1240 }
1241 list_del_init(&rdev->same_set);
1242 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1243 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001244 sysfs_remove_link(&rdev->kobj, "block");
1245 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
1248/*
1249 * prevent the device from being mounted, repartitioned or
1250 * otherwise reused by a RAID array (or any other kernel
1251 * subsystem), by bd_claiming the device.
1252 */
1253static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1254{
1255 int err = 0;
1256 struct block_device *bdev;
1257 char b[BDEVNAME_SIZE];
1258
1259 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1260 if (IS_ERR(bdev)) {
1261 printk(KERN_ERR "md: could not open %s.\n",
1262 __bdevname(dev, b));
1263 return PTR_ERR(bdev);
1264 }
1265 err = bd_claim(bdev, rdev);
1266 if (err) {
1267 printk(KERN_ERR "md: could not bd_claim %s.\n",
1268 bdevname(bdev, b));
1269 blkdev_put(bdev);
1270 return err;
1271 }
1272 rdev->bdev = bdev;
1273 return err;
1274}
1275
1276static void unlock_rdev(mdk_rdev_t *rdev)
1277{
1278 struct block_device *bdev = rdev->bdev;
1279 rdev->bdev = NULL;
1280 if (!bdev)
1281 MD_BUG();
1282 bd_release(bdev);
1283 blkdev_put(bdev);
1284}
1285
1286void md_autodetect_dev(dev_t dev);
1287
1288static void export_rdev(mdk_rdev_t * rdev)
1289{
1290 char b[BDEVNAME_SIZE];
1291 printk(KERN_INFO "md: export_rdev(%s)\n",
1292 bdevname(rdev->bdev,b));
1293 if (rdev->mddev)
1294 MD_BUG();
1295 free_disk_sb(rdev);
1296 list_del_init(&rdev->same_set);
1297#ifndef MODULE
1298 md_autodetect_dev(rdev->bdev->bd_dev);
1299#endif
1300 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001301 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
1304static void kick_rdev_from_array(mdk_rdev_t * rdev)
1305{
1306 unbind_rdev_from_array(rdev);
1307 export_rdev(rdev);
1308}
1309
1310static void export_array(mddev_t *mddev)
1311{
1312 struct list_head *tmp;
1313 mdk_rdev_t *rdev;
1314
1315 ITERATE_RDEV(mddev,rdev,tmp) {
1316 if (!rdev->mddev) {
1317 MD_BUG();
1318 continue;
1319 }
1320 kick_rdev_from_array(rdev);
1321 }
1322 if (!list_empty(&mddev->disks))
1323 MD_BUG();
1324 mddev->raid_disks = 0;
1325 mddev->major_version = 0;
1326}
1327
1328static void print_desc(mdp_disk_t *desc)
1329{
1330 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1331 desc->major,desc->minor,desc->raid_disk,desc->state);
1332}
1333
1334static void print_sb(mdp_super_t *sb)
1335{
1336 int i;
1337
1338 printk(KERN_INFO
1339 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1340 sb->major_version, sb->minor_version, sb->patch_version,
1341 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1342 sb->ctime);
1343 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1344 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1345 sb->md_minor, sb->layout, sb->chunk_size);
1346 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1347 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1348 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1349 sb->failed_disks, sb->spare_disks,
1350 sb->sb_csum, (unsigned long)sb->events_lo);
1351
1352 printk(KERN_INFO);
1353 for (i = 0; i < MD_SB_DISKS; i++) {
1354 mdp_disk_t *desc;
1355
1356 desc = sb->disks + i;
1357 if (desc->number || desc->major || desc->minor ||
1358 desc->raid_disk || (desc->state && (desc->state != 4))) {
1359 printk(" D %2d: ", i);
1360 print_desc(desc);
1361 }
1362 }
1363 printk(KERN_INFO "md: THIS: ");
1364 print_desc(&sb->this_disk);
1365
1366}
1367
1368static void print_rdev(mdk_rdev_t *rdev)
1369{
1370 char b[BDEVNAME_SIZE];
1371 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1372 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001373 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1374 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (rdev->sb_loaded) {
1376 printk(KERN_INFO "md: rdev superblock:\n");
1377 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1378 } else
1379 printk(KERN_INFO "md: no rdev superblock!\n");
1380}
1381
1382void md_print_devices(void)
1383{
1384 struct list_head *tmp, *tmp2;
1385 mdk_rdev_t *rdev;
1386 mddev_t *mddev;
1387 char b[BDEVNAME_SIZE];
1388
1389 printk("\n");
1390 printk("md: **********************************\n");
1391 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1392 printk("md: **********************************\n");
1393 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
NeilBrown32a76272005-06-21 17:17:14 -07001395 if (mddev->bitmap)
1396 bitmap_print_sb(mddev->bitmap);
1397 else
1398 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 ITERATE_RDEV(mddev,rdev,tmp2)
1400 printk("<%s>", bdevname(rdev->bdev,b));
1401 printk("\n");
1402
1403 ITERATE_RDEV(mddev,rdev,tmp2)
1404 print_rdev(rdev);
1405 }
1406 printk("md: **********************************\n");
1407 printk("\n");
1408}
1409
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411static void sync_sbs(mddev_t * mddev)
1412{
1413 mdk_rdev_t *rdev;
1414 struct list_head *tmp;
1415
1416 ITERATE_RDEV(mddev,rdev,tmp) {
1417 super_types[mddev->major_version].
1418 sync_super(mddev, rdev);
1419 rdev->sb_loaded = 1;
1420 }
1421}
1422
1423static void md_update_sb(mddev_t * mddev)
1424{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001425 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 struct list_head *tmp;
1427 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001428 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001431 spin_lock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001432 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 mddev->utime = get_seconds();
1434 mddev->events ++;
1435
1436 if (!mddev->events) {
1437 /*
1438 * oops, this 64-bit counter should never wrap.
1439 * Either we are in around ~1 trillion A.C., assuming
1440 * 1 reboot per second, or we have a bug:
1441 */
1442 MD_BUG();
1443 mddev->events --;
1444 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001445 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 sync_sbs(mddev);
1447
1448 /*
1449 * do not write anything to disk if using
1450 * nonpersistent superblocks
1451 */
NeilBrown06d91a52005-06-21 17:17:12 -07001452 if (!mddev->persistent) {
1453 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001454 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001455 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001457 }
NeilBrowna9701a32005-11-08 21:39:34 -08001458 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 dprintk(KERN_INFO
1461 "md: updating %s RAID superblock on device (in sync %d)\n",
1462 mdname(mddev),mddev->in_sync);
1463
NeilBrown32a76272005-06-21 17:17:14 -07001464 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 ITERATE_RDEV(mddev,rdev,tmp) {
1466 char b[BDEVNAME_SIZE];
1467 dprintk(KERN_INFO "md: ");
NeilBrownb2d444d2005-11-08 21:39:31 -08001468 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 dprintk("(skipping faulty ");
1470
1471 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001472 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001473 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001474 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001475 rdev->sb_page);
1476 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1477 bdevname(rdev->bdev,b),
1478 (unsigned long long)rdev->sb_offset);
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 } else
1481 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001482 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /* only need to write one superblock... */
1484 break;
1485 }
NeilBrowna9701a32005-11-08 21:39:34 -08001486 md_super_wait(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001487 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1488
NeilBrowna9701a32005-11-08 21:39:34 -08001489 spin_lock_irq(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001490 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001491 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001492 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001493 goto repeat;
1494 }
1495 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001496 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001497 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
NeilBrown86e6ffd2005-11-08 21:39:24 -08001501struct rdev_sysfs_entry {
1502 struct attribute attr;
1503 ssize_t (*show)(mdk_rdev_t *, char *);
1504 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1505};
1506
1507static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001508state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001509{
1510 char *sep = "";
1511 int len=0;
1512
NeilBrownb2d444d2005-11-08 21:39:31 -08001513 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001514 len+= sprintf(page+len, "%sfaulty",sep);
1515 sep = ",";
1516 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001517 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001518 len += sprintf(page+len, "%sin_sync",sep);
1519 sep = ",";
1520 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001521 if (!test_bit(Faulty, &rdev->flags) &&
1522 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001523 len += sprintf(page+len, "%sspare", sep);
1524 sep = ",";
1525 }
1526 return len+sprintf(page+len, "\n");
1527}
1528
NeilBrown96de1e62005-11-08 21:39:39 -08001529static struct rdev_sysfs_entry
1530rdev_state = __ATTR_RO(state);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001531
1532static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001533super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001534{
1535 if (rdev->sb_loaded && rdev->sb_size) {
1536 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1537 return rdev->sb_size;
1538 } else
1539 return 0;
1540}
NeilBrown96de1e62005-11-08 21:39:39 -08001541static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1542
NeilBrown86e6ffd2005-11-08 21:39:24 -08001543static struct attribute *rdev_default_attrs[] = {
1544 &rdev_state.attr,
1545 &rdev_super.attr,
1546 NULL,
1547};
1548static ssize_t
1549rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1550{
1551 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1552 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1553
1554 if (!entry->show)
1555 return -EIO;
1556 return entry->show(rdev, page);
1557}
1558
1559static ssize_t
1560rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1561 const char *page, size_t length)
1562{
1563 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1564 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1565
1566 if (!entry->store)
1567 return -EIO;
1568 return entry->store(rdev, page, length);
1569}
1570
1571static void rdev_free(struct kobject *ko)
1572{
1573 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1574 kfree(rdev);
1575}
1576static struct sysfs_ops rdev_sysfs_ops = {
1577 .show = rdev_attr_show,
1578 .store = rdev_attr_store,
1579};
1580static struct kobj_type rdev_ktype = {
1581 .release = rdev_free,
1582 .sysfs_ops = &rdev_sysfs_ops,
1583 .default_attrs = rdev_default_attrs,
1584};
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586/*
1587 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1588 *
1589 * mark the device faulty if:
1590 *
1591 * - the device is nonexistent (zero size)
1592 * - the device has no valid superblock
1593 *
1594 * a faulty rdev _never_ has rdev->sb set.
1595 */
1596static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1597{
1598 char b[BDEVNAME_SIZE];
1599 int err;
1600 mdk_rdev_t *rdev;
1601 sector_t size;
1602
1603 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1604 if (!rdev) {
1605 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1606 return ERR_PTR(-ENOMEM);
1607 }
1608 memset(rdev, 0, sizeof(*rdev));
1609
1610 if ((err = alloc_disk_sb(rdev)))
1611 goto abort_free;
1612
1613 err = lock_rdev(rdev, newdev);
1614 if (err)
1615 goto abort_free;
1616
NeilBrown86e6ffd2005-11-08 21:39:24 -08001617 rdev->kobj.parent = NULL;
1618 rdev->kobj.ktype = &rdev_ktype;
1619 kobject_init(&rdev->kobj);
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 rdev->desc_nr = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001622 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 rdev->data_offset = 0;
1624 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001625 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1628 if (!size) {
1629 printk(KERN_WARNING
1630 "md: %s has zero or unknown size, marking faulty!\n",
1631 bdevname(rdev->bdev,b));
1632 err = -EINVAL;
1633 goto abort_free;
1634 }
1635
1636 if (super_format >= 0) {
1637 err = super_types[super_format].
1638 load_super(rdev, NULL, super_minor);
1639 if (err == -EINVAL) {
1640 printk(KERN_WARNING
1641 "md: %s has invalid sb, not importing!\n",
1642 bdevname(rdev->bdev,b));
1643 goto abort_free;
1644 }
1645 if (err < 0) {
1646 printk(KERN_WARNING
1647 "md: could not read %s's sb, not importing!\n",
1648 bdevname(rdev->bdev,b));
1649 goto abort_free;
1650 }
1651 }
1652 INIT_LIST_HEAD(&rdev->same_set);
1653
1654 return rdev;
1655
1656abort_free:
1657 if (rdev->sb_page) {
1658 if (rdev->bdev)
1659 unlock_rdev(rdev);
1660 free_disk_sb(rdev);
1661 }
1662 kfree(rdev);
1663 return ERR_PTR(err);
1664}
1665
1666/*
1667 * Check a full RAID array for plausibility
1668 */
1669
1670
NeilBrowna757e642005-04-16 15:26:42 -07001671static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 int i;
1674 struct list_head *tmp;
1675 mdk_rdev_t *rdev, *freshest;
1676 char b[BDEVNAME_SIZE];
1677
1678 freshest = NULL;
1679 ITERATE_RDEV(mddev,rdev,tmp)
1680 switch (super_types[mddev->major_version].
1681 load_super(rdev, freshest, mddev->minor_version)) {
1682 case 1:
1683 freshest = rdev;
1684 break;
1685 case 0:
1686 break;
1687 default:
1688 printk( KERN_ERR \
1689 "md: fatal superblock inconsistency in %s"
1690 " -- removing from array\n",
1691 bdevname(rdev->bdev,b));
1692 kick_rdev_from_array(rdev);
1693 }
1694
1695
1696 super_types[mddev->major_version].
1697 validate_super(mddev, freshest);
1698
1699 i = 0;
1700 ITERATE_RDEV(mddev,rdev,tmp) {
1701 if (rdev != freshest)
1702 if (super_types[mddev->major_version].
1703 validate_super(mddev, rdev)) {
1704 printk(KERN_WARNING "md: kicking non-fresh %s"
1705 " from array!\n",
1706 bdevname(rdev->bdev,b));
1707 kick_rdev_from_array(rdev);
1708 continue;
1709 }
1710 if (mddev->level == LEVEL_MULTIPATH) {
1711 rdev->desc_nr = i++;
1712 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001713 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715 }
1716
1717
1718
1719 if (mddev->recovery_cp != MaxSector &&
1720 mddev->level >= 1)
1721 printk(KERN_ERR "md: %s: raid array is not clean"
1722 " -- starting background reconstruction\n",
1723 mdname(mddev));
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
NeilBrowneae17012005-11-08 21:39:23 -08001727static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001728level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08001729{
1730 mdk_personality_t *p = mddev->pers;
NeilBrownbb636542005-11-08 21:39:45 -08001731 if (p == NULL && mddev->raid_disks == 0)
NeilBrowneae17012005-11-08 21:39:23 -08001732 return 0;
1733 if (mddev->level >= 0)
Neil Brownbcb97942005-12-20 11:07:00 +11001734 return sprintf(page, "raid%d\n", mddev->level);
NeilBrowneae17012005-11-08 21:39:23 -08001735 else
1736 return sprintf(page, "%s\n", p->name);
1737}
1738
NeilBrown96de1e62005-11-08 21:39:39 -08001739static struct md_sysfs_entry md_level = __ATTR_RO(level);
NeilBrowneae17012005-11-08 21:39:23 -08001740
1741static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001742raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08001743{
NeilBrownbb636542005-11-08 21:39:45 -08001744 if (mddev->raid_disks == 0)
1745 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08001746 return sprintf(page, "%d\n", mddev->raid_disks);
1747}
1748
NeilBrown96de1e62005-11-08 21:39:39 -08001749static struct md_sysfs_entry md_raid_disks = __ATTR_RO(raid_disks);
NeilBrowneae17012005-11-08 21:39:23 -08001750
NeilBrown24dd4692005-11-08 21:39:26 -08001751static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08001752action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08001753{
NeilBrown7eec3142005-11-08 21:39:44 -08001754 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08001755 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1756 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
1757 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08001758 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1759 type = "resync";
1760 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1761 type = "check";
1762 else
1763 type = "repair";
1764 } else
1765 type = "recover";
1766 }
1767 return sprintf(page, "%s\n", type);
1768}
1769
1770static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08001771action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08001772{
NeilBrown7eec3142005-11-08 21:39:44 -08001773 if (!mddev->pers || !mddev->pers->sync_request)
1774 return -EINVAL;
1775
1776 if (strcmp(page, "idle")==0 || strcmp(page, "idle\n")==0) {
1777 if (mddev->sync_thread) {
1778 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1779 md_unregister_thread(mddev->sync_thread);
1780 mddev->sync_thread = NULL;
1781 mddev->recovery = 0;
1782 }
1783 return len;
1784 }
NeilBrown31399d92005-11-08 21:39:29 -08001785
1786 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1787 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08001788 return -EBUSY;
NeilBrown7eec3142005-11-08 21:39:44 -08001789 if (strcmp(page, "resync")==0 || strcmp(page, "resync\n")==0 ||
1790 strcmp(page, "recover")==0 || strcmp(page, "recover\n")==0)
1791 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1792 else {
1793 if (strcmp(page, "check")==0 || strcmp(page, "check\n")==0)
1794 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1795 else if (strcmp(page, "repair")!=0 && strcmp(page, "repair\n")!=0)
1796 return -EINVAL;
1797 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1798 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1799 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1800 }
NeilBrown24dd4692005-11-08 21:39:26 -08001801 md_wakeup_thread(mddev->thread);
1802 return len;
1803}
1804
NeilBrown9d888832005-11-08 21:39:26 -08001805static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001806mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08001807{
1808 return sprintf(page, "%llu\n",
1809 (unsigned long long) mddev->resync_mismatches);
1810}
1811
NeilBrown96de1e62005-11-08 21:39:39 -08001812static struct md_sysfs_entry
NeilBrown7eec3142005-11-08 21:39:44 -08001813md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08001814
NeilBrown96de1e62005-11-08 21:39:39 -08001815
1816static struct md_sysfs_entry
1817md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08001818
NeilBrowneae17012005-11-08 21:39:23 -08001819static struct attribute *md_default_attrs[] = {
1820 &md_level.attr,
1821 &md_raid_disks.attr,
NeilBrown411036f2005-11-08 21:39:40 -08001822 NULL,
1823};
1824
1825static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08001826 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08001827 &md_mismatches.attr,
NeilBrowneae17012005-11-08 21:39:23 -08001828 NULL,
1829};
NeilBrown411036f2005-11-08 21:39:40 -08001830static struct attribute_group md_redundancy_group = {
1831 .name = NULL,
1832 .attrs = md_redundancy_attrs,
1833};
1834
NeilBrowneae17012005-11-08 21:39:23 -08001835
1836static ssize_t
1837md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1838{
1839 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1840 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08001841 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08001842
1843 if (!entry->show)
1844 return -EIO;
NeilBrown96de1e62005-11-08 21:39:39 -08001845 mddev_lock(mddev);
1846 rv = entry->show(mddev, page);
1847 mddev_unlock(mddev);
1848 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08001849}
1850
1851static ssize_t
1852md_attr_store(struct kobject *kobj, struct attribute *attr,
1853 const char *page, size_t length)
1854{
1855 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1856 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08001857 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08001858
1859 if (!entry->store)
1860 return -EIO;
NeilBrown96de1e62005-11-08 21:39:39 -08001861 mddev_lock(mddev);
1862 rv = entry->store(mddev, page, length);
1863 mddev_unlock(mddev);
1864 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08001865}
1866
1867static void md_free(struct kobject *ko)
1868{
1869 mddev_t *mddev = container_of(ko, mddev_t, kobj);
1870 kfree(mddev);
1871}
1872
1873static struct sysfs_ops md_sysfs_ops = {
1874 .show = md_attr_show,
1875 .store = md_attr_store,
1876};
1877static struct kobj_type md_ktype = {
1878 .release = md_free,
1879 .sysfs_ops = &md_sysfs_ops,
1880 .default_attrs = md_default_attrs,
1881};
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883int mdp_major = 0;
1884
1885static struct kobject *md_probe(dev_t dev, int *part, void *data)
1886{
1887 static DECLARE_MUTEX(disks_sem);
1888 mddev_t *mddev = mddev_find(dev);
1889 struct gendisk *disk;
1890 int partitioned = (MAJOR(dev) != MD_MAJOR);
1891 int shift = partitioned ? MdpMinorShift : 0;
1892 int unit = MINOR(dev) >> shift;
1893
1894 if (!mddev)
1895 return NULL;
1896
1897 down(&disks_sem);
1898 if (mddev->gendisk) {
1899 up(&disks_sem);
1900 mddev_put(mddev);
1901 return NULL;
1902 }
1903 disk = alloc_disk(1 << shift);
1904 if (!disk) {
1905 up(&disks_sem);
1906 mddev_put(mddev);
1907 return NULL;
1908 }
1909 disk->major = MAJOR(dev);
1910 disk->first_minor = unit << shift;
1911 if (partitioned) {
1912 sprintf(disk->disk_name, "md_d%d", unit);
1913 sprintf(disk->devfs_name, "md/d%d", unit);
1914 } else {
1915 sprintf(disk->disk_name, "md%d", unit);
1916 sprintf(disk->devfs_name, "md/%d", unit);
1917 }
1918 disk->fops = &md_fops;
1919 disk->private_data = mddev;
1920 disk->queue = mddev->queue;
1921 add_disk(disk);
1922 mddev->gendisk = disk;
1923 up(&disks_sem);
NeilBrown9c791972005-11-08 21:39:28 -08001924 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08001925 mddev->kobj.k_name = NULL;
1926 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
1927 mddev->kobj.ktype = &md_ktype;
1928 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return NULL;
1930}
1931
1932void md_wakeup_thread(mdk_thread_t *thread);
1933
1934static void md_safemode_timeout(unsigned long data)
1935{
1936 mddev_t *mddev = (mddev_t *) data;
1937
1938 mddev->safemode = 1;
1939 md_wakeup_thread(mddev->thread);
1940}
1941
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08001942static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944static int do_md_run(mddev_t * mddev)
1945{
1946 int pnum, err;
1947 int chunk_size;
1948 struct list_head *tmp;
1949 mdk_rdev_t *rdev;
1950 struct gendisk *disk;
1951 char b[BDEVNAME_SIZE];
1952
NeilBrowna757e642005-04-16 15:26:42 -07001953 if (list_empty(&mddev->disks))
1954 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 if (mddev->pers)
1958 return -EBUSY;
1959
1960 /*
1961 * Analyze all RAID superblock(s)
1962 */
NeilBrowna757e642005-04-16 15:26:42 -07001963 if (!mddev->raid_disks)
1964 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 chunk_size = mddev->chunk_size;
1967 pnum = level_to_pers(mddev->level);
1968
1969 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1970 if (!chunk_size) {
1971 /*
1972 * 'default chunksize' in the old md code used to
1973 * be PAGE_SIZE, baaad.
1974 * we abort here to be on the safe side. We don't
1975 * want to continue the bad practice.
1976 */
1977 printk(KERN_ERR
1978 "no chunksize specified, see 'man raidtab'\n");
1979 return -EINVAL;
1980 }
1981 if (chunk_size > MAX_CHUNK_SIZE) {
1982 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1983 chunk_size, MAX_CHUNK_SIZE);
1984 return -EINVAL;
1985 }
1986 /*
1987 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1988 */
1989 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07001990 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return -EINVAL;
1992 }
1993 if (chunk_size < PAGE_SIZE) {
1994 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1995 chunk_size, PAGE_SIZE);
1996 return -EINVAL;
1997 }
1998
1999 /* devices must have minimum size of one chunk */
2000 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002001 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 continue;
2003 if (rdev->size < chunk_size / 1024) {
2004 printk(KERN_WARNING
2005 "md: Dev %s smaller than chunk_size:"
2006 " %lluk < %dk\n",
2007 bdevname(rdev->bdev,b),
2008 (unsigned long long)rdev->size,
2009 chunk_size / 1024);
2010 return -EINVAL;
2011 }
2012 }
2013 }
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015#ifdef CONFIG_KMOD
2016 if (!pers[pnum])
2017 {
2018 request_module("md-personality-%d", pnum);
2019 }
2020#endif
2021
2022 /*
2023 * Drop all container device buffers, from now on
2024 * the only valid external interface is through the md
2025 * device.
2026 * Also find largest hardsector size
2027 */
2028 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002029 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 continue;
2031 sync_blockdev(rdev->bdev);
2032 invalidate_bdev(rdev->bdev, 0);
2033 }
2034
2035 md_probe(mddev->unit, NULL, NULL);
2036 disk = mddev->gendisk;
2037 if (!disk)
2038 return -ENOMEM;
2039
2040 spin_lock(&pers_lock);
2041 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
2042 spin_unlock(&pers_lock);
2043 printk(KERN_WARNING "md: personality %d is not loaded!\n",
2044 pnum);
2045 return -EINVAL;
2046 }
2047
2048 mddev->pers = pers[pnum];
2049 spin_unlock(&pers_lock);
2050
NeilBrown657390d2005-08-26 18:34:16 -07002051 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08002053 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002054 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
NeilBrownf91de922005-11-08 21:39:36 -08002056 if (start_readonly)
2057 mddev->ro = 2; /* read-only, but switch on first write */
2058
NeilBrownb15c2e52006-01-06 00:20:16 -08002059 err = mddev->pers->run(mddev);
2060 if (!err && mddev->pers->sync_request) {
2061 err = bitmap_create(mddev);
2062 if (err) {
2063 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2064 mdname(mddev), err);
2065 mddev->pers->stop(mddev);
2066 }
2067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (err) {
2069 printk(KERN_ERR "md: pers->run() failed ...\n");
2070 module_put(mddev->pers->owner);
2071 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002072 bitmap_destroy(mddev);
2073 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
NeilBrown411036f2005-11-08 21:39:40 -08002075 if (mddev->pers->sync_request)
2076 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08002077 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2078 mddev->ro = 0;
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 atomic_set(&mddev->writes_pending,0);
2081 mddev->safemode = 0;
2082 mddev->safemode_timer.function = md_safemode_timeout;
2083 mddev->safemode_timer.data = (unsigned long) mddev;
2084 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2085 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002086
2087 ITERATE_RDEV(mddev,rdev,tmp)
2088 if (rdev->raid_disk >= 0) {
2089 char nm[20];
2090 sprintf(nm, "rd%d", rdev->raid_disk);
2091 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002095 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 if (mddev->sb_dirty)
2098 md_update_sb(mddev);
2099
2100 set_capacity(disk, mddev->array_size<<1);
2101
2102 /* If we call blk_queue_make_request here, it will
2103 * re-initialise max_sectors etc which may have been
2104 * refined inside -> run. So just set the bits we need to set.
2105 * Most initialisation happended when we called
2106 * blk_queue_make_request(..., md_fail_request)
2107 * earlier.
2108 */
2109 mddev->queue->queuedata = mddev;
2110 mddev->queue->make_request_fn = mddev->pers->make_request;
2111
2112 mddev->changed = 1;
2113 return 0;
2114}
2115
2116static int restart_array(mddev_t *mddev)
2117{
2118 struct gendisk *disk = mddev->gendisk;
2119 int err;
2120
2121 /*
2122 * Complain if it has no devices
2123 */
2124 err = -ENXIO;
2125 if (list_empty(&mddev->disks))
2126 goto out;
2127
2128 if (mddev->pers) {
2129 err = -EBUSY;
2130 if (!mddev->ro)
2131 goto out;
2132
2133 mddev->safemode = 0;
2134 mddev->ro = 0;
2135 set_disk_ro(disk, 0);
2136
2137 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2138 mdname(mddev));
2139 /*
2140 * Kick recovery or resync if necessary
2141 */
2142 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2143 md_wakeup_thread(mddev->thread);
2144 err = 0;
2145 } else {
2146 printk(KERN_ERR "md: %s has no personality assigned.\n",
2147 mdname(mddev));
2148 err = -EINVAL;
2149 }
2150
2151out:
2152 return err;
2153}
2154
2155static int do_md_stop(mddev_t * mddev, int ro)
2156{
2157 int err = 0;
2158 struct gendisk *disk = mddev->gendisk;
2159
2160 if (mddev->pers) {
2161 if (atomic_read(&mddev->active)>2) {
2162 printk("md: %s still in use.\n",mdname(mddev));
2163 return -EBUSY;
2164 }
2165
2166 if (mddev->sync_thread) {
2167 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2168 md_unregister_thread(mddev->sync_thread);
2169 mddev->sync_thread = NULL;
2170 }
2171
2172 del_timer_sync(&mddev->safemode_timer);
2173
2174 invalidate_partition(disk, 0);
2175
2176 if (ro) {
2177 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08002178 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 goto out;
2180 mddev->ro = 1;
2181 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002182 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08002183 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if (mddev->ro)
2185 set_disk_ro(disk, 0);
2186 blk_queue_make_request(mddev->queue, md_fail_request);
2187 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08002188 if (mddev->pers->sync_request)
2189 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 module_put(mddev->pers->owner);
2192 mddev->pers = NULL;
2193 if (mddev->ro)
2194 mddev->ro = 0;
2195 }
2196 if (!mddev->in_sync) {
2197 /* mark array as shutdown cleanly */
2198 mddev->in_sync = 1;
2199 md_update_sb(mddev);
2200 }
2201 if (ro)
2202 set_disk_ro(disk, 1);
2203 }
NeilBrown32a76272005-06-21 17:17:14 -07002204
2205 bitmap_destroy(mddev);
2206 if (mddev->bitmap_file) {
2207 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2208 fput(mddev->bitmap_file);
2209 mddev->bitmap_file = NULL;
2210 }
NeilBrown92232142005-08-18 11:24:16 -07002211 mddev->bitmap_offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07002212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /*
2214 * Free resources if final stop
2215 */
2216 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002217 mdk_rdev_t *rdev;
2218 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 struct gendisk *disk;
2220 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2221
NeilBrown86e6ffd2005-11-08 21:39:24 -08002222 ITERATE_RDEV(mddev,rdev,tmp)
2223 if (rdev->raid_disk >= 0) {
2224 char nm[20];
2225 sprintf(nm, "rd%d", rdev->raid_disk);
2226 sysfs_remove_link(&mddev->kobj, nm);
2227 }
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 export_array(mddev);
2230
2231 mddev->array_size = 0;
2232 disk = mddev->gendisk;
2233 if (disk)
2234 set_capacity(disk, 0);
2235 mddev->changed = 1;
2236 } else
2237 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2238 mdname(mddev));
2239 err = 0;
2240out:
2241 return err;
2242}
2243
2244static void autorun_array(mddev_t *mddev)
2245{
2246 mdk_rdev_t *rdev;
2247 struct list_head *tmp;
2248 int err;
2249
NeilBrowna757e642005-04-16 15:26:42 -07002250 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 printk(KERN_INFO "md: running: ");
2254
2255 ITERATE_RDEV(mddev,rdev,tmp) {
2256 char b[BDEVNAME_SIZE];
2257 printk("<%s>", bdevname(rdev->bdev,b));
2258 }
2259 printk("\n");
2260
2261 err = do_md_run (mddev);
2262 if (err) {
2263 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2264 do_md_stop (mddev, 0);
2265 }
2266}
2267
2268/*
2269 * lets try to run arrays based on all disks that have arrived
2270 * until now. (those are in pending_raid_disks)
2271 *
2272 * the method: pick the first pending disk, collect all disks with
2273 * the same UUID, remove all from the pending list and put them into
2274 * the 'same_array' list. Then order this list based on superblock
2275 * update time (freshest comes first), kick out 'old' disks and
2276 * compare superblocks. If everything's fine then run it.
2277 *
2278 * If "unit" is allocated, then bump its reference count
2279 */
2280static void autorun_devices(int part)
2281{
2282 struct list_head candidates;
2283 struct list_head *tmp;
2284 mdk_rdev_t *rdev0, *rdev;
2285 mddev_t *mddev;
2286 char b[BDEVNAME_SIZE];
2287
2288 printk(KERN_INFO "md: autorun ...\n");
2289 while (!list_empty(&pending_raid_disks)) {
2290 dev_t dev;
2291 rdev0 = list_entry(pending_raid_disks.next,
2292 mdk_rdev_t, same_set);
2293
2294 printk(KERN_INFO "md: considering %s ...\n",
2295 bdevname(rdev0->bdev,b));
2296 INIT_LIST_HEAD(&candidates);
2297 ITERATE_RDEV_PENDING(rdev,tmp)
2298 if (super_90_load(rdev, rdev0, 0) >= 0) {
2299 printk(KERN_INFO "md: adding %s ...\n",
2300 bdevname(rdev->bdev,b));
2301 list_move(&rdev->same_set, &candidates);
2302 }
2303 /*
2304 * now we have a set of devices, with all of them having
2305 * mostly sane superblocks. It's time to allocate the
2306 * mddev.
2307 */
2308 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2309 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2310 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2311 break;
2312 }
2313 if (part)
2314 dev = MKDEV(mdp_major,
2315 rdev0->preferred_minor << MdpMinorShift);
2316 else
2317 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2318
2319 md_probe(dev, NULL, NULL);
2320 mddev = mddev_find(dev);
2321 if (!mddev) {
2322 printk(KERN_ERR
2323 "md: cannot allocate memory for md drive.\n");
2324 break;
2325 }
2326 if (mddev_lock(mddev))
2327 printk(KERN_WARNING "md: %s locked, cannot run\n",
2328 mdname(mddev));
2329 else if (mddev->raid_disks || mddev->major_version
2330 || !list_empty(&mddev->disks)) {
2331 printk(KERN_WARNING
2332 "md: %s already running, cannot run %s\n",
2333 mdname(mddev), bdevname(rdev0->bdev,b));
2334 mddev_unlock(mddev);
2335 } else {
2336 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2337 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2338 list_del_init(&rdev->same_set);
2339 if (bind_rdev_to_array(rdev, mddev))
2340 export_rdev(rdev);
2341 }
2342 autorun_array(mddev);
2343 mddev_unlock(mddev);
2344 }
2345 /* on success, candidates will be empty, on error
2346 * it won't...
2347 */
2348 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2349 export_rdev(rdev);
2350 mddev_put(mddev);
2351 }
2352 printk(KERN_INFO "md: ... autorun DONE.\n");
2353}
2354
2355/*
2356 * import RAID devices based on one partition
2357 * if possible, the array gets run as well.
2358 */
2359
2360static int autostart_array(dev_t startdev)
2361{
2362 char b[BDEVNAME_SIZE];
2363 int err = -EINVAL, i;
2364 mdp_super_t *sb = NULL;
2365 mdk_rdev_t *start_rdev = NULL, *rdev;
2366
2367 start_rdev = md_import_device(startdev, 0, 0);
2368 if (IS_ERR(start_rdev))
2369 return err;
2370
2371
2372 /* NOTE: this can only work for 0.90.0 superblocks */
2373 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2374 if (sb->major_version != 0 ||
2375 sb->minor_version != 90 ) {
2376 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2377 export_rdev(start_rdev);
2378 return err;
2379 }
2380
NeilBrownb2d444d2005-11-08 21:39:31 -08002381 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 printk(KERN_WARNING
2383 "md: can not autostart based on faulty %s!\n",
2384 bdevname(start_rdev->bdev,b));
2385 export_rdev(start_rdev);
2386 return err;
2387 }
2388 list_add(&start_rdev->same_set, &pending_raid_disks);
2389
2390 for (i = 0; i < MD_SB_DISKS; i++) {
2391 mdp_disk_t *desc = sb->disks + i;
2392 dev_t dev = MKDEV(desc->major, desc->minor);
2393
2394 if (!dev)
2395 continue;
2396 if (dev == startdev)
2397 continue;
2398 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2399 continue;
2400 rdev = md_import_device(dev, 0, 0);
2401 if (IS_ERR(rdev))
2402 continue;
2403
2404 list_add(&rdev->same_set, &pending_raid_disks);
2405 }
2406
2407 /*
2408 * possibly return codes
2409 */
2410 autorun_devices(0);
2411 return 0;
2412
2413}
2414
2415
2416static int get_version(void __user * arg)
2417{
2418 mdu_version_t ver;
2419
2420 ver.major = MD_MAJOR_VERSION;
2421 ver.minor = MD_MINOR_VERSION;
2422 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2423
2424 if (copy_to_user(arg, &ver, sizeof(ver)))
2425 return -EFAULT;
2426
2427 return 0;
2428}
2429
2430static int get_array_info(mddev_t * mddev, void __user * arg)
2431{
2432 mdu_array_info_t info;
2433 int nr,working,active,failed,spare;
2434 mdk_rdev_t *rdev;
2435 struct list_head *tmp;
2436
2437 nr=working=active=failed=spare=0;
2438 ITERATE_RDEV(mddev,rdev,tmp) {
2439 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002440 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 failed++;
2442 else {
2443 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002444 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 active++;
2446 else
2447 spare++;
2448 }
2449 }
2450
2451 info.major_version = mddev->major_version;
2452 info.minor_version = mddev->minor_version;
2453 info.patch_version = MD_PATCHLEVEL_VERSION;
2454 info.ctime = mddev->ctime;
2455 info.level = mddev->level;
2456 info.size = mddev->size;
2457 info.nr_disks = nr;
2458 info.raid_disks = mddev->raid_disks;
2459 info.md_minor = mddev->md_minor;
2460 info.not_persistent= !mddev->persistent;
2461
2462 info.utime = mddev->utime;
2463 info.state = 0;
2464 if (mddev->in_sync)
2465 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07002466 if (mddev->bitmap && mddev->bitmap_offset)
2467 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 info.active_disks = active;
2469 info.working_disks = working;
2470 info.failed_disks = failed;
2471 info.spare_disks = spare;
2472
2473 info.layout = mddev->layout;
2474 info.chunk_size = mddev->chunk_size;
2475
2476 if (copy_to_user(arg, &info, sizeof(info)))
2477 return -EFAULT;
2478
2479 return 0;
2480}
2481
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002482static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07002483{
2484 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2485 char *ptr, *buf = NULL;
2486 int err = -ENOMEM;
2487
2488 file = kmalloc(sizeof(*file), GFP_KERNEL);
2489 if (!file)
2490 goto out;
2491
2492 /* bitmap disabled, zero the first byte and copy out */
2493 if (!mddev->bitmap || !mddev->bitmap->file) {
2494 file->pathname[0] = '\0';
2495 goto copy_out;
2496 }
2497
2498 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2499 if (!buf)
2500 goto out;
2501
2502 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2503 if (!ptr)
2504 goto out;
2505
2506 strcpy(file->pathname, ptr);
2507
2508copy_out:
2509 err = 0;
2510 if (copy_to_user(arg, file, sizeof(*file)))
2511 err = -EFAULT;
2512out:
2513 kfree(buf);
2514 kfree(file);
2515 return err;
2516}
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518static int get_disk_info(mddev_t * mddev, void __user * arg)
2519{
2520 mdu_disk_info_t info;
2521 unsigned int nr;
2522 mdk_rdev_t *rdev;
2523
2524 if (copy_from_user(&info, arg, sizeof(info)))
2525 return -EFAULT;
2526
2527 nr = info.number;
2528
2529 rdev = find_rdev_nr(mddev, nr);
2530 if (rdev) {
2531 info.major = MAJOR(rdev->bdev->bd_dev);
2532 info.minor = MINOR(rdev->bdev->bd_dev);
2533 info.raid_disk = rdev->raid_disk;
2534 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08002535 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002537 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 info.state |= (1<<MD_DISK_ACTIVE);
2539 info.state |= (1<<MD_DISK_SYNC);
2540 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002541 if (test_bit(WriteMostly, &rdev->flags))
2542 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 } else {
2544 info.major = info.minor = 0;
2545 info.raid_disk = -1;
2546 info.state = (1<<MD_DISK_REMOVED);
2547 }
2548
2549 if (copy_to_user(arg, &info, sizeof(info)))
2550 return -EFAULT;
2551
2552 return 0;
2553}
2554
2555static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2556{
2557 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2558 mdk_rdev_t *rdev;
2559 dev_t dev = MKDEV(info->major,info->minor);
2560
2561 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2562 return -EOVERFLOW;
2563
2564 if (!mddev->raid_disks) {
2565 int err;
2566 /* expecting a device which has a superblock */
2567 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2568 if (IS_ERR(rdev)) {
2569 printk(KERN_WARNING
2570 "md: md_import_device returned %ld\n",
2571 PTR_ERR(rdev));
2572 return PTR_ERR(rdev);
2573 }
2574 if (!list_empty(&mddev->disks)) {
2575 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2576 mdk_rdev_t, same_set);
2577 int err = super_types[mddev->major_version]
2578 .load_super(rdev, rdev0, mddev->minor_version);
2579 if (err < 0) {
2580 printk(KERN_WARNING
2581 "md: %s has different UUID to %s\n",
2582 bdevname(rdev->bdev,b),
2583 bdevname(rdev0->bdev,b2));
2584 export_rdev(rdev);
2585 return -EINVAL;
2586 }
2587 }
2588 err = bind_rdev_to_array(rdev, mddev);
2589 if (err)
2590 export_rdev(rdev);
2591 return err;
2592 }
2593
2594 /*
2595 * add_new_disk can be used once the array is assembled
2596 * to add "hot spares". They must already have a superblock
2597 * written
2598 */
2599 if (mddev->pers) {
2600 int err;
2601 if (!mddev->pers->hot_add_disk) {
2602 printk(KERN_WARNING
2603 "%s: personality does not support diskops!\n",
2604 mdname(mddev));
2605 return -EINVAL;
2606 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07002607 if (mddev->persistent)
2608 rdev = md_import_device(dev, mddev->major_version,
2609 mddev->minor_version);
2610 else
2611 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 if (IS_ERR(rdev)) {
2613 printk(KERN_WARNING
2614 "md: md_import_device returned %ld\n",
2615 PTR_ERR(rdev));
2616 return PTR_ERR(rdev);
2617 }
NeilBrown41158c72005-06-21 17:17:25 -07002618 /* set save_raid_disk if appropriate */
2619 if (!mddev->persistent) {
2620 if (info->state & (1<<MD_DISK_SYNC) &&
2621 info->raid_disk < mddev->raid_disks)
2622 rdev->raid_disk = info->raid_disk;
2623 else
2624 rdev->raid_disk = -1;
2625 } else
2626 super_types[mddev->major_version].
2627 validate_super(mddev, rdev);
2628 rdev->saved_raid_disk = rdev->raid_disk;
2629
NeilBrownb2d444d2005-11-08 21:39:31 -08002630 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002631 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2632 set_bit(WriteMostly, &rdev->flags);
2633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 rdev->raid_disk = -1;
2635 err = bind_rdev_to_array(rdev, mddev);
2636 if (err)
2637 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07002638
2639 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002640 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return err;
2642 }
2643
2644 /* otherwise, add_new_disk is only allowed
2645 * for major_version==0 superblocks
2646 */
2647 if (mddev->major_version != 0) {
2648 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2649 mdname(mddev));
2650 return -EINVAL;
2651 }
2652
2653 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2654 int err;
2655 rdev = md_import_device (dev, -1, 0);
2656 if (IS_ERR(rdev)) {
2657 printk(KERN_WARNING
2658 "md: error, md_import_device() returned %ld\n",
2659 PTR_ERR(rdev));
2660 return PTR_ERR(rdev);
2661 }
2662 rdev->desc_nr = info->number;
2663 if (info->raid_disk < mddev->raid_disks)
2664 rdev->raid_disk = info->raid_disk;
2665 else
2666 rdev->raid_disk = -1;
2667
NeilBrownb2d444d2005-11-08 21:39:31 -08002668 rdev->flags = 0;
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08002671 if (info->state & (1<<MD_DISK_SYNC))
2672 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002674 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2675 set_bit(WriteMostly, &rdev->flags);
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 err = bind_rdev_to_array(rdev, mddev);
2678 if (err) {
2679 export_rdev(rdev);
2680 return err;
2681 }
2682
2683 if (!mddev->persistent) {
2684 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2685 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2686 } else
2687 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2688 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2689
2690 if (!mddev->size || (mddev->size > rdev->size))
2691 mddev->size = rdev->size;
2692 }
2693
2694 return 0;
2695}
2696
2697static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2698{
2699 char b[BDEVNAME_SIZE];
2700 mdk_rdev_t *rdev;
2701
2702 if (!mddev->pers)
2703 return -ENODEV;
2704
2705 rdev = find_rdev(mddev, dev);
2706 if (!rdev)
2707 return -ENXIO;
2708
2709 if (rdev->raid_disk >= 0)
2710 goto busy;
2711
2712 kick_rdev_from_array(rdev);
2713 md_update_sb(mddev);
2714
2715 return 0;
2716busy:
2717 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2718 bdevname(rdev->bdev,b), mdname(mddev));
2719 return -EBUSY;
2720}
2721
2722static int hot_add_disk(mddev_t * mddev, dev_t dev)
2723{
2724 char b[BDEVNAME_SIZE];
2725 int err;
2726 unsigned int size;
2727 mdk_rdev_t *rdev;
2728
2729 if (!mddev->pers)
2730 return -ENODEV;
2731
2732 if (mddev->major_version != 0) {
2733 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2734 " version-0 superblocks.\n",
2735 mdname(mddev));
2736 return -EINVAL;
2737 }
2738 if (!mddev->pers->hot_add_disk) {
2739 printk(KERN_WARNING
2740 "%s: personality does not support diskops!\n",
2741 mdname(mddev));
2742 return -EINVAL;
2743 }
2744
2745 rdev = md_import_device (dev, -1, 0);
2746 if (IS_ERR(rdev)) {
2747 printk(KERN_WARNING
2748 "md: error, md_import_device() returned %ld\n",
2749 PTR_ERR(rdev));
2750 return -EINVAL;
2751 }
2752
2753 if (mddev->persistent)
2754 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2755 else
2756 rdev->sb_offset =
2757 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2758
2759 size = calc_dev_size(rdev, mddev->chunk_size);
2760 rdev->size = size;
2761
2762 if (size < mddev->size) {
2763 printk(KERN_WARNING
2764 "%s: disk size %llu blocks < array size %llu\n",
2765 mdname(mddev), (unsigned long long)size,
2766 (unsigned long long)mddev->size);
2767 err = -ENOSPC;
2768 goto abort_export;
2769 }
2770
NeilBrownb2d444d2005-11-08 21:39:31 -08002771 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 printk(KERN_WARNING
2773 "md: can not hot-add faulty %s disk to %s!\n",
2774 bdevname(rdev->bdev,b), mdname(mddev));
2775 err = -EINVAL;
2776 goto abort_export;
2777 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002778 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 rdev->desc_nr = -1;
2780 bind_rdev_to_array(rdev, mddev);
2781
2782 /*
2783 * The rest should better be atomic, we can have disk failures
2784 * noticed in interrupt contexts ...
2785 */
2786
2787 if (rdev->desc_nr == mddev->max_disks) {
2788 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2789 mdname(mddev));
2790 err = -EBUSY;
2791 goto abort_unbind_export;
2792 }
2793
2794 rdev->raid_disk = -1;
2795
2796 md_update_sb(mddev);
2797
2798 /*
2799 * Kick recovery, maybe this spare has to be added to the
2800 * array immediately.
2801 */
2802 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2803 md_wakeup_thread(mddev->thread);
2804
2805 return 0;
2806
2807abort_unbind_export:
2808 unbind_rdev_from_array(rdev);
2809
2810abort_export:
2811 export_rdev(rdev);
2812 return err;
2813}
2814
NeilBrown32a76272005-06-21 17:17:14 -07002815/* similar to deny_write_access, but accounts for our holding a reference
2816 * to the file ourselves */
2817static int deny_bitmap_write_access(struct file * file)
2818{
2819 struct inode *inode = file->f_mapping->host;
2820
2821 spin_lock(&inode->i_lock);
2822 if (atomic_read(&inode->i_writecount) > 1) {
2823 spin_unlock(&inode->i_lock);
2824 return -ETXTBSY;
2825 }
2826 atomic_set(&inode->i_writecount, -1);
2827 spin_unlock(&inode->i_lock);
2828
2829 return 0;
2830}
2831
2832static int set_bitmap_file(mddev_t *mddev, int fd)
2833{
2834 int err;
2835
NeilBrown36fa3062005-09-09 16:23:45 -07002836 if (mddev->pers) {
2837 if (!mddev->pers->quiesce)
2838 return -EBUSY;
2839 if (mddev->recovery || mddev->sync_thread)
2840 return -EBUSY;
2841 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07002842 }
2843
NeilBrown36fa3062005-09-09 16:23:45 -07002844
2845 if (fd >= 0) {
2846 if (mddev->bitmap)
2847 return -EEXIST; /* cannot add when bitmap is present */
2848 mddev->bitmap_file = fget(fd);
2849
2850 if (mddev->bitmap_file == NULL) {
2851 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2852 mdname(mddev));
2853 return -EBADF;
2854 }
2855
2856 err = deny_bitmap_write_access(mddev->bitmap_file);
2857 if (err) {
2858 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2859 mdname(mddev));
2860 fput(mddev->bitmap_file);
2861 mddev->bitmap_file = NULL;
2862 return err;
2863 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002864 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07002865 } else if (mddev->bitmap == NULL)
2866 return -ENOENT; /* cannot remove what isn't there */
2867 err = 0;
2868 if (mddev->pers) {
2869 mddev->pers->quiesce(mddev, 1);
2870 if (fd >= 0)
2871 err = bitmap_create(mddev);
2872 if (fd < 0 || err)
2873 bitmap_destroy(mddev);
2874 mddev->pers->quiesce(mddev, 0);
2875 } else if (fd < 0) {
2876 if (mddev->bitmap_file)
2877 fput(mddev->bitmap_file);
2878 mddev->bitmap_file = NULL;
2879 }
2880
NeilBrown32a76272005-06-21 17:17:14 -07002881 return err;
2882}
2883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884/*
2885 * set_array_info is used two different ways
2886 * The original usage is when creating a new array.
2887 * In this usage, raid_disks is > 0 and it together with
2888 * level, size, not_persistent,layout,chunksize determine the
2889 * shape of the array.
2890 * This will always create an array with a type-0.90.0 superblock.
2891 * The newer usage is when assembling an array.
2892 * In this case raid_disks will be 0, and the major_version field is
2893 * use to determine which style super-blocks are to be found on the devices.
2894 * The minor and patch _version numbers are also kept incase the
2895 * super_block handler wishes to interpret them.
2896 */
2897static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2898{
2899
2900 if (info->raid_disks == 0) {
2901 /* just setting version number for superblock loading */
2902 if (info->major_version < 0 ||
2903 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2904 super_types[info->major_version].name == NULL) {
2905 /* maybe try to auto-load a module? */
2906 printk(KERN_INFO
2907 "md: superblock version %d not known\n",
2908 info->major_version);
2909 return -EINVAL;
2910 }
2911 mddev->major_version = info->major_version;
2912 mddev->minor_version = info->minor_version;
2913 mddev->patch_version = info->patch_version;
2914 return 0;
2915 }
2916 mddev->major_version = MD_MAJOR_VERSION;
2917 mddev->minor_version = MD_MINOR_VERSION;
2918 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2919 mddev->ctime = get_seconds();
2920
2921 mddev->level = info->level;
2922 mddev->size = info->size;
2923 mddev->raid_disks = info->raid_disks;
2924 /* don't set md_minor, it is determined by which /dev/md* was
2925 * openned
2926 */
2927 if (info->state & (1<<MD_SB_CLEAN))
2928 mddev->recovery_cp = MaxSector;
2929 else
2930 mddev->recovery_cp = 0;
2931 mddev->persistent = ! info->not_persistent;
2932
2933 mddev->layout = info->layout;
2934 mddev->chunk_size = info->chunk_size;
2935
2936 mddev->max_disks = MD_SB_DISKS;
2937
2938 mddev->sb_dirty = 1;
2939
NeilBrownb2a27032005-11-28 13:44:12 -08002940 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
2941 mddev->bitmap_offset = 0;
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 /*
2944 * Generate a 128 bit UUID
2945 */
2946 get_random_bytes(mddev->uuid, 16);
2947
2948 return 0;
2949}
2950
2951/*
2952 * update_array_info is used to change the configuration of an
2953 * on-line array.
2954 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2955 * fields in the info are checked against the array.
2956 * Any differences that cannot be handled will cause an error.
2957 * Normally, only one change can be managed at a time.
2958 */
2959static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2960{
2961 int rv = 0;
2962 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07002963 int state = 0;
2964
2965 /* calculate expected state,ignoring low bits */
2966 if (mddev->bitmap && mddev->bitmap_offset)
2967 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
2969 if (mddev->major_version != info->major_version ||
2970 mddev->minor_version != info->minor_version ||
2971/* mddev->patch_version != info->patch_version || */
2972 mddev->ctime != info->ctime ||
2973 mddev->level != info->level ||
2974/* mddev->layout != info->layout || */
2975 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07002976 mddev->chunk_size != info->chunk_size ||
2977 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2978 ((state^info->state) & 0xfffffe00)
2979 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return -EINVAL;
2981 /* Check there is only one change */
2982 if (mddev->size != info->size) cnt++;
2983 if (mddev->raid_disks != info->raid_disks) cnt++;
2984 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07002985 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (cnt == 0) return 0;
2987 if (cnt > 1) return -EINVAL;
2988
2989 if (mddev->layout != info->layout) {
2990 /* Change layout
2991 * we don't need to do anything at the md level, the
2992 * personality will take care of it all.
2993 */
2994 if (mddev->pers->reconfig == NULL)
2995 return -EINVAL;
2996 else
2997 return mddev->pers->reconfig(mddev, info->layout, -1);
2998 }
2999 if (mddev->size != info->size) {
3000 mdk_rdev_t * rdev;
3001 struct list_head *tmp;
3002 if (mddev->pers->resize == NULL)
3003 return -EINVAL;
3004 /* The "size" is the amount of each device that is used.
3005 * This can only make sense for arrays with redundancy.
3006 * linear and raid0 always use whatever space is available
3007 * We can only consider changing the size if no resync
3008 * or reconstruction is happening, and if the new size
3009 * is acceptable. It must fit before the sb_offset or,
3010 * if that is <data_offset, it must fit before the
3011 * size of each device.
3012 * If size is zero, we find the largest size that fits.
3013 */
3014 if (mddev->sync_thread)
3015 return -EBUSY;
3016 ITERATE_RDEV(mddev,rdev,tmp) {
3017 sector_t avail;
3018 int fit = (info->size == 0);
3019 if (rdev->sb_offset > rdev->data_offset)
3020 avail = (rdev->sb_offset*2) - rdev->data_offset;
3021 else
3022 avail = get_capacity(rdev->bdev->bd_disk)
3023 - rdev->data_offset;
3024 if (fit && (info->size == 0 || info->size > avail/2))
3025 info->size = avail/2;
3026 if (avail < ((sector_t)info->size << 1))
3027 return -ENOSPC;
3028 }
3029 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
3030 if (!rv) {
3031 struct block_device *bdev;
3032
3033 bdev = bdget_disk(mddev->gendisk, 0);
3034 if (bdev) {
3035 down(&bdev->bd_inode->i_sem);
3036 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3037 up(&bdev->bd_inode->i_sem);
3038 bdput(bdev);
3039 }
3040 }
3041 }
3042 if (mddev->raid_disks != info->raid_disks) {
3043 /* change the number of raid disks */
3044 if (mddev->pers->reshape == NULL)
3045 return -EINVAL;
3046 if (info->raid_disks <= 0 ||
3047 info->raid_disks >= mddev->max_disks)
3048 return -EINVAL;
3049 if (mddev->sync_thread)
3050 return -EBUSY;
3051 rv = mddev->pers->reshape(mddev, info->raid_disks);
3052 if (!rv) {
3053 struct block_device *bdev;
3054
3055 bdev = bdget_disk(mddev->gendisk, 0);
3056 if (bdev) {
3057 down(&bdev->bd_inode->i_sem);
3058 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3059 up(&bdev->bd_inode->i_sem);
3060 bdput(bdev);
3061 }
3062 }
3063 }
NeilBrown36fa3062005-09-09 16:23:45 -07003064 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3065 if (mddev->pers->quiesce == NULL)
3066 return -EINVAL;
3067 if (mddev->recovery || mddev->sync_thread)
3068 return -EBUSY;
3069 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3070 /* add the bitmap */
3071 if (mddev->bitmap)
3072 return -EEXIST;
3073 if (mddev->default_bitmap_offset == 0)
3074 return -EINVAL;
3075 mddev->bitmap_offset = mddev->default_bitmap_offset;
3076 mddev->pers->quiesce(mddev, 1);
3077 rv = bitmap_create(mddev);
3078 if (rv)
3079 bitmap_destroy(mddev);
3080 mddev->pers->quiesce(mddev, 0);
3081 } else {
3082 /* remove the bitmap */
3083 if (!mddev->bitmap)
3084 return -ENOENT;
3085 if (mddev->bitmap->file)
3086 return -EINVAL;
3087 mddev->pers->quiesce(mddev, 1);
3088 bitmap_destroy(mddev);
3089 mddev->pers->quiesce(mddev, 0);
3090 mddev->bitmap_offset = 0;
3091 }
3092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 md_update_sb(mddev);
3094 return rv;
3095}
3096
3097static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3098{
3099 mdk_rdev_t *rdev;
3100
3101 if (mddev->pers == NULL)
3102 return -ENODEV;
3103
3104 rdev = find_rdev(mddev, dev);
3105 if (!rdev)
3106 return -ENODEV;
3107
3108 md_error(mddev, rdev);
3109 return 0;
3110}
3111
3112static int md_ioctl(struct inode *inode, struct file *file,
3113 unsigned int cmd, unsigned long arg)
3114{
3115 int err = 0;
3116 void __user *argp = (void __user *)arg;
3117 struct hd_geometry __user *loc = argp;
3118 mddev_t *mddev = NULL;
3119
3120 if (!capable(CAP_SYS_ADMIN))
3121 return -EACCES;
3122
3123 /*
3124 * Commands dealing with the RAID driver but not any
3125 * particular array:
3126 */
3127 switch (cmd)
3128 {
3129 case RAID_VERSION:
3130 err = get_version(argp);
3131 goto done;
3132
3133 case PRINT_RAID_DEBUG:
3134 err = 0;
3135 md_print_devices();
3136 goto done;
3137
3138#ifndef MODULE
3139 case RAID_AUTORUN:
3140 err = 0;
3141 autostart_arrays(arg);
3142 goto done;
3143#endif
3144 default:;
3145 }
3146
3147 /*
3148 * Commands creating/starting a new array:
3149 */
3150
3151 mddev = inode->i_bdev->bd_disk->private_data;
3152
3153 if (!mddev) {
3154 BUG();
3155 goto abort;
3156 }
3157
3158
3159 if (cmd == START_ARRAY) {
3160 /* START_ARRAY doesn't need to lock the array as autostart_array
3161 * does the locking, and it could even be a different array
3162 */
3163 static int cnt = 3;
3164 if (cnt > 0 ) {
3165 printk(KERN_WARNING
3166 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08003167 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 current->comm, current->pid);
3169 cnt--;
3170 }
3171 err = autostart_array(new_decode_dev(arg));
3172 if (err) {
3173 printk(KERN_WARNING "md: autostart failed!\n");
3174 goto abort;
3175 }
3176 goto done;
3177 }
3178
3179 err = mddev_lock(mddev);
3180 if (err) {
3181 printk(KERN_INFO
3182 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3183 err, cmd);
3184 goto abort;
3185 }
3186
3187 switch (cmd)
3188 {
3189 case SET_ARRAY_INFO:
3190 {
3191 mdu_array_info_t info;
3192 if (!arg)
3193 memset(&info, 0, sizeof(info));
3194 else if (copy_from_user(&info, argp, sizeof(info))) {
3195 err = -EFAULT;
3196 goto abort_unlock;
3197 }
3198 if (mddev->pers) {
3199 err = update_array_info(mddev, &info);
3200 if (err) {
3201 printk(KERN_WARNING "md: couldn't update"
3202 " array info. %d\n", err);
3203 goto abort_unlock;
3204 }
3205 goto done_unlock;
3206 }
3207 if (!list_empty(&mddev->disks)) {
3208 printk(KERN_WARNING
3209 "md: array %s already has disks!\n",
3210 mdname(mddev));
3211 err = -EBUSY;
3212 goto abort_unlock;
3213 }
3214 if (mddev->raid_disks) {
3215 printk(KERN_WARNING
3216 "md: array %s already initialised!\n",
3217 mdname(mddev));
3218 err = -EBUSY;
3219 goto abort_unlock;
3220 }
3221 err = set_array_info(mddev, &info);
3222 if (err) {
3223 printk(KERN_WARNING "md: couldn't set"
3224 " array info. %d\n", err);
3225 goto abort_unlock;
3226 }
3227 }
3228 goto done_unlock;
3229
3230 default:;
3231 }
3232
3233 /*
3234 * Commands querying/configuring an existing array:
3235 */
NeilBrown32a76272005-06-21 17:17:14 -07003236 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3237 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3238 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3239 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 err = -ENODEV;
3241 goto abort_unlock;
3242 }
3243
3244 /*
3245 * Commands even a read-only array can execute:
3246 */
3247 switch (cmd)
3248 {
3249 case GET_ARRAY_INFO:
3250 err = get_array_info(mddev, argp);
3251 goto done_unlock;
3252
NeilBrown32a76272005-06-21 17:17:14 -07003253 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003254 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003255 goto done_unlock;
3256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 case GET_DISK_INFO:
3258 err = get_disk_info(mddev, argp);
3259 goto done_unlock;
3260
3261 case RESTART_ARRAY_RW:
3262 err = restart_array(mddev);
3263 goto done_unlock;
3264
3265 case STOP_ARRAY:
3266 err = do_md_stop (mddev, 0);
3267 goto done_unlock;
3268
3269 case STOP_ARRAY_RO:
3270 err = do_md_stop (mddev, 1);
3271 goto done_unlock;
3272
3273 /*
3274 * We have a problem here : there is no easy way to give a CHS
3275 * virtual geometry. We currently pretend that we have a 2 heads
3276 * 4 sectors (with a BIG number of cylinders...). This drives
3277 * dosfs just mad... ;-)
3278 */
3279 case HDIO_GETGEO:
3280 if (!loc) {
3281 err = -EINVAL;
3282 goto abort_unlock;
3283 }
3284 err = put_user (2, (char __user *) &loc->heads);
3285 if (err)
3286 goto abort_unlock;
3287 err = put_user (4, (char __user *) &loc->sectors);
3288 if (err)
3289 goto abort_unlock;
3290 err = put_user(get_capacity(mddev->gendisk)/8,
3291 (short __user *) &loc->cylinders);
3292 if (err)
3293 goto abort_unlock;
3294 err = put_user (get_start_sect(inode->i_bdev),
3295 (long __user *) &loc->start);
3296 goto done_unlock;
3297 }
3298
3299 /*
3300 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08003301 * superblock, so we do not allow them on read-only arrays.
3302 * However non-MD ioctls (e.g. get-size) will still come through
3303 * here and hit the 'default' below, so only disallow
3304 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 */
NeilBrownf91de922005-11-08 21:39:36 -08003306 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3307 mddev->ro && mddev->pers) {
3308 if (mddev->ro == 2) {
3309 mddev->ro = 0;
3310 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3311 md_wakeup_thread(mddev->thread);
3312
3313 } else {
3314 err = -EROFS;
3315 goto abort_unlock;
3316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 }
3318
3319 switch (cmd)
3320 {
3321 case ADD_NEW_DISK:
3322 {
3323 mdu_disk_info_t info;
3324 if (copy_from_user(&info, argp, sizeof(info)))
3325 err = -EFAULT;
3326 else
3327 err = add_new_disk(mddev, &info);
3328 goto done_unlock;
3329 }
3330
3331 case HOT_REMOVE_DISK:
3332 err = hot_remove_disk(mddev, new_decode_dev(arg));
3333 goto done_unlock;
3334
3335 case HOT_ADD_DISK:
3336 err = hot_add_disk(mddev, new_decode_dev(arg));
3337 goto done_unlock;
3338
3339 case SET_DISK_FAULTY:
3340 err = set_disk_faulty(mddev, new_decode_dev(arg));
3341 goto done_unlock;
3342
3343 case RUN_ARRAY:
3344 err = do_md_run (mddev);
3345 goto done_unlock;
3346
NeilBrown32a76272005-06-21 17:17:14 -07003347 case SET_BITMAP_FILE:
3348 err = set_bitmap_file(mddev, (int)arg);
3349 goto done_unlock;
3350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 default:
3352 if (_IOC_TYPE(cmd) == MD_MAJOR)
3353 printk(KERN_WARNING "md: %s(pid %d) used"
3354 " obsolete MD ioctl, upgrade your"
3355 " software to use new ictls.\n",
3356 current->comm, current->pid);
3357 err = -EINVAL;
3358 goto abort_unlock;
3359 }
3360
3361done_unlock:
3362abort_unlock:
3363 mddev_unlock(mddev);
3364
3365 return err;
3366done:
3367 if (err)
3368 MD_BUG();
3369abort:
3370 return err;
3371}
3372
3373static int md_open(struct inode *inode, struct file *file)
3374{
3375 /*
3376 * Succeed if we can lock the mddev, which confirms that
3377 * it isn't being stopped right now.
3378 */
3379 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3380 int err;
3381
3382 if ((err = mddev_lock(mddev)))
3383 goto out;
3384
3385 err = 0;
3386 mddev_get(mddev);
3387 mddev_unlock(mddev);
3388
3389 check_disk_change(inode->i_bdev);
3390 out:
3391 return err;
3392}
3393
3394static int md_release(struct inode *inode, struct file * file)
3395{
3396 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3397
3398 if (!mddev)
3399 BUG();
3400 mddev_put(mddev);
3401
3402 return 0;
3403}
3404
3405static int md_media_changed(struct gendisk *disk)
3406{
3407 mddev_t *mddev = disk->private_data;
3408
3409 return mddev->changed;
3410}
3411
3412static int md_revalidate(struct gendisk *disk)
3413{
3414 mddev_t *mddev = disk->private_data;
3415
3416 mddev->changed = 0;
3417 return 0;
3418}
3419static struct block_device_operations md_fops =
3420{
3421 .owner = THIS_MODULE,
3422 .open = md_open,
3423 .release = md_release,
3424 .ioctl = md_ioctl,
3425 .media_changed = md_media_changed,
3426 .revalidate_disk= md_revalidate,
3427};
3428
Adrian Bunk75c96f82005-05-05 16:16:09 -07003429static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
3431 mdk_thread_t *thread = arg;
3432
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 /*
3434 * md_thread is a 'system-thread', it's priority should be very
3435 * high. We avoid resource deadlocks individually in each
3436 * raid personality. (RAID5 does preallocation) We also use RR and
3437 * the very same RT priority as kswapd, thus we will never get
3438 * into a priority inversion deadlock.
3439 *
3440 * we definitely have to have equal or higher priority than
3441 * bdflush, otherwise bdflush will deadlock if there are too
3442 * many dirty RAID5 blocks.
3443 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
NeilBrown6985c432005-10-19 21:23:47 -07003445 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07003446 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
NeilBrown93588e22005-11-15 00:09:12 -08003448 /* We need to wait INTERRUPTIBLE so that
3449 * we don't add to the load-average.
3450 * That means we need to be sure no signals are
3451 * pending
3452 */
3453 if (signal_pending(current))
3454 flush_signals(current);
3455
3456 wait_event_interruptible_timeout
3457 (thread->wqueue,
3458 test_bit(THREAD_WAKEUP, &thread->flags)
3459 || kthread_should_stop(),
3460 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003461 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
3463 clear_bit(THREAD_WAKEUP, &thread->flags);
3464
NeilBrown787453c2005-11-08 21:39:43 -08003465 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 }
NeilBrowna6fb0932005-09-09 16:23:56 -07003467
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 return 0;
3469}
3470
3471void md_wakeup_thread(mdk_thread_t *thread)
3472{
3473 if (thread) {
3474 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3475 set_bit(THREAD_WAKEUP, &thread->flags);
3476 wake_up(&thread->wqueue);
3477 }
3478}
3479
3480mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3481 const char *name)
3482{
3483 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
NeilBrowna6fb0932005-09-09 16:23:56 -07003485 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 if (!thread)
3487 return NULL;
3488
3489 memset(thread, 0, sizeof(mdk_thread_t));
3490 init_waitqueue_head(&thread->wqueue);
3491
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 thread->run = run;
3493 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07003494 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07003495 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07003496 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 kfree(thread);
3498 return NULL;
3499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 return thread;
3501}
3502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503void md_unregister_thread(mdk_thread_t *thread)
3504{
NeilBrownd28446f2005-04-16 15:26:41 -07003505 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07003506
3507 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 kfree(thread);
3509}
3510
3511void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3512{
3513 if (!mddev) {
3514 MD_BUG();
3515 return;
3516 }
3517
NeilBrownb2d444d2005-11-08 21:39:31 -08003518 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 return;
NeilBrown32a76272005-06-21 17:17:14 -07003520/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3522 mdname(mddev),
3523 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3524 __builtin_return_address(0),__builtin_return_address(1),
3525 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07003526*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 if (!mddev->pers->error_handler)
3528 return;
3529 mddev->pers->error_handler(mddev,rdev);
3530 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3531 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3532 md_wakeup_thread(mddev->thread);
3533}
3534
3535/* seq_file implementation /proc/mdstat */
3536
3537static void status_unused(struct seq_file *seq)
3538{
3539 int i = 0;
3540 mdk_rdev_t *rdev;
3541 struct list_head *tmp;
3542
3543 seq_printf(seq, "unused devices: ");
3544
3545 ITERATE_RDEV_PENDING(rdev,tmp) {
3546 char b[BDEVNAME_SIZE];
3547 i++;
3548 seq_printf(seq, "%s ",
3549 bdevname(rdev->bdev,b));
3550 }
3551 if (!i)
3552 seq_printf(seq, "<none>");
3553
3554 seq_printf(seq, "\n");
3555}
3556
3557
3558static void status_resync(struct seq_file *seq, mddev_t * mddev)
3559{
3560 unsigned long max_blocks, resync, res, dt, db, rt;
3561
3562 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3563
3564 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3565 max_blocks = mddev->resync_max_sectors >> 1;
3566 else
3567 max_blocks = mddev->size;
3568
3569 /*
3570 * Should not happen.
3571 */
3572 if (!max_blocks) {
3573 MD_BUG();
3574 return;
3575 }
3576 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3577 {
3578 int i, x = res/50, y = 20-x;
3579 seq_printf(seq, "[");
3580 for (i = 0; i < x; i++)
3581 seq_printf(seq, "=");
3582 seq_printf(seq, ">");
3583 for (i = 0; i < y; i++)
3584 seq_printf(seq, ".");
3585 seq_printf(seq, "] ");
3586 }
3587 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3588 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3589 "resync" : "recovery"),
3590 res/10, res % 10, resync, max_blocks);
3591
3592 /*
3593 * We do not want to overflow, so the order of operands and
3594 * the * 100 / 100 trick are important. We do a +1 to be
3595 * safe against division by zero. We only estimate anyway.
3596 *
3597 * dt: time from mark until now
3598 * db: blocks written from mark until now
3599 * rt: remaining time
3600 */
3601 dt = ((jiffies - mddev->resync_mark) / HZ);
3602 if (!dt) dt++;
3603 db = resync - (mddev->resync_mark_cnt/2);
3604 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3605
3606 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3607
3608 seq_printf(seq, " speed=%ldK/sec", db/dt);
3609}
3610
3611static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3612{
3613 struct list_head *tmp;
3614 loff_t l = *pos;
3615 mddev_t *mddev;
3616
3617 if (l >= 0x10000)
3618 return NULL;
3619 if (!l--)
3620 /* header */
3621 return (void*)1;
3622
3623 spin_lock(&all_mddevs_lock);
3624 list_for_each(tmp,&all_mddevs)
3625 if (!l--) {
3626 mddev = list_entry(tmp, mddev_t, all_mddevs);
3627 mddev_get(mddev);
3628 spin_unlock(&all_mddevs_lock);
3629 return mddev;
3630 }
3631 spin_unlock(&all_mddevs_lock);
3632 if (!l--)
3633 return (void*)2;/* tail */
3634 return NULL;
3635}
3636
3637static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3638{
3639 struct list_head *tmp;
3640 mddev_t *next_mddev, *mddev = v;
3641
3642 ++*pos;
3643 if (v == (void*)2)
3644 return NULL;
3645
3646 spin_lock(&all_mddevs_lock);
3647 if (v == (void*)1)
3648 tmp = all_mddevs.next;
3649 else
3650 tmp = mddev->all_mddevs.next;
3651 if (tmp != &all_mddevs)
3652 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3653 else {
3654 next_mddev = (void*)2;
3655 *pos = 0x10000;
3656 }
3657 spin_unlock(&all_mddevs_lock);
3658
3659 if (v != (void*)1)
3660 mddev_put(mddev);
3661 return next_mddev;
3662
3663}
3664
3665static void md_seq_stop(struct seq_file *seq, void *v)
3666{
3667 mddev_t *mddev = v;
3668
3669 if (mddev && v != (void*)1 && v != (void*)2)
3670 mddev_put(mddev);
3671}
3672
3673static int md_seq_show(struct seq_file *seq, void *v)
3674{
3675 mddev_t *mddev = v;
3676 sector_t size;
3677 struct list_head *tmp2;
3678 mdk_rdev_t *rdev;
3679 int i;
NeilBrown32a76272005-06-21 17:17:14 -07003680 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
3682 if (v == (void*)1) {
3683 seq_printf(seq, "Personalities : ");
3684 spin_lock(&pers_lock);
3685 for (i = 0; i < MAX_PERSONALITY; i++)
3686 if (pers[i])
3687 seq_printf(seq, "[%s] ", pers[i]->name);
3688
3689 spin_unlock(&pers_lock);
3690 seq_printf(seq, "\n");
3691 return 0;
3692 }
3693 if (v == (void*)2) {
3694 status_unused(seq);
3695 return 0;
3696 }
3697
3698 if (mddev_lock(mddev)!=0)
3699 return -EINTR;
3700 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3701 seq_printf(seq, "%s : %sactive", mdname(mddev),
3702 mddev->pers ? "" : "in");
3703 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08003704 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08003706 if (mddev->ro==2)
3707 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 seq_printf(seq, " %s", mddev->pers->name);
3709 }
3710
3711 size = 0;
3712 ITERATE_RDEV(mddev,rdev,tmp2) {
3713 char b[BDEVNAME_SIZE];
3714 seq_printf(seq, " %s[%d]",
3715 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003716 if (test_bit(WriteMostly, &rdev->flags))
3717 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08003718 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 seq_printf(seq, "(F)");
3720 continue;
NeilBrownb325a322005-09-09 16:24:00 -07003721 } else if (rdev->raid_disk < 0)
3722 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 size += rdev->size;
3724 }
3725
3726 if (!list_empty(&mddev->disks)) {
3727 if (mddev->pers)
3728 seq_printf(seq, "\n %llu blocks",
3729 (unsigned long long)mddev->array_size);
3730 else
3731 seq_printf(seq, "\n %llu blocks",
3732 (unsigned long long)size);
3733 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07003734 if (mddev->persistent) {
3735 if (mddev->major_version != 0 ||
3736 mddev->minor_version != 90) {
3737 seq_printf(seq," super %d.%d",
3738 mddev->major_version,
3739 mddev->minor_version);
3740 }
3741 } else
3742 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
3744 if (mddev->pers) {
3745 mddev->pers->status (seq, mddev);
3746 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08003747 if (mddev->pers->sync_request) {
3748 if (mddev->curr_resync > 2) {
3749 status_resync (seq, mddev);
3750 seq_printf(seq, "\n ");
3751 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3752 seq_printf(seq, "\tresync=DELAYED\n ");
3753 else if (mddev->recovery_cp < MaxSector)
3754 seq_printf(seq, "\tresync=PENDING\n ");
3755 }
NeilBrown32a76272005-06-21 17:17:14 -07003756 } else
3757 seq_printf(seq, "\n ");
3758
3759 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07003760 unsigned long chunk_kb;
3761 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07003762 spin_lock_irqsave(&bitmap->lock, flags);
3763 chunk_kb = bitmap->chunksize >> 10;
3764 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3765 "%lu%s chunk",
3766 bitmap->pages - bitmap->missing_pages,
3767 bitmap->pages,
3768 (bitmap->pages - bitmap->missing_pages)
3769 << (PAGE_SHIFT - 10),
3770 chunk_kb ? chunk_kb : bitmap->chunksize,
3771 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07003772 if (bitmap->file) {
3773 seq_printf(seq, ", file: ");
3774 seq_path(seq, bitmap->file->f_vfsmnt,
3775 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07003776 }
NeilBrown78d742d2005-06-21 17:17:15 -07003777
NeilBrown32a76272005-06-21 17:17:14 -07003778 seq_printf(seq, "\n");
3779 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 }
3781
3782 seq_printf(seq, "\n");
3783 }
3784 mddev_unlock(mddev);
3785
3786 return 0;
3787}
3788
3789static struct seq_operations md_seq_ops = {
3790 .start = md_seq_start,
3791 .next = md_seq_next,
3792 .stop = md_seq_stop,
3793 .show = md_seq_show,
3794};
3795
3796static int md_seq_open(struct inode *inode, struct file *file)
3797{
3798 int error;
3799
3800 error = seq_open(file, &md_seq_ops);
3801 return error;
3802}
3803
3804static struct file_operations md_seq_fops = {
3805 .open = md_seq_open,
3806 .read = seq_read,
3807 .llseek = seq_lseek,
3808 .release = seq_release,
3809};
3810
3811int register_md_personality(int pnum, mdk_personality_t *p)
3812{
3813 if (pnum >= MAX_PERSONALITY) {
3814 printk(KERN_ERR
3815 "md: tried to install personality %s as nr %d, but max is %lu\n",
3816 p->name, pnum, MAX_PERSONALITY-1);
3817 return -EINVAL;
3818 }
3819
3820 spin_lock(&pers_lock);
3821 if (pers[pnum]) {
3822 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 return -EBUSY;
3824 }
3825
3826 pers[pnum] = p;
3827 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3828 spin_unlock(&pers_lock);
3829 return 0;
3830}
3831
3832int unregister_md_personality(int pnum)
3833{
NeilBrowna757e642005-04-16 15:26:42 -07003834 if (pnum >= MAX_PERSONALITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
3837 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3838 spin_lock(&pers_lock);
3839 pers[pnum] = NULL;
3840 spin_unlock(&pers_lock);
3841 return 0;
3842}
3843
3844static int is_mddev_idle(mddev_t *mddev)
3845{
3846 mdk_rdev_t * rdev;
3847 struct list_head *tmp;
3848 int idle;
3849 unsigned long curr_events;
3850
3851 idle = 1;
3852 ITERATE_RDEV(mddev,rdev,tmp) {
3853 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01003854 curr_events = disk_stat_read(disk, sectors[0]) +
3855 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08003857 /* The difference between curr_events and last_events
3858 * will be affected by any new non-sync IO (making
3859 * curr_events bigger) and any difference in the amount of
3860 * in-flight syncio (making current_events bigger or smaller)
3861 * The amount in-flight is currently limited to
3862 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
3863 * which is at most 4096 sectors.
3864 * These numbers are fairly fragile and should be made
3865 * more robust, probably by enforcing the
3866 * 'window size' that md_do_sync sort-of uses.
3867 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 * Note: the following is an unsigned comparison.
3869 */
NeilBrownc0e48522005-11-18 01:11:01 -08003870 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 rdev->last_events = curr_events;
3872 idle = 0;
3873 }
3874 }
3875 return idle;
3876}
3877
3878void md_done_sync(mddev_t *mddev, int blocks, int ok)
3879{
3880 /* another "blocks" (512byte) blocks have been synced */
3881 atomic_sub(blocks, &mddev->recovery_active);
3882 wake_up(&mddev->recovery_wait);
3883 if (!ok) {
3884 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3885 md_wakeup_thread(mddev->thread);
3886 // stop recovery, signal do_sync ....
3887 }
3888}
3889
3890
NeilBrown06d91a52005-06-21 17:17:12 -07003891/* md_write_start(mddev, bi)
3892 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07003893 * in superblock) before writing, schedule a superblock update
3894 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07003895 */
NeilBrown3d310eb2005-06-21 17:17:26 -07003896void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897{
NeilBrown06d91a52005-06-21 17:17:12 -07003898 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07003899 return;
NeilBrown06d91a52005-06-21 17:17:12 -07003900
NeilBrownf91de922005-11-08 21:39:36 -08003901 BUG_ON(mddev->ro == 1);
3902 if (mddev->ro == 2) {
3903 /* need to switch to read/write */
3904 mddev->ro = 0;
3905 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3906 md_wakeup_thread(mddev->thread);
3907 }
NeilBrown06d91a52005-06-21 17:17:12 -07003908 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07003909 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08003910 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07003911 if (mddev->in_sync) {
3912 mddev->in_sync = 0;
3913 mddev->sb_dirty = 1;
3914 md_wakeup_thread(mddev->thread);
3915 }
NeilBrowna9701a32005-11-08 21:39:34 -08003916 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07003917 }
NeilBrown3d310eb2005-06-21 17:17:26 -07003918 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919}
3920
3921void md_write_end(mddev_t *mddev)
3922{
3923 if (atomic_dec_and_test(&mddev->writes_pending)) {
3924 if (mddev->safemode == 2)
3925 md_wakeup_thread(mddev->thread);
3926 else
3927 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3928 }
3929}
3930
Adrian Bunk75c96f82005-05-05 16:16:09 -07003931static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
3933#define SYNC_MARKS 10
3934#define SYNC_MARK_STEP (3*HZ)
3935static void md_do_sync(mddev_t *mddev)
3936{
3937 mddev_t *mddev2;
3938 unsigned int currspeed = 0,
3939 window;
NeilBrown57afd892005-06-21 17:17:13 -07003940 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 unsigned long mark[SYNC_MARKS];
3942 sector_t mark_cnt[SYNC_MARKS];
3943 int last_mark,m;
3944 struct list_head *tmp;
3945 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07003946 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
3948 /* just incase thread restarts... */
3949 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3950 return;
3951
3952 /* we overload curr_resync somewhat here.
3953 * 0 == not engaged in resync at all
3954 * 2 == checking that there is no conflict with another sync
3955 * 1 == like 2, but have yielded to allow conflicting resync to
3956 * commense
3957 * other == active in resync - this many blocks
3958 *
3959 * Before starting a resync we must have set curr_resync to
3960 * 2, and then checked that every "conflicting" array has curr_resync
3961 * less than ours. When we find one that is the same or higher
3962 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3963 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3964 * This will mean we have to start checking from the beginning again.
3965 *
3966 */
3967
3968 do {
3969 mddev->curr_resync = 2;
3970
3971 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08003972 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07003973 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 goto skip;
3975 }
3976 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 if (mddev2 == mddev)
3978 continue;
3979 if (mddev2->curr_resync &&
3980 match_mddev_units(mddev,mddev2)) {
3981 DEFINE_WAIT(wq);
3982 if (mddev < mddev2 && mddev->curr_resync == 2) {
3983 /* arbitrarily yield */
3984 mddev->curr_resync = 1;
3985 wake_up(&resync_wait);
3986 }
3987 if (mddev > mddev2 && mddev->curr_resync == 1)
3988 /* no need to wait here, we can wait the next
3989 * time 'round when curr_resync == 2
3990 */
3991 continue;
NeilBrown787453c2005-11-08 21:39:43 -08003992 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
3993 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07003994 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 printk(KERN_INFO "md: delaying resync of %s"
3996 " until %s has finished resync (they"
3997 " share one or more physical units)\n",
3998 mdname(mddev), mdname(mddev2));
3999 mddev_put(mddev2);
4000 schedule();
4001 finish_wait(&resync_wait, &wq);
4002 goto try_again;
4003 }
4004 finish_wait(&resync_wait, &wq);
4005 }
4006 }
4007 } while (mddev->curr_resync < 2);
4008
NeilBrown9d888832005-11-08 21:39:26 -08004009 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07004011 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 */
4013 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08004014 mddev->resync_mismatches = 0;
4015 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 /* recovery follows the physical size of devices */
4017 max_sectors = mddev->size << 1;
4018
4019 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4020 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
4021 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
Adrian Bunk338cec32005-09-10 00:26:54 -07004022 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 "(but not more than %d KB/sec) for reconstruction.\n",
4024 sysctl_speed_limit_max);
4025
4026 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07004027 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08004028 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4029 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 j = mddev->recovery_cp;
4031 else
4032 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07004033 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 for (m = 0; m < SYNC_MARKS; m++) {
4035 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004036 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 }
4038 last_mark = 0;
4039 mddev->resync_mark = mark[last_mark];
4040 mddev->resync_mark_cnt = mark_cnt[last_mark];
4041
4042 /*
4043 * Tune reconstruction:
4044 */
4045 window = 32*(PAGE_SIZE/512);
4046 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4047 window/2,(unsigned long long) max_sectors/2);
4048
4049 atomic_set(&mddev->recovery_active, 0);
4050 init_waitqueue_head(&mddev->recovery_wait);
4051 last_check = 0;
4052
4053 if (j>2) {
4054 printk(KERN_INFO
4055 "md: resuming recovery of %s from checkpoint.\n",
4056 mdname(mddev));
4057 mddev->curr_resync = j;
4058 }
4059
4060 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07004061 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
NeilBrown57afd892005-06-21 17:17:13 -07004063 skipped = 0;
4064 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4065 currspeed < sysctl_speed_limit_min);
4066 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4068 goto out;
4069 }
NeilBrown57afd892005-06-21 17:17:13 -07004070
4071 if (!skipped) { /* actual IO requested */
4072 io_sectors += sectors;
4073 atomic_add(sectors, &mddev->recovery_active);
4074 }
4075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 j += sectors;
4077 if (j>1) mddev->curr_resync = j;
4078
NeilBrown57afd892005-06-21 17:17:13 -07004079
4080 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 continue;
4082
NeilBrown57afd892005-06-21 17:17:13 -07004083 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
4085 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4086 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4087 break;
4088
4089 repeat:
4090 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4091 /* step marks */
4092 int next = (last_mark+1) % SYNC_MARKS;
4093
4094 mddev->resync_mark = mark[next];
4095 mddev->resync_mark_cnt = mark_cnt[next];
4096 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004097 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 last_mark = next;
4099 }
4100
4101
NeilBrown787453c2005-11-08 21:39:43 -08004102 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 /*
4104 * got a signal, exit.
4105 */
4106 printk(KERN_INFO
4107 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4109 goto out;
4110 }
4111
4112 /*
4113 * this loop exits only if either when we are slower than
4114 * the 'hard' speed limit, or the system was IO-idle for
4115 * a jiffy.
4116 * the system might be non-idle CPU-wise, but we only care
4117 * about not overloading the IO subsystem. (things like an
4118 * e2fsck being done on the RAID array should execute fast)
4119 */
4120 mddev->queue->unplug_fn(mddev->queue);
4121 cond_resched();
4122
NeilBrown57afd892005-06-21 17:17:13 -07004123 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4124 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
4126 if (currspeed > sysctl_speed_limit_min) {
4127 if ((currspeed > sysctl_speed_limit_max) ||
4128 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08004129 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 goto repeat;
4131 }
4132 }
4133 }
4134 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4135 /*
4136 * this also signals 'finished resyncing' to md_stop
4137 */
4138 out:
4139 mddev->queue->unplug_fn(mddev->queue);
4140
4141 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4142
4143 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004144 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
4146 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4147 mddev->curr_resync > 2 &&
4148 mddev->curr_resync >= mddev->recovery_cp) {
4149 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4150 printk(KERN_INFO
4151 "md: checkpointing recovery of %s.\n",
4152 mdname(mddev));
4153 mddev->recovery_cp = mddev->curr_resync;
4154 } else
4155 mddev->recovery_cp = MaxSector;
4156 }
4157
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 skip:
4159 mddev->curr_resync = 0;
4160 wake_up(&resync_wait);
4161 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4162 md_wakeup_thread(mddev->thread);
4163}
4164
4165
4166/*
4167 * This routine is regularly called by all per-raid-array threads to
4168 * deal with generic issues like resync and super-block update.
4169 * Raid personalities that don't have a thread (linear/raid0) do not
4170 * need this as they never do any recovery or update the superblock.
4171 *
4172 * It does not do any resync itself, but rather "forks" off other threads
4173 * to do that as needed.
4174 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4175 * "->recovery" and create a thread at ->sync_thread.
4176 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4177 * and wakeups up this thread which will reap the thread and finish up.
4178 * This thread also removes any faulty devices (with nr_pending == 0).
4179 *
4180 * The overall approach is:
4181 * 1/ if the superblock needs updating, update it.
4182 * 2/ If a recovery thread is running, don't do anything else.
4183 * 3/ If recovery has finished, clean up, possibly marking spares active.
4184 * 4/ If there are any faulty devices, remove them.
4185 * 5/ If array is degraded, try to add spares devices
4186 * 6/ If array has spares or is not in-sync, start a resync thread.
4187 */
4188void md_check_recovery(mddev_t *mddev)
4189{
4190 mdk_rdev_t *rdev;
4191 struct list_head *rtmp;
4192
4193
NeilBrown5f404022005-06-21 17:17:16 -07004194 if (mddev->bitmap)
4195 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
4197 if (mddev->ro)
4198 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004199
4200 if (signal_pending(current)) {
4201 if (mddev->pers->sync_request) {
4202 printk(KERN_INFO "md: %s in immediate safe mode\n",
4203 mdname(mddev));
4204 mddev->safemode = 2;
4205 }
4206 flush_signals(current);
4207 }
4208
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 if ( ! (
4210 mddev->sb_dirty ||
4211 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004212 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4213 (mddev->safemode == 1) ||
4214 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4215 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 ))
4217 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 if (mddev_trylock(mddev)==0) {
4220 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004221
NeilBrowna9701a32005-11-08 21:39:34 -08004222 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004223 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4224 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4225 mddev->in_sync = 1;
4226 mddev->sb_dirty = 1;
4227 }
4228 if (mddev->safemode == 1)
4229 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08004230 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 if (mddev->sb_dirty)
4233 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004234
NeilBrown06d91a52005-06-21 17:17:12 -07004235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4237 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4238 /* resync/recovery still happening */
4239 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4240 goto unlock;
4241 }
4242 if (mddev->sync_thread) {
4243 /* resync has finished, collect result */
4244 md_unregister_thread(mddev->sync_thread);
4245 mddev->sync_thread = NULL;
4246 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4247 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4248 /* success...*/
4249 /* activate any spares */
4250 mddev->pers->spare_active(mddev);
4251 }
4252 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004253
4254 /* if array is no-longer degraded, then any saved_raid_disk
4255 * information must be scrapped
4256 */
4257 if (!mddev->degraded)
4258 ITERATE_RDEV(mddev,rdev,rtmp)
4259 rdev->saved_raid_disk = -1;
4260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 mddev->recovery = 0;
4262 /* flag recovery needed just to double check */
4263 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4264 goto unlock;
4265 }
NeilBrown24dd4692005-11-08 21:39:26 -08004266 /* Clear some bits that don't mean anything, but
4267 * might be left set
4268 */
4269 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4270 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4271 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4272 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
4274 /* no recovery is running.
4275 * remove any failed drives, then
4276 * add spares if possible.
4277 * Spare are also removed and re-added, to allow
4278 * the personality to fail the re-add.
4279 */
4280 ITERATE_RDEV(mddev,rdev,rtmp)
4281 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004282 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004284 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4285 char nm[20];
4286 sprintf(nm,"rd%d", rdev->raid_disk);
4287 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 }
4291
4292 if (mddev->degraded) {
4293 ITERATE_RDEV(mddev,rdev,rtmp)
4294 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08004295 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004296 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4297 char nm[20];
4298 sprintf(nm, "rd%d", rdev->raid_disk);
4299 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 spares++;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004301 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 break;
4303 }
4304 }
4305
NeilBrown24dd4692005-11-08 21:39:26 -08004306 if (spares) {
4307 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4308 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4309 } else if (mddev->recovery_cp < MaxSector) {
4310 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4311 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4312 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08004314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 if (mddev->pers->sync_request) {
4316 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07004317 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4318 /* We are adding a device or devices to an array
4319 * which has the bitmap stored on all devices.
4320 * So make sure all bitmap pages get written
4321 */
4322 bitmap_write_all(mddev->bitmap);
4323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 mddev->sync_thread = md_register_thread(md_do_sync,
4325 mddev,
4326 "%s_resync");
4327 if (!mddev->sync_thread) {
4328 printk(KERN_ERR "%s: could not start resync"
4329 " thread...\n",
4330 mdname(mddev));
4331 /* leave the spares where they are, it shouldn't hurt */
4332 mddev->recovery = 0;
4333 } else {
4334 md_wakeup_thread(mddev->sync_thread);
4335 }
4336 }
4337 unlock:
4338 mddev_unlock(mddev);
4339 }
4340}
4341
Adrian Bunk75c96f82005-05-05 16:16:09 -07004342static int md_notify_reboot(struct notifier_block *this,
4343 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344{
4345 struct list_head *tmp;
4346 mddev_t *mddev;
4347
4348 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4349
4350 printk(KERN_INFO "md: stopping all md devices.\n");
4351
4352 ITERATE_MDDEV(mddev,tmp)
4353 if (mddev_trylock(mddev)==0)
4354 do_md_stop (mddev, 1);
4355 /*
4356 * certain more exotic SCSI devices are known to be
4357 * volatile wrt too early system reboots. While the
4358 * right place to handle this issue is the given
4359 * driver, we do want to have a safe RAID driver ...
4360 */
4361 mdelay(1000*1);
4362 }
4363 return NOTIFY_DONE;
4364}
4365
Adrian Bunk75c96f82005-05-05 16:16:09 -07004366static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 .notifier_call = md_notify_reboot,
4368 .next = NULL,
4369 .priority = INT_MAX, /* before any real devices */
4370};
4371
4372static void md_geninit(void)
4373{
4374 struct proc_dir_entry *p;
4375
4376 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4377
4378 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4379 if (p)
4380 p->proc_fops = &md_seq_fops;
4381}
4382
Adrian Bunk75c96f82005-05-05 16:16:09 -07004383static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384{
4385 int minor;
4386
4387 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4388 " MD_SB_DISKS=%d\n",
4389 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4390 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08004391 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07004392 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
4394 if (register_blkdev(MAJOR_NR, "md"))
4395 return -1;
4396 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4397 unregister_blkdev(MAJOR_NR, "md");
4398 return -1;
4399 }
4400 devfs_mk_dir("md");
4401 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4402 md_probe, NULL, NULL);
4403 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4404 md_probe, NULL, NULL);
4405
4406 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4407 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4408 S_IFBLK|S_IRUSR|S_IWUSR,
4409 "md/%d", minor);
4410
4411 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4412 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4413 S_IFBLK|S_IRUSR|S_IWUSR,
4414 "md/mdp%d", minor);
4415
4416
4417 register_reboot_notifier(&md_notifier);
4418 raid_table_header = register_sysctl_table(raid_root_table, 1);
4419
4420 md_geninit();
4421 return (0);
4422}
4423
4424
4425#ifndef MODULE
4426
4427/*
4428 * Searches all registered partitions for autorun RAID arrays
4429 * at boot time.
4430 */
4431static dev_t detected_devices[128];
4432static int dev_cnt;
4433
4434void md_autodetect_dev(dev_t dev)
4435{
4436 if (dev_cnt >= 0 && dev_cnt < 127)
4437 detected_devices[dev_cnt++] = dev;
4438}
4439
4440
4441static void autostart_arrays(int part)
4442{
4443 mdk_rdev_t *rdev;
4444 int i;
4445
4446 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4447
4448 for (i = 0; i < dev_cnt; i++) {
4449 dev_t dev = detected_devices[i];
4450
4451 rdev = md_import_device(dev,0, 0);
4452 if (IS_ERR(rdev))
4453 continue;
4454
NeilBrownb2d444d2005-11-08 21:39:31 -08004455 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 MD_BUG();
4457 continue;
4458 }
4459 list_add(&rdev->same_set, &pending_raid_disks);
4460 }
4461 dev_cnt = 0;
4462
4463 autorun_devices(part);
4464}
4465
4466#endif
4467
4468static __exit void md_exit(void)
4469{
4470 mddev_t *mddev;
4471 struct list_head *tmp;
4472 int i;
4473 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4474 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4475 for (i=0; i < MAX_MD_DEVS; i++)
4476 devfs_remove("md/%d", i);
4477 for (i=0; i < MAX_MD_DEVS; i++)
4478 devfs_remove("md/d%d", i);
4479
4480 devfs_remove("md");
4481
4482 unregister_blkdev(MAJOR_NR,"md");
4483 unregister_blkdev(mdp_major, "mdp");
4484 unregister_reboot_notifier(&md_notifier);
4485 unregister_sysctl_table(raid_table_header);
4486 remove_proc_entry("mdstat", NULL);
4487 ITERATE_MDDEV(mddev,tmp) {
4488 struct gendisk *disk = mddev->gendisk;
4489 if (!disk)
4490 continue;
4491 export_array(mddev);
4492 del_gendisk(disk);
4493 put_disk(disk);
4494 mddev->gendisk = NULL;
4495 mddev_put(mddev);
4496 }
4497}
4498
4499module_init(md_init)
4500module_exit(md_exit)
4501
NeilBrownf91de922005-11-08 21:39:36 -08004502static int get_ro(char *buffer, struct kernel_param *kp)
4503{
4504 return sprintf(buffer, "%d", start_readonly);
4505}
4506static int set_ro(const char *val, struct kernel_param *kp)
4507{
4508 char *e;
4509 int num = simple_strtoul(val, &e, 10);
4510 if (*val && (*e == '\0' || *e == '\n')) {
4511 start_readonly = num;
4512 return 0;;
4513 }
4514 return -EINVAL;
4515}
4516
4517module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004518module_param(start_dirty_degraded, int, 0644);
4519
NeilBrownf91de922005-11-08 21:39:36 -08004520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521EXPORT_SYMBOL(register_md_personality);
4522EXPORT_SYMBOL(unregister_md_personality);
4523EXPORT_SYMBOL(md_error);
4524EXPORT_SYMBOL(md_done_sync);
4525EXPORT_SYMBOL(md_write_start);
4526EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527EXPORT_SYMBOL(md_register_thread);
4528EXPORT_SYMBOL(md_unregister_thread);
4529EXPORT_SYMBOL(md_wakeup_thread);
4530EXPORT_SYMBOL(md_print_devices);
4531EXPORT_SYMBOL(md_check_recovery);
4532MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07004533MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07004534MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);