blob: 1364a1c97e6f5f64dc6cb0be0686d343e97334af [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}
NeilBrownddaf22a2006-01-06 00:20:19 -0800464EXPORT_SYMBOL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
NeilBrown0002b272005-09-09 16:23:53 -0700466static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 char b[BDEVNAME_SIZE];
469 if (!rdev->sb_page) {
470 MD_BUG();
471 return -EINVAL;
472 }
473 if (rdev->sb_loaded)
474 return 0;
475
476
NeilBrown0002b272005-09-09 16:23:53 -0700477 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto fail;
479 rdev->sb_loaded = 1;
480 return 0;
481
482fail:
483 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
484 bdevname(rdev->bdev,b));
485 return -EINVAL;
486}
487
488static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
489{
490 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
491 (sb1->set_uuid1 == sb2->set_uuid1) &&
492 (sb1->set_uuid2 == sb2->set_uuid2) &&
493 (sb1->set_uuid3 == sb2->set_uuid3))
494
495 return 1;
496
497 return 0;
498}
499
500
501static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
502{
503 int ret;
504 mdp_super_t *tmp1, *tmp2;
505
506 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
507 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
508
509 if (!tmp1 || !tmp2) {
510 ret = 0;
511 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
512 goto abort;
513 }
514
515 *tmp1 = *sb1;
516 *tmp2 = *sb2;
517
518 /*
519 * nr_disks is not constant
520 */
521 tmp1->nr_disks = 0;
522 tmp2->nr_disks = 0;
523
524 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
525 ret = 0;
526 else
527 ret = 1;
528
529abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700530 kfree(tmp1);
531 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return ret;
533}
534
535static unsigned int calc_sb_csum(mdp_super_t * sb)
536{
537 unsigned int disk_csum, csum;
538
539 disk_csum = sb->sb_csum;
540 sb->sb_csum = 0;
541 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
542 sb->sb_csum = disk_csum;
543 return csum;
544}
545
546
547/*
548 * Handle superblock details.
549 * We want to be able to handle multiple superblock formats
550 * so we have a common interface to them all, and an array of
551 * different handlers.
552 * We rely on user-space to write the initial superblock, and support
553 * reading and updating of superblocks.
554 * Interface methods are:
555 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
556 * loads and validates a superblock on dev.
557 * if refdev != NULL, compare superblocks on both devices
558 * Return:
559 * 0 - dev has a superblock that is compatible with refdev
560 * 1 - dev has a superblock that is compatible and newer than refdev
561 * so dev should be used as the refdev in future
562 * -EINVAL superblock incompatible or invalid
563 * -othererror e.g. -EIO
564 *
565 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
566 * Verify that dev is acceptable into mddev.
567 * The first time, mddev->raid_disks will be 0, and data from
568 * dev should be merged in. Subsequent calls check that dev
569 * is new enough. Return 0 or -EINVAL
570 *
571 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
572 * Update the superblock for rdev with data in mddev
573 * This does not write to disc.
574 *
575 */
576
577struct super_type {
578 char *name;
579 struct module *owner;
580 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
581 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
582 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
583};
584
585/*
586 * load_super for 0.90.0
587 */
588static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
589{
590 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
591 mdp_super_t *sb;
592 int ret;
593 sector_t sb_offset;
594
595 /*
596 * Calculate the position of the superblock,
597 * it's at the end of the disk.
598 *
599 * It also happens to be a multiple of 4Kb.
600 */
601 sb_offset = calc_dev_sboffset(rdev->bdev);
602 rdev->sb_offset = sb_offset;
603
NeilBrown0002b272005-09-09 16:23:53 -0700604 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (ret) return ret;
606
607 ret = -EINVAL;
608
609 bdevname(rdev->bdev, b);
610 sb = (mdp_super_t*)page_address(rdev->sb_page);
611
612 if (sb->md_magic != MD_SB_MAGIC) {
613 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
614 b);
615 goto abort;
616 }
617
618 if (sb->major_version != 0 ||
619 sb->minor_version != 90) {
620 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
621 sb->major_version, sb->minor_version,
622 b);
623 goto abort;
624 }
625
626 if (sb->raid_disks <= 0)
627 goto abort;
628
629 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
630 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
631 b);
632 goto abort;
633 }
634
635 rdev->preferred_minor = sb->md_minor;
636 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700637 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (sb->level == LEVEL_MULTIPATH)
640 rdev->desc_nr = -1;
641 else
642 rdev->desc_nr = sb->this_disk.number;
643
644 if (refdev == 0)
645 ret = 1;
646 else {
647 __u64 ev1, ev2;
648 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
649 if (!uuid_equal(refsb, sb)) {
650 printk(KERN_WARNING "md: %s has different UUID to %s\n",
651 b, bdevname(refdev->bdev,b2));
652 goto abort;
653 }
654 if (!sb_equal(refsb, sb)) {
655 printk(KERN_WARNING "md: %s has same UUID"
656 " but different superblock to %s\n",
657 b, bdevname(refdev->bdev, b2));
658 goto abort;
659 }
660 ev1 = md_event(sb);
661 ev2 = md_event(refsb);
662 if (ev1 > ev2)
663 ret = 1;
664 else
665 ret = 0;
666 }
667 rdev->size = calc_dev_size(rdev, sb->chunk_size);
668
669 abort:
670 return ret;
671}
672
673/*
674 * validate_super for 0.90.0
675 */
676static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
677{
678 mdp_disk_t *desc;
679 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
680
NeilBrown41158c72005-06-21 17:17:25 -0700681 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800682 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (mddev->raid_disks == 0) {
684 mddev->major_version = 0;
685 mddev->minor_version = sb->minor_version;
686 mddev->patch_version = sb->patch_version;
687 mddev->persistent = ! sb->not_persistent;
688 mddev->chunk_size = sb->chunk_size;
689 mddev->ctime = sb->ctime;
690 mddev->utime = sb->utime;
691 mddev->level = sb->level;
692 mddev->layout = sb->layout;
693 mddev->raid_disks = sb->raid_disks;
694 mddev->size = sb->size;
695 mddev->events = md_event(sb);
NeilBrown92232142005-08-18 11:24:16 -0700696 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700697 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (sb->state & (1<<MD_SB_CLEAN))
700 mddev->recovery_cp = MaxSector;
701 else {
702 if (sb->events_hi == sb->cp_events_hi &&
703 sb->events_lo == sb->cp_events_lo) {
704 mddev->recovery_cp = sb->recovery_cp;
705 } else
706 mddev->recovery_cp = 0;
707 }
708
709 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
710 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
711 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
712 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
713
714 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700715
716 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
717 mddev->bitmap_file == NULL) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800718 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
719 && mddev->level != 10) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700720 /* FIXME use a better test */
NeilBrown6cce3b22006-01-06 00:20:16 -0800721 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -0700722 return -EINVAL;
723 }
NeilBrown36fa3062005-09-09 16:23:45 -0700724 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700725 }
726
NeilBrown41158c72005-06-21 17:17:25 -0700727 } else if (mddev->pers == NULL) {
728 /* Insist on good event counter while assembling */
729 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ++ev1;
731 if (ev1 < mddev->events)
732 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700733 } else if (mddev->bitmap) {
734 /* if adding to array with a bitmap, then we can accept an
735 * older device ... but not too old.
736 */
737 __u64 ev1 = md_event(sb);
738 if (ev1 < mddev->bitmap->events_cleared)
739 return 0;
740 } else /* just a hot-add of a new device, leave raid_disk at -1 */
741 return 0;
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 desc = sb->disks + rdev->desc_nr;
745
746 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800747 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 else if (desc->state & (1<<MD_DISK_SYNC) &&
749 desc->raid_disk < mddev->raid_disks) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800750 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 rdev->raid_disk = desc->raid_disk;
752 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700753 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
754 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700755 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800756 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 0;
758}
759
760/*
761 * sync_super for 0.90.0
762 */
763static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
764{
765 mdp_super_t *sb;
766 struct list_head *tmp;
767 mdk_rdev_t *rdev2;
768 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /* make rdev->sb match mddev data..
772 *
773 * 1/ zero out disks
774 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
775 * 3/ any empty disks < next_spare become removed
776 *
777 * disks[0] gets initialised to REMOVED because
778 * we cannot be sure from other fields if it has
779 * been initialised or not.
780 */
781 int i;
782 int active=0, working=0,failed=0,spare=0,nr_disks=0;
783
NeilBrown61181562005-09-09 16:24:02 -0700784 rdev->sb_size = MD_SB_BYTES;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 sb = (mdp_super_t*)page_address(rdev->sb_page);
787
788 memset(sb, 0, sizeof(*sb));
789
790 sb->md_magic = MD_SB_MAGIC;
791 sb->major_version = mddev->major_version;
792 sb->minor_version = mddev->minor_version;
793 sb->patch_version = mddev->patch_version;
794 sb->gvalid_words = 0; /* ignored */
795 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
796 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
797 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
798 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
799
800 sb->ctime = mddev->ctime;
801 sb->level = mddev->level;
802 sb->size = mddev->size;
803 sb->raid_disks = mddev->raid_disks;
804 sb->md_minor = mddev->md_minor;
805 sb->not_persistent = !mddev->persistent;
806 sb->utime = mddev->utime;
807 sb->state = 0;
808 sb->events_hi = (mddev->events>>32);
809 sb->events_lo = (u32)mddev->events;
810
811 if (mddev->in_sync)
812 {
813 sb->recovery_cp = mddev->recovery_cp;
814 sb->cp_events_hi = (mddev->events>>32);
815 sb->cp_events_lo = (u32)mddev->events;
816 if (mddev->recovery_cp == MaxSector)
817 sb->state = (1<< MD_SB_CLEAN);
818 } else
819 sb->recovery_cp = 0;
820
821 sb->layout = mddev->layout;
822 sb->chunk_size = mddev->chunk_size;
823
NeilBrowna654b9d82005-06-21 17:17:27 -0700824 if (mddev->bitmap && mddev->bitmap_file == NULL)
825 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 sb->disks[0].state = (1<<MD_DISK_REMOVED);
828 ITERATE_RDEV(mddev,rdev2,tmp) {
829 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800830 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800831 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
832 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800833 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800835 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800836 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 d = &sb->disks[rdev2->desc_nr];
838 nr_disks++;
839 d->number = rdev2->desc_nr;
840 d->major = MAJOR(rdev2->bdev->bd_dev);
841 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800842 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
843 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 d->raid_disk = rdev2->raid_disk;
845 else
846 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrownb2d444d2005-11-08 21:39:31 -0800847 if (test_bit(Faulty, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 d->state = (1<<MD_DISK_FAULTY);
849 failed++;
NeilBrownb2d444d2005-11-08 21:39:31 -0800850 } else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 d->state = (1<<MD_DISK_ACTIVE);
852 d->state |= (1<<MD_DISK_SYNC);
853 active++;
854 working++;
855 } else {
856 d->state = 0;
857 spare++;
858 working++;
859 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700860 if (test_bit(WriteMostly, &rdev2->flags))
861 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* now set the "removed" and "faulty" bits on any missing devices */
864 for (i=0 ; i < mddev->raid_disks ; i++) {
865 mdp_disk_t *d = &sb->disks[i];
866 if (d->state == 0 && d->number == 0) {
867 d->number = i;
868 d->raid_disk = i;
869 d->state = (1<<MD_DISK_REMOVED);
870 d->state |= (1<<MD_DISK_FAULTY);
871 failed++;
872 }
873 }
874 sb->nr_disks = nr_disks;
875 sb->active_disks = active;
876 sb->working_disks = working;
877 sb->failed_disks = failed;
878 sb->spare_disks = spare;
879
880 sb->this_disk = sb->disks[rdev->desc_nr];
881 sb->sb_csum = calc_sb_csum(sb);
882}
883
884/*
885 * version 1 superblock
886 */
887
888static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
889{
890 unsigned int disk_csum, csum;
891 unsigned long long newcsum;
892 int size = 256 + le32_to_cpu(sb->max_dev)*2;
893 unsigned int *isuper = (unsigned int*)sb;
894 int i;
895
896 disk_csum = sb->sb_csum;
897 sb->sb_csum = 0;
898 newcsum = 0;
899 for (i=0; size>=4; size -= 4 )
900 newcsum += le32_to_cpu(*isuper++);
901
902 if (size == 2)
903 newcsum += le16_to_cpu(*(unsigned short*) isuper);
904
905 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
906 sb->sb_csum = disk_csum;
907 return cpu_to_le32(csum);
908}
909
910static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
911{
912 struct mdp_superblock_1 *sb;
913 int ret;
914 sector_t sb_offset;
915 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -0700916 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /*
919 * Calculate the position of the superblock.
920 * It is always aligned to a 4K boundary and
921 * depeding on minor_version, it can be:
922 * 0: At least 8K, but less than 12K, from end of device
923 * 1: At start of device
924 * 2: 4K from start of device.
925 */
926 switch(minor_version) {
927 case 0:
928 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
929 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -0700930 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* convert from sectors to K */
932 sb_offset /= 2;
933 break;
934 case 1:
935 sb_offset = 0;
936 break;
937 case 2:
938 sb_offset = 4;
939 break;
940 default:
941 return -EINVAL;
942 }
943 rdev->sb_offset = sb_offset;
944
NeilBrown0002b272005-09-09 16:23:53 -0700945 /* superblock is rarely larger than 1K, but it can be larger,
946 * and it is safe to read 4k, so we do that
947 */
948 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (ret) return ret;
950
951
952 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
953
954 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
955 sb->major_version != cpu_to_le32(1) ||
956 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
957 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -0700958 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return -EINVAL;
960
961 if (calc_sb_1_csum(sb) != sb->sb_csum) {
962 printk("md: invalid superblock checksum on %s\n",
963 bdevname(rdev->bdev,b));
964 return -EINVAL;
965 }
966 if (le64_to_cpu(sb->data_size) < 10) {
967 printk("md: data_size too small on %s\n",
968 bdevname(rdev->bdev,b));
969 return -EINVAL;
970 }
971 rdev->preferred_minor = 0xffff;
972 rdev->data_offset = le64_to_cpu(sb->data_offset);
973
NeilBrown0002b272005-09-09 16:23:53 -0700974 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -0700975 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -0700976 if (rdev->sb_size & bmask)
977 rdev-> sb_size = (rdev->sb_size | bmask)+1;
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (refdev == 0)
980 return 1;
981 else {
982 __u64 ev1, ev2;
983 struct mdp_superblock_1 *refsb =
984 (struct mdp_superblock_1*)page_address(refdev->sb_page);
985
986 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
987 sb->level != refsb->level ||
988 sb->layout != refsb->layout ||
989 sb->chunksize != refsb->chunksize) {
990 printk(KERN_WARNING "md: %s has strangely different"
991 " superblock to %s\n",
992 bdevname(rdev->bdev,b),
993 bdevname(refdev->bdev,b2));
994 return -EINVAL;
995 }
996 ev1 = le64_to_cpu(sb->events);
997 ev2 = le64_to_cpu(refsb->events);
998
999 if (ev1 > ev2)
1000 return 1;
1001 }
1002 if (minor_version)
1003 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1004 else
1005 rdev->size = rdev->sb_offset;
1006 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1007 return -EINVAL;
1008 rdev->size = le64_to_cpu(sb->data_size)/2;
1009 if (le32_to_cpu(sb->chunksize))
1010 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1011 return 0;
1012}
1013
1014static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1015{
1016 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1017
NeilBrown41158c72005-06-21 17:17:25 -07001018 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001019 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (mddev->raid_disks == 0) {
1021 mddev->major_version = 1;
1022 mddev->patch_version = 0;
1023 mddev->persistent = 1;
1024 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1025 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1026 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1027 mddev->level = le32_to_cpu(sb->level);
1028 mddev->layout = le32_to_cpu(sb->layout);
1029 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1030 mddev->size = le64_to_cpu(sb->size)/2;
1031 mddev->events = le64_to_cpu(sb->events);
NeilBrown92232142005-08-18 11:24:16 -07001032 mddev->bitmap_offset = 0;
NeilBrown53e87fb2005-09-09 16:23:58 -07001033 mddev->default_bitmap_offset = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1036 memcpy(mddev->uuid, sb->set_uuid, 16);
1037
1038 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001039
NeilBrown71c08052005-09-09 16:23:51 -07001040 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -07001041 mddev->bitmap_file == NULL ) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001042 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1043 && mddev->level != 10) {
1044 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -07001045 return -EINVAL;
1046 }
1047 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1048 }
NeilBrown41158c72005-06-21 17:17:25 -07001049 } else if (mddev->pers == NULL) {
1050 /* Insist of good event counter while assembling */
1051 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 ++ev1;
1053 if (ev1 < mddev->events)
1054 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001055 } else if (mddev->bitmap) {
1056 /* If adding to array with a bitmap, then we can accept an
1057 * older device, but not too old.
1058 */
1059 __u64 ev1 = le64_to_cpu(sb->events);
1060 if (ev1 < mddev->bitmap->events_cleared)
1061 return 0;
1062 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 if (mddev->level != LEVEL_MULTIPATH) {
1066 int role;
1067 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1068 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1069 switch(role) {
1070 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 break;
1072 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001073 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 break;
1075 default:
NeilBrownb2d444d2005-11-08 21:39:31 -08001076 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 rdev->raid_disk = role;
1078 break;
1079 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001080 if (sb->devflags & WriteMostly1)
1081 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001082 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001083 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return 0;
1086}
1087
1088static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1089{
1090 struct mdp_superblock_1 *sb;
1091 struct list_head *tmp;
1092 mdk_rdev_t *rdev2;
1093 int max_dev, i;
1094 /* make rdev->sb match mddev and rdev data. */
1095
1096 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1097
1098 sb->feature_map = 0;
1099 sb->pad0 = 0;
1100 memset(sb->pad1, 0, sizeof(sb->pad1));
1101 memset(sb->pad2, 0, sizeof(sb->pad2));
1102 memset(sb->pad3, 0, sizeof(sb->pad3));
1103
1104 sb->utime = cpu_to_le64((__u64)mddev->utime);
1105 sb->events = cpu_to_le64(mddev->events);
1106 if (mddev->in_sync)
1107 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1108 else
1109 sb->resync_offset = cpu_to_le64(0);
1110
NeilBrowna654b9d82005-06-21 17:17:27 -07001111 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1112 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001113 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001114 }
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 max_dev = 0;
1117 ITERATE_RDEV(mddev,rdev2,tmp)
1118 if (rdev2->desc_nr+1 > max_dev)
1119 max_dev = rdev2->desc_nr+1;
1120
1121 sb->max_dev = cpu_to_le32(max_dev);
1122 for (i=0; i<max_dev;i++)
1123 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1124
1125 ITERATE_RDEV(mddev,rdev2,tmp) {
1126 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001127 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001129 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1131 else
1132 sb->dev_roles[i] = cpu_to_le16(0xffff);
1133 }
1134
1135 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1136 sb->sb_csum = calc_sb_1_csum(sb);
1137}
1138
1139
Adrian Bunk75c96f82005-05-05 16:16:09 -07001140static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 [0] = {
1142 .name = "0.90.0",
1143 .owner = THIS_MODULE,
1144 .load_super = super_90_load,
1145 .validate_super = super_90_validate,
1146 .sync_super = super_90_sync,
1147 },
1148 [1] = {
1149 .name = "md-1",
1150 .owner = THIS_MODULE,
1151 .load_super = super_1_load,
1152 .validate_super = super_1_validate,
1153 .sync_super = super_1_sync,
1154 },
1155};
1156
1157static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1158{
1159 struct list_head *tmp;
1160 mdk_rdev_t *rdev;
1161
1162 ITERATE_RDEV(mddev,rdev,tmp)
1163 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1164 return rdev;
1165
1166 return NULL;
1167}
1168
1169static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1170{
1171 struct list_head *tmp;
1172 mdk_rdev_t *rdev;
1173
1174 ITERATE_RDEV(mddev1,rdev,tmp)
1175 if (match_dev_unit(mddev2, rdev))
1176 return 1;
1177
1178 return 0;
1179}
1180
1181static LIST_HEAD(pending_raid_disks);
1182
1183static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1184{
1185 mdk_rdev_t *same_pdev;
1186 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001187 struct kobject *ko;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 if (rdev->mddev) {
1190 MD_BUG();
1191 return -EINVAL;
1192 }
1193 same_pdev = match_dev_unit(mddev, rdev);
1194 if (same_pdev)
1195 printk(KERN_WARNING
1196 "%s: WARNING: %s appears to be on the same physical"
1197 " disk as %s. True\n protection against single-disk"
1198 " failure might be compromised.\n",
1199 mdname(mddev), bdevname(rdev->bdev,b),
1200 bdevname(same_pdev->bdev,b2));
1201
1202 /* Verify rdev->desc_nr is unique.
1203 * If it is -1, assign a free number, else
1204 * check number is not in use
1205 */
1206 if (rdev->desc_nr < 0) {
1207 int choice = 0;
1208 if (mddev->pers) choice = mddev->raid_disks;
1209 while (find_rdev_nr(mddev, choice))
1210 choice++;
1211 rdev->desc_nr = choice;
1212 } else {
1213 if (find_rdev_nr(mddev, rdev->desc_nr))
1214 return -EBUSY;
1215 }
NeilBrown19133a42005-11-08 21:39:35 -08001216 bdevname(rdev->bdev,b);
1217 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1218 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 list_add(&rdev->same_set, &mddev->disks);
1221 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001222 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001223
NeilBrown9c791972005-11-08 21:39:28 -08001224 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001225 kobject_add(&rdev->kobj);
1226
NeilBrownf637b9f2005-11-08 21:39:37 -08001227 if (rdev->bdev->bd_part)
1228 ko = &rdev->bdev->bd_part->kobj;
1229 else
1230 ko = &rdev->bdev->bd_disk->kobj;
1231 sysfs_create_link(&rdev->kobj, ko, "block");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return 0;
1233}
1234
1235static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1236{
1237 char b[BDEVNAME_SIZE];
1238 if (!rdev->mddev) {
1239 MD_BUG();
1240 return;
1241 }
1242 list_del_init(&rdev->same_set);
1243 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1244 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001245 sysfs_remove_link(&rdev->kobj, "block");
1246 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
1249/*
1250 * prevent the device from being mounted, repartitioned or
1251 * otherwise reused by a RAID array (or any other kernel
1252 * subsystem), by bd_claiming the device.
1253 */
1254static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1255{
1256 int err = 0;
1257 struct block_device *bdev;
1258 char b[BDEVNAME_SIZE];
1259
1260 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1261 if (IS_ERR(bdev)) {
1262 printk(KERN_ERR "md: could not open %s.\n",
1263 __bdevname(dev, b));
1264 return PTR_ERR(bdev);
1265 }
1266 err = bd_claim(bdev, rdev);
1267 if (err) {
1268 printk(KERN_ERR "md: could not bd_claim %s.\n",
1269 bdevname(bdev, b));
1270 blkdev_put(bdev);
1271 return err;
1272 }
1273 rdev->bdev = bdev;
1274 return err;
1275}
1276
1277static void unlock_rdev(mdk_rdev_t *rdev)
1278{
1279 struct block_device *bdev = rdev->bdev;
1280 rdev->bdev = NULL;
1281 if (!bdev)
1282 MD_BUG();
1283 bd_release(bdev);
1284 blkdev_put(bdev);
1285}
1286
1287void md_autodetect_dev(dev_t dev);
1288
1289static void export_rdev(mdk_rdev_t * rdev)
1290{
1291 char b[BDEVNAME_SIZE];
1292 printk(KERN_INFO "md: export_rdev(%s)\n",
1293 bdevname(rdev->bdev,b));
1294 if (rdev->mddev)
1295 MD_BUG();
1296 free_disk_sb(rdev);
1297 list_del_init(&rdev->same_set);
1298#ifndef MODULE
1299 md_autodetect_dev(rdev->bdev->bd_dev);
1300#endif
1301 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001302 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
1305static void kick_rdev_from_array(mdk_rdev_t * rdev)
1306{
1307 unbind_rdev_from_array(rdev);
1308 export_rdev(rdev);
1309}
1310
1311static void export_array(mddev_t *mddev)
1312{
1313 struct list_head *tmp;
1314 mdk_rdev_t *rdev;
1315
1316 ITERATE_RDEV(mddev,rdev,tmp) {
1317 if (!rdev->mddev) {
1318 MD_BUG();
1319 continue;
1320 }
1321 kick_rdev_from_array(rdev);
1322 }
1323 if (!list_empty(&mddev->disks))
1324 MD_BUG();
1325 mddev->raid_disks = 0;
1326 mddev->major_version = 0;
1327}
1328
1329static void print_desc(mdp_disk_t *desc)
1330{
1331 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1332 desc->major,desc->minor,desc->raid_disk,desc->state);
1333}
1334
1335static void print_sb(mdp_super_t *sb)
1336{
1337 int i;
1338
1339 printk(KERN_INFO
1340 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1341 sb->major_version, sb->minor_version, sb->patch_version,
1342 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1343 sb->ctime);
1344 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1345 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1346 sb->md_minor, sb->layout, sb->chunk_size);
1347 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1348 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1349 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1350 sb->failed_disks, sb->spare_disks,
1351 sb->sb_csum, (unsigned long)sb->events_lo);
1352
1353 printk(KERN_INFO);
1354 for (i = 0; i < MD_SB_DISKS; i++) {
1355 mdp_disk_t *desc;
1356
1357 desc = sb->disks + i;
1358 if (desc->number || desc->major || desc->minor ||
1359 desc->raid_disk || (desc->state && (desc->state != 4))) {
1360 printk(" D %2d: ", i);
1361 print_desc(desc);
1362 }
1363 }
1364 printk(KERN_INFO "md: THIS: ");
1365 print_desc(&sb->this_disk);
1366
1367}
1368
1369static void print_rdev(mdk_rdev_t *rdev)
1370{
1371 char b[BDEVNAME_SIZE];
1372 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1373 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001374 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1375 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (rdev->sb_loaded) {
1377 printk(KERN_INFO "md: rdev superblock:\n");
1378 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1379 } else
1380 printk(KERN_INFO "md: no rdev superblock!\n");
1381}
1382
1383void md_print_devices(void)
1384{
1385 struct list_head *tmp, *tmp2;
1386 mdk_rdev_t *rdev;
1387 mddev_t *mddev;
1388 char b[BDEVNAME_SIZE];
1389
1390 printk("\n");
1391 printk("md: **********************************\n");
1392 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1393 printk("md: **********************************\n");
1394 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
NeilBrown32a76272005-06-21 17:17:14 -07001396 if (mddev->bitmap)
1397 bitmap_print_sb(mddev->bitmap);
1398 else
1399 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 ITERATE_RDEV(mddev,rdev,tmp2)
1401 printk("<%s>", bdevname(rdev->bdev,b));
1402 printk("\n");
1403
1404 ITERATE_RDEV(mddev,rdev,tmp2)
1405 print_rdev(rdev);
1406 }
1407 printk("md: **********************************\n");
1408 printk("\n");
1409}
1410
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412static void sync_sbs(mddev_t * mddev)
1413{
1414 mdk_rdev_t *rdev;
1415 struct list_head *tmp;
1416
1417 ITERATE_RDEV(mddev,rdev,tmp) {
1418 super_types[mddev->major_version].
1419 sync_super(mddev, rdev);
1420 rdev->sb_loaded = 1;
1421 }
1422}
1423
1424static void md_update_sb(mddev_t * mddev)
1425{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001426 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct list_head *tmp;
1428 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001429 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001432 spin_lock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001433 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 mddev->utime = get_seconds();
1435 mddev->events ++;
1436
1437 if (!mddev->events) {
1438 /*
1439 * oops, this 64-bit counter should never wrap.
1440 * Either we are in around ~1 trillion A.C., assuming
1441 * 1 reboot per second, or we have a bug:
1442 */
1443 MD_BUG();
1444 mddev->events --;
1445 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001446 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 sync_sbs(mddev);
1448
1449 /*
1450 * do not write anything to disk if using
1451 * nonpersistent superblocks
1452 */
NeilBrown06d91a52005-06-21 17:17:12 -07001453 if (!mddev->persistent) {
1454 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001455 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001456 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001458 }
NeilBrowna9701a32005-11-08 21:39:34 -08001459 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 dprintk(KERN_INFO
1462 "md: updating %s RAID superblock on device (in sync %d)\n",
1463 mdname(mddev),mddev->in_sync);
1464
NeilBrown32a76272005-06-21 17:17:14 -07001465 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 ITERATE_RDEV(mddev,rdev,tmp) {
1467 char b[BDEVNAME_SIZE];
1468 dprintk(KERN_INFO "md: ");
NeilBrownb2d444d2005-11-08 21:39:31 -08001469 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 dprintk("(skipping faulty ");
1471
1472 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001473 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001474 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001475 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001476 rdev->sb_page);
1477 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1478 bdevname(rdev->bdev,b),
1479 (unsigned long long)rdev->sb_offset);
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 } else
1482 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001483 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /* only need to write one superblock... */
1485 break;
1486 }
NeilBrowna9701a32005-11-08 21:39:34 -08001487 md_super_wait(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001488 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1489
NeilBrowna9701a32005-11-08 21:39:34 -08001490 spin_lock_irq(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001491 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001492 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001493 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001494 goto repeat;
1495 }
1496 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001497 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001498 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
NeilBrown86e6ffd2005-11-08 21:39:24 -08001502struct rdev_sysfs_entry {
1503 struct attribute attr;
1504 ssize_t (*show)(mdk_rdev_t *, char *);
1505 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1506};
1507
1508static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001509state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001510{
1511 char *sep = "";
1512 int len=0;
1513
NeilBrownb2d444d2005-11-08 21:39:31 -08001514 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001515 len+= sprintf(page+len, "%sfaulty",sep);
1516 sep = ",";
1517 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001518 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001519 len += sprintf(page+len, "%sin_sync",sep);
1520 sep = ",";
1521 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001522 if (!test_bit(Faulty, &rdev->flags) &&
1523 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001524 len += sprintf(page+len, "%sspare", sep);
1525 sep = ",";
1526 }
1527 return len+sprintf(page+len, "\n");
1528}
1529
NeilBrown96de1e62005-11-08 21:39:39 -08001530static struct rdev_sysfs_entry
1531rdev_state = __ATTR_RO(state);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001532
1533static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001534super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001535{
1536 if (rdev->sb_loaded && rdev->sb_size) {
1537 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1538 return rdev->sb_size;
1539 } else
1540 return 0;
1541}
NeilBrown96de1e62005-11-08 21:39:39 -08001542static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1543
NeilBrown86e6ffd2005-11-08 21:39:24 -08001544static struct attribute *rdev_default_attrs[] = {
1545 &rdev_state.attr,
1546 &rdev_super.attr,
1547 NULL,
1548};
1549static ssize_t
1550rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1551{
1552 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1553 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1554
1555 if (!entry->show)
1556 return -EIO;
1557 return entry->show(rdev, page);
1558}
1559
1560static ssize_t
1561rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1562 const char *page, size_t length)
1563{
1564 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1565 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1566
1567 if (!entry->store)
1568 return -EIO;
1569 return entry->store(rdev, page, length);
1570}
1571
1572static void rdev_free(struct kobject *ko)
1573{
1574 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1575 kfree(rdev);
1576}
1577static struct sysfs_ops rdev_sysfs_ops = {
1578 .show = rdev_attr_show,
1579 .store = rdev_attr_store,
1580};
1581static struct kobj_type rdev_ktype = {
1582 .release = rdev_free,
1583 .sysfs_ops = &rdev_sysfs_ops,
1584 .default_attrs = rdev_default_attrs,
1585};
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587/*
1588 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1589 *
1590 * mark the device faulty if:
1591 *
1592 * - the device is nonexistent (zero size)
1593 * - the device has no valid superblock
1594 *
1595 * a faulty rdev _never_ has rdev->sb set.
1596 */
1597static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1598{
1599 char b[BDEVNAME_SIZE];
1600 int err;
1601 mdk_rdev_t *rdev;
1602 sector_t size;
1603
1604 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1605 if (!rdev) {
1606 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1607 return ERR_PTR(-ENOMEM);
1608 }
1609 memset(rdev, 0, sizeof(*rdev));
1610
1611 if ((err = alloc_disk_sb(rdev)))
1612 goto abort_free;
1613
1614 err = lock_rdev(rdev, newdev);
1615 if (err)
1616 goto abort_free;
1617
NeilBrown86e6ffd2005-11-08 21:39:24 -08001618 rdev->kobj.parent = NULL;
1619 rdev->kobj.ktype = &rdev_ktype;
1620 kobject_init(&rdev->kobj);
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 rdev->desc_nr = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001623 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 rdev->data_offset = 0;
1625 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001626 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1629 if (!size) {
1630 printk(KERN_WARNING
1631 "md: %s has zero or unknown size, marking faulty!\n",
1632 bdevname(rdev->bdev,b));
1633 err = -EINVAL;
1634 goto abort_free;
1635 }
1636
1637 if (super_format >= 0) {
1638 err = super_types[super_format].
1639 load_super(rdev, NULL, super_minor);
1640 if (err == -EINVAL) {
1641 printk(KERN_WARNING
1642 "md: %s has invalid sb, not importing!\n",
1643 bdevname(rdev->bdev,b));
1644 goto abort_free;
1645 }
1646 if (err < 0) {
1647 printk(KERN_WARNING
1648 "md: could not read %s's sb, not importing!\n",
1649 bdevname(rdev->bdev,b));
1650 goto abort_free;
1651 }
1652 }
1653 INIT_LIST_HEAD(&rdev->same_set);
1654
1655 return rdev;
1656
1657abort_free:
1658 if (rdev->sb_page) {
1659 if (rdev->bdev)
1660 unlock_rdev(rdev);
1661 free_disk_sb(rdev);
1662 }
1663 kfree(rdev);
1664 return ERR_PTR(err);
1665}
1666
1667/*
1668 * Check a full RAID array for plausibility
1669 */
1670
1671
NeilBrowna757e642005-04-16 15:26:42 -07001672static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 int i;
1675 struct list_head *tmp;
1676 mdk_rdev_t *rdev, *freshest;
1677 char b[BDEVNAME_SIZE];
1678
1679 freshest = NULL;
1680 ITERATE_RDEV(mddev,rdev,tmp)
1681 switch (super_types[mddev->major_version].
1682 load_super(rdev, freshest, mddev->minor_version)) {
1683 case 1:
1684 freshest = rdev;
1685 break;
1686 case 0:
1687 break;
1688 default:
1689 printk( KERN_ERR \
1690 "md: fatal superblock inconsistency in %s"
1691 " -- removing from array\n",
1692 bdevname(rdev->bdev,b));
1693 kick_rdev_from_array(rdev);
1694 }
1695
1696
1697 super_types[mddev->major_version].
1698 validate_super(mddev, freshest);
1699
1700 i = 0;
1701 ITERATE_RDEV(mddev,rdev,tmp) {
1702 if (rdev != freshest)
1703 if (super_types[mddev->major_version].
1704 validate_super(mddev, rdev)) {
1705 printk(KERN_WARNING "md: kicking non-fresh %s"
1706 " from array!\n",
1707 bdevname(rdev->bdev,b));
1708 kick_rdev_from_array(rdev);
1709 continue;
1710 }
1711 if (mddev->level == LEVEL_MULTIPATH) {
1712 rdev->desc_nr = i++;
1713 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001714 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716 }
1717
1718
1719
1720 if (mddev->recovery_cp != MaxSector &&
1721 mddev->level >= 1)
1722 printk(KERN_ERR "md: %s: raid array is not clean"
1723 " -- starting background reconstruction\n",
1724 mdname(mddev));
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
NeilBrowneae17012005-11-08 21:39:23 -08001728static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001729level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08001730{
1731 mdk_personality_t *p = mddev->pers;
NeilBrownbb636542005-11-08 21:39:45 -08001732 if (p == NULL && mddev->raid_disks == 0)
NeilBrowneae17012005-11-08 21:39:23 -08001733 return 0;
1734 if (mddev->level >= 0)
Neil Brownbcb97942005-12-20 11:07:00 +11001735 return sprintf(page, "raid%d\n", mddev->level);
NeilBrowneae17012005-11-08 21:39:23 -08001736 else
1737 return sprintf(page, "%s\n", p->name);
1738}
1739
NeilBrown96de1e62005-11-08 21:39:39 -08001740static struct md_sysfs_entry md_level = __ATTR_RO(level);
NeilBrowneae17012005-11-08 21:39:23 -08001741
1742static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001743raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08001744{
NeilBrownbb636542005-11-08 21:39:45 -08001745 if (mddev->raid_disks == 0)
1746 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08001747 return sprintf(page, "%d\n", mddev->raid_disks);
1748}
1749
NeilBrown96de1e62005-11-08 21:39:39 -08001750static struct md_sysfs_entry md_raid_disks = __ATTR_RO(raid_disks);
NeilBrowneae17012005-11-08 21:39:23 -08001751
NeilBrown24dd4692005-11-08 21:39:26 -08001752static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08001753action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08001754{
NeilBrown7eec3142005-11-08 21:39:44 -08001755 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08001756 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1757 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
1758 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08001759 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1760 type = "resync";
1761 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1762 type = "check";
1763 else
1764 type = "repair";
1765 } else
1766 type = "recover";
1767 }
1768 return sprintf(page, "%s\n", type);
1769}
1770
1771static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08001772action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08001773{
NeilBrown7eec3142005-11-08 21:39:44 -08001774 if (!mddev->pers || !mddev->pers->sync_request)
1775 return -EINVAL;
1776
1777 if (strcmp(page, "idle")==0 || strcmp(page, "idle\n")==0) {
1778 if (mddev->sync_thread) {
1779 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1780 md_unregister_thread(mddev->sync_thread);
1781 mddev->sync_thread = NULL;
1782 mddev->recovery = 0;
1783 }
1784 return len;
1785 }
NeilBrown31399d92005-11-08 21:39:29 -08001786
1787 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1788 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08001789 return -EBUSY;
NeilBrown7eec3142005-11-08 21:39:44 -08001790 if (strcmp(page, "resync")==0 || strcmp(page, "resync\n")==0 ||
1791 strcmp(page, "recover")==0 || strcmp(page, "recover\n")==0)
1792 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1793 else {
1794 if (strcmp(page, "check")==0 || strcmp(page, "check\n")==0)
1795 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1796 else if (strcmp(page, "repair")!=0 && strcmp(page, "repair\n")!=0)
1797 return -EINVAL;
1798 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1799 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1800 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1801 }
NeilBrown24dd4692005-11-08 21:39:26 -08001802 md_wakeup_thread(mddev->thread);
1803 return len;
1804}
1805
NeilBrown9d888832005-11-08 21:39:26 -08001806static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001807mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08001808{
1809 return sprintf(page, "%llu\n",
1810 (unsigned long long) mddev->resync_mismatches);
1811}
1812
NeilBrown96de1e62005-11-08 21:39:39 -08001813static struct md_sysfs_entry
NeilBrown7eec3142005-11-08 21:39:44 -08001814md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08001815
NeilBrown96de1e62005-11-08 21:39:39 -08001816
1817static struct md_sysfs_entry
1818md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08001819
NeilBrowneae17012005-11-08 21:39:23 -08001820static struct attribute *md_default_attrs[] = {
1821 &md_level.attr,
1822 &md_raid_disks.attr,
NeilBrown411036f2005-11-08 21:39:40 -08001823 NULL,
1824};
1825
1826static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08001827 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08001828 &md_mismatches.attr,
NeilBrowneae17012005-11-08 21:39:23 -08001829 NULL,
1830};
NeilBrown411036f2005-11-08 21:39:40 -08001831static struct attribute_group md_redundancy_group = {
1832 .name = NULL,
1833 .attrs = md_redundancy_attrs,
1834};
1835
NeilBrowneae17012005-11-08 21:39:23 -08001836
1837static ssize_t
1838md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1839{
1840 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1841 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08001842 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08001843
1844 if (!entry->show)
1845 return -EIO;
NeilBrown96de1e62005-11-08 21:39:39 -08001846 mddev_lock(mddev);
1847 rv = entry->show(mddev, page);
1848 mddev_unlock(mddev);
1849 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08001850}
1851
1852static ssize_t
1853md_attr_store(struct kobject *kobj, struct attribute *attr,
1854 const char *page, size_t length)
1855{
1856 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1857 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08001858 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08001859
1860 if (!entry->store)
1861 return -EIO;
NeilBrown96de1e62005-11-08 21:39:39 -08001862 mddev_lock(mddev);
1863 rv = entry->store(mddev, page, length);
1864 mddev_unlock(mddev);
1865 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08001866}
1867
1868static void md_free(struct kobject *ko)
1869{
1870 mddev_t *mddev = container_of(ko, mddev_t, kobj);
1871 kfree(mddev);
1872}
1873
1874static struct sysfs_ops md_sysfs_ops = {
1875 .show = md_attr_show,
1876 .store = md_attr_store,
1877};
1878static struct kobj_type md_ktype = {
1879 .release = md_free,
1880 .sysfs_ops = &md_sysfs_ops,
1881 .default_attrs = md_default_attrs,
1882};
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884int mdp_major = 0;
1885
1886static struct kobject *md_probe(dev_t dev, int *part, void *data)
1887{
1888 static DECLARE_MUTEX(disks_sem);
1889 mddev_t *mddev = mddev_find(dev);
1890 struct gendisk *disk;
1891 int partitioned = (MAJOR(dev) != MD_MAJOR);
1892 int shift = partitioned ? MdpMinorShift : 0;
1893 int unit = MINOR(dev) >> shift;
1894
1895 if (!mddev)
1896 return NULL;
1897
1898 down(&disks_sem);
1899 if (mddev->gendisk) {
1900 up(&disks_sem);
1901 mddev_put(mddev);
1902 return NULL;
1903 }
1904 disk = alloc_disk(1 << shift);
1905 if (!disk) {
1906 up(&disks_sem);
1907 mddev_put(mddev);
1908 return NULL;
1909 }
1910 disk->major = MAJOR(dev);
1911 disk->first_minor = unit << shift;
1912 if (partitioned) {
1913 sprintf(disk->disk_name, "md_d%d", unit);
1914 sprintf(disk->devfs_name, "md/d%d", unit);
1915 } else {
1916 sprintf(disk->disk_name, "md%d", unit);
1917 sprintf(disk->devfs_name, "md/%d", unit);
1918 }
1919 disk->fops = &md_fops;
1920 disk->private_data = mddev;
1921 disk->queue = mddev->queue;
1922 add_disk(disk);
1923 mddev->gendisk = disk;
1924 up(&disks_sem);
NeilBrown9c791972005-11-08 21:39:28 -08001925 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08001926 mddev->kobj.k_name = NULL;
1927 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
1928 mddev->kobj.ktype = &md_ktype;
1929 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return NULL;
1931}
1932
1933void md_wakeup_thread(mdk_thread_t *thread);
1934
1935static void md_safemode_timeout(unsigned long data)
1936{
1937 mddev_t *mddev = (mddev_t *) data;
1938
1939 mddev->safemode = 1;
1940 md_wakeup_thread(mddev->thread);
1941}
1942
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08001943static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945static int do_md_run(mddev_t * mddev)
1946{
1947 int pnum, err;
1948 int chunk_size;
1949 struct list_head *tmp;
1950 mdk_rdev_t *rdev;
1951 struct gendisk *disk;
1952 char b[BDEVNAME_SIZE];
1953
NeilBrowna757e642005-04-16 15:26:42 -07001954 if (list_empty(&mddev->disks))
1955 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 if (mddev->pers)
1959 return -EBUSY;
1960
1961 /*
1962 * Analyze all RAID superblock(s)
1963 */
NeilBrowna757e642005-04-16 15:26:42 -07001964 if (!mddev->raid_disks)
1965 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 chunk_size = mddev->chunk_size;
1968 pnum = level_to_pers(mddev->level);
1969
1970 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1971 if (!chunk_size) {
1972 /*
1973 * 'default chunksize' in the old md code used to
1974 * be PAGE_SIZE, baaad.
1975 * we abort here to be on the safe side. We don't
1976 * want to continue the bad practice.
1977 */
1978 printk(KERN_ERR
1979 "no chunksize specified, see 'man raidtab'\n");
1980 return -EINVAL;
1981 }
1982 if (chunk_size > MAX_CHUNK_SIZE) {
1983 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1984 chunk_size, MAX_CHUNK_SIZE);
1985 return -EINVAL;
1986 }
1987 /*
1988 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1989 */
1990 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07001991 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return -EINVAL;
1993 }
1994 if (chunk_size < PAGE_SIZE) {
1995 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1996 chunk_size, PAGE_SIZE);
1997 return -EINVAL;
1998 }
1999
2000 /* devices must have minimum size of one chunk */
2001 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002002 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 continue;
2004 if (rdev->size < chunk_size / 1024) {
2005 printk(KERN_WARNING
2006 "md: Dev %s smaller than chunk_size:"
2007 " %lluk < %dk\n",
2008 bdevname(rdev->bdev,b),
2009 (unsigned long long)rdev->size,
2010 chunk_size / 1024);
2011 return -EINVAL;
2012 }
2013 }
2014 }
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016#ifdef CONFIG_KMOD
2017 if (!pers[pnum])
2018 {
2019 request_module("md-personality-%d", pnum);
2020 }
2021#endif
2022
2023 /*
2024 * Drop all container device buffers, from now on
2025 * the only valid external interface is through the md
2026 * device.
2027 * Also find largest hardsector size
2028 */
2029 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002030 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 continue;
2032 sync_blockdev(rdev->bdev);
2033 invalidate_bdev(rdev->bdev, 0);
2034 }
2035
2036 md_probe(mddev->unit, NULL, NULL);
2037 disk = mddev->gendisk;
2038 if (!disk)
2039 return -ENOMEM;
2040
2041 spin_lock(&pers_lock);
2042 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
2043 spin_unlock(&pers_lock);
2044 printk(KERN_WARNING "md: personality %d is not loaded!\n",
2045 pnum);
2046 return -EINVAL;
2047 }
2048
2049 mddev->pers = pers[pnum];
2050 spin_unlock(&pers_lock);
2051
NeilBrown657390d2005-08-26 18:34:16 -07002052 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08002054 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002055 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
NeilBrownf91de922005-11-08 21:39:36 -08002057 if (start_readonly)
2058 mddev->ro = 2; /* read-only, but switch on first write */
2059
NeilBrownb15c2e52006-01-06 00:20:16 -08002060 err = mddev->pers->run(mddev);
2061 if (!err && mddev->pers->sync_request) {
2062 err = bitmap_create(mddev);
2063 if (err) {
2064 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2065 mdname(mddev), err);
2066 mddev->pers->stop(mddev);
2067 }
2068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (err) {
2070 printk(KERN_ERR "md: pers->run() failed ...\n");
2071 module_put(mddev->pers->owner);
2072 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002073 bitmap_destroy(mddev);
2074 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
NeilBrown411036f2005-11-08 21:39:40 -08002076 if (mddev->pers->sync_request)
2077 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08002078 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2079 mddev->ro = 0;
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 atomic_set(&mddev->writes_pending,0);
2082 mddev->safemode = 0;
2083 mddev->safemode_timer.function = md_safemode_timeout;
2084 mddev->safemode_timer.data = (unsigned long) mddev;
2085 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2086 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002087
2088 ITERATE_RDEV(mddev,rdev,tmp)
2089 if (rdev->raid_disk >= 0) {
2090 char nm[20];
2091 sprintf(nm, "rd%d", rdev->raid_disk);
2092 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002096 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 if (mddev->sb_dirty)
2099 md_update_sb(mddev);
2100
2101 set_capacity(disk, mddev->array_size<<1);
2102
2103 /* If we call blk_queue_make_request here, it will
2104 * re-initialise max_sectors etc which may have been
2105 * refined inside -> run. So just set the bits we need to set.
2106 * Most initialisation happended when we called
2107 * blk_queue_make_request(..., md_fail_request)
2108 * earlier.
2109 */
2110 mddev->queue->queuedata = mddev;
2111 mddev->queue->make_request_fn = mddev->pers->make_request;
2112
2113 mddev->changed = 1;
2114 return 0;
2115}
2116
2117static int restart_array(mddev_t *mddev)
2118{
2119 struct gendisk *disk = mddev->gendisk;
2120 int err;
2121
2122 /*
2123 * Complain if it has no devices
2124 */
2125 err = -ENXIO;
2126 if (list_empty(&mddev->disks))
2127 goto out;
2128
2129 if (mddev->pers) {
2130 err = -EBUSY;
2131 if (!mddev->ro)
2132 goto out;
2133
2134 mddev->safemode = 0;
2135 mddev->ro = 0;
2136 set_disk_ro(disk, 0);
2137
2138 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2139 mdname(mddev));
2140 /*
2141 * Kick recovery or resync if necessary
2142 */
2143 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2144 md_wakeup_thread(mddev->thread);
2145 err = 0;
2146 } else {
2147 printk(KERN_ERR "md: %s has no personality assigned.\n",
2148 mdname(mddev));
2149 err = -EINVAL;
2150 }
2151
2152out:
2153 return err;
2154}
2155
2156static int do_md_stop(mddev_t * mddev, int ro)
2157{
2158 int err = 0;
2159 struct gendisk *disk = mddev->gendisk;
2160
2161 if (mddev->pers) {
2162 if (atomic_read(&mddev->active)>2) {
2163 printk("md: %s still in use.\n",mdname(mddev));
2164 return -EBUSY;
2165 }
2166
2167 if (mddev->sync_thread) {
2168 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2169 md_unregister_thread(mddev->sync_thread);
2170 mddev->sync_thread = NULL;
2171 }
2172
2173 del_timer_sync(&mddev->safemode_timer);
2174
2175 invalidate_partition(disk, 0);
2176
2177 if (ro) {
2178 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08002179 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 goto out;
2181 mddev->ro = 1;
2182 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002183 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08002184 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (mddev->ro)
2186 set_disk_ro(disk, 0);
2187 blk_queue_make_request(mddev->queue, md_fail_request);
2188 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08002189 if (mddev->pers->sync_request)
2190 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 module_put(mddev->pers->owner);
2193 mddev->pers = NULL;
2194 if (mddev->ro)
2195 mddev->ro = 0;
2196 }
2197 if (!mddev->in_sync) {
2198 /* mark array as shutdown cleanly */
2199 mddev->in_sync = 1;
2200 md_update_sb(mddev);
2201 }
2202 if (ro)
2203 set_disk_ro(disk, 1);
2204 }
NeilBrown32a76272005-06-21 17:17:14 -07002205
2206 bitmap_destroy(mddev);
2207 if (mddev->bitmap_file) {
2208 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2209 fput(mddev->bitmap_file);
2210 mddev->bitmap_file = NULL;
2211 }
NeilBrown92232142005-08-18 11:24:16 -07002212 mddev->bitmap_offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /*
2215 * Free resources if final stop
2216 */
2217 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002218 mdk_rdev_t *rdev;
2219 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 struct gendisk *disk;
2221 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2222
NeilBrown86e6ffd2005-11-08 21:39:24 -08002223 ITERATE_RDEV(mddev,rdev,tmp)
2224 if (rdev->raid_disk >= 0) {
2225 char nm[20];
2226 sprintf(nm, "rd%d", rdev->raid_disk);
2227 sysfs_remove_link(&mddev->kobj, nm);
2228 }
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 export_array(mddev);
2231
2232 mddev->array_size = 0;
2233 disk = mddev->gendisk;
2234 if (disk)
2235 set_capacity(disk, 0);
2236 mddev->changed = 1;
2237 } else
2238 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2239 mdname(mddev));
2240 err = 0;
2241out:
2242 return err;
2243}
2244
2245static void autorun_array(mddev_t *mddev)
2246{
2247 mdk_rdev_t *rdev;
2248 struct list_head *tmp;
2249 int err;
2250
NeilBrowna757e642005-04-16 15:26:42 -07002251 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 printk(KERN_INFO "md: running: ");
2255
2256 ITERATE_RDEV(mddev,rdev,tmp) {
2257 char b[BDEVNAME_SIZE];
2258 printk("<%s>", bdevname(rdev->bdev,b));
2259 }
2260 printk("\n");
2261
2262 err = do_md_run (mddev);
2263 if (err) {
2264 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2265 do_md_stop (mddev, 0);
2266 }
2267}
2268
2269/*
2270 * lets try to run arrays based on all disks that have arrived
2271 * until now. (those are in pending_raid_disks)
2272 *
2273 * the method: pick the first pending disk, collect all disks with
2274 * the same UUID, remove all from the pending list and put them into
2275 * the 'same_array' list. Then order this list based on superblock
2276 * update time (freshest comes first), kick out 'old' disks and
2277 * compare superblocks. If everything's fine then run it.
2278 *
2279 * If "unit" is allocated, then bump its reference count
2280 */
2281static void autorun_devices(int part)
2282{
2283 struct list_head candidates;
2284 struct list_head *tmp;
2285 mdk_rdev_t *rdev0, *rdev;
2286 mddev_t *mddev;
2287 char b[BDEVNAME_SIZE];
2288
2289 printk(KERN_INFO "md: autorun ...\n");
2290 while (!list_empty(&pending_raid_disks)) {
2291 dev_t dev;
2292 rdev0 = list_entry(pending_raid_disks.next,
2293 mdk_rdev_t, same_set);
2294
2295 printk(KERN_INFO "md: considering %s ...\n",
2296 bdevname(rdev0->bdev,b));
2297 INIT_LIST_HEAD(&candidates);
2298 ITERATE_RDEV_PENDING(rdev,tmp)
2299 if (super_90_load(rdev, rdev0, 0) >= 0) {
2300 printk(KERN_INFO "md: adding %s ...\n",
2301 bdevname(rdev->bdev,b));
2302 list_move(&rdev->same_set, &candidates);
2303 }
2304 /*
2305 * now we have a set of devices, with all of them having
2306 * mostly sane superblocks. It's time to allocate the
2307 * mddev.
2308 */
2309 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2310 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2311 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2312 break;
2313 }
2314 if (part)
2315 dev = MKDEV(mdp_major,
2316 rdev0->preferred_minor << MdpMinorShift);
2317 else
2318 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2319
2320 md_probe(dev, NULL, NULL);
2321 mddev = mddev_find(dev);
2322 if (!mddev) {
2323 printk(KERN_ERR
2324 "md: cannot allocate memory for md drive.\n");
2325 break;
2326 }
2327 if (mddev_lock(mddev))
2328 printk(KERN_WARNING "md: %s locked, cannot run\n",
2329 mdname(mddev));
2330 else if (mddev->raid_disks || mddev->major_version
2331 || !list_empty(&mddev->disks)) {
2332 printk(KERN_WARNING
2333 "md: %s already running, cannot run %s\n",
2334 mdname(mddev), bdevname(rdev0->bdev,b));
2335 mddev_unlock(mddev);
2336 } else {
2337 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2338 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2339 list_del_init(&rdev->same_set);
2340 if (bind_rdev_to_array(rdev, mddev))
2341 export_rdev(rdev);
2342 }
2343 autorun_array(mddev);
2344 mddev_unlock(mddev);
2345 }
2346 /* on success, candidates will be empty, on error
2347 * it won't...
2348 */
2349 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2350 export_rdev(rdev);
2351 mddev_put(mddev);
2352 }
2353 printk(KERN_INFO "md: ... autorun DONE.\n");
2354}
2355
2356/*
2357 * import RAID devices based on one partition
2358 * if possible, the array gets run as well.
2359 */
2360
2361static int autostart_array(dev_t startdev)
2362{
2363 char b[BDEVNAME_SIZE];
2364 int err = -EINVAL, i;
2365 mdp_super_t *sb = NULL;
2366 mdk_rdev_t *start_rdev = NULL, *rdev;
2367
2368 start_rdev = md_import_device(startdev, 0, 0);
2369 if (IS_ERR(start_rdev))
2370 return err;
2371
2372
2373 /* NOTE: this can only work for 0.90.0 superblocks */
2374 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2375 if (sb->major_version != 0 ||
2376 sb->minor_version != 90 ) {
2377 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2378 export_rdev(start_rdev);
2379 return err;
2380 }
2381
NeilBrownb2d444d2005-11-08 21:39:31 -08002382 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 printk(KERN_WARNING
2384 "md: can not autostart based on faulty %s!\n",
2385 bdevname(start_rdev->bdev,b));
2386 export_rdev(start_rdev);
2387 return err;
2388 }
2389 list_add(&start_rdev->same_set, &pending_raid_disks);
2390
2391 for (i = 0; i < MD_SB_DISKS; i++) {
2392 mdp_disk_t *desc = sb->disks + i;
2393 dev_t dev = MKDEV(desc->major, desc->minor);
2394
2395 if (!dev)
2396 continue;
2397 if (dev == startdev)
2398 continue;
2399 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2400 continue;
2401 rdev = md_import_device(dev, 0, 0);
2402 if (IS_ERR(rdev))
2403 continue;
2404
2405 list_add(&rdev->same_set, &pending_raid_disks);
2406 }
2407
2408 /*
2409 * possibly return codes
2410 */
2411 autorun_devices(0);
2412 return 0;
2413
2414}
2415
2416
2417static int get_version(void __user * arg)
2418{
2419 mdu_version_t ver;
2420
2421 ver.major = MD_MAJOR_VERSION;
2422 ver.minor = MD_MINOR_VERSION;
2423 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2424
2425 if (copy_to_user(arg, &ver, sizeof(ver)))
2426 return -EFAULT;
2427
2428 return 0;
2429}
2430
2431static int get_array_info(mddev_t * mddev, void __user * arg)
2432{
2433 mdu_array_info_t info;
2434 int nr,working,active,failed,spare;
2435 mdk_rdev_t *rdev;
2436 struct list_head *tmp;
2437
2438 nr=working=active=failed=spare=0;
2439 ITERATE_RDEV(mddev,rdev,tmp) {
2440 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002441 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 failed++;
2443 else {
2444 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002445 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 active++;
2447 else
2448 spare++;
2449 }
2450 }
2451
2452 info.major_version = mddev->major_version;
2453 info.minor_version = mddev->minor_version;
2454 info.patch_version = MD_PATCHLEVEL_VERSION;
2455 info.ctime = mddev->ctime;
2456 info.level = mddev->level;
2457 info.size = mddev->size;
2458 info.nr_disks = nr;
2459 info.raid_disks = mddev->raid_disks;
2460 info.md_minor = mddev->md_minor;
2461 info.not_persistent= !mddev->persistent;
2462
2463 info.utime = mddev->utime;
2464 info.state = 0;
2465 if (mddev->in_sync)
2466 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07002467 if (mddev->bitmap && mddev->bitmap_offset)
2468 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 info.active_disks = active;
2470 info.working_disks = working;
2471 info.failed_disks = failed;
2472 info.spare_disks = spare;
2473
2474 info.layout = mddev->layout;
2475 info.chunk_size = mddev->chunk_size;
2476
2477 if (copy_to_user(arg, &info, sizeof(info)))
2478 return -EFAULT;
2479
2480 return 0;
2481}
2482
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002483static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07002484{
2485 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2486 char *ptr, *buf = NULL;
2487 int err = -ENOMEM;
2488
2489 file = kmalloc(sizeof(*file), GFP_KERNEL);
2490 if (!file)
2491 goto out;
2492
2493 /* bitmap disabled, zero the first byte and copy out */
2494 if (!mddev->bitmap || !mddev->bitmap->file) {
2495 file->pathname[0] = '\0';
2496 goto copy_out;
2497 }
2498
2499 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2500 if (!buf)
2501 goto out;
2502
2503 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2504 if (!ptr)
2505 goto out;
2506
2507 strcpy(file->pathname, ptr);
2508
2509copy_out:
2510 err = 0;
2511 if (copy_to_user(arg, file, sizeof(*file)))
2512 err = -EFAULT;
2513out:
2514 kfree(buf);
2515 kfree(file);
2516 return err;
2517}
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519static int get_disk_info(mddev_t * mddev, void __user * arg)
2520{
2521 mdu_disk_info_t info;
2522 unsigned int nr;
2523 mdk_rdev_t *rdev;
2524
2525 if (copy_from_user(&info, arg, sizeof(info)))
2526 return -EFAULT;
2527
2528 nr = info.number;
2529
2530 rdev = find_rdev_nr(mddev, nr);
2531 if (rdev) {
2532 info.major = MAJOR(rdev->bdev->bd_dev);
2533 info.minor = MINOR(rdev->bdev->bd_dev);
2534 info.raid_disk = rdev->raid_disk;
2535 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08002536 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002538 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 info.state |= (1<<MD_DISK_ACTIVE);
2540 info.state |= (1<<MD_DISK_SYNC);
2541 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002542 if (test_bit(WriteMostly, &rdev->flags))
2543 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 } else {
2545 info.major = info.minor = 0;
2546 info.raid_disk = -1;
2547 info.state = (1<<MD_DISK_REMOVED);
2548 }
2549
2550 if (copy_to_user(arg, &info, sizeof(info)))
2551 return -EFAULT;
2552
2553 return 0;
2554}
2555
2556static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2557{
2558 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2559 mdk_rdev_t *rdev;
2560 dev_t dev = MKDEV(info->major,info->minor);
2561
2562 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2563 return -EOVERFLOW;
2564
2565 if (!mddev->raid_disks) {
2566 int err;
2567 /* expecting a device which has a superblock */
2568 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2569 if (IS_ERR(rdev)) {
2570 printk(KERN_WARNING
2571 "md: md_import_device returned %ld\n",
2572 PTR_ERR(rdev));
2573 return PTR_ERR(rdev);
2574 }
2575 if (!list_empty(&mddev->disks)) {
2576 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2577 mdk_rdev_t, same_set);
2578 int err = super_types[mddev->major_version]
2579 .load_super(rdev, rdev0, mddev->minor_version);
2580 if (err < 0) {
2581 printk(KERN_WARNING
2582 "md: %s has different UUID to %s\n",
2583 bdevname(rdev->bdev,b),
2584 bdevname(rdev0->bdev,b2));
2585 export_rdev(rdev);
2586 return -EINVAL;
2587 }
2588 }
2589 err = bind_rdev_to_array(rdev, mddev);
2590 if (err)
2591 export_rdev(rdev);
2592 return err;
2593 }
2594
2595 /*
2596 * add_new_disk can be used once the array is assembled
2597 * to add "hot spares". They must already have a superblock
2598 * written
2599 */
2600 if (mddev->pers) {
2601 int err;
2602 if (!mddev->pers->hot_add_disk) {
2603 printk(KERN_WARNING
2604 "%s: personality does not support diskops!\n",
2605 mdname(mddev));
2606 return -EINVAL;
2607 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07002608 if (mddev->persistent)
2609 rdev = md_import_device(dev, mddev->major_version,
2610 mddev->minor_version);
2611 else
2612 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (IS_ERR(rdev)) {
2614 printk(KERN_WARNING
2615 "md: md_import_device returned %ld\n",
2616 PTR_ERR(rdev));
2617 return PTR_ERR(rdev);
2618 }
NeilBrown41158c72005-06-21 17:17:25 -07002619 /* set save_raid_disk if appropriate */
2620 if (!mddev->persistent) {
2621 if (info->state & (1<<MD_DISK_SYNC) &&
2622 info->raid_disk < mddev->raid_disks)
2623 rdev->raid_disk = info->raid_disk;
2624 else
2625 rdev->raid_disk = -1;
2626 } else
2627 super_types[mddev->major_version].
2628 validate_super(mddev, rdev);
2629 rdev->saved_raid_disk = rdev->raid_disk;
2630
NeilBrownb2d444d2005-11-08 21:39:31 -08002631 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002632 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2633 set_bit(WriteMostly, &rdev->flags);
2634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 rdev->raid_disk = -1;
2636 err = bind_rdev_to_array(rdev, mddev);
2637 if (err)
2638 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07002639
2640 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002641 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return err;
2643 }
2644
2645 /* otherwise, add_new_disk is only allowed
2646 * for major_version==0 superblocks
2647 */
2648 if (mddev->major_version != 0) {
2649 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2650 mdname(mddev));
2651 return -EINVAL;
2652 }
2653
2654 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2655 int err;
2656 rdev = md_import_device (dev, -1, 0);
2657 if (IS_ERR(rdev)) {
2658 printk(KERN_WARNING
2659 "md: error, md_import_device() returned %ld\n",
2660 PTR_ERR(rdev));
2661 return PTR_ERR(rdev);
2662 }
2663 rdev->desc_nr = info->number;
2664 if (info->raid_disk < mddev->raid_disks)
2665 rdev->raid_disk = info->raid_disk;
2666 else
2667 rdev->raid_disk = -1;
2668
NeilBrownb2d444d2005-11-08 21:39:31 -08002669 rdev->flags = 0;
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08002672 if (info->state & (1<<MD_DISK_SYNC))
2673 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002675 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2676 set_bit(WriteMostly, &rdev->flags);
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 err = bind_rdev_to_array(rdev, mddev);
2679 if (err) {
2680 export_rdev(rdev);
2681 return err;
2682 }
2683
2684 if (!mddev->persistent) {
2685 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2686 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2687 } else
2688 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2689 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2690
2691 if (!mddev->size || (mddev->size > rdev->size))
2692 mddev->size = rdev->size;
2693 }
2694
2695 return 0;
2696}
2697
2698static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2699{
2700 char b[BDEVNAME_SIZE];
2701 mdk_rdev_t *rdev;
2702
2703 if (!mddev->pers)
2704 return -ENODEV;
2705
2706 rdev = find_rdev(mddev, dev);
2707 if (!rdev)
2708 return -ENXIO;
2709
2710 if (rdev->raid_disk >= 0)
2711 goto busy;
2712
2713 kick_rdev_from_array(rdev);
2714 md_update_sb(mddev);
2715
2716 return 0;
2717busy:
2718 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2719 bdevname(rdev->bdev,b), mdname(mddev));
2720 return -EBUSY;
2721}
2722
2723static int hot_add_disk(mddev_t * mddev, dev_t dev)
2724{
2725 char b[BDEVNAME_SIZE];
2726 int err;
2727 unsigned int size;
2728 mdk_rdev_t *rdev;
2729
2730 if (!mddev->pers)
2731 return -ENODEV;
2732
2733 if (mddev->major_version != 0) {
2734 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2735 " version-0 superblocks.\n",
2736 mdname(mddev));
2737 return -EINVAL;
2738 }
2739 if (!mddev->pers->hot_add_disk) {
2740 printk(KERN_WARNING
2741 "%s: personality does not support diskops!\n",
2742 mdname(mddev));
2743 return -EINVAL;
2744 }
2745
2746 rdev = md_import_device (dev, -1, 0);
2747 if (IS_ERR(rdev)) {
2748 printk(KERN_WARNING
2749 "md: error, md_import_device() returned %ld\n",
2750 PTR_ERR(rdev));
2751 return -EINVAL;
2752 }
2753
2754 if (mddev->persistent)
2755 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2756 else
2757 rdev->sb_offset =
2758 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2759
2760 size = calc_dev_size(rdev, mddev->chunk_size);
2761 rdev->size = size;
2762
2763 if (size < mddev->size) {
2764 printk(KERN_WARNING
2765 "%s: disk size %llu blocks < array size %llu\n",
2766 mdname(mddev), (unsigned long long)size,
2767 (unsigned long long)mddev->size);
2768 err = -ENOSPC;
2769 goto abort_export;
2770 }
2771
NeilBrownb2d444d2005-11-08 21:39:31 -08002772 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 printk(KERN_WARNING
2774 "md: can not hot-add faulty %s disk to %s!\n",
2775 bdevname(rdev->bdev,b), mdname(mddev));
2776 err = -EINVAL;
2777 goto abort_export;
2778 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002779 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 rdev->desc_nr = -1;
2781 bind_rdev_to_array(rdev, mddev);
2782
2783 /*
2784 * The rest should better be atomic, we can have disk failures
2785 * noticed in interrupt contexts ...
2786 */
2787
2788 if (rdev->desc_nr == mddev->max_disks) {
2789 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2790 mdname(mddev));
2791 err = -EBUSY;
2792 goto abort_unbind_export;
2793 }
2794
2795 rdev->raid_disk = -1;
2796
2797 md_update_sb(mddev);
2798
2799 /*
2800 * Kick recovery, maybe this spare has to be added to the
2801 * array immediately.
2802 */
2803 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2804 md_wakeup_thread(mddev->thread);
2805
2806 return 0;
2807
2808abort_unbind_export:
2809 unbind_rdev_from_array(rdev);
2810
2811abort_export:
2812 export_rdev(rdev);
2813 return err;
2814}
2815
NeilBrown32a76272005-06-21 17:17:14 -07002816/* similar to deny_write_access, but accounts for our holding a reference
2817 * to the file ourselves */
2818static int deny_bitmap_write_access(struct file * file)
2819{
2820 struct inode *inode = file->f_mapping->host;
2821
2822 spin_lock(&inode->i_lock);
2823 if (atomic_read(&inode->i_writecount) > 1) {
2824 spin_unlock(&inode->i_lock);
2825 return -ETXTBSY;
2826 }
2827 atomic_set(&inode->i_writecount, -1);
2828 spin_unlock(&inode->i_lock);
2829
2830 return 0;
2831}
2832
2833static int set_bitmap_file(mddev_t *mddev, int fd)
2834{
2835 int err;
2836
NeilBrown36fa3062005-09-09 16:23:45 -07002837 if (mddev->pers) {
2838 if (!mddev->pers->quiesce)
2839 return -EBUSY;
2840 if (mddev->recovery || mddev->sync_thread)
2841 return -EBUSY;
2842 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07002843 }
2844
NeilBrown36fa3062005-09-09 16:23:45 -07002845
2846 if (fd >= 0) {
2847 if (mddev->bitmap)
2848 return -EEXIST; /* cannot add when bitmap is present */
2849 mddev->bitmap_file = fget(fd);
2850
2851 if (mddev->bitmap_file == NULL) {
2852 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2853 mdname(mddev));
2854 return -EBADF;
2855 }
2856
2857 err = deny_bitmap_write_access(mddev->bitmap_file);
2858 if (err) {
2859 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2860 mdname(mddev));
2861 fput(mddev->bitmap_file);
2862 mddev->bitmap_file = NULL;
2863 return err;
2864 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002865 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07002866 } else if (mddev->bitmap == NULL)
2867 return -ENOENT; /* cannot remove what isn't there */
2868 err = 0;
2869 if (mddev->pers) {
2870 mddev->pers->quiesce(mddev, 1);
2871 if (fd >= 0)
2872 err = bitmap_create(mddev);
2873 if (fd < 0 || err)
2874 bitmap_destroy(mddev);
2875 mddev->pers->quiesce(mddev, 0);
2876 } else if (fd < 0) {
2877 if (mddev->bitmap_file)
2878 fput(mddev->bitmap_file);
2879 mddev->bitmap_file = NULL;
2880 }
2881
NeilBrown32a76272005-06-21 17:17:14 -07002882 return err;
2883}
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885/*
2886 * set_array_info is used two different ways
2887 * The original usage is when creating a new array.
2888 * In this usage, raid_disks is > 0 and it together with
2889 * level, size, not_persistent,layout,chunksize determine the
2890 * shape of the array.
2891 * This will always create an array with a type-0.90.0 superblock.
2892 * The newer usage is when assembling an array.
2893 * In this case raid_disks will be 0, and the major_version field is
2894 * use to determine which style super-blocks are to be found on the devices.
2895 * The minor and patch _version numbers are also kept incase the
2896 * super_block handler wishes to interpret them.
2897 */
2898static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2899{
2900
2901 if (info->raid_disks == 0) {
2902 /* just setting version number for superblock loading */
2903 if (info->major_version < 0 ||
2904 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2905 super_types[info->major_version].name == NULL) {
2906 /* maybe try to auto-load a module? */
2907 printk(KERN_INFO
2908 "md: superblock version %d not known\n",
2909 info->major_version);
2910 return -EINVAL;
2911 }
2912 mddev->major_version = info->major_version;
2913 mddev->minor_version = info->minor_version;
2914 mddev->patch_version = info->patch_version;
2915 return 0;
2916 }
2917 mddev->major_version = MD_MAJOR_VERSION;
2918 mddev->minor_version = MD_MINOR_VERSION;
2919 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2920 mddev->ctime = get_seconds();
2921
2922 mddev->level = info->level;
2923 mddev->size = info->size;
2924 mddev->raid_disks = info->raid_disks;
2925 /* don't set md_minor, it is determined by which /dev/md* was
2926 * openned
2927 */
2928 if (info->state & (1<<MD_SB_CLEAN))
2929 mddev->recovery_cp = MaxSector;
2930 else
2931 mddev->recovery_cp = 0;
2932 mddev->persistent = ! info->not_persistent;
2933
2934 mddev->layout = info->layout;
2935 mddev->chunk_size = info->chunk_size;
2936
2937 mddev->max_disks = MD_SB_DISKS;
2938
2939 mddev->sb_dirty = 1;
2940
NeilBrownb2a27032005-11-28 13:44:12 -08002941 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
2942 mddev->bitmap_offset = 0;
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 /*
2945 * Generate a 128 bit UUID
2946 */
2947 get_random_bytes(mddev->uuid, 16);
2948
2949 return 0;
2950}
2951
2952/*
2953 * update_array_info is used to change the configuration of an
2954 * on-line array.
2955 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2956 * fields in the info are checked against the array.
2957 * Any differences that cannot be handled will cause an error.
2958 * Normally, only one change can be managed at a time.
2959 */
2960static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2961{
2962 int rv = 0;
2963 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07002964 int state = 0;
2965
2966 /* calculate expected state,ignoring low bits */
2967 if (mddev->bitmap && mddev->bitmap_offset)
2968 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 if (mddev->major_version != info->major_version ||
2971 mddev->minor_version != info->minor_version ||
2972/* mddev->patch_version != info->patch_version || */
2973 mddev->ctime != info->ctime ||
2974 mddev->level != info->level ||
2975/* mddev->layout != info->layout || */
2976 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07002977 mddev->chunk_size != info->chunk_size ||
2978 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2979 ((state^info->state) & 0xfffffe00)
2980 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return -EINVAL;
2982 /* Check there is only one change */
2983 if (mddev->size != info->size) cnt++;
2984 if (mddev->raid_disks != info->raid_disks) cnt++;
2985 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07002986 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 if (cnt == 0) return 0;
2988 if (cnt > 1) return -EINVAL;
2989
2990 if (mddev->layout != info->layout) {
2991 /* Change layout
2992 * we don't need to do anything at the md level, the
2993 * personality will take care of it all.
2994 */
2995 if (mddev->pers->reconfig == NULL)
2996 return -EINVAL;
2997 else
2998 return mddev->pers->reconfig(mddev, info->layout, -1);
2999 }
3000 if (mddev->size != info->size) {
3001 mdk_rdev_t * rdev;
3002 struct list_head *tmp;
3003 if (mddev->pers->resize == NULL)
3004 return -EINVAL;
3005 /* The "size" is the amount of each device that is used.
3006 * This can only make sense for arrays with redundancy.
3007 * linear and raid0 always use whatever space is available
3008 * We can only consider changing the size if no resync
3009 * or reconstruction is happening, and if the new size
3010 * is acceptable. It must fit before the sb_offset or,
3011 * if that is <data_offset, it must fit before the
3012 * size of each device.
3013 * If size is zero, we find the largest size that fits.
3014 */
3015 if (mddev->sync_thread)
3016 return -EBUSY;
3017 ITERATE_RDEV(mddev,rdev,tmp) {
3018 sector_t avail;
3019 int fit = (info->size == 0);
3020 if (rdev->sb_offset > rdev->data_offset)
3021 avail = (rdev->sb_offset*2) - rdev->data_offset;
3022 else
3023 avail = get_capacity(rdev->bdev->bd_disk)
3024 - rdev->data_offset;
3025 if (fit && (info->size == 0 || info->size > avail/2))
3026 info->size = avail/2;
3027 if (avail < ((sector_t)info->size << 1))
3028 return -ENOSPC;
3029 }
3030 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
3031 if (!rv) {
3032 struct block_device *bdev;
3033
3034 bdev = bdget_disk(mddev->gendisk, 0);
3035 if (bdev) {
3036 down(&bdev->bd_inode->i_sem);
3037 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3038 up(&bdev->bd_inode->i_sem);
3039 bdput(bdev);
3040 }
3041 }
3042 }
3043 if (mddev->raid_disks != info->raid_disks) {
3044 /* change the number of raid disks */
3045 if (mddev->pers->reshape == NULL)
3046 return -EINVAL;
3047 if (info->raid_disks <= 0 ||
3048 info->raid_disks >= mddev->max_disks)
3049 return -EINVAL;
3050 if (mddev->sync_thread)
3051 return -EBUSY;
3052 rv = mddev->pers->reshape(mddev, info->raid_disks);
3053 if (!rv) {
3054 struct block_device *bdev;
3055
3056 bdev = bdget_disk(mddev->gendisk, 0);
3057 if (bdev) {
3058 down(&bdev->bd_inode->i_sem);
3059 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3060 up(&bdev->bd_inode->i_sem);
3061 bdput(bdev);
3062 }
3063 }
3064 }
NeilBrown36fa3062005-09-09 16:23:45 -07003065 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3066 if (mddev->pers->quiesce == NULL)
3067 return -EINVAL;
3068 if (mddev->recovery || mddev->sync_thread)
3069 return -EBUSY;
3070 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3071 /* add the bitmap */
3072 if (mddev->bitmap)
3073 return -EEXIST;
3074 if (mddev->default_bitmap_offset == 0)
3075 return -EINVAL;
3076 mddev->bitmap_offset = mddev->default_bitmap_offset;
3077 mddev->pers->quiesce(mddev, 1);
3078 rv = bitmap_create(mddev);
3079 if (rv)
3080 bitmap_destroy(mddev);
3081 mddev->pers->quiesce(mddev, 0);
3082 } else {
3083 /* remove the bitmap */
3084 if (!mddev->bitmap)
3085 return -ENOENT;
3086 if (mddev->bitmap->file)
3087 return -EINVAL;
3088 mddev->pers->quiesce(mddev, 1);
3089 bitmap_destroy(mddev);
3090 mddev->pers->quiesce(mddev, 0);
3091 mddev->bitmap_offset = 0;
3092 }
3093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 md_update_sb(mddev);
3095 return rv;
3096}
3097
3098static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3099{
3100 mdk_rdev_t *rdev;
3101
3102 if (mddev->pers == NULL)
3103 return -ENODEV;
3104
3105 rdev = find_rdev(mddev, dev);
3106 if (!rdev)
3107 return -ENODEV;
3108
3109 md_error(mddev, rdev);
3110 return 0;
3111}
3112
3113static int md_ioctl(struct inode *inode, struct file *file,
3114 unsigned int cmd, unsigned long arg)
3115{
3116 int err = 0;
3117 void __user *argp = (void __user *)arg;
3118 struct hd_geometry __user *loc = argp;
3119 mddev_t *mddev = NULL;
3120
3121 if (!capable(CAP_SYS_ADMIN))
3122 return -EACCES;
3123
3124 /*
3125 * Commands dealing with the RAID driver but not any
3126 * particular array:
3127 */
3128 switch (cmd)
3129 {
3130 case RAID_VERSION:
3131 err = get_version(argp);
3132 goto done;
3133
3134 case PRINT_RAID_DEBUG:
3135 err = 0;
3136 md_print_devices();
3137 goto done;
3138
3139#ifndef MODULE
3140 case RAID_AUTORUN:
3141 err = 0;
3142 autostart_arrays(arg);
3143 goto done;
3144#endif
3145 default:;
3146 }
3147
3148 /*
3149 * Commands creating/starting a new array:
3150 */
3151
3152 mddev = inode->i_bdev->bd_disk->private_data;
3153
3154 if (!mddev) {
3155 BUG();
3156 goto abort;
3157 }
3158
3159
3160 if (cmd == START_ARRAY) {
3161 /* START_ARRAY doesn't need to lock the array as autostart_array
3162 * does the locking, and it could even be a different array
3163 */
3164 static int cnt = 3;
3165 if (cnt > 0 ) {
3166 printk(KERN_WARNING
3167 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08003168 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 current->comm, current->pid);
3170 cnt--;
3171 }
3172 err = autostart_array(new_decode_dev(arg));
3173 if (err) {
3174 printk(KERN_WARNING "md: autostart failed!\n");
3175 goto abort;
3176 }
3177 goto done;
3178 }
3179
3180 err = mddev_lock(mddev);
3181 if (err) {
3182 printk(KERN_INFO
3183 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3184 err, cmd);
3185 goto abort;
3186 }
3187
3188 switch (cmd)
3189 {
3190 case SET_ARRAY_INFO:
3191 {
3192 mdu_array_info_t info;
3193 if (!arg)
3194 memset(&info, 0, sizeof(info));
3195 else if (copy_from_user(&info, argp, sizeof(info))) {
3196 err = -EFAULT;
3197 goto abort_unlock;
3198 }
3199 if (mddev->pers) {
3200 err = update_array_info(mddev, &info);
3201 if (err) {
3202 printk(KERN_WARNING "md: couldn't update"
3203 " array info. %d\n", err);
3204 goto abort_unlock;
3205 }
3206 goto done_unlock;
3207 }
3208 if (!list_empty(&mddev->disks)) {
3209 printk(KERN_WARNING
3210 "md: array %s already has disks!\n",
3211 mdname(mddev));
3212 err = -EBUSY;
3213 goto abort_unlock;
3214 }
3215 if (mddev->raid_disks) {
3216 printk(KERN_WARNING
3217 "md: array %s already initialised!\n",
3218 mdname(mddev));
3219 err = -EBUSY;
3220 goto abort_unlock;
3221 }
3222 err = set_array_info(mddev, &info);
3223 if (err) {
3224 printk(KERN_WARNING "md: couldn't set"
3225 " array info. %d\n", err);
3226 goto abort_unlock;
3227 }
3228 }
3229 goto done_unlock;
3230
3231 default:;
3232 }
3233
3234 /*
3235 * Commands querying/configuring an existing array:
3236 */
NeilBrown32a76272005-06-21 17:17:14 -07003237 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3238 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3239 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3240 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 err = -ENODEV;
3242 goto abort_unlock;
3243 }
3244
3245 /*
3246 * Commands even a read-only array can execute:
3247 */
3248 switch (cmd)
3249 {
3250 case GET_ARRAY_INFO:
3251 err = get_array_info(mddev, argp);
3252 goto done_unlock;
3253
NeilBrown32a76272005-06-21 17:17:14 -07003254 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003255 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003256 goto done_unlock;
3257
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 case GET_DISK_INFO:
3259 err = get_disk_info(mddev, argp);
3260 goto done_unlock;
3261
3262 case RESTART_ARRAY_RW:
3263 err = restart_array(mddev);
3264 goto done_unlock;
3265
3266 case STOP_ARRAY:
3267 err = do_md_stop (mddev, 0);
3268 goto done_unlock;
3269
3270 case STOP_ARRAY_RO:
3271 err = do_md_stop (mddev, 1);
3272 goto done_unlock;
3273
3274 /*
3275 * We have a problem here : there is no easy way to give a CHS
3276 * virtual geometry. We currently pretend that we have a 2 heads
3277 * 4 sectors (with a BIG number of cylinders...). This drives
3278 * dosfs just mad... ;-)
3279 */
3280 case HDIO_GETGEO:
3281 if (!loc) {
3282 err = -EINVAL;
3283 goto abort_unlock;
3284 }
3285 err = put_user (2, (char __user *) &loc->heads);
3286 if (err)
3287 goto abort_unlock;
3288 err = put_user (4, (char __user *) &loc->sectors);
3289 if (err)
3290 goto abort_unlock;
3291 err = put_user(get_capacity(mddev->gendisk)/8,
3292 (short __user *) &loc->cylinders);
3293 if (err)
3294 goto abort_unlock;
3295 err = put_user (get_start_sect(inode->i_bdev),
3296 (long __user *) &loc->start);
3297 goto done_unlock;
3298 }
3299
3300 /*
3301 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08003302 * superblock, so we do not allow them on read-only arrays.
3303 * However non-MD ioctls (e.g. get-size) will still come through
3304 * here and hit the 'default' below, so only disallow
3305 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 */
NeilBrownf91de922005-11-08 21:39:36 -08003307 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3308 mddev->ro && mddev->pers) {
3309 if (mddev->ro == 2) {
3310 mddev->ro = 0;
3311 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3312 md_wakeup_thread(mddev->thread);
3313
3314 } else {
3315 err = -EROFS;
3316 goto abort_unlock;
3317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 }
3319
3320 switch (cmd)
3321 {
3322 case ADD_NEW_DISK:
3323 {
3324 mdu_disk_info_t info;
3325 if (copy_from_user(&info, argp, sizeof(info)))
3326 err = -EFAULT;
3327 else
3328 err = add_new_disk(mddev, &info);
3329 goto done_unlock;
3330 }
3331
3332 case HOT_REMOVE_DISK:
3333 err = hot_remove_disk(mddev, new_decode_dev(arg));
3334 goto done_unlock;
3335
3336 case HOT_ADD_DISK:
3337 err = hot_add_disk(mddev, new_decode_dev(arg));
3338 goto done_unlock;
3339
3340 case SET_DISK_FAULTY:
3341 err = set_disk_faulty(mddev, new_decode_dev(arg));
3342 goto done_unlock;
3343
3344 case RUN_ARRAY:
3345 err = do_md_run (mddev);
3346 goto done_unlock;
3347
NeilBrown32a76272005-06-21 17:17:14 -07003348 case SET_BITMAP_FILE:
3349 err = set_bitmap_file(mddev, (int)arg);
3350 goto done_unlock;
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 default:
3353 if (_IOC_TYPE(cmd) == MD_MAJOR)
3354 printk(KERN_WARNING "md: %s(pid %d) used"
3355 " obsolete MD ioctl, upgrade your"
3356 " software to use new ictls.\n",
3357 current->comm, current->pid);
3358 err = -EINVAL;
3359 goto abort_unlock;
3360 }
3361
3362done_unlock:
3363abort_unlock:
3364 mddev_unlock(mddev);
3365
3366 return err;
3367done:
3368 if (err)
3369 MD_BUG();
3370abort:
3371 return err;
3372}
3373
3374static int md_open(struct inode *inode, struct file *file)
3375{
3376 /*
3377 * Succeed if we can lock the mddev, which confirms that
3378 * it isn't being stopped right now.
3379 */
3380 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3381 int err;
3382
3383 if ((err = mddev_lock(mddev)))
3384 goto out;
3385
3386 err = 0;
3387 mddev_get(mddev);
3388 mddev_unlock(mddev);
3389
3390 check_disk_change(inode->i_bdev);
3391 out:
3392 return err;
3393}
3394
3395static int md_release(struct inode *inode, struct file * file)
3396{
3397 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3398
3399 if (!mddev)
3400 BUG();
3401 mddev_put(mddev);
3402
3403 return 0;
3404}
3405
3406static int md_media_changed(struct gendisk *disk)
3407{
3408 mddev_t *mddev = disk->private_data;
3409
3410 return mddev->changed;
3411}
3412
3413static int md_revalidate(struct gendisk *disk)
3414{
3415 mddev_t *mddev = disk->private_data;
3416
3417 mddev->changed = 0;
3418 return 0;
3419}
3420static struct block_device_operations md_fops =
3421{
3422 .owner = THIS_MODULE,
3423 .open = md_open,
3424 .release = md_release,
3425 .ioctl = md_ioctl,
3426 .media_changed = md_media_changed,
3427 .revalidate_disk= md_revalidate,
3428};
3429
Adrian Bunk75c96f82005-05-05 16:16:09 -07003430static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
3432 mdk_thread_t *thread = arg;
3433
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 /*
3435 * md_thread is a 'system-thread', it's priority should be very
3436 * high. We avoid resource deadlocks individually in each
3437 * raid personality. (RAID5 does preallocation) We also use RR and
3438 * the very same RT priority as kswapd, thus we will never get
3439 * into a priority inversion deadlock.
3440 *
3441 * we definitely have to have equal or higher priority than
3442 * bdflush, otherwise bdflush will deadlock if there are too
3443 * many dirty RAID5 blocks.
3444 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
NeilBrown6985c432005-10-19 21:23:47 -07003446 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07003447 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
NeilBrown93588e22005-11-15 00:09:12 -08003449 /* We need to wait INTERRUPTIBLE so that
3450 * we don't add to the load-average.
3451 * That means we need to be sure no signals are
3452 * pending
3453 */
3454 if (signal_pending(current))
3455 flush_signals(current);
3456
3457 wait_event_interruptible_timeout
3458 (thread->wqueue,
3459 test_bit(THREAD_WAKEUP, &thread->flags)
3460 || kthread_should_stop(),
3461 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003462 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
3464 clear_bit(THREAD_WAKEUP, &thread->flags);
3465
NeilBrown787453c2005-11-08 21:39:43 -08003466 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
NeilBrowna6fb0932005-09-09 16:23:56 -07003468
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 return 0;
3470}
3471
3472void md_wakeup_thread(mdk_thread_t *thread)
3473{
3474 if (thread) {
3475 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3476 set_bit(THREAD_WAKEUP, &thread->flags);
3477 wake_up(&thread->wqueue);
3478 }
3479}
3480
3481mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3482 const char *name)
3483{
3484 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
NeilBrowna6fb0932005-09-09 16:23:56 -07003486 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 if (!thread)
3488 return NULL;
3489
3490 memset(thread, 0, sizeof(mdk_thread_t));
3491 init_waitqueue_head(&thread->wqueue);
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 thread->run = run;
3494 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07003495 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07003496 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07003497 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 kfree(thread);
3499 return NULL;
3500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 return thread;
3502}
3503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504void md_unregister_thread(mdk_thread_t *thread)
3505{
NeilBrownd28446f2005-04-16 15:26:41 -07003506 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07003507
3508 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 kfree(thread);
3510}
3511
3512void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3513{
3514 if (!mddev) {
3515 MD_BUG();
3516 return;
3517 }
3518
NeilBrownb2d444d2005-11-08 21:39:31 -08003519 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 return;
NeilBrown32a76272005-06-21 17:17:14 -07003521/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3523 mdname(mddev),
3524 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3525 __builtin_return_address(0),__builtin_return_address(1),
3526 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07003527*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 if (!mddev->pers->error_handler)
3529 return;
3530 mddev->pers->error_handler(mddev,rdev);
3531 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3532 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3533 md_wakeup_thread(mddev->thread);
3534}
3535
3536/* seq_file implementation /proc/mdstat */
3537
3538static void status_unused(struct seq_file *seq)
3539{
3540 int i = 0;
3541 mdk_rdev_t *rdev;
3542 struct list_head *tmp;
3543
3544 seq_printf(seq, "unused devices: ");
3545
3546 ITERATE_RDEV_PENDING(rdev,tmp) {
3547 char b[BDEVNAME_SIZE];
3548 i++;
3549 seq_printf(seq, "%s ",
3550 bdevname(rdev->bdev,b));
3551 }
3552 if (!i)
3553 seq_printf(seq, "<none>");
3554
3555 seq_printf(seq, "\n");
3556}
3557
3558
3559static void status_resync(struct seq_file *seq, mddev_t * mddev)
3560{
3561 unsigned long max_blocks, resync, res, dt, db, rt;
3562
3563 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3564
3565 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3566 max_blocks = mddev->resync_max_sectors >> 1;
3567 else
3568 max_blocks = mddev->size;
3569
3570 /*
3571 * Should not happen.
3572 */
3573 if (!max_blocks) {
3574 MD_BUG();
3575 return;
3576 }
3577 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3578 {
3579 int i, x = res/50, y = 20-x;
3580 seq_printf(seq, "[");
3581 for (i = 0; i < x; i++)
3582 seq_printf(seq, "=");
3583 seq_printf(seq, ">");
3584 for (i = 0; i < y; i++)
3585 seq_printf(seq, ".");
3586 seq_printf(seq, "] ");
3587 }
3588 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3589 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3590 "resync" : "recovery"),
3591 res/10, res % 10, resync, max_blocks);
3592
3593 /*
3594 * We do not want to overflow, so the order of operands and
3595 * the * 100 / 100 trick are important. We do a +1 to be
3596 * safe against division by zero. We only estimate anyway.
3597 *
3598 * dt: time from mark until now
3599 * db: blocks written from mark until now
3600 * rt: remaining time
3601 */
3602 dt = ((jiffies - mddev->resync_mark) / HZ);
3603 if (!dt) dt++;
3604 db = resync - (mddev->resync_mark_cnt/2);
3605 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3606
3607 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3608
3609 seq_printf(seq, " speed=%ldK/sec", db/dt);
3610}
3611
3612static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3613{
3614 struct list_head *tmp;
3615 loff_t l = *pos;
3616 mddev_t *mddev;
3617
3618 if (l >= 0x10000)
3619 return NULL;
3620 if (!l--)
3621 /* header */
3622 return (void*)1;
3623
3624 spin_lock(&all_mddevs_lock);
3625 list_for_each(tmp,&all_mddevs)
3626 if (!l--) {
3627 mddev = list_entry(tmp, mddev_t, all_mddevs);
3628 mddev_get(mddev);
3629 spin_unlock(&all_mddevs_lock);
3630 return mddev;
3631 }
3632 spin_unlock(&all_mddevs_lock);
3633 if (!l--)
3634 return (void*)2;/* tail */
3635 return NULL;
3636}
3637
3638static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3639{
3640 struct list_head *tmp;
3641 mddev_t *next_mddev, *mddev = v;
3642
3643 ++*pos;
3644 if (v == (void*)2)
3645 return NULL;
3646
3647 spin_lock(&all_mddevs_lock);
3648 if (v == (void*)1)
3649 tmp = all_mddevs.next;
3650 else
3651 tmp = mddev->all_mddevs.next;
3652 if (tmp != &all_mddevs)
3653 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3654 else {
3655 next_mddev = (void*)2;
3656 *pos = 0x10000;
3657 }
3658 spin_unlock(&all_mddevs_lock);
3659
3660 if (v != (void*)1)
3661 mddev_put(mddev);
3662 return next_mddev;
3663
3664}
3665
3666static void md_seq_stop(struct seq_file *seq, void *v)
3667{
3668 mddev_t *mddev = v;
3669
3670 if (mddev && v != (void*)1 && v != (void*)2)
3671 mddev_put(mddev);
3672}
3673
3674static int md_seq_show(struct seq_file *seq, void *v)
3675{
3676 mddev_t *mddev = v;
3677 sector_t size;
3678 struct list_head *tmp2;
3679 mdk_rdev_t *rdev;
3680 int i;
NeilBrown32a76272005-06-21 17:17:14 -07003681 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
3683 if (v == (void*)1) {
3684 seq_printf(seq, "Personalities : ");
3685 spin_lock(&pers_lock);
3686 for (i = 0; i < MAX_PERSONALITY; i++)
3687 if (pers[i])
3688 seq_printf(seq, "[%s] ", pers[i]->name);
3689
3690 spin_unlock(&pers_lock);
3691 seq_printf(seq, "\n");
3692 return 0;
3693 }
3694 if (v == (void*)2) {
3695 status_unused(seq);
3696 return 0;
3697 }
3698
3699 if (mddev_lock(mddev)!=0)
3700 return -EINTR;
3701 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3702 seq_printf(seq, "%s : %sactive", mdname(mddev),
3703 mddev->pers ? "" : "in");
3704 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08003705 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08003707 if (mddev->ro==2)
3708 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 seq_printf(seq, " %s", mddev->pers->name);
3710 }
3711
3712 size = 0;
3713 ITERATE_RDEV(mddev,rdev,tmp2) {
3714 char b[BDEVNAME_SIZE];
3715 seq_printf(seq, " %s[%d]",
3716 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003717 if (test_bit(WriteMostly, &rdev->flags))
3718 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08003719 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 seq_printf(seq, "(F)");
3721 continue;
NeilBrownb325a322005-09-09 16:24:00 -07003722 } else if (rdev->raid_disk < 0)
3723 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 size += rdev->size;
3725 }
3726
3727 if (!list_empty(&mddev->disks)) {
3728 if (mddev->pers)
3729 seq_printf(seq, "\n %llu blocks",
3730 (unsigned long long)mddev->array_size);
3731 else
3732 seq_printf(seq, "\n %llu blocks",
3733 (unsigned long long)size);
3734 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07003735 if (mddev->persistent) {
3736 if (mddev->major_version != 0 ||
3737 mddev->minor_version != 90) {
3738 seq_printf(seq," super %d.%d",
3739 mddev->major_version,
3740 mddev->minor_version);
3741 }
3742 } else
3743 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
3745 if (mddev->pers) {
3746 mddev->pers->status (seq, mddev);
3747 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08003748 if (mddev->pers->sync_request) {
3749 if (mddev->curr_resync > 2) {
3750 status_resync (seq, mddev);
3751 seq_printf(seq, "\n ");
3752 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3753 seq_printf(seq, "\tresync=DELAYED\n ");
3754 else if (mddev->recovery_cp < MaxSector)
3755 seq_printf(seq, "\tresync=PENDING\n ");
3756 }
NeilBrown32a76272005-06-21 17:17:14 -07003757 } else
3758 seq_printf(seq, "\n ");
3759
3760 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07003761 unsigned long chunk_kb;
3762 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07003763 spin_lock_irqsave(&bitmap->lock, flags);
3764 chunk_kb = bitmap->chunksize >> 10;
3765 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3766 "%lu%s chunk",
3767 bitmap->pages - bitmap->missing_pages,
3768 bitmap->pages,
3769 (bitmap->pages - bitmap->missing_pages)
3770 << (PAGE_SHIFT - 10),
3771 chunk_kb ? chunk_kb : bitmap->chunksize,
3772 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07003773 if (bitmap->file) {
3774 seq_printf(seq, ", file: ");
3775 seq_path(seq, bitmap->file->f_vfsmnt,
3776 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07003777 }
NeilBrown78d742d2005-06-21 17:17:15 -07003778
NeilBrown32a76272005-06-21 17:17:14 -07003779 seq_printf(seq, "\n");
3780 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 }
3782
3783 seq_printf(seq, "\n");
3784 }
3785 mddev_unlock(mddev);
3786
3787 return 0;
3788}
3789
3790static struct seq_operations md_seq_ops = {
3791 .start = md_seq_start,
3792 .next = md_seq_next,
3793 .stop = md_seq_stop,
3794 .show = md_seq_show,
3795};
3796
3797static int md_seq_open(struct inode *inode, struct file *file)
3798{
3799 int error;
3800
3801 error = seq_open(file, &md_seq_ops);
3802 return error;
3803}
3804
3805static struct file_operations md_seq_fops = {
3806 .open = md_seq_open,
3807 .read = seq_read,
3808 .llseek = seq_lseek,
3809 .release = seq_release,
3810};
3811
3812int register_md_personality(int pnum, mdk_personality_t *p)
3813{
3814 if (pnum >= MAX_PERSONALITY) {
3815 printk(KERN_ERR
3816 "md: tried to install personality %s as nr %d, but max is %lu\n",
3817 p->name, pnum, MAX_PERSONALITY-1);
3818 return -EINVAL;
3819 }
3820
3821 spin_lock(&pers_lock);
3822 if (pers[pnum]) {
3823 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 return -EBUSY;
3825 }
3826
3827 pers[pnum] = p;
3828 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3829 spin_unlock(&pers_lock);
3830 return 0;
3831}
3832
3833int unregister_md_personality(int pnum)
3834{
NeilBrowna757e642005-04-16 15:26:42 -07003835 if (pnum >= MAX_PERSONALITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
3838 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3839 spin_lock(&pers_lock);
3840 pers[pnum] = NULL;
3841 spin_unlock(&pers_lock);
3842 return 0;
3843}
3844
3845static int is_mddev_idle(mddev_t *mddev)
3846{
3847 mdk_rdev_t * rdev;
3848 struct list_head *tmp;
3849 int idle;
3850 unsigned long curr_events;
3851
3852 idle = 1;
3853 ITERATE_RDEV(mddev,rdev,tmp) {
3854 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01003855 curr_events = disk_stat_read(disk, sectors[0]) +
3856 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08003858 /* The difference between curr_events and last_events
3859 * will be affected by any new non-sync IO (making
3860 * curr_events bigger) and any difference in the amount of
3861 * in-flight syncio (making current_events bigger or smaller)
3862 * The amount in-flight is currently limited to
3863 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
3864 * which is at most 4096 sectors.
3865 * These numbers are fairly fragile and should be made
3866 * more robust, probably by enforcing the
3867 * 'window size' that md_do_sync sort-of uses.
3868 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 * Note: the following is an unsigned comparison.
3870 */
NeilBrownc0e48522005-11-18 01:11:01 -08003871 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 rdev->last_events = curr_events;
3873 idle = 0;
3874 }
3875 }
3876 return idle;
3877}
3878
3879void md_done_sync(mddev_t *mddev, int blocks, int ok)
3880{
3881 /* another "blocks" (512byte) blocks have been synced */
3882 atomic_sub(blocks, &mddev->recovery_active);
3883 wake_up(&mddev->recovery_wait);
3884 if (!ok) {
3885 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3886 md_wakeup_thread(mddev->thread);
3887 // stop recovery, signal do_sync ....
3888 }
3889}
3890
3891
NeilBrown06d91a52005-06-21 17:17:12 -07003892/* md_write_start(mddev, bi)
3893 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07003894 * in superblock) before writing, schedule a superblock update
3895 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07003896 */
NeilBrown3d310eb2005-06-21 17:17:26 -07003897void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898{
NeilBrown06d91a52005-06-21 17:17:12 -07003899 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07003900 return;
NeilBrown06d91a52005-06-21 17:17:12 -07003901
NeilBrownf91de922005-11-08 21:39:36 -08003902 BUG_ON(mddev->ro == 1);
3903 if (mddev->ro == 2) {
3904 /* need to switch to read/write */
3905 mddev->ro = 0;
3906 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3907 md_wakeup_thread(mddev->thread);
3908 }
NeilBrown06d91a52005-06-21 17:17:12 -07003909 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07003910 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08003911 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07003912 if (mddev->in_sync) {
3913 mddev->in_sync = 0;
3914 mddev->sb_dirty = 1;
3915 md_wakeup_thread(mddev->thread);
3916 }
NeilBrowna9701a32005-11-08 21:39:34 -08003917 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07003918 }
NeilBrown3d310eb2005-06-21 17:17:26 -07003919 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920}
3921
3922void md_write_end(mddev_t *mddev)
3923{
3924 if (atomic_dec_and_test(&mddev->writes_pending)) {
3925 if (mddev->safemode == 2)
3926 md_wakeup_thread(mddev->thread);
3927 else
3928 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3929 }
3930}
3931
Adrian Bunk75c96f82005-05-05 16:16:09 -07003932static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
3934#define SYNC_MARKS 10
3935#define SYNC_MARK_STEP (3*HZ)
3936static void md_do_sync(mddev_t *mddev)
3937{
3938 mddev_t *mddev2;
3939 unsigned int currspeed = 0,
3940 window;
NeilBrown57afd892005-06-21 17:17:13 -07003941 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 unsigned long mark[SYNC_MARKS];
3943 sector_t mark_cnt[SYNC_MARKS];
3944 int last_mark,m;
3945 struct list_head *tmp;
3946 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07003947 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
3949 /* just incase thread restarts... */
3950 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3951 return;
3952
3953 /* we overload curr_resync somewhat here.
3954 * 0 == not engaged in resync at all
3955 * 2 == checking that there is no conflict with another sync
3956 * 1 == like 2, but have yielded to allow conflicting resync to
3957 * commense
3958 * other == active in resync - this many blocks
3959 *
3960 * Before starting a resync we must have set curr_resync to
3961 * 2, and then checked that every "conflicting" array has curr_resync
3962 * less than ours. When we find one that is the same or higher
3963 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3964 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3965 * This will mean we have to start checking from the beginning again.
3966 *
3967 */
3968
3969 do {
3970 mddev->curr_resync = 2;
3971
3972 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08003973 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07003974 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 goto skip;
3976 }
3977 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 if (mddev2 == mddev)
3979 continue;
3980 if (mddev2->curr_resync &&
3981 match_mddev_units(mddev,mddev2)) {
3982 DEFINE_WAIT(wq);
3983 if (mddev < mddev2 && mddev->curr_resync == 2) {
3984 /* arbitrarily yield */
3985 mddev->curr_resync = 1;
3986 wake_up(&resync_wait);
3987 }
3988 if (mddev > mddev2 && mddev->curr_resync == 1)
3989 /* no need to wait here, we can wait the next
3990 * time 'round when curr_resync == 2
3991 */
3992 continue;
NeilBrown787453c2005-11-08 21:39:43 -08003993 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
3994 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07003995 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 printk(KERN_INFO "md: delaying resync of %s"
3997 " until %s has finished resync (they"
3998 " share one or more physical units)\n",
3999 mdname(mddev), mdname(mddev2));
4000 mddev_put(mddev2);
4001 schedule();
4002 finish_wait(&resync_wait, &wq);
4003 goto try_again;
4004 }
4005 finish_wait(&resync_wait, &wq);
4006 }
4007 }
4008 } while (mddev->curr_resync < 2);
4009
NeilBrown9d888832005-11-08 21:39:26 -08004010 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07004012 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 */
4014 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08004015 mddev->resync_mismatches = 0;
4016 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 /* recovery follows the physical size of devices */
4018 max_sectors = mddev->size << 1;
4019
4020 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4021 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
4022 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
Adrian Bunk338cec32005-09-10 00:26:54 -07004023 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 "(but not more than %d KB/sec) for reconstruction.\n",
4025 sysctl_speed_limit_max);
4026
4027 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07004028 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08004029 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4030 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 j = mddev->recovery_cp;
4032 else
4033 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07004034 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 for (m = 0; m < SYNC_MARKS; m++) {
4036 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004037 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 }
4039 last_mark = 0;
4040 mddev->resync_mark = mark[last_mark];
4041 mddev->resync_mark_cnt = mark_cnt[last_mark];
4042
4043 /*
4044 * Tune reconstruction:
4045 */
4046 window = 32*(PAGE_SIZE/512);
4047 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4048 window/2,(unsigned long long) max_sectors/2);
4049
4050 atomic_set(&mddev->recovery_active, 0);
4051 init_waitqueue_head(&mddev->recovery_wait);
4052 last_check = 0;
4053
4054 if (j>2) {
4055 printk(KERN_INFO
4056 "md: resuming recovery of %s from checkpoint.\n",
4057 mdname(mddev));
4058 mddev->curr_resync = j;
4059 }
4060
4061 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07004062 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
NeilBrown57afd892005-06-21 17:17:13 -07004064 skipped = 0;
4065 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4066 currspeed < sysctl_speed_limit_min);
4067 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4069 goto out;
4070 }
NeilBrown57afd892005-06-21 17:17:13 -07004071
4072 if (!skipped) { /* actual IO requested */
4073 io_sectors += sectors;
4074 atomic_add(sectors, &mddev->recovery_active);
4075 }
4076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 j += sectors;
4078 if (j>1) mddev->curr_resync = j;
4079
NeilBrown57afd892005-06-21 17:17:13 -07004080
4081 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 continue;
4083
NeilBrown57afd892005-06-21 17:17:13 -07004084 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
4086 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4087 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4088 break;
4089
4090 repeat:
4091 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4092 /* step marks */
4093 int next = (last_mark+1) % SYNC_MARKS;
4094
4095 mddev->resync_mark = mark[next];
4096 mddev->resync_mark_cnt = mark_cnt[next];
4097 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004098 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 last_mark = next;
4100 }
4101
4102
NeilBrown787453c2005-11-08 21:39:43 -08004103 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 /*
4105 * got a signal, exit.
4106 */
4107 printk(KERN_INFO
4108 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4110 goto out;
4111 }
4112
4113 /*
4114 * this loop exits only if either when we are slower than
4115 * the 'hard' speed limit, or the system was IO-idle for
4116 * a jiffy.
4117 * the system might be non-idle CPU-wise, but we only care
4118 * about not overloading the IO subsystem. (things like an
4119 * e2fsck being done on the RAID array should execute fast)
4120 */
4121 mddev->queue->unplug_fn(mddev->queue);
4122 cond_resched();
4123
NeilBrown57afd892005-06-21 17:17:13 -07004124 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4125 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
4127 if (currspeed > sysctl_speed_limit_min) {
4128 if ((currspeed > sysctl_speed_limit_max) ||
4129 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08004130 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 goto repeat;
4132 }
4133 }
4134 }
4135 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4136 /*
4137 * this also signals 'finished resyncing' to md_stop
4138 */
4139 out:
4140 mddev->queue->unplug_fn(mddev->queue);
4141
4142 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4143
4144 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004145 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
4147 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4148 mddev->curr_resync > 2 &&
4149 mddev->curr_resync >= mddev->recovery_cp) {
4150 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4151 printk(KERN_INFO
4152 "md: checkpointing recovery of %s.\n",
4153 mdname(mddev));
4154 mddev->recovery_cp = mddev->curr_resync;
4155 } else
4156 mddev->recovery_cp = MaxSector;
4157 }
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 skip:
4160 mddev->curr_resync = 0;
4161 wake_up(&resync_wait);
4162 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4163 md_wakeup_thread(mddev->thread);
4164}
4165
4166
4167/*
4168 * This routine is regularly called by all per-raid-array threads to
4169 * deal with generic issues like resync and super-block update.
4170 * Raid personalities that don't have a thread (linear/raid0) do not
4171 * need this as they never do any recovery or update the superblock.
4172 *
4173 * It does not do any resync itself, but rather "forks" off other threads
4174 * to do that as needed.
4175 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4176 * "->recovery" and create a thread at ->sync_thread.
4177 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4178 * and wakeups up this thread which will reap the thread and finish up.
4179 * This thread also removes any faulty devices (with nr_pending == 0).
4180 *
4181 * The overall approach is:
4182 * 1/ if the superblock needs updating, update it.
4183 * 2/ If a recovery thread is running, don't do anything else.
4184 * 3/ If recovery has finished, clean up, possibly marking spares active.
4185 * 4/ If there are any faulty devices, remove them.
4186 * 5/ If array is degraded, try to add spares devices
4187 * 6/ If array has spares or is not in-sync, start a resync thread.
4188 */
4189void md_check_recovery(mddev_t *mddev)
4190{
4191 mdk_rdev_t *rdev;
4192 struct list_head *rtmp;
4193
4194
NeilBrown5f404022005-06-21 17:17:16 -07004195 if (mddev->bitmap)
4196 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
4198 if (mddev->ro)
4199 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004200
4201 if (signal_pending(current)) {
4202 if (mddev->pers->sync_request) {
4203 printk(KERN_INFO "md: %s in immediate safe mode\n",
4204 mdname(mddev));
4205 mddev->safemode = 2;
4206 }
4207 flush_signals(current);
4208 }
4209
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 if ( ! (
4211 mddev->sb_dirty ||
4212 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004213 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4214 (mddev->safemode == 1) ||
4215 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4216 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 ))
4218 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004219
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 if (mddev_trylock(mddev)==0) {
4221 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004222
NeilBrowna9701a32005-11-08 21:39:34 -08004223 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004224 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4225 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4226 mddev->in_sync = 1;
4227 mddev->sb_dirty = 1;
4228 }
4229 if (mddev->safemode == 1)
4230 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08004231 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 if (mddev->sb_dirty)
4234 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004235
NeilBrown06d91a52005-06-21 17:17:12 -07004236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4238 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4239 /* resync/recovery still happening */
4240 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4241 goto unlock;
4242 }
4243 if (mddev->sync_thread) {
4244 /* resync has finished, collect result */
4245 md_unregister_thread(mddev->sync_thread);
4246 mddev->sync_thread = NULL;
4247 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4248 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4249 /* success...*/
4250 /* activate any spares */
4251 mddev->pers->spare_active(mddev);
4252 }
4253 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004254
4255 /* if array is no-longer degraded, then any saved_raid_disk
4256 * information must be scrapped
4257 */
4258 if (!mddev->degraded)
4259 ITERATE_RDEV(mddev,rdev,rtmp)
4260 rdev->saved_raid_disk = -1;
4261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 mddev->recovery = 0;
4263 /* flag recovery needed just to double check */
4264 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4265 goto unlock;
4266 }
NeilBrown24dd4692005-11-08 21:39:26 -08004267 /* Clear some bits that don't mean anything, but
4268 * might be left set
4269 */
4270 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4271 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4272 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4273 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
4275 /* no recovery is running.
4276 * remove any failed drives, then
4277 * add spares if possible.
4278 * Spare are also removed and re-added, to allow
4279 * the personality to fail the re-add.
4280 */
4281 ITERATE_RDEV(mddev,rdev,rtmp)
4282 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004283 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004285 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4286 char nm[20];
4287 sprintf(nm,"rd%d", rdev->raid_disk);
4288 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 }
4292
4293 if (mddev->degraded) {
4294 ITERATE_RDEV(mddev,rdev,rtmp)
4295 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08004296 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004297 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4298 char nm[20];
4299 sprintf(nm, "rd%d", rdev->raid_disk);
4300 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 spares++;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004302 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 break;
4304 }
4305 }
4306
NeilBrown24dd4692005-11-08 21:39:26 -08004307 if (spares) {
4308 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4309 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4310 } else if (mddev->recovery_cp < MaxSector) {
4311 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4312 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4313 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08004315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 if (mddev->pers->sync_request) {
4317 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07004318 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4319 /* We are adding a device or devices to an array
4320 * which has the bitmap stored on all devices.
4321 * So make sure all bitmap pages get written
4322 */
4323 bitmap_write_all(mddev->bitmap);
4324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 mddev->sync_thread = md_register_thread(md_do_sync,
4326 mddev,
4327 "%s_resync");
4328 if (!mddev->sync_thread) {
4329 printk(KERN_ERR "%s: could not start resync"
4330 " thread...\n",
4331 mdname(mddev));
4332 /* leave the spares where they are, it shouldn't hurt */
4333 mddev->recovery = 0;
4334 } else {
4335 md_wakeup_thread(mddev->sync_thread);
4336 }
4337 }
4338 unlock:
4339 mddev_unlock(mddev);
4340 }
4341}
4342
Adrian Bunk75c96f82005-05-05 16:16:09 -07004343static int md_notify_reboot(struct notifier_block *this,
4344 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
4346 struct list_head *tmp;
4347 mddev_t *mddev;
4348
4349 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4350
4351 printk(KERN_INFO "md: stopping all md devices.\n");
4352
4353 ITERATE_MDDEV(mddev,tmp)
4354 if (mddev_trylock(mddev)==0)
4355 do_md_stop (mddev, 1);
4356 /*
4357 * certain more exotic SCSI devices are known to be
4358 * volatile wrt too early system reboots. While the
4359 * right place to handle this issue is the given
4360 * driver, we do want to have a safe RAID driver ...
4361 */
4362 mdelay(1000*1);
4363 }
4364 return NOTIFY_DONE;
4365}
4366
Adrian Bunk75c96f82005-05-05 16:16:09 -07004367static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 .notifier_call = md_notify_reboot,
4369 .next = NULL,
4370 .priority = INT_MAX, /* before any real devices */
4371};
4372
4373static void md_geninit(void)
4374{
4375 struct proc_dir_entry *p;
4376
4377 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4378
4379 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4380 if (p)
4381 p->proc_fops = &md_seq_fops;
4382}
4383
Adrian Bunk75c96f82005-05-05 16:16:09 -07004384static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
4386 int minor;
4387
4388 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4389 " MD_SB_DISKS=%d\n",
4390 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4391 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08004392 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07004393 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
4395 if (register_blkdev(MAJOR_NR, "md"))
4396 return -1;
4397 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4398 unregister_blkdev(MAJOR_NR, "md");
4399 return -1;
4400 }
4401 devfs_mk_dir("md");
4402 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4403 md_probe, NULL, NULL);
4404 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4405 md_probe, NULL, NULL);
4406
4407 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4408 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4409 S_IFBLK|S_IRUSR|S_IWUSR,
4410 "md/%d", minor);
4411
4412 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4413 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4414 S_IFBLK|S_IRUSR|S_IWUSR,
4415 "md/mdp%d", minor);
4416
4417
4418 register_reboot_notifier(&md_notifier);
4419 raid_table_header = register_sysctl_table(raid_root_table, 1);
4420
4421 md_geninit();
4422 return (0);
4423}
4424
4425
4426#ifndef MODULE
4427
4428/*
4429 * Searches all registered partitions for autorun RAID arrays
4430 * at boot time.
4431 */
4432static dev_t detected_devices[128];
4433static int dev_cnt;
4434
4435void md_autodetect_dev(dev_t dev)
4436{
4437 if (dev_cnt >= 0 && dev_cnt < 127)
4438 detected_devices[dev_cnt++] = dev;
4439}
4440
4441
4442static void autostart_arrays(int part)
4443{
4444 mdk_rdev_t *rdev;
4445 int i;
4446
4447 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4448
4449 for (i = 0; i < dev_cnt; i++) {
4450 dev_t dev = detected_devices[i];
4451
4452 rdev = md_import_device(dev,0, 0);
4453 if (IS_ERR(rdev))
4454 continue;
4455
NeilBrownb2d444d2005-11-08 21:39:31 -08004456 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 MD_BUG();
4458 continue;
4459 }
4460 list_add(&rdev->same_set, &pending_raid_disks);
4461 }
4462 dev_cnt = 0;
4463
4464 autorun_devices(part);
4465}
4466
4467#endif
4468
4469static __exit void md_exit(void)
4470{
4471 mddev_t *mddev;
4472 struct list_head *tmp;
4473 int i;
4474 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4475 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4476 for (i=0; i < MAX_MD_DEVS; i++)
4477 devfs_remove("md/%d", i);
4478 for (i=0; i < MAX_MD_DEVS; i++)
4479 devfs_remove("md/d%d", i);
4480
4481 devfs_remove("md");
4482
4483 unregister_blkdev(MAJOR_NR,"md");
4484 unregister_blkdev(mdp_major, "mdp");
4485 unregister_reboot_notifier(&md_notifier);
4486 unregister_sysctl_table(raid_table_header);
4487 remove_proc_entry("mdstat", NULL);
4488 ITERATE_MDDEV(mddev,tmp) {
4489 struct gendisk *disk = mddev->gendisk;
4490 if (!disk)
4491 continue;
4492 export_array(mddev);
4493 del_gendisk(disk);
4494 put_disk(disk);
4495 mddev->gendisk = NULL;
4496 mddev_put(mddev);
4497 }
4498}
4499
4500module_init(md_init)
4501module_exit(md_exit)
4502
NeilBrownf91de922005-11-08 21:39:36 -08004503static int get_ro(char *buffer, struct kernel_param *kp)
4504{
4505 return sprintf(buffer, "%d", start_readonly);
4506}
4507static int set_ro(const char *val, struct kernel_param *kp)
4508{
4509 char *e;
4510 int num = simple_strtoul(val, &e, 10);
4511 if (*val && (*e == '\0' || *e == '\n')) {
4512 start_readonly = num;
4513 return 0;;
4514 }
4515 return -EINVAL;
4516}
4517
4518module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004519module_param(start_dirty_degraded, int, 0644);
4520
NeilBrownf91de922005-11-08 21:39:36 -08004521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522EXPORT_SYMBOL(register_md_personality);
4523EXPORT_SYMBOL(unregister_md_personality);
4524EXPORT_SYMBOL(md_error);
4525EXPORT_SYMBOL(md_done_sync);
4526EXPORT_SYMBOL(md_write_start);
4527EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528EXPORT_SYMBOL(md_register_thread);
4529EXPORT_SYMBOL(md_unregister_thread);
4530EXPORT_SYMBOL(md_wakeup_thread);
4531EXPORT_SYMBOL(md_print_devices);
4532EXPORT_SYMBOL(md_check_recovery);
4533MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07004534MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07004535MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);