blob: d002b8301fc22aa4b8ed5746223719f57dc5b76f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
35#include <linux/module.h>
36#include <linux/config.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070037#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/linkage.h>
39#include <linux/raid/md.h>
NeilBrown32a76272005-06-21 17:17:14 -070040#include <linux/raid/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/sysctl.h>
42#include <linux/devfs_fs_kernel.h>
43#include <linux/buffer_head.h> /* for invalidate_bdev */
44#include <linux/suspend.h>
45
46#include <linux/init.h>
47
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#ifdef CONFIG_KMOD
51#include <linux/kmod.h>
52#endif
53
54#include <asm/unaligned.h>
55
56#define MAJOR_NR MD_MAJOR
57#define MD_DRIVER
58
59/* 63 partitions with the alternate major number (mdp) */
60#define MdpMinorShift 6
61
62#define DEBUG 0
63#define dprintk(x...) ((void)(DEBUG && printk(x)))
64
65
66#ifndef MODULE
67static void autostart_arrays (int part);
68#endif
69
70static mdk_personality_t *pers[MAX_PERSONALITY];
71static DEFINE_SPINLOCK(pers_lock);
72
73/*
74 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
75 * is 1000 KB/sec, so the extra system load does not show up that much.
76 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070077 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * subsystem is idle. There is also an 'absolute maximum' reconstruction
79 * speed limit - in case reconstruction slows down your system despite
80 * idle IO detection.
81 *
82 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
83 */
84
85static int sysctl_speed_limit_min = 1000;
86static int sysctl_speed_limit_max = 200000;
87
88static struct ctl_table_header *raid_table_header;
89
90static ctl_table raid_table[] = {
91 {
92 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
93 .procname = "speed_limit_min",
94 .data = &sysctl_speed_limit_min,
95 .maxlen = sizeof(int),
96 .mode = 0644,
97 .proc_handler = &proc_dointvec,
98 },
99 {
100 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
101 .procname = "speed_limit_max",
102 .data = &sysctl_speed_limit_max,
103 .maxlen = sizeof(int),
104 .mode = 0644,
105 .proc_handler = &proc_dointvec,
106 },
107 { .ctl_name = 0 }
108};
109
110static ctl_table raid_dir_table[] = {
111 {
112 .ctl_name = DEV_RAID,
113 .procname = "raid",
114 .maxlen = 0,
115 .mode = 0555,
116 .child = raid_table,
117 },
118 { .ctl_name = 0 }
119};
120
121static ctl_table raid_root_table[] = {
122 {
123 .ctl_name = CTL_DEV,
124 .procname = "dev",
125 .maxlen = 0,
126 .mode = 0555,
127 .child = raid_dir_table,
128 },
129 { .ctl_name = 0 }
130};
131
132static struct block_device_operations md_fops;
133
NeilBrownf91de922005-11-08 21:39:36 -0800134static int start_readonly;
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
137 * Enables to iterate over all existing md arrays
138 * all_mddevs_lock protects this list.
139 */
140static LIST_HEAD(all_mddevs);
141static DEFINE_SPINLOCK(all_mddevs_lock);
142
143
144/*
145 * iterates through all used mddevs in the system.
146 * We take care to grab the all_mddevs_lock whenever navigating
147 * the list, and to always hold a refcount when unlocked.
148 * Any code which breaks out of this loop while own
149 * a reference to the current mddev and must mddev_put it.
150 */
151#define ITERATE_MDDEV(mddev,tmp) \
152 \
153 for (({ spin_lock(&all_mddevs_lock); \
154 tmp = all_mddevs.next; \
155 mddev = NULL;}); \
156 ({ if (tmp != &all_mddevs) \
157 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
158 spin_unlock(&all_mddevs_lock); \
159 if (mddev) mddev_put(mddev); \
160 mddev = list_entry(tmp, mddev_t, all_mddevs); \
161 tmp != &all_mddevs;}); \
162 ({ spin_lock(&all_mddevs_lock); \
163 tmp = tmp->next;}) \
164 )
165
166
167static int md_fail_request (request_queue_t *q, struct bio *bio)
168{
169 bio_io_error(bio, bio->bi_size);
170 return 0;
171}
172
173static inline mddev_t *mddev_get(mddev_t *mddev)
174{
175 atomic_inc(&mddev->active);
176 return mddev;
177}
178
179static void mddev_put(mddev_t *mddev)
180{
181 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
182 return;
183 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
184 list_del(&mddev->all_mddevs);
185 blk_put_queue(mddev->queue);
NeilBrowneae17012005-11-08 21:39:23 -0800186 kobject_unregister(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 spin_unlock(&all_mddevs_lock);
189}
190
191static mddev_t * mddev_find(dev_t unit)
192{
193 mddev_t *mddev, *new = NULL;
194
195 retry:
196 spin_lock(&all_mddevs_lock);
197 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
198 if (mddev->unit == unit) {
199 mddev_get(mddev);
200 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700201 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return mddev;
203 }
204
205 if (new) {
206 list_add(&new->all_mddevs, &all_mddevs);
207 spin_unlock(&all_mddevs_lock);
208 return new;
209 }
210 spin_unlock(&all_mddevs_lock);
211
212 new = (mddev_t *) kmalloc(sizeof(*new), GFP_KERNEL);
213 if (!new)
214 return NULL;
215
216 memset(new, 0, sizeof(*new));
217
218 new->unit = unit;
219 if (MAJOR(unit) == MD_MAJOR)
220 new->md_minor = MINOR(unit);
221 else
222 new->md_minor = MINOR(unit) >> MdpMinorShift;
223
224 init_MUTEX(&new->reconfig_sem);
225 INIT_LIST_HEAD(&new->disks);
226 INIT_LIST_HEAD(&new->all_mddevs);
227 init_timer(&new->safemode_timer);
228 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700229 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700230 init_waitqueue_head(&new->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 new->queue = blk_alloc_queue(GFP_KERNEL);
233 if (!new->queue) {
234 kfree(new);
235 return NULL;
236 }
237
238 blk_queue_make_request(new->queue, md_fail_request);
239
240 goto retry;
241}
242
243static inline int mddev_lock(mddev_t * mddev)
244{
245 return down_interruptible(&mddev->reconfig_sem);
246}
247
248static inline void mddev_lock_uninterruptible(mddev_t * mddev)
249{
250 down(&mddev->reconfig_sem);
251}
252
253static inline int mddev_trylock(mddev_t * mddev)
254{
255 return down_trylock(&mddev->reconfig_sem);
256}
257
258static inline void mddev_unlock(mddev_t * mddev)
259{
260 up(&mddev->reconfig_sem);
261
NeilBrown005eca52005-08-22 13:11:08 -0700262 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
266{
267 mdk_rdev_t * rdev;
268 struct list_head *tmp;
269
270 ITERATE_RDEV(mddev,rdev,tmp) {
271 if (rdev->desc_nr == nr)
272 return rdev;
273 }
274 return NULL;
275}
276
277static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
278{
279 struct list_head *tmp;
280 mdk_rdev_t *rdev;
281
282 ITERATE_RDEV(mddev,rdev,tmp) {
283 if (rdev->bdev->bd_dev == dev)
284 return rdev;
285 }
286 return NULL;
287}
288
Jesper Juhl77933d72005-07-27 11:46:09 -0700289static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
292 return MD_NEW_SIZE_BLOCKS(size);
293}
294
295static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
296{
297 sector_t size;
298
299 size = rdev->sb_offset;
300
301 if (chunk_size)
302 size &= ~((sector_t)chunk_size/1024 - 1);
303 return size;
304}
305
306static int alloc_disk_sb(mdk_rdev_t * rdev)
307{
308 if (rdev->sb_page)
309 MD_BUG();
310
311 rdev->sb_page = alloc_page(GFP_KERNEL);
312 if (!rdev->sb_page) {
313 printk(KERN_ALERT "md: out of memory.\n");
314 return -EINVAL;
315 }
316
317 return 0;
318}
319
320static void free_disk_sb(mdk_rdev_t * rdev)
321{
322 if (rdev->sb_page) {
323 page_cache_release(rdev->sb_page);
324 rdev->sb_loaded = 0;
325 rdev->sb_page = NULL;
326 rdev->sb_offset = 0;
327 rdev->size = 0;
328 }
329}
330
331
NeilBrown7bfa19f2005-06-21 17:17:28 -0700332static int super_written(struct bio *bio, unsigned int bytes_done, int error)
333{
334 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800335 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700336 if (bio->bi_size)
337 return 1;
338
339 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
NeilBrowna9701a32005-11-08 21:39:34 -0800340 md_error(mddev, rdev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700341
NeilBrowna9701a32005-11-08 21:39:34 -0800342 if (atomic_dec_and_test(&mddev->pending_writes))
343 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700344 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700345 return 0;
346}
347
NeilBrowna9701a32005-11-08 21:39:34 -0800348static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
349{
350 struct bio *bio2 = bio->bi_private;
351 mdk_rdev_t *rdev = bio2->bi_private;
352 mddev_t *mddev = rdev->mddev;
353 if (bio->bi_size)
354 return 1;
355
356 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
357 error == -EOPNOTSUPP) {
358 unsigned long flags;
359 /* barriers don't appear to be supported :-( */
360 set_bit(BarriersNotsupp, &rdev->flags);
361 mddev->barriers_work = 0;
362 spin_lock_irqsave(&mddev->write_lock, flags);
363 bio2->bi_next = mddev->biolist;
364 mddev->biolist = bio2;
365 spin_unlock_irqrestore(&mddev->write_lock, flags);
366 wake_up(&mddev->sb_wait);
367 bio_put(bio);
368 return 0;
369 }
370 bio_put(bio2);
371 bio->bi_private = rdev;
372 return super_written(bio, bytes_done, error);
373}
374
NeilBrown7bfa19f2005-06-21 17:17:28 -0700375void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
376 sector_t sector, int size, struct page *page)
377{
378 /* write first size bytes of page to sector of rdev
379 * Increment mddev->pending_writes before returning
380 * and decrement it on completion, waking up sb_wait
381 * if zero is reached.
382 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800383 *
384 * As we might need to resubmit the request if BIO_RW_BARRIER
385 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700386 */
387 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800388 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700389
390 bio->bi_bdev = rdev->bdev;
391 bio->bi_sector = sector;
392 bio_add_page(bio, page, size, 0);
393 bio->bi_private = rdev;
394 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800395 bio->bi_rw = rw;
396
NeilBrown7bfa19f2005-06-21 17:17:28 -0700397 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800398 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
399 struct bio *rbio;
400 rw |= (1<<BIO_RW_BARRIER);
401 rbio = bio_clone(bio, GFP_NOIO);
402 rbio->bi_private = bio;
403 rbio->bi_end_io = super_written_barrier;
404 submit_bio(rw, rbio);
405 } else
406 submit_bio(rw, bio);
407}
408
409void md_super_wait(mddev_t *mddev)
410{
411 /* wait for all superblock writes that were scheduled to complete.
412 * if any had to be retried (due to BARRIER problems), retry them
413 */
414 DEFINE_WAIT(wq);
415 for(;;) {
416 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
417 if (atomic_read(&mddev->pending_writes)==0)
418 break;
419 while (mddev->biolist) {
420 struct bio *bio;
421 spin_lock_irq(&mddev->write_lock);
422 bio = mddev->biolist;
423 mddev->biolist = bio->bi_next ;
424 bio->bi_next = NULL;
425 spin_unlock_irq(&mddev->write_lock);
426 submit_bio(bio->bi_rw, bio);
427 }
428 schedule();
429 }
430 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
434{
435 if (bio->bi_size)
436 return 1;
437
438 complete((struct completion*)bio->bi_private);
439 return 0;
440}
441
NeilBrowna654b9d82005-06-21 17:17:27 -0700442int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct page *page, int rw)
444{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700445 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct completion event;
447 int ret;
448
449 rw |= (1 << BIO_RW_SYNC);
450
451 bio->bi_bdev = bdev;
452 bio->bi_sector = sector;
453 bio_add_page(bio, page, size, 0);
454 init_completion(&event);
455 bio->bi_private = &event;
456 bio->bi_end_io = bi_complete;
457 submit_bio(rw, bio);
458 wait_for_completion(&event);
459
460 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
461 bio_put(bio);
462 return ret;
463}
464
NeilBrown0002b272005-09-09 16:23:53 -0700465static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 char b[BDEVNAME_SIZE];
468 if (!rdev->sb_page) {
469 MD_BUG();
470 return -EINVAL;
471 }
472 if (rdev->sb_loaded)
473 return 0;
474
475
NeilBrown0002b272005-09-09 16:23:53 -0700476 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto fail;
478 rdev->sb_loaded = 1;
479 return 0;
480
481fail:
482 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
483 bdevname(rdev->bdev,b));
484 return -EINVAL;
485}
486
487static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
488{
489 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
490 (sb1->set_uuid1 == sb2->set_uuid1) &&
491 (sb1->set_uuid2 == sb2->set_uuid2) &&
492 (sb1->set_uuid3 == sb2->set_uuid3))
493
494 return 1;
495
496 return 0;
497}
498
499
500static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
501{
502 int ret;
503 mdp_super_t *tmp1, *tmp2;
504
505 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
506 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
507
508 if (!tmp1 || !tmp2) {
509 ret = 0;
510 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
511 goto abort;
512 }
513
514 *tmp1 = *sb1;
515 *tmp2 = *sb2;
516
517 /*
518 * nr_disks is not constant
519 */
520 tmp1->nr_disks = 0;
521 tmp2->nr_disks = 0;
522
523 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
524 ret = 0;
525 else
526 ret = 1;
527
528abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700529 kfree(tmp1);
530 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return ret;
532}
533
534static unsigned int calc_sb_csum(mdp_super_t * sb)
535{
536 unsigned int disk_csum, csum;
537
538 disk_csum = sb->sb_csum;
539 sb->sb_csum = 0;
540 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
541 sb->sb_csum = disk_csum;
542 return csum;
543}
544
545
546/*
547 * Handle superblock details.
548 * We want to be able to handle multiple superblock formats
549 * so we have a common interface to them all, and an array of
550 * different handlers.
551 * We rely on user-space to write the initial superblock, and support
552 * reading and updating of superblocks.
553 * Interface methods are:
554 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
555 * loads and validates a superblock on dev.
556 * if refdev != NULL, compare superblocks on both devices
557 * Return:
558 * 0 - dev has a superblock that is compatible with refdev
559 * 1 - dev has a superblock that is compatible and newer than refdev
560 * so dev should be used as the refdev in future
561 * -EINVAL superblock incompatible or invalid
562 * -othererror e.g. -EIO
563 *
564 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
565 * Verify that dev is acceptable into mddev.
566 * The first time, mddev->raid_disks will be 0, and data from
567 * dev should be merged in. Subsequent calls check that dev
568 * is new enough. Return 0 or -EINVAL
569 *
570 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
571 * Update the superblock for rdev with data in mddev
572 * This does not write to disc.
573 *
574 */
575
576struct super_type {
577 char *name;
578 struct module *owner;
579 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
580 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
581 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
582};
583
584/*
585 * load_super for 0.90.0
586 */
587static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
588{
589 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
590 mdp_super_t *sb;
591 int ret;
592 sector_t sb_offset;
593
594 /*
595 * Calculate the position of the superblock,
596 * it's at the end of the disk.
597 *
598 * It also happens to be a multiple of 4Kb.
599 */
600 sb_offset = calc_dev_sboffset(rdev->bdev);
601 rdev->sb_offset = sb_offset;
602
NeilBrown0002b272005-09-09 16:23:53 -0700603 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (ret) return ret;
605
606 ret = -EINVAL;
607
608 bdevname(rdev->bdev, b);
609 sb = (mdp_super_t*)page_address(rdev->sb_page);
610
611 if (sb->md_magic != MD_SB_MAGIC) {
612 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
613 b);
614 goto abort;
615 }
616
617 if (sb->major_version != 0 ||
618 sb->minor_version != 90) {
619 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
620 sb->major_version, sb->minor_version,
621 b);
622 goto abort;
623 }
624
625 if (sb->raid_disks <= 0)
626 goto abort;
627
628 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
629 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
630 b);
631 goto abort;
632 }
633
634 rdev->preferred_minor = sb->md_minor;
635 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700636 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (sb->level == LEVEL_MULTIPATH)
639 rdev->desc_nr = -1;
640 else
641 rdev->desc_nr = sb->this_disk.number;
642
643 if (refdev == 0)
644 ret = 1;
645 else {
646 __u64 ev1, ev2;
647 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
648 if (!uuid_equal(refsb, sb)) {
649 printk(KERN_WARNING "md: %s has different UUID to %s\n",
650 b, bdevname(refdev->bdev,b2));
651 goto abort;
652 }
653 if (!sb_equal(refsb, sb)) {
654 printk(KERN_WARNING "md: %s has same UUID"
655 " but different superblock to %s\n",
656 b, bdevname(refdev->bdev, b2));
657 goto abort;
658 }
659 ev1 = md_event(sb);
660 ev2 = md_event(refsb);
661 if (ev1 > ev2)
662 ret = 1;
663 else
664 ret = 0;
665 }
666 rdev->size = calc_dev_size(rdev, sb->chunk_size);
667
668 abort:
669 return ret;
670}
671
672/*
673 * validate_super for 0.90.0
674 */
675static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
676{
677 mdp_disk_t *desc;
678 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
679
NeilBrown41158c72005-06-21 17:17:25 -0700680 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800681 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (mddev->raid_disks == 0) {
683 mddev->major_version = 0;
684 mddev->minor_version = sb->minor_version;
685 mddev->patch_version = sb->patch_version;
686 mddev->persistent = ! sb->not_persistent;
687 mddev->chunk_size = sb->chunk_size;
688 mddev->ctime = sb->ctime;
689 mddev->utime = sb->utime;
690 mddev->level = sb->level;
691 mddev->layout = sb->layout;
692 mddev->raid_disks = sb->raid_disks;
693 mddev->size = sb->size;
694 mddev->events = md_event(sb);
NeilBrown92232142005-08-18 11:24:16 -0700695 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700696 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 if (sb->state & (1<<MD_SB_CLEAN))
699 mddev->recovery_cp = MaxSector;
700 else {
701 if (sb->events_hi == sb->cp_events_hi &&
702 sb->events_lo == sb->cp_events_lo) {
703 mddev->recovery_cp = sb->recovery_cp;
704 } else
705 mddev->recovery_cp = 0;
706 }
707
708 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
709 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
710 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
711 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
712
713 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700714
715 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
716 mddev->bitmap_file == NULL) {
NeilBrown934ce7c2005-09-09 16:23:55 -0700717 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700718 /* FIXME use a better test */
719 printk(KERN_WARNING "md: bitmaps only support for raid1\n");
720 return -EINVAL;
721 }
NeilBrown36fa3062005-09-09 16:23:45 -0700722 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700723 }
724
NeilBrown41158c72005-06-21 17:17:25 -0700725 } else if (mddev->pers == NULL) {
726 /* Insist on good event counter while assembling */
727 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 ++ev1;
729 if (ev1 < mddev->events)
730 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700731 } else if (mddev->bitmap) {
732 /* if adding to array with a bitmap, then we can accept an
733 * older device ... but not too old.
734 */
735 __u64 ev1 = md_event(sb);
736 if (ev1 < mddev->bitmap->events_cleared)
737 return 0;
738 } else /* just a hot-add of a new device, leave raid_disk at -1 */
739 return 0;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 desc = sb->disks + rdev->desc_nr;
743
744 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800745 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 else if (desc->state & (1<<MD_DISK_SYNC) &&
747 desc->raid_disk < mddev->raid_disks) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800748 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 rdev->raid_disk = desc->raid_disk;
750 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700751 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
752 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700753 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800754 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
756}
757
758/*
759 * sync_super for 0.90.0
760 */
761static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
762{
763 mdp_super_t *sb;
764 struct list_head *tmp;
765 mdk_rdev_t *rdev2;
766 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /* make rdev->sb match mddev data..
770 *
771 * 1/ zero out disks
772 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
773 * 3/ any empty disks < next_spare become removed
774 *
775 * disks[0] gets initialised to REMOVED because
776 * we cannot be sure from other fields if it has
777 * been initialised or not.
778 */
779 int i;
780 int active=0, working=0,failed=0,spare=0,nr_disks=0;
781
NeilBrown61181562005-09-09 16:24:02 -0700782 rdev->sb_size = MD_SB_BYTES;
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 sb = (mdp_super_t*)page_address(rdev->sb_page);
785
786 memset(sb, 0, sizeof(*sb));
787
788 sb->md_magic = MD_SB_MAGIC;
789 sb->major_version = mddev->major_version;
790 sb->minor_version = mddev->minor_version;
791 sb->patch_version = mddev->patch_version;
792 sb->gvalid_words = 0; /* ignored */
793 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
794 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
795 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
796 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
797
798 sb->ctime = mddev->ctime;
799 sb->level = mddev->level;
800 sb->size = mddev->size;
801 sb->raid_disks = mddev->raid_disks;
802 sb->md_minor = mddev->md_minor;
803 sb->not_persistent = !mddev->persistent;
804 sb->utime = mddev->utime;
805 sb->state = 0;
806 sb->events_hi = (mddev->events>>32);
807 sb->events_lo = (u32)mddev->events;
808
809 if (mddev->in_sync)
810 {
811 sb->recovery_cp = mddev->recovery_cp;
812 sb->cp_events_hi = (mddev->events>>32);
813 sb->cp_events_lo = (u32)mddev->events;
814 if (mddev->recovery_cp == MaxSector)
815 sb->state = (1<< MD_SB_CLEAN);
816 } else
817 sb->recovery_cp = 0;
818
819 sb->layout = mddev->layout;
820 sb->chunk_size = mddev->chunk_size;
821
NeilBrowna654b9d82005-06-21 17:17:27 -0700822 if (mddev->bitmap && mddev->bitmap_file == NULL)
823 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 sb->disks[0].state = (1<<MD_DISK_REMOVED);
826 ITERATE_RDEV(mddev,rdev2,tmp) {
827 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800828 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800829 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
830 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800831 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800833 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800834 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 d = &sb->disks[rdev2->desc_nr];
836 nr_disks++;
837 d->number = rdev2->desc_nr;
838 d->major = MAJOR(rdev2->bdev->bd_dev);
839 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800840 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
841 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 d->raid_disk = rdev2->raid_disk;
843 else
844 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrownb2d444d2005-11-08 21:39:31 -0800845 if (test_bit(Faulty, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 d->state = (1<<MD_DISK_FAULTY);
847 failed++;
NeilBrownb2d444d2005-11-08 21:39:31 -0800848 } else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 d->state = (1<<MD_DISK_ACTIVE);
850 d->state |= (1<<MD_DISK_SYNC);
851 active++;
852 working++;
853 } else {
854 d->state = 0;
855 spare++;
856 working++;
857 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700858 if (test_bit(WriteMostly, &rdev2->flags))
859 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* now set the "removed" and "faulty" bits on any missing devices */
862 for (i=0 ; i < mddev->raid_disks ; i++) {
863 mdp_disk_t *d = &sb->disks[i];
864 if (d->state == 0 && d->number == 0) {
865 d->number = i;
866 d->raid_disk = i;
867 d->state = (1<<MD_DISK_REMOVED);
868 d->state |= (1<<MD_DISK_FAULTY);
869 failed++;
870 }
871 }
872 sb->nr_disks = nr_disks;
873 sb->active_disks = active;
874 sb->working_disks = working;
875 sb->failed_disks = failed;
876 sb->spare_disks = spare;
877
878 sb->this_disk = sb->disks[rdev->desc_nr];
879 sb->sb_csum = calc_sb_csum(sb);
880}
881
882/*
883 * version 1 superblock
884 */
885
886static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
887{
888 unsigned int disk_csum, csum;
889 unsigned long long newcsum;
890 int size = 256 + le32_to_cpu(sb->max_dev)*2;
891 unsigned int *isuper = (unsigned int*)sb;
892 int i;
893
894 disk_csum = sb->sb_csum;
895 sb->sb_csum = 0;
896 newcsum = 0;
897 for (i=0; size>=4; size -= 4 )
898 newcsum += le32_to_cpu(*isuper++);
899
900 if (size == 2)
901 newcsum += le16_to_cpu(*(unsigned short*) isuper);
902
903 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
904 sb->sb_csum = disk_csum;
905 return cpu_to_le32(csum);
906}
907
908static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
909{
910 struct mdp_superblock_1 *sb;
911 int ret;
912 sector_t sb_offset;
913 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -0700914 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 /*
917 * Calculate the position of the superblock.
918 * It is always aligned to a 4K boundary and
919 * depeding on minor_version, it can be:
920 * 0: At least 8K, but less than 12K, from end of device
921 * 1: At start of device
922 * 2: 4K from start of device.
923 */
924 switch(minor_version) {
925 case 0:
926 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
927 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -0700928 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* convert from sectors to K */
930 sb_offset /= 2;
931 break;
932 case 1:
933 sb_offset = 0;
934 break;
935 case 2:
936 sb_offset = 4;
937 break;
938 default:
939 return -EINVAL;
940 }
941 rdev->sb_offset = sb_offset;
942
NeilBrown0002b272005-09-09 16:23:53 -0700943 /* superblock is rarely larger than 1K, but it can be larger,
944 * and it is safe to read 4k, so we do that
945 */
946 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (ret) return ret;
948
949
950 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
951
952 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
953 sb->major_version != cpu_to_le32(1) ||
954 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
955 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -0700956 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return -EINVAL;
958
959 if (calc_sb_1_csum(sb) != sb->sb_csum) {
960 printk("md: invalid superblock checksum on %s\n",
961 bdevname(rdev->bdev,b));
962 return -EINVAL;
963 }
964 if (le64_to_cpu(sb->data_size) < 10) {
965 printk("md: data_size too small on %s\n",
966 bdevname(rdev->bdev,b));
967 return -EINVAL;
968 }
969 rdev->preferred_minor = 0xffff;
970 rdev->data_offset = le64_to_cpu(sb->data_offset);
971
NeilBrown0002b272005-09-09 16:23:53 -0700972 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -0700973 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -0700974 if (rdev->sb_size & bmask)
975 rdev-> sb_size = (rdev->sb_size | bmask)+1;
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (refdev == 0)
978 return 1;
979 else {
980 __u64 ev1, ev2;
981 struct mdp_superblock_1 *refsb =
982 (struct mdp_superblock_1*)page_address(refdev->sb_page);
983
984 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
985 sb->level != refsb->level ||
986 sb->layout != refsb->layout ||
987 sb->chunksize != refsb->chunksize) {
988 printk(KERN_WARNING "md: %s has strangely different"
989 " superblock to %s\n",
990 bdevname(rdev->bdev,b),
991 bdevname(refdev->bdev,b2));
992 return -EINVAL;
993 }
994 ev1 = le64_to_cpu(sb->events);
995 ev2 = le64_to_cpu(refsb->events);
996
997 if (ev1 > ev2)
998 return 1;
999 }
1000 if (minor_version)
1001 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1002 else
1003 rdev->size = rdev->sb_offset;
1004 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1005 return -EINVAL;
1006 rdev->size = le64_to_cpu(sb->data_size)/2;
1007 if (le32_to_cpu(sb->chunksize))
1008 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1009 return 0;
1010}
1011
1012static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1013{
1014 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1015
NeilBrown41158c72005-06-21 17:17:25 -07001016 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001017 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (mddev->raid_disks == 0) {
1019 mddev->major_version = 1;
1020 mddev->patch_version = 0;
1021 mddev->persistent = 1;
1022 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1023 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1024 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1025 mddev->level = le32_to_cpu(sb->level);
1026 mddev->layout = le32_to_cpu(sb->layout);
1027 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1028 mddev->size = le64_to_cpu(sb->size)/2;
1029 mddev->events = le64_to_cpu(sb->events);
NeilBrown92232142005-08-18 11:24:16 -07001030 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07001031 mddev->default_bitmap_offset = 0;
NeilBrown53e87fb2005-09-09 16:23:58 -07001032 mddev->default_bitmap_offset = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1035 memcpy(mddev->uuid, sb->set_uuid, 16);
1036
1037 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001038
NeilBrown71c08052005-09-09 16:23:51 -07001039 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -07001040 mddev->bitmap_file == NULL ) {
1041 if (mddev->level != 1) {
1042 printk(KERN_WARNING "md: bitmaps only supported for raid1\n");
1043 return -EINVAL;
1044 }
1045 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1046 }
NeilBrown41158c72005-06-21 17:17:25 -07001047 } else if (mddev->pers == NULL) {
1048 /* Insist of good event counter while assembling */
1049 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 ++ev1;
1051 if (ev1 < mddev->events)
1052 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001053 } else if (mddev->bitmap) {
1054 /* If adding to array with a bitmap, then we can accept an
1055 * older device, but not too old.
1056 */
1057 __u64 ev1 = le64_to_cpu(sb->events);
1058 if (ev1 < mddev->bitmap->events_cleared)
1059 return 0;
1060 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 if (mddev->level != LEVEL_MULTIPATH) {
1064 int role;
1065 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1066 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1067 switch(role) {
1068 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 break;
1070 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001071 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 break;
1073 default:
NeilBrownb2d444d2005-11-08 21:39:31 -08001074 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 rdev->raid_disk = role;
1076 break;
1077 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001078 if (sb->devflags & WriteMostly1)
1079 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001080 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001081 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return 0;
1084}
1085
1086static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1087{
1088 struct mdp_superblock_1 *sb;
1089 struct list_head *tmp;
1090 mdk_rdev_t *rdev2;
1091 int max_dev, i;
1092 /* make rdev->sb match mddev and rdev data. */
1093
1094 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1095
1096 sb->feature_map = 0;
1097 sb->pad0 = 0;
1098 memset(sb->pad1, 0, sizeof(sb->pad1));
1099 memset(sb->pad2, 0, sizeof(sb->pad2));
1100 memset(sb->pad3, 0, sizeof(sb->pad3));
1101
1102 sb->utime = cpu_to_le64((__u64)mddev->utime);
1103 sb->events = cpu_to_le64(mddev->events);
1104 if (mddev->in_sync)
1105 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1106 else
1107 sb->resync_offset = cpu_to_le64(0);
1108
NeilBrowna654b9d82005-06-21 17:17:27 -07001109 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1110 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001111 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001112 }
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 max_dev = 0;
1115 ITERATE_RDEV(mddev,rdev2,tmp)
1116 if (rdev2->desc_nr+1 > max_dev)
1117 max_dev = rdev2->desc_nr+1;
1118
1119 sb->max_dev = cpu_to_le32(max_dev);
1120 for (i=0; i<max_dev;i++)
1121 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1122
1123 ITERATE_RDEV(mddev,rdev2,tmp) {
1124 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001125 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001127 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1129 else
1130 sb->dev_roles[i] = cpu_to_le16(0xffff);
1131 }
1132
1133 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1134 sb->sb_csum = calc_sb_1_csum(sb);
1135}
1136
1137
Adrian Bunk75c96f82005-05-05 16:16:09 -07001138static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 [0] = {
1140 .name = "0.90.0",
1141 .owner = THIS_MODULE,
1142 .load_super = super_90_load,
1143 .validate_super = super_90_validate,
1144 .sync_super = super_90_sync,
1145 },
1146 [1] = {
1147 .name = "md-1",
1148 .owner = THIS_MODULE,
1149 .load_super = super_1_load,
1150 .validate_super = super_1_validate,
1151 .sync_super = super_1_sync,
1152 },
1153};
1154
1155static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1156{
1157 struct list_head *tmp;
1158 mdk_rdev_t *rdev;
1159
1160 ITERATE_RDEV(mddev,rdev,tmp)
1161 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1162 return rdev;
1163
1164 return NULL;
1165}
1166
1167static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1168{
1169 struct list_head *tmp;
1170 mdk_rdev_t *rdev;
1171
1172 ITERATE_RDEV(mddev1,rdev,tmp)
1173 if (match_dev_unit(mddev2, rdev))
1174 return 1;
1175
1176 return 0;
1177}
1178
1179static LIST_HEAD(pending_raid_disks);
1180
1181static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1182{
1183 mdk_rdev_t *same_pdev;
1184 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1185
1186 if (rdev->mddev) {
1187 MD_BUG();
1188 return -EINVAL;
1189 }
1190 same_pdev = match_dev_unit(mddev, rdev);
1191 if (same_pdev)
1192 printk(KERN_WARNING
1193 "%s: WARNING: %s appears to be on the same physical"
1194 " disk as %s. True\n protection against single-disk"
1195 " failure might be compromised.\n",
1196 mdname(mddev), bdevname(rdev->bdev,b),
1197 bdevname(same_pdev->bdev,b2));
1198
1199 /* Verify rdev->desc_nr is unique.
1200 * If it is -1, assign a free number, else
1201 * check number is not in use
1202 */
1203 if (rdev->desc_nr < 0) {
1204 int choice = 0;
1205 if (mddev->pers) choice = mddev->raid_disks;
1206 while (find_rdev_nr(mddev, choice))
1207 choice++;
1208 rdev->desc_nr = choice;
1209 } else {
1210 if (find_rdev_nr(mddev, rdev->desc_nr))
1211 return -EBUSY;
1212 }
NeilBrown19133a42005-11-08 21:39:35 -08001213 bdevname(rdev->bdev,b);
1214 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1215 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 list_add(&rdev->same_set, &mddev->disks);
1218 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001219 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001220
NeilBrown9c791972005-11-08 21:39:28 -08001221 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001222 kobject_add(&rdev->kobj);
1223
1224 sysfs_create_link(&rdev->kobj, &rdev->bdev->bd_disk->kobj, "block");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return 0;
1226}
1227
1228static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1229{
1230 char b[BDEVNAME_SIZE];
1231 if (!rdev->mddev) {
1232 MD_BUG();
1233 return;
1234 }
1235 list_del_init(&rdev->same_set);
1236 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1237 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001238 sysfs_remove_link(&rdev->kobj, "block");
1239 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242/*
1243 * prevent the device from being mounted, repartitioned or
1244 * otherwise reused by a RAID array (or any other kernel
1245 * subsystem), by bd_claiming the device.
1246 */
1247static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1248{
1249 int err = 0;
1250 struct block_device *bdev;
1251 char b[BDEVNAME_SIZE];
1252
1253 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1254 if (IS_ERR(bdev)) {
1255 printk(KERN_ERR "md: could not open %s.\n",
1256 __bdevname(dev, b));
1257 return PTR_ERR(bdev);
1258 }
1259 err = bd_claim(bdev, rdev);
1260 if (err) {
1261 printk(KERN_ERR "md: could not bd_claim %s.\n",
1262 bdevname(bdev, b));
1263 blkdev_put(bdev);
1264 return err;
1265 }
1266 rdev->bdev = bdev;
1267 return err;
1268}
1269
1270static void unlock_rdev(mdk_rdev_t *rdev)
1271{
1272 struct block_device *bdev = rdev->bdev;
1273 rdev->bdev = NULL;
1274 if (!bdev)
1275 MD_BUG();
1276 bd_release(bdev);
1277 blkdev_put(bdev);
1278}
1279
1280void md_autodetect_dev(dev_t dev);
1281
1282static void export_rdev(mdk_rdev_t * rdev)
1283{
1284 char b[BDEVNAME_SIZE];
1285 printk(KERN_INFO "md: export_rdev(%s)\n",
1286 bdevname(rdev->bdev,b));
1287 if (rdev->mddev)
1288 MD_BUG();
1289 free_disk_sb(rdev);
1290 list_del_init(&rdev->same_set);
1291#ifndef MODULE
1292 md_autodetect_dev(rdev->bdev->bd_dev);
1293#endif
1294 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001295 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
1298static void kick_rdev_from_array(mdk_rdev_t * rdev)
1299{
1300 unbind_rdev_from_array(rdev);
1301 export_rdev(rdev);
1302}
1303
1304static void export_array(mddev_t *mddev)
1305{
1306 struct list_head *tmp;
1307 mdk_rdev_t *rdev;
1308
1309 ITERATE_RDEV(mddev,rdev,tmp) {
1310 if (!rdev->mddev) {
1311 MD_BUG();
1312 continue;
1313 }
1314 kick_rdev_from_array(rdev);
1315 }
1316 if (!list_empty(&mddev->disks))
1317 MD_BUG();
1318 mddev->raid_disks = 0;
1319 mddev->major_version = 0;
1320}
1321
1322static void print_desc(mdp_disk_t *desc)
1323{
1324 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1325 desc->major,desc->minor,desc->raid_disk,desc->state);
1326}
1327
1328static void print_sb(mdp_super_t *sb)
1329{
1330 int i;
1331
1332 printk(KERN_INFO
1333 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1334 sb->major_version, sb->minor_version, sb->patch_version,
1335 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1336 sb->ctime);
1337 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1338 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1339 sb->md_minor, sb->layout, sb->chunk_size);
1340 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1341 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1342 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1343 sb->failed_disks, sb->spare_disks,
1344 sb->sb_csum, (unsigned long)sb->events_lo);
1345
1346 printk(KERN_INFO);
1347 for (i = 0; i < MD_SB_DISKS; i++) {
1348 mdp_disk_t *desc;
1349
1350 desc = sb->disks + i;
1351 if (desc->number || desc->major || desc->minor ||
1352 desc->raid_disk || (desc->state && (desc->state != 4))) {
1353 printk(" D %2d: ", i);
1354 print_desc(desc);
1355 }
1356 }
1357 printk(KERN_INFO "md: THIS: ");
1358 print_desc(&sb->this_disk);
1359
1360}
1361
1362static void print_rdev(mdk_rdev_t *rdev)
1363{
1364 char b[BDEVNAME_SIZE];
1365 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1366 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001367 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1368 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (rdev->sb_loaded) {
1370 printk(KERN_INFO "md: rdev superblock:\n");
1371 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1372 } else
1373 printk(KERN_INFO "md: no rdev superblock!\n");
1374}
1375
1376void md_print_devices(void)
1377{
1378 struct list_head *tmp, *tmp2;
1379 mdk_rdev_t *rdev;
1380 mddev_t *mddev;
1381 char b[BDEVNAME_SIZE];
1382
1383 printk("\n");
1384 printk("md: **********************************\n");
1385 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1386 printk("md: **********************************\n");
1387 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
NeilBrown32a76272005-06-21 17:17:14 -07001389 if (mddev->bitmap)
1390 bitmap_print_sb(mddev->bitmap);
1391 else
1392 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 ITERATE_RDEV(mddev,rdev,tmp2)
1394 printk("<%s>", bdevname(rdev->bdev,b));
1395 printk("\n");
1396
1397 ITERATE_RDEV(mddev,rdev,tmp2)
1398 print_rdev(rdev);
1399 }
1400 printk("md: **********************************\n");
1401 printk("\n");
1402}
1403
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405static void sync_sbs(mddev_t * mddev)
1406{
1407 mdk_rdev_t *rdev;
1408 struct list_head *tmp;
1409
1410 ITERATE_RDEV(mddev,rdev,tmp) {
1411 super_types[mddev->major_version].
1412 sync_super(mddev, rdev);
1413 rdev->sb_loaded = 1;
1414 }
1415}
1416
1417static void md_update_sb(mddev_t * mddev)
1418{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001419 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct list_head *tmp;
1421 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001422 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001425 spin_lock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001426 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 mddev->utime = get_seconds();
1428 mddev->events ++;
1429
1430 if (!mddev->events) {
1431 /*
1432 * oops, this 64-bit counter should never wrap.
1433 * Either we are in around ~1 trillion A.C., assuming
1434 * 1 reboot per second, or we have a bug:
1435 */
1436 MD_BUG();
1437 mddev->events --;
1438 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001439 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 sync_sbs(mddev);
1441
1442 /*
1443 * do not write anything to disk if using
1444 * nonpersistent superblocks
1445 */
NeilBrown06d91a52005-06-21 17:17:12 -07001446 if (!mddev->persistent) {
1447 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001448 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001449 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001451 }
NeilBrowna9701a32005-11-08 21:39:34 -08001452 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 dprintk(KERN_INFO
1455 "md: updating %s RAID superblock on device (in sync %d)\n",
1456 mdname(mddev),mddev->in_sync);
1457
NeilBrown32a76272005-06-21 17:17:14 -07001458 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 ITERATE_RDEV(mddev,rdev,tmp) {
1460 char b[BDEVNAME_SIZE];
1461 dprintk(KERN_INFO "md: ");
NeilBrownb2d444d2005-11-08 21:39:31 -08001462 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 dprintk("(skipping faulty ");
1464
1465 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001466 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001467 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001468 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001469 rdev->sb_page);
1470 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1471 bdevname(rdev->bdev,b),
1472 (unsigned long long)rdev->sb_offset);
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 } else
1475 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001476 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 /* only need to write one superblock... */
1478 break;
1479 }
NeilBrowna9701a32005-11-08 21:39:34 -08001480 md_super_wait(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001481 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1482
NeilBrowna9701a32005-11-08 21:39:34 -08001483 spin_lock_irq(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001484 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001485 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001486 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001487 goto repeat;
1488 }
1489 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001490 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001491 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
NeilBrown86e6ffd2005-11-08 21:39:24 -08001495struct rdev_sysfs_entry {
1496 struct attribute attr;
1497 ssize_t (*show)(mdk_rdev_t *, char *);
1498 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1499};
1500
1501static ssize_t
1502rdev_show_state(mdk_rdev_t *rdev, char *page)
1503{
1504 char *sep = "";
1505 int len=0;
1506
NeilBrownb2d444d2005-11-08 21:39:31 -08001507 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001508 len+= sprintf(page+len, "%sfaulty",sep);
1509 sep = ",";
1510 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001511 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001512 len += sprintf(page+len, "%sin_sync",sep);
1513 sep = ",";
1514 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001515 if (!test_bit(Faulty, &rdev->flags) &&
1516 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001517 len += sprintf(page+len, "%sspare", sep);
1518 sep = ",";
1519 }
1520 return len+sprintf(page+len, "\n");
1521}
1522
1523static struct rdev_sysfs_entry rdev_state = {
1524 .attr = {.name = "state", .mode = S_IRUGO },
1525 .show = rdev_show_state,
1526};
1527
1528static ssize_t
1529rdev_show_super(mdk_rdev_t *rdev, char *page)
1530{
1531 if (rdev->sb_loaded && rdev->sb_size) {
1532 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1533 return rdev->sb_size;
1534 } else
1535 return 0;
1536}
1537static struct rdev_sysfs_entry rdev_super = {
1538 .attr = {.name = "super", .mode = S_IRUGO },
1539 .show = rdev_show_super,
1540};
1541static struct attribute *rdev_default_attrs[] = {
1542 &rdev_state.attr,
1543 &rdev_super.attr,
1544 NULL,
1545};
1546static ssize_t
1547rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1548{
1549 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1550 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1551
1552 if (!entry->show)
1553 return -EIO;
1554 return entry->show(rdev, page);
1555}
1556
1557static ssize_t
1558rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1559 const char *page, size_t length)
1560{
1561 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1562 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1563
1564 if (!entry->store)
1565 return -EIO;
1566 return entry->store(rdev, page, length);
1567}
1568
1569static void rdev_free(struct kobject *ko)
1570{
1571 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1572 kfree(rdev);
1573}
1574static struct sysfs_ops rdev_sysfs_ops = {
1575 .show = rdev_attr_show,
1576 .store = rdev_attr_store,
1577};
1578static struct kobj_type rdev_ktype = {
1579 .release = rdev_free,
1580 .sysfs_ops = &rdev_sysfs_ops,
1581 .default_attrs = rdev_default_attrs,
1582};
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584/*
1585 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1586 *
1587 * mark the device faulty if:
1588 *
1589 * - the device is nonexistent (zero size)
1590 * - the device has no valid superblock
1591 *
1592 * a faulty rdev _never_ has rdev->sb set.
1593 */
1594static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1595{
1596 char b[BDEVNAME_SIZE];
1597 int err;
1598 mdk_rdev_t *rdev;
1599 sector_t size;
1600
1601 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1602 if (!rdev) {
1603 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1604 return ERR_PTR(-ENOMEM);
1605 }
1606 memset(rdev, 0, sizeof(*rdev));
1607
1608 if ((err = alloc_disk_sb(rdev)))
1609 goto abort_free;
1610
1611 err = lock_rdev(rdev, newdev);
1612 if (err)
1613 goto abort_free;
1614
NeilBrown86e6ffd2005-11-08 21:39:24 -08001615 rdev->kobj.parent = NULL;
1616 rdev->kobj.ktype = &rdev_ktype;
1617 kobject_init(&rdev->kobj);
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 rdev->desc_nr = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001620 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 rdev->data_offset = 0;
1622 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001623 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1626 if (!size) {
1627 printk(KERN_WARNING
1628 "md: %s has zero or unknown size, marking faulty!\n",
1629 bdevname(rdev->bdev,b));
1630 err = -EINVAL;
1631 goto abort_free;
1632 }
1633
1634 if (super_format >= 0) {
1635 err = super_types[super_format].
1636 load_super(rdev, NULL, super_minor);
1637 if (err == -EINVAL) {
1638 printk(KERN_WARNING
1639 "md: %s has invalid sb, not importing!\n",
1640 bdevname(rdev->bdev,b));
1641 goto abort_free;
1642 }
1643 if (err < 0) {
1644 printk(KERN_WARNING
1645 "md: could not read %s's sb, not importing!\n",
1646 bdevname(rdev->bdev,b));
1647 goto abort_free;
1648 }
1649 }
1650 INIT_LIST_HEAD(&rdev->same_set);
1651
1652 return rdev;
1653
1654abort_free:
1655 if (rdev->sb_page) {
1656 if (rdev->bdev)
1657 unlock_rdev(rdev);
1658 free_disk_sb(rdev);
1659 }
1660 kfree(rdev);
1661 return ERR_PTR(err);
1662}
1663
1664/*
1665 * Check a full RAID array for plausibility
1666 */
1667
1668
NeilBrowna757e642005-04-16 15:26:42 -07001669static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 int i;
1672 struct list_head *tmp;
1673 mdk_rdev_t *rdev, *freshest;
1674 char b[BDEVNAME_SIZE];
1675
1676 freshest = NULL;
1677 ITERATE_RDEV(mddev,rdev,tmp)
1678 switch (super_types[mddev->major_version].
1679 load_super(rdev, freshest, mddev->minor_version)) {
1680 case 1:
1681 freshest = rdev;
1682 break;
1683 case 0:
1684 break;
1685 default:
1686 printk( KERN_ERR \
1687 "md: fatal superblock inconsistency in %s"
1688 " -- removing from array\n",
1689 bdevname(rdev->bdev,b));
1690 kick_rdev_from_array(rdev);
1691 }
1692
1693
1694 super_types[mddev->major_version].
1695 validate_super(mddev, freshest);
1696
1697 i = 0;
1698 ITERATE_RDEV(mddev,rdev,tmp) {
1699 if (rdev != freshest)
1700 if (super_types[mddev->major_version].
1701 validate_super(mddev, rdev)) {
1702 printk(KERN_WARNING "md: kicking non-fresh %s"
1703 " from array!\n",
1704 bdevname(rdev->bdev,b));
1705 kick_rdev_from_array(rdev);
1706 continue;
1707 }
1708 if (mddev->level == LEVEL_MULTIPATH) {
1709 rdev->desc_nr = i++;
1710 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001711 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713 }
1714
1715
1716
1717 if (mddev->recovery_cp != MaxSector &&
1718 mddev->level >= 1)
1719 printk(KERN_ERR "md: %s: raid array is not clean"
1720 " -- starting background reconstruction\n",
1721 mdname(mddev));
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
NeilBrowneae17012005-11-08 21:39:23 -08001725static ssize_t
1726md_show_level(mddev_t *mddev, char *page)
1727{
1728 mdk_personality_t *p = mddev->pers;
1729 if (p == NULL)
1730 return 0;
1731 if (mddev->level >= 0)
1732 return sprintf(page, "RAID-%d\n", mddev->level);
1733 else
1734 return sprintf(page, "%s\n", p->name);
1735}
1736
1737static struct md_sysfs_entry md_level = {
1738 .attr = {.name = "level", .mode = S_IRUGO },
1739 .show = md_show_level,
1740};
1741
1742static ssize_t
1743md_show_rdisks(mddev_t *mddev, char *page)
1744{
1745 return sprintf(page, "%d\n", mddev->raid_disks);
1746}
1747
1748static struct md_sysfs_entry md_raid_disks = {
1749 .attr = {.name = "raid_disks", .mode = S_IRUGO },
1750 .show = md_show_rdisks,
1751};
1752
NeilBrown24dd4692005-11-08 21:39:26 -08001753static ssize_t
1754md_show_scan(mddev_t *mddev, char *page)
1755{
1756 char *type = "none";
NeilBrown31399d92005-11-08 21:39:29 -08001757 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1758 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
1759 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08001760 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1761 type = "resync";
1762 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1763 type = "check";
1764 else
1765 type = "repair";
1766 } else
1767 type = "recover";
1768 }
1769 return sprintf(page, "%s\n", type);
1770}
1771
1772static ssize_t
1773md_store_scan(mddev_t *mddev, const char *page, size_t len)
1774{
1775 int canscan=0;
NeilBrown31399d92005-11-08 21:39:29 -08001776
1777 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1778 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08001779 return -EBUSY;
1780 down(&mddev->reconfig_sem);
1781 if (mddev->pers && mddev->pers->sync_request)
1782 canscan=1;
1783 up(&mddev->reconfig_sem);
1784 if (!canscan)
1785 return -EINVAL;
1786
1787 if (strcmp(page, "check")==0 || strcmp(page, "check\n")==0)
1788 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1789 else if (strcmp(page, "repair")!=0 && strcmp(page, "repair\n")!=0)
1790 return -EINVAL;
1791 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1792 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1793 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1794 md_wakeup_thread(mddev->thread);
1795 return len;
1796}
1797
NeilBrown9d888832005-11-08 21:39:26 -08001798static ssize_t
1799md_show_mismatch(mddev_t *mddev, char *page)
1800{
1801 return sprintf(page, "%llu\n",
1802 (unsigned long long) mddev->resync_mismatches);
1803}
1804
NeilBrown24dd4692005-11-08 21:39:26 -08001805static struct md_sysfs_entry md_scan_mode = {
1806 .attr = {.name = "scan_mode", .mode = S_IRUGO|S_IWUSR },
1807 .show = md_show_scan,
1808 .store = md_store_scan,
1809};
1810
NeilBrown9d888832005-11-08 21:39:26 -08001811static struct md_sysfs_entry md_mismatches = {
1812 .attr = {.name = "mismatch_cnt", .mode = S_IRUGO },
1813 .show = md_show_mismatch,
1814};
1815
NeilBrowneae17012005-11-08 21:39:23 -08001816static struct attribute *md_default_attrs[] = {
1817 &md_level.attr,
1818 &md_raid_disks.attr,
NeilBrown24dd4692005-11-08 21:39:26 -08001819 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08001820 &md_mismatches.attr,
NeilBrowneae17012005-11-08 21:39:23 -08001821 NULL,
1822};
1823
1824static ssize_t
1825md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1826{
1827 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1828 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1829
1830 if (!entry->show)
1831 return -EIO;
1832 return entry->show(mddev, page);
1833}
1834
1835static ssize_t
1836md_attr_store(struct kobject *kobj, struct attribute *attr,
1837 const char *page, size_t length)
1838{
1839 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1840 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1841
1842 if (!entry->store)
1843 return -EIO;
1844 return entry->store(mddev, page, length);
1845}
1846
1847static void md_free(struct kobject *ko)
1848{
1849 mddev_t *mddev = container_of(ko, mddev_t, kobj);
1850 kfree(mddev);
1851}
1852
1853static struct sysfs_ops md_sysfs_ops = {
1854 .show = md_attr_show,
1855 .store = md_attr_store,
1856};
1857static struct kobj_type md_ktype = {
1858 .release = md_free,
1859 .sysfs_ops = &md_sysfs_ops,
1860 .default_attrs = md_default_attrs,
1861};
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863int mdp_major = 0;
1864
1865static struct kobject *md_probe(dev_t dev, int *part, void *data)
1866{
1867 static DECLARE_MUTEX(disks_sem);
1868 mddev_t *mddev = mddev_find(dev);
1869 struct gendisk *disk;
1870 int partitioned = (MAJOR(dev) != MD_MAJOR);
1871 int shift = partitioned ? MdpMinorShift : 0;
1872 int unit = MINOR(dev) >> shift;
1873
1874 if (!mddev)
1875 return NULL;
1876
1877 down(&disks_sem);
1878 if (mddev->gendisk) {
1879 up(&disks_sem);
1880 mddev_put(mddev);
1881 return NULL;
1882 }
1883 disk = alloc_disk(1 << shift);
1884 if (!disk) {
1885 up(&disks_sem);
1886 mddev_put(mddev);
1887 return NULL;
1888 }
1889 disk->major = MAJOR(dev);
1890 disk->first_minor = unit << shift;
1891 if (partitioned) {
1892 sprintf(disk->disk_name, "md_d%d", unit);
1893 sprintf(disk->devfs_name, "md/d%d", unit);
1894 } else {
1895 sprintf(disk->disk_name, "md%d", unit);
1896 sprintf(disk->devfs_name, "md/%d", unit);
1897 }
1898 disk->fops = &md_fops;
1899 disk->private_data = mddev;
1900 disk->queue = mddev->queue;
1901 add_disk(disk);
1902 mddev->gendisk = disk;
1903 up(&disks_sem);
NeilBrown9c791972005-11-08 21:39:28 -08001904 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08001905 mddev->kobj.k_name = NULL;
1906 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
1907 mddev->kobj.ktype = &md_ktype;
1908 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return NULL;
1910}
1911
1912void md_wakeup_thread(mdk_thread_t *thread);
1913
1914static void md_safemode_timeout(unsigned long data)
1915{
1916 mddev_t *mddev = (mddev_t *) data;
1917
1918 mddev->safemode = 1;
1919 md_wakeup_thread(mddev->thread);
1920}
1921
1922
1923static int do_md_run(mddev_t * mddev)
1924{
1925 int pnum, err;
1926 int chunk_size;
1927 struct list_head *tmp;
1928 mdk_rdev_t *rdev;
1929 struct gendisk *disk;
1930 char b[BDEVNAME_SIZE];
1931
NeilBrowna757e642005-04-16 15:26:42 -07001932 if (list_empty(&mddev->disks))
1933 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 if (mddev->pers)
1937 return -EBUSY;
1938
1939 /*
1940 * Analyze all RAID superblock(s)
1941 */
NeilBrowna757e642005-04-16 15:26:42 -07001942 if (!mddev->raid_disks)
1943 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 chunk_size = mddev->chunk_size;
1946 pnum = level_to_pers(mddev->level);
1947
1948 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1949 if (!chunk_size) {
1950 /*
1951 * 'default chunksize' in the old md code used to
1952 * be PAGE_SIZE, baaad.
1953 * we abort here to be on the safe side. We don't
1954 * want to continue the bad practice.
1955 */
1956 printk(KERN_ERR
1957 "no chunksize specified, see 'man raidtab'\n");
1958 return -EINVAL;
1959 }
1960 if (chunk_size > MAX_CHUNK_SIZE) {
1961 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1962 chunk_size, MAX_CHUNK_SIZE);
1963 return -EINVAL;
1964 }
1965 /*
1966 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1967 */
1968 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07001969 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return -EINVAL;
1971 }
1972 if (chunk_size < PAGE_SIZE) {
1973 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1974 chunk_size, PAGE_SIZE);
1975 return -EINVAL;
1976 }
1977
1978 /* devices must have minimum size of one chunk */
1979 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001980 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 continue;
1982 if (rdev->size < chunk_size / 1024) {
1983 printk(KERN_WARNING
1984 "md: Dev %s smaller than chunk_size:"
1985 " %lluk < %dk\n",
1986 bdevname(rdev->bdev,b),
1987 (unsigned long long)rdev->size,
1988 chunk_size / 1024);
1989 return -EINVAL;
1990 }
1991 }
1992 }
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994#ifdef CONFIG_KMOD
1995 if (!pers[pnum])
1996 {
1997 request_module("md-personality-%d", pnum);
1998 }
1999#endif
2000
2001 /*
2002 * Drop all container device buffers, from now on
2003 * the only valid external interface is through the md
2004 * device.
2005 * Also find largest hardsector size
2006 */
2007 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002008 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 continue;
2010 sync_blockdev(rdev->bdev);
2011 invalidate_bdev(rdev->bdev, 0);
2012 }
2013
2014 md_probe(mddev->unit, NULL, NULL);
2015 disk = mddev->gendisk;
2016 if (!disk)
2017 return -ENOMEM;
2018
2019 spin_lock(&pers_lock);
2020 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
2021 spin_unlock(&pers_lock);
2022 printk(KERN_WARNING "md: personality %d is not loaded!\n",
2023 pnum);
2024 return -EINVAL;
2025 }
2026
2027 mddev->pers = pers[pnum];
2028 spin_unlock(&pers_lock);
2029
NeilBrown657390d2005-08-26 18:34:16 -07002030 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08002032 mddev->barriers_work = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
NeilBrownf91de922005-11-08 21:39:36 -08002034 if (start_readonly)
2035 mddev->ro = 2; /* read-only, but switch on first write */
2036
NeilBrown32a76272005-06-21 17:17:14 -07002037 /* before we start the array running, initialise the bitmap */
2038 err = bitmap_create(mddev);
2039 if (err)
2040 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2041 mdname(mddev), err);
2042 else
2043 err = mddev->pers->run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (err) {
2045 printk(KERN_ERR "md: pers->run() failed ...\n");
2046 module_put(mddev->pers->owner);
2047 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002048 bitmap_destroy(mddev);
2049 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051 atomic_set(&mddev->writes_pending,0);
2052 mddev->safemode = 0;
2053 mddev->safemode_timer.function = md_safemode_timeout;
2054 mddev->safemode_timer.data = (unsigned long) mddev;
2055 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2056 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002057
2058 ITERATE_RDEV(mddev,rdev,tmp)
2059 if (rdev->raid_disk >= 0) {
2060 char nm[20];
2061 sprintf(nm, "rd%d", rdev->raid_disk);
2062 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002066 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 if (mddev->sb_dirty)
2069 md_update_sb(mddev);
2070
2071 set_capacity(disk, mddev->array_size<<1);
2072
2073 /* If we call blk_queue_make_request here, it will
2074 * re-initialise max_sectors etc which may have been
2075 * refined inside -> run. So just set the bits we need to set.
2076 * Most initialisation happended when we called
2077 * blk_queue_make_request(..., md_fail_request)
2078 * earlier.
2079 */
2080 mddev->queue->queuedata = mddev;
2081 mddev->queue->make_request_fn = mddev->pers->make_request;
2082
2083 mddev->changed = 1;
2084 return 0;
2085}
2086
2087static int restart_array(mddev_t *mddev)
2088{
2089 struct gendisk *disk = mddev->gendisk;
2090 int err;
2091
2092 /*
2093 * Complain if it has no devices
2094 */
2095 err = -ENXIO;
2096 if (list_empty(&mddev->disks))
2097 goto out;
2098
2099 if (mddev->pers) {
2100 err = -EBUSY;
2101 if (!mddev->ro)
2102 goto out;
2103
2104 mddev->safemode = 0;
2105 mddev->ro = 0;
2106 set_disk_ro(disk, 0);
2107
2108 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2109 mdname(mddev));
2110 /*
2111 * Kick recovery or resync if necessary
2112 */
2113 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2114 md_wakeup_thread(mddev->thread);
2115 err = 0;
2116 } else {
2117 printk(KERN_ERR "md: %s has no personality assigned.\n",
2118 mdname(mddev));
2119 err = -EINVAL;
2120 }
2121
2122out:
2123 return err;
2124}
2125
2126static int do_md_stop(mddev_t * mddev, int ro)
2127{
2128 int err = 0;
2129 struct gendisk *disk = mddev->gendisk;
2130
2131 if (mddev->pers) {
2132 if (atomic_read(&mddev->active)>2) {
2133 printk("md: %s still in use.\n",mdname(mddev));
2134 return -EBUSY;
2135 }
2136
2137 if (mddev->sync_thread) {
2138 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2139 md_unregister_thread(mddev->sync_thread);
2140 mddev->sync_thread = NULL;
2141 }
2142
2143 del_timer_sync(&mddev->safemode_timer);
2144
2145 invalidate_partition(disk, 0);
2146
2147 if (ro) {
2148 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08002149 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 goto out;
2151 mddev->ro = 1;
2152 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002153 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08002154 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (mddev->ro)
2156 set_disk_ro(disk, 0);
2157 blk_queue_make_request(mddev->queue, md_fail_request);
2158 mddev->pers->stop(mddev);
2159 module_put(mddev->pers->owner);
2160 mddev->pers = NULL;
2161 if (mddev->ro)
2162 mddev->ro = 0;
2163 }
2164 if (!mddev->in_sync) {
2165 /* mark array as shutdown cleanly */
2166 mddev->in_sync = 1;
2167 md_update_sb(mddev);
2168 }
2169 if (ro)
2170 set_disk_ro(disk, 1);
2171 }
NeilBrown32a76272005-06-21 17:17:14 -07002172
2173 bitmap_destroy(mddev);
2174 if (mddev->bitmap_file) {
2175 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2176 fput(mddev->bitmap_file);
2177 mddev->bitmap_file = NULL;
2178 }
NeilBrown92232142005-08-18 11:24:16 -07002179 mddev->bitmap_offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07002180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /*
2182 * Free resources if final stop
2183 */
2184 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002185 mdk_rdev_t *rdev;
2186 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 struct gendisk *disk;
2188 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2189
NeilBrown86e6ffd2005-11-08 21:39:24 -08002190 ITERATE_RDEV(mddev,rdev,tmp)
2191 if (rdev->raid_disk >= 0) {
2192 char nm[20];
2193 sprintf(nm, "rd%d", rdev->raid_disk);
2194 sysfs_remove_link(&mddev->kobj, nm);
2195 }
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 export_array(mddev);
2198
2199 mddev->array_size = 0;
2200 disk = mddev->gendisk;
2201 if (disk)
2202 set_capacity(disk, 0);
2203 mddev->changed = 1;
2204 } else
2205 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2206 mdname(mddev));
2207 err = 0;
2208out:
2209 return err;
2210}
2211
2212static void autorun_array(mddev_t *mddev)
2213{
2214 mdk_rdev_t *rdev;
2215 struct list_head *tmp;
2216 int err;
2217
NeilBrowna757e642005-04-16 15:26:42 -07002218 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 printk(KERN_INFO "md: running: ");
2222
2223 ITERATE_RDEV(mddev,rdev,tmp) {
2224 char b[BDEVNAME_SIZE];
2225 printk("<%s>", bdevname(rdev->bdev,b));
2226 }
2227 printk("\n");
2228
2229 err = do_md_run (mddev);
2230 if (err) {
2231 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2232 do_md_stop (mddev, 0);
2233 }
2234}
2235
2236/*
2237 * lets try to run arrays based on all disks that have arrived
2238 * until now. (those are in pending_raid_disks)
2239 *
2240 * the method: pick the first pending disk, collect all disks with
2241 * the same UUID, remove all from the pending list and put them into
2242 * the 'same_array' list. Then order this list based on superblock
2243 * update time (freshest comes first), kick out 'old' disks and
2244 * compare superblocks. If everything's fine then run it.
2245 *
2246 * If "unit" is allocated, then bump its reference count
2247 */
2248static void autorun_devices(int part)
2249{
2250 struct list_head candidates;
2251 struct list_head *tmp;
2252 mdk_rdev_t *rdev0, *rdev;
2253 mddev_t *mddev;
2254 char b[BDEVNAME_SIZE];
2255
2256 printk(KERN_INFO "md: autorun ...\n");
2257 while (!list_empty(&pending_raid_disks)) {
2258 dev_t dev;
2259 rdev0 = list_entry(pending_raid_disks.next,
2260 mdk_rdev_t, same_set);
2261
2262 printk(KERN_INFO "md: considering %s ...\n",
2263 bdevname(rdev0->bdev,b));
2264 INIT_LIST_HEAD(&candidates);
2265 ITERATE_RDEV_PENDING(rdev,tmp)
2266 if (super_90_load(rdev, rdev0, 0) >= 0) {
2267 printk(KERN_INFO "md: adding %s ...\n",
2268 bdevname(rdev->bdev,b));
2269 list_move(&rdev->same_set, &candidates);
2270 }
2271 /*
2272 * now we have a set of devices, with all of them having
2273 * mostly sane superblocks. It's time to allocate the
2274 * mddev.
2275 */
2276 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2277 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2278 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2279 break;
2280 }
2281 if (part)
2282 dev = MKDEV(mdp_major,
2283 rdev0->preferred_minor << MdpMinorShift);
2284 else
2285 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2286
2287 md_probe(dev, NULL, NULL);
2288 mddev = mddev_find(dev);
2289 if (!mddev) {
2290 printk(KERN_ERR
2291 "md: cannot allocate memory for md drive.\n");
2292 break;
2293 }
2294 if (mddev_lock(mddev))
2295 printk(KERN_WARNING "md: %s locked, cannot run\n",
2296 mdname(mddev));
2297 else if (mddev->raid_disks || mddev->major_version
2298 || !list_empty(&mddev->disks)) {
2299 printk(KERN_WARNING
2300 "md: %s already running, cannot run %s\n",
2301 mdname(mddev), bdevname(rdev0->bdev,b));
2302 mddev_unlock(mddev);
2303 } else {
2304 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2305 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2306 list_del_init(&rdev->same_set);
2307 if (bind_rdev_to_array(rdev, mddev))
2308 export_rdev(rdev);
2309 }
2310 autorun_array(mddev);
2311 mddev_unlock(mddev);
2312 }
2313 /* on success, candidates will be empty, on error
2314 * it won't...
2315 */
2316 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2317 export_rdev(rdev);
2318 mddev_put(mddev);
2319 }
2320 printk(KERN_INFO "md: ... autorun DONE.\n");
2321}
2322
2323/*
2324 * import RAID devices based on one partition
2325 * if possible, the array gets run as well.
2326 */
2327
2328static int autostart_array(dev_t startdev)
2329{
2330 char b[BDEVNAME_SIZE];
2331 int err = -EINVAL, i;
2332 mdp_super_t *sb = NULL;
2333 mdk_rdev_t *start_rdev = NULL, *rdev;
2334
2335 start_rdev = md_import_device(startdev, 0, 0);
2336 if (IS_ERR(start_rdev))
2337 return err;
2338
2339
2340 /* NOTE: this can only work for 0.90.0 superblocks */
2341 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2342 if (sb->major_version != 0 ||
2343 sb->minor_version != 90 ) {
2344 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2345 export_rdev(start_rdev);
2346 return err;
2347 }
2348
NeilBrownb2d444d2005-11-08 21:39:31 -08002349 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 printk(KERN_WARNING
2351 "md: can not autostart based on faulty %s!\n",
2352 bdevname(start_rdev->bdev,b));
2353 export_rdev(start_rdev);
2354 return err;
2355 }
2356 list_add(&start_rdev->same_set, &pending_raid_disks);
2357
2358 for (i = 0; i < MD_SB_DISKS; i++) {
2359 mdp_disk_t *desc = sb->disks + i;
2360 dev_t dev = MKDEV(desc->major, desc->minor);
2361
2362 if (!dev)
2363 continue;
2364 if (dev == startdev)
2365 continue;
2366 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2367 continue;
2368 rdev = md_import_device(dev, 0, 0);
2369 if (IS_ERR(rdev))
2370 continue;
2371
2372 list_add(&rdev->same_set, &pending_raid_disks);
2373 }
2374
2375 /*
2376 * possibly return codes
2377 */
2378 autorun_devices(0);
2379 return 0;
2380
2381}
2382
2383
2384static int get_version(void __user * arg)
2385{
2386 mdu_version_t ver;
2387
2388 ver.major = MD_MAJOR_VERSION;
2389 ver.minor = MD_MINOR_VERSION;
2390 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2391
2392 if (copy_to_user(arg, &ver, sizeof(ver)))
2393 return -EFAULT;
2394
2395 return 0;
2396}
2397
2398static int get_array_info(mddev_t * mddev, void __user * arg)
2399{
2400 mdu_array_info_t info;
2401 int nr,working,active,failed,spare;
2402 mdk_rdev_t *rdev;
2403 struct list_head *tmp;
2404
2405 nr=working=active=failed=spare=0;
2406 ITERATE_RDEV(mddev,rdev,tmp) {
2407 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002408 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 failed++;
2410 else {
2411 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002412 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 active++;
2414 else
2415 spare++;
2416 }
2417 }
2418
2419 info.major_version = mddev->major_version;
2420 info.minor_version = mddev->minor_version;
2421 info.patch_version = MD_PATCHLEVEL_VERSION;
2422 info.ctime = mddev->ctime;
2423 info.level = mddev->level;
2424 info.size = mddev->size;
2425 info.nr_disks = nr;
2426 info.raid_disks = mddev->raid_disks;
2427 info.md_minor = mddev->md_minor;
2428 info.not_persistent= !mddev->persistent;
2429
2430 info.utime = mddev->utime;
2431 info.state = 0;
2432 if (mddev->in_sync)
2433 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07002434 if (mddev->bitmap && mddev->bitmap_offset)
2435 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 info.active_disks = active;
2437 info.working_disks = working;
2438 info.failed_disks = failed;
2439 info.spare_disks = spare;
2440
2441 info.layout = mddev->layout;
2442 info.chunk_size = mddev->chunk_size;
2443
2444 if (copy_to_user(arg, &info, sizeof(info)))
2445 return -EFAULT;
2446
2447 return 0;
2448}
2449
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002450static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07002451{
2452 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2453 char *ptr, *buf = NULL;
2454 int err = -ENOMEM;
2455
2456 file = kmalloc(sizeof(*file), GFP_KERNEL);
2457 if (!file)
2458 goto out;
2459
2460 /* bitmap disabled, zero the first byte and copy out */
2461 if (!mddev->bitmap || !mddev->bitmap->file) {
2462 file->pathname[0] = '\0';
2463 goto copy_out;
2464 }
2465
2466 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2467 if (!buf)
2468 goto out;
2469
2470 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2471 if (!ptr)
2472 goto out;
2473
2474 strcpy(file->pathname, ptr);
2475
2476copy_out:
2477 err = 0;
2478 if (copy_to_user(arg, file, sizeof(*file)))
2479 err = -EFAULT;
2480out:
2481 kfree(buf);
2482 kfree(file);
2483 return err;
2484}
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486static int get_disk_info(mddev_t * mddev, void __user * arg)
2487{
2488 mdu_disk_info_t info;
2489 unsigned int nr;
2490 mdk_rdev_t *rdev;
2491
2492 if (copy_from_user(&info, arg, sizeof(info)))
2493 return -EFAULT;
2494
2495 nr = info.number;
2496
2497 rdev = find_rdev_nr(mddev, nr);
2498 if (rdev) {
2499 info.major = MAJOR(rdev->bdev->bd_dev);
2500 info.minor = MINOR(rdev->bdev->bd_dev);
2501 info.raid_disk = rdev->raid_disk;
2502 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08002503 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002505 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 info.state |= (1<<MD_DISK_ACTIVE);
2507 info.state |= (1<<MD_DISK_SYNC);
2508 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002509 if (test_bit(WriteMostly, &rdev->flags))
2510 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 } else {
2512 info.major = info.minor = 0;
2513 info.raid_disk = -1;
2514 info.state = (1<<MD_DISK_REMOVED);
2515 }
2516
2517 if (copy_to_user(arg, &info, sizeof(info)))
2518 return -EFAULT;
2519
2520 return 0;
2521}
2522
2523static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2524{
2525 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2526 mdk_rdev_t *rdev;
2527 dev_t dev = MKDEV(info->major,info->minor);
2528
2529 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2530 return -EOVERFLOW;
2531
2532 if (!mddev->raid_disks) {
2533 int err;
2534 /* expecting a device which has a superblock */
2535 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2536 if (IS_ERR(rdev)) {
2537 printk(KERN_WARNING
2538 "md: md_import_device returned %ld\n",
2539 PTR_ERR(rdev));
2540 return PTR_ERR(rdev);
2541 }
2542 if (!list_empty(&mddev->disks)) {
2543 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2544 mdk_rdev_t, same_set);
2545 int err = super_types[mddev->major_version]
2546 .load_super(rdev, rdev0, mddev->minor_version);
2547 if (err < 0) {
2548 printk(KERN_WARNING
2549 "md: %s has different UUID to %s\n",
2550 bdevname(rdev->bdev,b),
2551 bdevname(rdev0->bdev,b2));
2552 export_rdev(rdev);
2553 return -EINVAL;
2554 }
2555 }
2556 err = bind_rdev_to_array(rdev, mddev);
2557 if (err)
2558 export_rdev(rdev);
2559 return err;
2560 }
2561
2562 /*
2563 * add_new_disk can be used once the array is assembled
2564 * to add "hot spares". They must already have a superblock
2565 * written
2566 */
2567 if (mddev->pers) {
2568 int err;
2569 if (!mddev->pers->hot_add_disk) {
2570 printk(KERN_WARNING
2571 "%s: personality does not support diskops!\n",
2572 mdname(mddev));
2573 return -EINVAL;
2574 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07002575 if (mddev->persistent)
2576 rdev = md_import_device(dev, mddev->major_version,
2577 mddev->minor_version);
2578 else
2579 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (IS_ERR(rdev)) {
2581 printk(KERN_WARNING
2582 "md: md_import_device returned %ld\n",
2583 PTR_ERR(rdev));
2584 return PTR_ERR(rdev);
2585 }
NeilBrown41158c72005-06-21 17:17:25 -07002586 /* set save_raid_disk if appropriate */
2587 if (!mddev->persistent) {
2588 if (info->state & (1<<MD_DISK_SYNC) &&
2589 info->raid_disk < mddev->raid_disks)
2590 rdev->raid_disk = info->raid_disk;
2591 else
2592 rdev->raid_disk = -1;
2593 } else
2594 super_types[mddev->major_version].
2595 validate_super(mddev, rdev);
2596 rdev->saved_raid_disk = rdev->raid_disk;
2597
NeilBrownb2d444d2005-11-08 21:39:31 -08002598 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002599 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2600 set_bit(WriteMostly, &rdev->flags);
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 rdev->raid_disk = -1;
2603 err = bind_rdev_to_array(rdev, mddev);
2604 if (err)
2605 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07002606
2607 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002608 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 return err;
2610 }
2611
2612 /* otherwise, add_new_disk is only allowed
2613 * for major_version==0 superblocks
2614 */
2615 if (mddev->major_version != 0) {
2616 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2617 mdname(mddev));
2618 return -EINVAL;
2619 }
2620
2621 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2622 int err;
2623 rdev = md_import_device (dev, -1, 0);
2624 if (IS_ERR(rdev)) {
2625 printk(KERN_WARNING
2626 "md: error, md_import_device() returned %ld\n",
2627 PTR_ERR(rdev));
2628 return PTR_ERR(rdev);
2629 }
2630 rdev->desc_nr = info->number;
2631 if (info->raid_disk < mddev->raid_disks)
2632 rdev->raid_disk = info->raid_disk;
2633 else
2634 rdev->raid_disk = -1;
2635
NeilBrownb2d444d2005-11-08 21:39:31 -08002636 rdev->flags = 0;
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08002639 if (info->state & (1<<MD_DISK_SYNC))
2640 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002642 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2643 set_bit(WriteMostly, &rdev->flags);
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 err = bind_rdev_to_array(rdev, mddev);
2646 if (err) {
2647 export_rdev(rdev);
2648 return err;
2649 }
2650
2651 if (!mddev->persistent) {
2652 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2653 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2654 } else
2655 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2656 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2657
2658 if (!mddev->size || (mddev->size > rdev->size))
2659 mddev->size = rdev->size;
2660 }
2661
2662 return 0;
2663}
2664
2665static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2666{
2667 char b[BDEVNAME_SIZE];
2668 mdk_rdev_t *rdev;
2669
2670 if (!mddev->pers)
2671 return -ENODEV;
2672
2673 rdev = find_rdev(mddev, dev);
2674 if (!rdev)
2675 return -ENXIO;
2676
2677 if (rdev->raid_disk >= 0)
2678 goto busy;
2679
2680 kick_rdev_from_array(rdev);
2681 md_update_sb(mddev);
2682
2683 return 0;
2684busy:
2685 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2686 bdevname(rdev->bdev,b), mdname(mddev));
2687 return -EBUSY;
2688}
2689
2690static int hot_add_disk(mddev_t * mddev, dev_t dev)
2691{
2692 char b[BDEVNAME_SIZE];
2693 int err;
2694 unsigned int size;
2695 mdk_rdev_t *rdev;
2696
2697 if (!mddev->pers)
2698 return -ENODEV;
2699
2700 if (mddev->major_version != 0) {
2701 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2702 " version-0 superblocks.\n",
2703 mdname(mddev));
2704 return -EINVAL;
2705 }
2706 if (!mddev->pers->hot_add_disk) {
2707 printk(KERN_WARNING
2708 "%s: personality does not support diskops!\n",
2709 mdname(mddev));
2710 return -EINVAL;
2711 }
2712
2713 rdev = md_import_device (dev, -1, 0);
2714 if (IS_ERR(rdev)) {
2715 printk(KERN_WARNING
2716 "md: error, md_import_device() returned %ld\n",
2717 PTR_ERR(rdev));
2718 return -EINVAL;
2719 }
2720
2721 if (mddev->persistent)
2722 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2723 else
2724 rdev->sb_offset =
2725 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2726
2727 size = calc_dev_size(rdev, mddev->chunk_size);
2728 rdev->size = size;
2729
2730 if (size < mddev->size) {
2731 printk(KERN_WARNING
2732 "%s: disk size %llu blocks < array size %llu\n",
2733 mdname(mddev), (unsigned long long)size,
2734 (unsigned long long)mddev->size);
2735 err = -ENOSPC;
2736 goto abort_export;
2737 }
2738
NeilBrownb2d444d2005-11-08 21:39:31 -08002739 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 printk(KERN_WARNING
2741 "md: can not hot-add faulty %s disk to %s!\n",
2742 bdevname(rdev->bdev,b), mdname(mddev));
2743 err = -EINVAL;
2744 goto abort_export;
2745 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002746 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 rdev->desc_nr = -1;
2748 bind_rdev_to_array(rdev, mddev);
2749
2750 /*
2751 * The rest should better be atomic, we can have disk failures
2752 * noticed in interrupt contexts ...
2753 */
2754
2755 if (rdev->desc_nr == mddev->max_disks) {
2756 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2757 mdname(mddev));
2758 err = -EBUSY;
2759 goto abort_unbind_export;
2760 }
2761
2762 rdev->raid_disk = -1;
2763
2764 md_update_sb(mddev);
2765
2766 /*
2767 * Kick recovery, maybe this spare has to be added to the
2768 * array immediately.
2769 */
2770 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2771 md_wakeup_thread(mddev->thread);
2772
2773 return 0;
2774
2775abort_unbind_export:
2776 unbind_rdev_from_array(rdev);
2777
2778abort_export:
2779 export_rdev(rdev);
2780 return err;
2781}
2782
NeilBrown32a76272005-06-21 17:17:14 -07002783/* similar to deny_write_access, but accounts for our holding a reference
2784 * to the file ourselves */
2785static int deny_bitmap_write_access(struct file * file)
2786{
2787 struct inode *inode = file->f_mapping->host;
2788
2789 spin_lock(&inode->i_lock);
2790 if (atomic_read(&inode->i_writecount) > 1) {
2791 spin_unlock(&inode->i_lock);
2792 return -ETXTBSY;
2793 }
2794 atomic_set(&inode->i_writecount, -1);
2795 spin_unlock(&inode->i_lock);
2796
2797 return 0;
2798}
2799
2800static int set_bitmap_file(mddev_t *mddev, int fd)
2801{
2802 int err;
2803
NeilBrown36fa3062005-09-09 16:23:45 -07002804 if (mddev->pers) {
2805 if (!mddev->pers->quiesce)
2806 return -EBUSY;
2807 if (mddev->recovery || mddev->sync_thread)
2808 return -EBUSY;
2809 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07002810 }
2811
NeilBrown36fa3062005-09-09 16:23:45 -07002812
2813 if (fd >= 0) {
2814 if (mddev->bitmap)
2815 return -EEXIST; /* cannot add when bitmap is present */
2816 mddev->bitmap_file = fget(fd);
2817
2818 if (mddev->bitmap_file == NULL) {
2819 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2820 mdname(mddev));
2821 return -EBADF;
2822 }
2823
2824 err = deny_bitmap_write_access(mddev->bitmap_file);
2825 if (err) {
2826 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2827 mdname(mddev));
2828 fput(mddev->bitmap_file);
2829 mddev->bitmap_file = NULL;
2830 return err;
2831 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002832 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07002833 } else if (mddev->bitmap == NULL)
2834 return -ENOENT; /* cannot remove what isn't there */
2835 err = 0;
2836 if (mddev->pers) {
2837 mddev->pers->quiesce(mddev, 1);
2838 if (fd >= 0)
2839 err = bitmap_create(mddev);
2840 if (fd < 0 || err)
2841 bitmap_destroy(mddev);
2842 mddev->pers->quiesce(mddev, 0);
2843 } else if (fd < 0) {
2844 if (mddev->bitmap_file)
2845 fput(mddev->bitmap_file);
2846 mddev->bitmap_file = NULL;
2847 }
2848
NeilBrown32a76272005-06-21 17:17:14 -07002849 return err;
2850}
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852/*
2853 * set_array_info is used two different ways
2854 * The original usage is when creating a new array.
2855 * In this usage, raid_disks is > 0 and it together with
2856 * level, size, not_persistent,layout,chunksize determine the
2857 * shape of the array.
2858 * This will always create an array with a type-0.90.0 superblock.
2859 * The newer usage is when assembling an array.
2860 * In this case raid_disks will be 0, and the major_version field is
2861 * use to determine which style super-blocks are to be found on the devices.
2862 * The minor and patch _version numbers are also kept incase the
2863 * super_block handler wishes to interpret them.
2864 */
2865static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2866{
2867
2868 if (info->raid_disks == 0) {
2869 /* just setting version number for superblock loading */
2870 if (info->major_version < 0 ||
2871 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2872 super_types[info->major_version].name == NULL) {
2873 /* maybe try to auto-load a module? */
2874 printk(KERN_INFO
2875 "md: superblock version %d not known\n",
2876 info->major_version);
2877 return -EINVAL;
2878 }
2879 mddev->major_version = info->major_version;
2880 mddev->minor_version = info->minor_version;
2881 mddev->patch_version = info->patch_version;
2882 return 0;
2883 }
2884 mddev->major_version = MD_MAJOR_VERSION;
2885 mddev->minor_version = MD_MINOR_VERSION;
2886 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2887 mddev->ctime = get_seconds();
2888
2889 mddev->level = info->level;
2890 mddev->size = info->size;
2891 mddev->raid_disks = info->raid_disks;
2892 /* don't set md_minor, it is determined by which /dev/md* was
2893 * openned
2894 */
2895 if (info->state & (1<<MD_SB_CLEAN))
2896 mddev->recovery_cp = MaxSector;
2897 else
2898 mddev->recovery_cp = 0;
2899 mddev->persistent = ! info->not_persistent;
2900
2901 mddev->layout = info->layout;
2902 mddev->chunk_size = info->chunk_size;
2903
2904 mddev->max_disks = MD_SB_DISKS;
2905
2906 mddev->sb_dirty = 1;
2907
2908 /*
2909 * Generate a 128 bit UUID
2910 */
2911 get_random_bytes(mddev->uuid, 16);
2912
2913 return 0;
2914}
2915
2916/*
2917 * update_array_info is used to change the configuration of an
2918 * on-line array.
2919 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2920 * fields in the info are checked against the array.
2921 * Any differences that cannot be handled will cause an error.
2922 * Normally, only one change can be managed at a time.
2923 */
2924static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2925{
2926 int rv = 0;
2927 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07002928 int state = 0;
2929
2930 /* calculate expected state,ignoring low bits */
2931 if (mddev->bitmap && mddev->bitmap_offset)
2932 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 if (mddev->major_version != info->major_version ||
2935 mddev->minor_version != info->minor_version ||
2936/* mddev->patch_version != info->patch_version || */
2937 mddev->ctime != info->ctime ||
2938 mddev->level != info->level ||
2939/* mddev->layout != info->layout || */
2940 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07002941 mddev->chunk_size != info->chunk_size ||
2942 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2943 ((state^info->state) & 0xfffffe00)
2944 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return -EINVAL;
2946 /* Check there is only one change */
2947 if (mddev->size != info->size) cnt++;
2948 if (mddev->raid_disks != info->raid_disks) cnt++;
2949 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07002950 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if (cnt == 0) return 0;
2952 if (cnt > 1) return -EINVAL;
2953
2954 if (mddev->layout != info->layout) {
2955 /* Change layout
2956 * we don't need to do anything at the md level, the
2957 * personality will take care of it all.
2958 */
2959 if (mddev->pers->reconfig == NULL)
2960 return -EINVAL;
2961 else
2962 return mddev->pers->reconfig(mddev, info->layout, -1);
2963 }
2964 if (mddev->size != info->size) {
2965 mdk_rdev_t * rdev;
2966 struct list_head *tmp;
2967 if (mddev->pers->resize == NULL)
2968 return -EINVAL;
2969 /* The "size" is the amount of each device that is used.
2970 * This can only make sense for arrays with redundancy.
2971 * linear and raid0 always use whatever space is available
2972 * We can only consider changing the size if no resync
2973 * or reconstruction is happening, and if the new size
2974 * is acceptable. It must fit before the sb_offset or,
2975 * if that is <data_offset, it must fit before the
2976 * size of each device.
2977 * If size is zero, we find the largest size that fits.
2978 */
2979 if (mddev->sync_thread)
2980 return -EBUSY;
2981 ITERATE_RDEV(mddev,rdev,tmp) {
2982 sector_t avail;
2983 int fit = (info->size == 0);
2984 if (rdev->sb_offset > rdev->data_offset)
2985 avail = (rdev->sb_offset*2) - rdev->data_offset;
2986 else
2987 avail = get_capacity(rdev->bdev->bd_disk)
2988 - rdev->data_offset;
2989 if (fit && (info->size == 0 || info->size > avail/2))
2990 info->size = avail/2;
2991 if (avail < ((sector_t)info->size << 1))
2992 return -ENOSPC;
2993 }
2994 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
2995 if (!rv) {
2996 struct block_device *bdev;
2997
2998 bdev = bdget_disk(mddev->gendisk, 0);
2999 if (bdev) {
3000 down(&bdev->bd_inode->i_sem);
3001 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3002 up(&bdev->bd_inode->i_sem);
3003 bdput(bdev);
3004 }
3005 }
3006 }
3007 if (mddev->raid_disks != info->raid_disks) {
3008 /* change the number of raid disks */
3009 if (mddev->pers->reshape == NULL)
3010 return -EINVAL;
3011 if (info->raid_disks <= 0 ||
3012 info->raid_disks >= mddev->max_disks)
3013 return -EINVAL;
3014 if (mddev->sync_thread)
3015 return -EBUSY;
3016 rv = mddev->pers->reshape(mddev, info->raid_disks);
3017 if (!rv) {
3018 struct block_device *bdev;
3019
3020 bdev = bdget_disk(mddev->gendisk, 0);
3021 if (bdev) {
3022 down(&bdev->bd_inode->i_sem);
3023 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3024 up(&bdev->bd_inode->i_sem);
3025 bdput(bdev);
3026 }
3027 }
3028 }
NeilBrown36fa3062005-09-09 16:23:45 -07003029 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3030 if (mddev->pers->quiesce == NULL)
3031 return -EINVAL;
3032 if (mddev->recovery || mddev->sync_thread)
3033 return -EBUSY;
3034 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3035 /* add the bitmap */
3036 if (mddev->bitmap)
3037 return -EEXIST;
3038 if (mddev->default_bitmap_offset == 0)
3039 return -EINVAL;
3040 mddev->bitmap_offset = mddev->default_bitmap_offset;
3041 mddev->pers->quiesce(mddev, 1);
3042 rv = bitmap_create(mddev);
3043 if (rv)
3044 bitmap_destroy(mddev);
3045 mddev->pers->quiesce(mddev, 0);
3046 } else {
3047 /* remove the bitmap */
3048 if (!mddev->bitmap)
3049 return -ENOENT;
3050 if (mddev->bitmap->file)
3051 return -EINVAL;
3052 mddev->pers->quiesce(mddev, 1);
3053 bitmap_destroy(mddev);
3054 mddev->pers->quiesce(mddev, 0);
3055 mddev->bitmap_offset = 0;
3056 }
3057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 md_update_sb(mddev);
3059 return rv;
3060}
3061
3062static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3063{
3064 mdk_rdev_t *rdev;
3065
3066 if (mddev->pers == NULL)
3067 return -ENODEV;
3068
3069 rdev = find_rdev(mddev, dev);
3070 if (!rdev)
3071 return -ENODEV;
3072
3073 md_error(mddev, rdev);
3074 return 0;
3075}
3076
3077static int md_ioctl(struct inode *inode, struct file *file,
3078 unsigned int cmd, unsigned long arg)
3079{
3080 int err = 0;
3081 void __user *argp = (void __user *)arg;
3082 struct hd_geometry __user *loc = argp;
3083 mddev_t *mddev = NULL;
3084
3085 if (!capable(CAP_SYS_ADMIN))
3086 return -EACCES;
3087
3088 /*
3089 * Commands dealing with the RAID driver but not any
3090 * particular array:
3091 */
3092 switch (cmd)
3093 {
3094 case RAID_VERSION:
3095 err = get_version(argp);
3096 goto done;
3097
3098 case PRINT_RAID_DEBUG:
3099 err = 0;
3100 md_print_devices();
3101 goto done;
3102
3103#ifndef MODULE
3104 case RAID_AUTORUN:
3105 err = 0;
3106 autostart_arrays(arg);
3107 goto done;
3108#endif
3109 default:;
3110 }
3111
3112 /*
3113 * Commands creating/starting a new array:
3114 */
3115
3116 mddev = inode->i_bdev->bd_disk->private_data;
3117
3118 if (!mddev) {
3119 BUG();
3120 goto abort;
3121 }
3122
3123
3124 if (cmd == START_ARRAY) {
3125 /* START_ARRAY doesn't need to lock the array as autostart_array
3126 * does the locking, and it could even be a different array
3127 */
3128 static int cnt = 3;
3129 if (cnt > 0 ) {
3130 printk(KERN_WARNING
3131 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
3132 "This will not be supported beyond 2.6\n",
3133 current->comm, current->pid);
3134 cnt--;
3135 }
3136 err = autostart_array(new_decode_dev(arg));
3137 if (err) {
3138 printk(KERN_WARNING "md: autostart failed!\n");
3139 goto abort;
3140 }
3141 goto done;
3142 }
3143
3144 err = mddev_lock(mddev);
3145 if (err) {
3146 printk(KERN_INFO
3147 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3148 err, cmd);
3149 goto abort;
3150 }
3151
3152 switch (cmd)
3153 {
3154 case SET_ARRAY_INFO:
3155 {
3156 mdu_array_info_t info;
3157 if (!arg)
3158 memset(&info, 0, sizeof(info));
3159 else if (copy_from_user(&info, argp, sizeof(info))) {
3160 err = -EFAULT;
3161 goto abort_unlock;
3162 }
3163 if (mddev->pers) {
3164 err = update_array_info(mddev, &info);
3165 if (err) {
3166 printk(KERN_WARNING "md: couldn't update"
3167 " array info. %d\n", err);
3168 goto abort_unlock;
3169 }
3170 goto done_unlock;
3171 }
3172 if (!list_empty(&mddev->disks)) {
3173 printk(KERN_WARNING
3174 "md: array %s already has disks!\n",
3175 mdname(mddev));
3176 err = -EBUSY;
3177 goto abort_unlock;
3178 }
3179 if (mddev->raid_disks) {
3180 printk(KERN_WARNING
3181 "md: array %s already initialised!\n",
3182 mdname(mddev));
3183 err = -EBUSY;
3184 goto abort_unlock;
3185 }
3186 err = set_array_info(mddev, &info);
3187 if (err) {
3188 printk(KERN_WARNING "md: couldn't set"
3189 " array info. %d\n", err);
3190 goto abort_unlock;
3191 }
3192 }
3193 goto done_unlock;
3194
3195 default:;
3196 }
3197
3198 /*
3199 * Commands querying/configuring an existing array:
3200 */
NeilBrown32a76272005-06-21 17:17:14 -07003201 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3202 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3203 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3204 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 err = -ENODEV;
3206 goto abort_unlock;
3207 }
3208
3209 /*
3210 * Commands even a read-only array can execute:
3211 */
3212 switch (cmd)
3213 {
3214 case GET_ARRAY_INFO:
3215 err = get_array_info(mddev, argp);
3216 goto done_unlock;
3217
NeilBrown32a76272005-06-21 17:17:14 -07003218 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003219 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003220 goto done_unlock;
3221
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 case GET_DISK_INFO:
3223 err = get_disk_info(mddev, argp);
3224 goto done_unlock;
3225
3226 case RESTART_ARRAY_RW:
3227 err = restart_array(mddev);
3228 goto done_unlock;
3229
3230 case STOP_ARRAY:
3231 err = do_md_stop (mddev, 0);
3232 goto done_unlock;
3233
3234 case STOP_ARRAY_RO:
3235 err = do_md_stop (mddev, 1);
3236 goto done_unlock;
3237
3238 /*
3239 * We have a problem here : there is no easy way to give a CHS
3240 * virtual geometry. We currently pretend that we have a 2 heads
3241 * 4 sectors (with a BIG number of cylinders...). This drives
3242 * dosfs just mad... ;-)
3243 */
3244 case HDIO_GETGEO:
3245 if (!loc) {
3246 err = -EINVAL;
3247 goto abort_unlock;
3248 }
3249 err = put_user (2, (char __user *) &loc->heads);
3250 if (err)
3251 goto abort_unlock;
3252 err = put_user (4, (char __user *) &loc->sectors);
3253 if (err)
3254 goto abort_unlock;
3255 err = put_user(get_capacity(mddev->gendisk)/8,
3256 (short __user *) &loc->cylinders);
3257 if (err)
3258 goto abort_unlock;
3259 err = put_user (get_start_sect(inode->i_bdev),
3260 (long __user *) &loc->start);
3261 goto done_unlock;
3262 }
3263
3264 /*
3265 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08003266 * superblock, so we do not allow them on read-only arrays.
3267 * However non-MD ioctls (e.g. get-size) will still come through
3268 * here and hit the 'default' below, so only disallow
3269 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 */
NeilBrownf91de922005-11-08 21:39:36 -08003271 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3272 mddev->ro && mddev->pers) {
3273 if (mddev->ro == 2) {
3274 mddev->ro = 0;
3275 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3276 md_wakeup_thread(mddev->thread);
3277
3278 } else {
3279 err = -EROFS;
3280 goto abort_unlock;
3281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
3283
3284 switch (cmd)
3285 {
3286 case ADD_NEW_DISK:
3287 {
3288 mdu_disk_info_t info;
3289 if (copy_from_user(&info, argp, sizeof(info)))
3290 err = -EFAULT;
3291 else
3292 err = add_new_disk(mddev, &info);
3293 goto done_unlock;
3294 }
3295
3296 case HOT_REMOVE_DISK:
3297 err = hot_remove_disk(mddev, new_decode_dev(arg));
3298 goto done_unlock;
3299
3300 case HOT_ADD_DISK:
3301 err = hot_add_disk(mddev, new_decode_dev(arg));
3302 goto done_unlock;
3303
3304 case SET_DISK_FAULTY:
3305 err = set_disk_faulty(mddev, new_decode_dev(arg));
3306 goto done_unlock;
3307
3308 case RUN_ARRAY:
3309 err = do_md_run (mddev);
3310 goto done_unlock;
3311
NeilBrown32a76272005-06-21 17:17:14 -07003312 case SET_BITMAP_FILE:
3313 err = set_bitmap_file(mddev, (int)arg);
3314 goto done_unlock;
3315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 default:
3317 if (_IOC_TYPE(cmd) == MD_MAJOR)
3318 printk(KERN_WARNING "md: %s(pid %d) used"
3319 " obsolete MD ioctl, upgrade your"
3320 " software to use new ictls.\n",
3321 current->comm, current->pid);
3322 err = -EINVAL;
3323 goto abort_unlock;
3324 }
3325
3326done_unlock:
3327abort_unlock:
3328 mddev_unlock(mddev);
3329
3330 return err;
3331done:
3332 if (err)
3333 MD_BUG();
3334abort:
3335 return err;
3336}
3337
3338static int md_open(struct inode *inode, struct file *file)
3339{
3340 /*
3341 * Succeed if we can lock the mddev, which confirms that
3342 * it isn't being stopped right now.
3343 */
3344 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3345 int err;
3346
3347 if ((err = mddev_lock(mddev)))
3348 goto out;
3349
3350 err = 0;
3351 mddev_get(mddev);
3352 mddev_unlock(mddev);
3353
3354 check_disk_change(inode->i_bdev);
3355 out:
3356 return err;
3357}
3358
3359static int md_release(struct inode *inode, struct file * file)
3360{
3361 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3362
3363 if (!mddev)
3364 BUG();
3365 mddev_put(mddev);
3366
3367 return 0;
3368}
3369
3370static int md_media_changed(struct gendisk *disk)
3371{
3372 mddev_t *mddev = disk->private_data;
3373
3374 return mddev->changed;
3375}
3376
3377static int md_revalidate(struct gendisk *disk)
3378{
3379 mddev_t *mddev = disk->private_data;
3380
3381 mddev->changed = 0;
3382 return 0;
3383}
3384static struct block_device_operations md_fops =
3385{
3386 .owner = THIS_MODULE,
3387 .open = md_open,
3388 .release = md_release,
3389 .ioctl = md_ioctl,
3390 .media_changed = md_media_changed,
3391 .revalidate_disk= md_revalidate,
3392};
3393
Adrian Bunk75c96f82005-05-05 16:16:09 -07003394static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395{
3396 mdk_thread_t *thread = arg;
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 /*
3399 * md_thread is a 'system-thread', it's priority should be very
3400 * high. We avoid resource deadlocks individually in each
3401 * raid personality. (RAID5 does preallocation) We also use RR and
3402 * the very same RT priority as kswapd, thus we will never get
3403 * into a priority inversion deadlock.
3404 *
3405 * we definitely have to have equal or higher priority than
3406 * bdflush, otherwise bdflush will deadlock if there are too
3407 * many dirty RAID5 blocks.
3408 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
NeilBrown6985c432005-10-19 21:23:47 -07003410 allow_signal(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 complete(thread->event);
NeilBrowna6fb0932005-09-09 16:23:56 -07003412 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 void (*run)(mddev_t *);
3414
NeilBrown32a76272005-06-21 17:17:14 -07003415 wait_event_interruptible_timeout(thread->wqueue,
NeilBrowna6fb0932005-09-09 16:23:56 -07003416 test_bit(THREAD_WAKEUP, &thread->flags)
3417 || kthread_should_stop(),
NeilBrown32a76272005-06-21 17:17:14 -07003418 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003419 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
3421 clear_bit(THREAD_WAKEUP, &thread->flags);
3422
3423 run = thread->run;
3424 if (run)
3425 run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 }
NeilBrowna6fb0932005-09-09 16:23:56 -07003427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return 0;
3429}
3430
3431void md_wakeup_thread(mdk_thread_t *thread)
3432{
3433 if (thread) {
3434 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3435 set_bit(THREAD_WAKEUP, &thread->flags);
3436 wake_up(&thread->wqueue);
3437 }
3438}
3439
3440mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3441 const char *name)
3442{
3443 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 struct completion event;
3445
NeilBrowna6fb0932005-09-09 16:23:56 -07003446 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 if (!thread)
3448 return NULL;
3449
3450 memset(thread, 0, sizeof(mdk_thread_t));
3451 init_waitqueue_head(&thread->wqueue);
3452
3453 init_completion(&event);
3454 thread->event = &event;
3455 thread->run = run;
3456 thread->mddev = mddev;
3457 thread->name = name;
NeilBrown32a76272005-06-21 17:17:14 -07003458 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07003459 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07003460 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 kfree(thread);
3462 return NULL;
3463 }
3464 wait_for_completion(&event);
3465 return thread;
3466}
3467
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468void md_unregister_thread(mdk_thread_t *thread)
3469{
NeilBrownd28446f2005-04-16 15:26:41 -07003470 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07003471
3472 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 kfree(thread);
3474}
3475
3476void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3477{
3478 if (!mddev) {
3479 MD_BUG();
3480 return;
3481 }
3482
NeilBrownb2d444d2005-11-08 21:39:31 -08003483 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 return;
NeilBrown32a76272005-06-21 17:17:14 -07003485/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3487 mdname(mddev),
3488 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3489 __builtin_return_address(0),__builtin_return_address(1),
3490 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07003491*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 if (!mddev->pers->error_handler)
3493 return;
3494 mddev->pers->error_handler(mddev,rdev);
3495 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3496 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3497 md_wakeup_thread(mddev->thread);
3498}
3499
3500/* seq_file implementation /proc/mdstat */
3501
3502static void status_unused(struct seq_file *seq)
3503{
3504 int i = 0;
3505 mdk_rdev_t *rdev;
3506 struct list_head *tmp;
3507
3508 seq_printf(seq, "unused devices: ");
3509
3510 ITERATE_RDEV_PENDING(rdev,tmp) {
3511 char b[BDEVNAME_SIZE];
3512 i++;
3513 seq_printf(seq, "%s ",
3514 bdevname(rdev->bdev,b));
3515 }
3516 if (!i)
3517 seq_printf(seq, "<none>");
3518
3519 seq_printf(seq, "\n");
3520}
3521
3522
3523static void status_resync(struct seq_file *seq, mddev_t * mddev)
3524{
3525 unsigned long max_blocks, resync, res, dt, db, rt;
3526
3527 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3528
3529 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3530 max_blocks = mddev->resync_max_sectors >> 1;
3531 else
3532 max_blocks = mddev->size;
3533
3534 /*
3535 * Should not happen.
3536 */
3537 if (!max_blocks) {
3538 MD_BUG();
3539 return;
3540 }
3541 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3542 {
3543 int i, x = res/50, y = 20-x;
3544 seq_printf(seq, "[");
3545 for (i = 0; i < x; i++)
3546 seq_printf(seq, "=");
3547 seq_printf(seq, ">");
3548 for (i = 0; i < y; i++)
3549 seq_printf(seq, ".");
3550 seq_printf(seq, "] ");
3551 }
3552 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3553 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3554 "resync" : "recovery"),
3555 res/10, res % 10, resync, max_blocks);
3556
3557 /*
3558 * We do not want to overflow, so the order of operands and
3559 * the * 100 / 100 trick are important. We do a +1 to be
3560 * safe against division by zero. We only estimate anyway.
3561 *
3562 * dt: time from mark until now
3563 * db: blocks written from mark until now
3564 * rt: remaining time
3565 */
3566 dt = ((jiffies - mddev->resync_mark) / HZ);
3567 if (!dt) dt++;
3568 db = resync - (mddev->resync_mark_cnt/2);
3569 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3570
3571 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3572
3573 seq_printf(seq, " speed=%ldK/sec", db/dt);
3574}
3575
3576static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3577{
3578 struct list_head *tmp;
3579 loff_t l = *pos;
3580 mddev_t *mddev;
3581
3582 if (l >= 0x10000)
3583 return NULL;
3584 if (!l--)
3585 /* header */
3586 return (void*)1;
3587
3588 spin_lock(&all_mddevs_lock);
3589 list_for_each(tmp,&all_mddevs)
3590 if (!l--) {
3591 mddev = list_entry(tmp, mddev_t, all_mddevs);
3592 mddev_get(mddev);
3593 spin_unlock(&all_mddevs_lock);
3594 return mddev;
3595 }
3596 spin_unlock(&all_mddevs_lock);
3597 if (!l--)
3598 return (void*)2;/* tail */
3599 return NULL;
3600}
3601
3602static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3603{
3604 struct list_head *tmp;
3605 mddev_t *next_mddev, *mddev = v;
3606
3607 ++*pos;
3608 if (v == (void*)2)
3609 return NULL;
3610
3611 spin_lock(&all_mddevs_lock);
3612 if (v == (void*)1)
3613 tmp = all_mddevs.next;
3614 else
3615 tmp = mddev->all_mddevs.next;
3616 if (tmp != &all_mddevs)
3617 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3618 else {
3619 next_mddev = (void*)2;
3620 *pos = 0x10000;
3621 }
3622 spin_unlock(&all_mddevs_lock);
3623
3624 if (v != (void*)1)
3625 mddev_put(mddev);
3626 return next_mddev;
3627
3628}
3629
3630static void md_seq_stop(struct seq_file *seq, void *v)
3631{
3632 mddev_t *mddev = v;
3633
3634 if (mddev && v != (void*)1 && v != (void*)2)
3635 mddev_put(mddev);
3636}
3637
3638static int md_seq_show(struct seq_file *seq, void *v)
3639{
3640 mddev_t *mddev = v;
3641 sector_t size;
3642 struct list_head *tmp2;
3643 mdk_rdev_t *rdev;
3644 int i;
NeilBrown32a76272005-06-21 17:17:14 -07003645 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
3647 if (v == (void*)1) {
3648 seq_printf(seq, "Personalities : ");
3649 spin_lock(&pers_lock);
3650 for (i = 0; i < MAX_PERSONALITY; i++)
3651 if (pers[i])
3652 seq_printf(seq, "[%s] ", pers[i]->name);
3653
3654 spin_unlock(&pers_lock);
3655 seq_printf(seq, "\n");
3656 return 0;
3657 }
3658 if (v == (void*)2) {
3659 status_unused(seq);
3660 return 0;
3661 }
3662
3663 if (mddev_lock(mddev)!=0)
3664 return -EINTR;
3665 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3666 seq_printf(seq, "%s : %sactive", mdname(mddev),
3667 mddev->pers ? "" : "in");
3668 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08003669 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08003671 if (mddev->ro==2)
3672 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 seq_printf(seq, " %s", mddev->pers->name);
3674 }
3675
3676 size = 0;
3677 ITERATE_RDEV(mddev,rdev,tmp2) {
3678 char b[BDEVNAME_SIZE];
3679 seq_printf(seq, " %s[%d]",
3680 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003681 if (test_bit(WriteMostly, &rdev->flags))
3682 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08003683 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 seq_printf(seq, "(F)");
3685 continue;
NeilBrownb325a322005-09-09 16:24:00 -07003686 } else if (rdev->raid_disk < 0)
3687 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 size += rdev->size;
3689 }
3690
3691 if (!list_empty(&mddev->disks)) {
3692 if (mddev->pers)
3693 seq_printf(seq, "\n %llu blocks",
3694 (unsigned long long)mddev->array_size);
3695 else
3696 seq_printf(seq, "\n %llu blocks",
3697 (unsigned long long)size);
3698 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07003699 if (mddev->persistent) {
3700 if (mddev->major_version != 0 ||
3701 mddev->minor_version != 90) {
3702 seq_printf(seq," super %d.%d",
3703 mddev->major_version,
3704 mddev->minor_version);
3705 }
3706 } else
3707 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709 if (mddev->pers) {
3710 mddev->pers->status (seq, mddev);
3711 seq_printf(seq, "\n ");
NeilBrown32a76272005-06-21 17:17:14 -07003712 if (mddev->curr_resync > 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 status_resync (seq, mddev);
NeilBrown32a76272005-06-21 17:17:14 -07003714 seq_printf(seq, "\n ");
3715 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
NeilBrownf91de922005-11-08 21:39:36 -08003716 seq_printf(seq, "\tresync=DELAYED\n ");
3717 else if (mddev->recovery_cp < MaxSector)
3718 seq_printf(seq, "\tresync=PENDING\n ");
NeilBrown32a76272005-06-21 17:17:14 -07003719 } else
3720 seq_printf(seq, "\n ");
3721
3722 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07003723 unsigned long chunk_kb;
3724 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07003725 spin_lock_irqsave(&bitmap->lock, flags);
3726 chunk_kb = bitmap->chunksize >> 10;
3727 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3728 "%lu%s chunk",
3729 bitmap->pages - bitmap->missing_pages,
3730 bitmap->pages,
3731 (bitmap->pages - bitmap->missing_pages)
3732 << (PAGE_SHIFT - 10),
3733 chunk_kb ? chunk_kb : bitmap->chunksize,
3734 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07003735 if (bitmap->file) {
3736 seq_printf(seq, ", file: ");
3737 seq_path(seq, bitmap->file->f_vfsmnt,
3738 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07003739 }
NeilBrown78d742d2005-06-21 17:17:15 -07003740
NeilBrown32a76272005-06-21 17:17:14 -07003741 seq_printf(seq, "\n");
3742 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 }
3744
3745 seq_printf(seq, "\n");
3746 }
3747 mddev_unlock(mddev);
3748
3749 return 0;
3750}
3751
3752static struct seq_operations md_seq_ops = {
3753 .start = md_seq_start,
3754 .next = md_seq_next,
3755 .stop = md_seq_stop,
3756 .show = md_seq_show,
3757};
3758
3759static int md_seq_open(struct inode *inode, struct file *file)
3760{
3761 int error;
3762
3763 error = seq_open(file, &md_seq_ops);
3764 return error;
3765}
3766
3767static struct file_operations md_seq_fops = {
3768 .open = md_seq_open,
3769 .read = seq_read,
3770 .llseek = seq_lseek,
3771 .release = seq_release,
3772};
3773
3774int register_md_personality(int pnum, mdk_personality_t *p)
3775{
3776 if (pnum >= MAX_PERSONALITY) {
3777 printk(KERN_ERR
3778 "md: tried to install personality %s as nr %d, but max is %lu\n",
3779 p->name, pnum, MAX_PERSONALITY-1);
3780 return -EINVAL;
3781 }
3782
3783 spin_lock(&pers_lock);
3784 if (pers[pnum]) {
3785 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 return -EBUSY;
3787 }
3788
3789 pers[pnum] = p;
3790 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3791 spin_unlock(&pers_lock);
3792 return 0;
3793}
3794
3795int unregister_md_personality(int pnum)
3796{
NeilBrowna757e642005-04-16 15:26:42 -07003797 if (pnum >= MAX_PERSONALITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
3800 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3801 spin_lock(&pers_lock);
3802 pers[pnum] = NULL;
3803 spin_unlock(&pers_lock);
3804 return 0;
3805}
3806
3807static int is_mddev_idle(mddev_t *mddev)
3808{
3809 mdk_rdev_t * rdev;
3810 struct list_head *tmp;
3811 int idle;
3812 unsigned long curr_events;
3813
3814 idle = 1;
3815 ITERATE_RDEV(mddev,rdev,tmp) {
3816 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01003817 curr_events = disk_stat_read(disk, sectors[0]) +
3818 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 atomic_read(&disk->sync_io);
3820 /* Allow some slack between valud of curr_events and last_events,
3821 * as there are some uninteresting races.
3822 * Note: the following is an unsigned comparison.
3823 */
3824 if ((curr_events - rdev->last_events + 32) > 64) {
3825 rdev->last_events = curr_events;
3826 idle = 0;
3827 }
3828 }
3829 return idle;
3830}
3831
3832void md_done_sync(mddev_t *mddev, int blocks, int ok)
3833{
3834 /* another "blocks" (512byte) blocks have been synced */
3835 atomic_sub(blocks, &mddev->recovery_active);
3836 wake_up(&mddev->recovery_wait);
3837 if (!ok) {
3838 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3839 md_wakeup_thread(mddev->thread);
3840 // stop recovery, signal do_sync ....
3841 }
3842}
3843
3844
NeilBrown06d91a52005-06-21 17:17:12 -07003845/* md_write_start(mddev, bi)
3846 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07003847 * in superblock) before writing, schedule a superblock update
3848 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07003849 */
NeilBrown3d310eb2005-06-21 17:17:26 -07003850void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
NeilBrown06d91a52005-06-21 17:17:12 -07003852 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07003853 return;
NeilBrown06d91a52005-06-21 17:17:12 -07003854
NeilBrownf91de922005-11-08 21:39:36 -08003855 BUG_ON(mddev->ro == 1);
3856 if (mddev->ro == 2) {
3857 /* need to switch to read/write */
3858 mddev->ro = 0;
3859 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3860 md_wakeup_thread(mddev->thread);
3861 }
NeilBrown06d91a52005-06-21 17:17:12 -07003862 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07003863 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08003864 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07003865 if (mddev->in_sync) {
3866 mddev->in_sync = 0;
3867 mddev->sb_dirty = 1;
3868 md_wakeup_thread(mddev->thread);
3869 }
NeilBrowna9701a32005-11-08 21:39:34 -08003870 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07003871 }
NeilBrown3d310eb2005-06-21 17:17:26 -07003872 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873}
3874
3875void md_write_end(mddev_t *mddev)
3876{
3877 if (atomic_dec_and_test(&mddev->writes_pending)) {
3878 if (mddev->safemode == 2)
3879 md_wakeup_thread(mddev->thread);
3880 else
3881 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3882 }
3883}
3884
Adrian Bunk75c96f82005-05-05 16:16:09 -07003885static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
3887#define SYNC_MARKS 10
3888#define SYNC_MARK_STEP (3*HZ)
3889static void md_do_sync(mddev_t *mddev)
3890{
3891 mddev_t *mddev2;
3892 unsigned int currspeed = 0,
3893 window;
NeilBrown57afd892005-06-21 17:17:13 -07003894 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 unsigned long mark[SYNC_MARKS];
3896 sector_t mark_cnt[SYNC_MARKS];
3897 int last_mark,m;
3898 struct list_head *tmp;
3899 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07003900 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
3902 /* just incase thread restarts... */
3903 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3904 return;
3905
3906 /* we overload curr_resync somewhat here.
3907 * 0 == not engaged in resync at all
3908 * 2 == checking that there is no conflict with another sync
3909 * 1 == like 2, but have yielded to allow conflicting resync to
3910 * commense
3911 * other == active in resync - this many blocks
3912 *
3913 * Before starting a resync we must have set curr_resync to
3914 * 2, and then checked that every "conflicting" array has curr_resync
3915 * less than ours. When we find one that is the same or higher
3916 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3917 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3918 * This will mean we have to start checking from the beginning again.
3919 *
3920 */
3921
3922 do {
3923 mddev->curr_resync = 2;
3924
3925 try_again:
NeilBrown8712e552005-10-26 01:58:58 -07003926 if (signal_pending(current) ||
3927 kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 flush_signals(current);
NeilBrown6985c432005-10-19 21:23:47 -07003929 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 goto skip;
3931 }
3932 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 if (mddev2 == mddev)
3934 continue;
3935 if (mddev2->curr_resync &&
3936 match_mddev_units(mddev,mddev2)) {
3937 DEFINE_WAIT(wq);
3938 if (mddev < mddev2 && mddev->curr_resync == 2) {
3939 /* arbitrarily yield */
3940 mddev->curr_resync = 1;
3941 wake_up(&resync_wait);
3942 }
3943 if (mddev > mddev2 && mddev->curr_resync == 1)
3944 /* no need to wait here, we can wait the next
3945 * time 'round when curr_resync == 2
3946 */
3947 continue;
3948 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown8712e552005-10-26 01:58:58 -07003949 if (!signal_pending(current) &&
3950 !kthread_should_stop() &&
3951 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 printk(KERN_INFO "md: delaying resync of %s"
3953 " until %s has finished resync (they"
3954 " share one or more physical units)\n",
3955 mdname(mddev), mdname(mddev2));
3956 mddev_put(mddev2);
3957 schedule();
3958 finish_wait(&resync_wait, &wq);
3959 goto try_again;
3960 }
3961 finish_wait(&resync_wait, &wq);
3962 }
3963 }
3964 } while (mddev->curr_resync < 2);
3965
NeilBrown9d888832005-11-08 21:39:26 -08003966 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07003968 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 */
3970 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08003971 mddev->resync_mismatches = 0;
3972 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 /* recovery follows the physical size of devices */
3974 max_sectors = mddev->size << 1;
3975
3976 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
3977 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
3978 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
Adrian Bunk338cec32005-09-10 00:26:54 -07003979 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 "(but not more than %d KB/sec) for reconstruction.\n",
3981 sysctl_speed_limit_max);
3982
3983 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07003984 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08003985 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
3986 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 j = mddev->recovery_cp;
3988 else
3989 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07003990 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 for (m = 0; m < SYNC_MARKS; m++) {
3992 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07003993 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 }
3995 last_mark = 0;
3996 mddev->resync_mark = mark[last_mark];
3997 mddev->resync_mark_cnt = mark_cnt[last_mark];
3998
3999 /*
4000 * Tune reconstruction:
4001 */
4002 window = 32*(PAGE_SIZE/512);
4003 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4004 window/2,(unsigned long long) max_sectors/2);
4005
4006 atomic_set(&mddev->recovery_active, 0);
4007 init_waitqueue_head(&mddev->recovery_wait);
4008 last_check = 0;
4009
4010 if (j>2) {
4011 printk(KERN_INFO
4012 "md: resuming recovery of %s from checkpoint.\n",
4013 mdname(mddev));
4014 mddev->curr_resync = j;
4015 }
4016
4017 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07004018 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
NeilBrown57afd892005-06-21 17:17:13 -07004020 skipped = 0;
4021 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4022 currspeed < sysctl_speed_limit_min);
4023 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4025 goto out;
4026 }
NeilBrown57afd892005-06-21 17:17:13 -07004027
4028 if (!skipped) { /* actual IO requested */
4029 io_sectors += sectors;
4030 atomic_add(sectors, &mddev->recovery_active);
4031 }
4032
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 j += sectors;
4034 if (j>1) mddev->curr_resync = j;
4035
NeilBrown57afd892005-06-21 17:17:13 -07004036
4037 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 continue;
4039
NeilBrown57afd892005-06-21 17:17:13 -07004040 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
4042 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4043 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4044 break;
4045
4046 repeat:
4047 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4048 /* step marks */
4049 int next = (last_mark+1) % SYNC_MARKS;
4050
4051 mddev->resync_mark = mark[next];
4052 mddev->resync_mark_cnt = mark_cnt[next];
4053 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004054 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 last_mark = next;
4056 }
4057
4058
NeilBrown8712e552005-10-26 01:58:58 -07004059 if (signal_pending(current) || kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 /*
4061 * got a signal, exit.
4062 */
4063 printk(KERN_INFO
4064 "md: md_do_sync() got signal ... exiting\n");
4065 flush_signals(current);
4066 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4067 goto out;
4068 }
4069
4070 /*
4071 * this loop exits only if either when we are slower than
4072 * the 'hard' speed limit, or the system was IO-idle for
4073 * a jiffy.
4074 * the system might be non-idle CPU-wise, but we only care
4075 * about not overloading the IO subsystem. (things like an
4076 * e2fsck being done on the RAID array should execute fast)
4077 */
4078 mddev->queue->unplug_fn(mddev->queue);
4079 cond_resched();
4080
NeilBrown57afd892005-06-21 17:17:13 -07004081 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4082 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
4084 if (currspeed > sysctl_speed_limit_min) {
4085 if ((currspeed > sysctl_speed_limit_max) ||
4086 !is_mddev_idle(mddev)) {
4087 msleep_interruptible(250);
4088 goto repeat;
4089 }
4090 }
4091 }
4092 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4093 /*
4094 * this also signals 'finished resyncing' to md_stop
4095 */
4096 out:
4097 mddev->queue->unplug_fn(mddev->queue);
4098
4099 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4100
4101 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004102 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
4104 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4105 mddev->curr_resync > 2 &&
4106 mddev->curr_resync >= mddev->recovery_cp) {
4107 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4108 printk(KERN_INFO
4109 "md: checkpointing recovery of %s.\n",
4110 mdname(mddev));
4111 mddev->recovery_cp = mddev->curr_resync;
4112 } else
4113 mddev->recovery_cp = MaxSector;
4114 }
4115
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 skip:
4117 mddev->curr_resync = 0;
4118 wake_up(&resync_wait);
4119 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4120 md_wakeup_thread(mddev->thread);
4121}
4122
4123
4124/*
4125 * This routine is regularly called by all per-raid-array threads to
4126 * deal with generic issues like resync and super-block update.
4127 * Raid personalities that don't have a thread (linear/raid0) do not
4128 * need this as they never do any recovery or update the superblock.
4129 *
4130 * It does not do any resync itself, but rather "forks" off other threads
4131 * to do that as needed.
4132 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4133 * "->recovery" and create a thread at ->sync_thread.
4134 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4135 * and wakeups up this thread which will reap the thread and finish up.
4136 * This thread also removes any faulty devices (with nr_pending == 0).
4137 *
4138 * The overall approach is:
4139 * 1/ if the superblock needs updating, update it.
4140 * 2/ If a recovery thread is running, don't do anything else.
4141 * 3/ If recovery has finished, clean up, possibly marking spares active.
4142 * 4/ If there are any faulty devices, remove them.
4143 * 5/ If array is degraded, try to add spares devices
4144 * 6/ If array has spares or is not in-sync, start a resync thread.
4145 */
4146void md_check_recovery(mddev_t *mddev)
4147{
4148 mdk_rdev_t *rdev;
4149 struct list_head *rtmp;
4150
4151
NeilBrown5f404022005-06-21 17:17:16 -07004152 if (mddev->bitmap)
4153 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
4155 if (mddev->ro)
4156 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004157
4158 if (signal_pending(current)) {
4159 if (mddev->pers->sync_request) {
4160 printk(KERN_INFO "md: %s in immediate safe mode\n",
4161 mdname(mddev));
4162 mddev->safemode = 2;
4163 }
4164 flush_signals(current);
4165 }
4166
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 if ( ! (
4168 mddev->sb_dirty ||
4169 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004170 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4171 (mddev->safemode == 1) ||
4172 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4173 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 ))
4175 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004176
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 if (mddev_trylock(mddev)==0) {
4178 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004179
NeilBrowna9701a32005-11-08 21:39:34 -08004180 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004181 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4182 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4183 mddev->in_sync = 1;
4184 mddev->sb_dirty = 1;
4185 }
4186 if (mddev->safemode == 1)
4187 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08004188 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004189
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 if (mddev->sb_dirty)
4191 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004192
NeilBrown06d91a52005-06-21 17:17:12 -07004193
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4195 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4196 /* resync/recovery still happening */
4197 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4198 goto unlock;
4199 }
4200 if (mddev->sync_thread) {
4201 /* resync has finished, collect result */
4202 md_unregister_thread(mddev->sync_thread);
4203 mddev->sync_thread = NULL;
4204 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4205 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4206 /* success...*/
4207 /* activate any spares */
4208 mddev->pers->spare_active(mddev);
4209 }
4210 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004211
4212 /* if array is no-longer degraded, then any saved_raid_disk
4213 * information must be scrapped
4214 */
4215 if (!mddev->degraded)
4216 ITERATE_RDEV(mddev,rdev,rtmp)
4217 rdev->saved_raid_disk = -1;
4218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 mddev->recovery = 0;
4220 /* flag recovery needed just to double check */
4221 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4222 goto unlock;
4223 }
NeilBrown24dd4692005-11-08 21:39:26 -08004224 /* Clear some bits that don't mean anything, but
4225 * might be left set
4226 */
4227 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4228 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4229 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4230 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
4232 /* no recovery is running.
4233 * remove any failed drives, then
4234 * add spares if possible.
4235 * Spare are also removed and re-added, to allow
4236 * the personality to fail the re-add.
4237 */
4238 ITERATE_RDEV(mddev,rdev,rtmp)
4239 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004240 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004242 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4243 char nm[20];
4244 sprintf(nm,"rd%d", rdev->raid_disk);
4245 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 }
4249
4250 if (mddev->degraded) {
4251 ITERATE_RDEV(mddev,rdev,rtmp)
4252 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08004253 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004254 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4255 char nm[20];
4256 sprintf(nm, "rd%d", rdev->raid_disk);
4257 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 spares++;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004259 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 break;
4261 }
4262 }
4263
NeilBrown24dd4692005-11-08 21:39:26 -08004264 if (spares) {
4265 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4266 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4267 } else if (mddev->recovery_cp < MaxSector) {
4268 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4269 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4270 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08004272
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 if (mddev->pers->sync_request) {
4274 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07004275 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4276 /* We are adding a device or devices to an array
4277 * which has the bitmap stored on all devices.
4278 * So make sure all bitmap pages get written
4279 */
4280 bitmap_write_all(mddev->bitmap);
4281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 mddev->sync_thread = md_register_thread(md_do_sync,
4283 mddev,
4284 "%s_resync");
4285 if (!mddev->sync_thread) {
4286 printk(KERN_ERR "%s: could not start resync"
4287 " thread...\n",
4288 mdname(mddev));
4289 /* leave the spares where they are, it shouldn't hurt */
4290 mddev->recovery = 0;
4291 } else {
4292 md_wakeup_thread(mddev->sync_thread);
4293 }
4294 }
4295 unlock:
4296 mddev_unlock(mddev);
4297 }
4298}
4299
Adrian Bunk75c96f82005-05-05 16:16:09 -07004300static int md_notify_reboot(struct notifier_block *this,
4301 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
4303 struct list_head *tmp;
4304 mddev_t *mddev;
4305
4306 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4307
4308 printk(KERN_INFO "md: stopping all md devices.\n");
4309
4310 ITERATE_MDDEV(mddev,tmp)
4311 if (mddev_trylock(mddev)==0)
4312 do_md_stop (mddev, 1);
4313 /*
4314 * certain more exotic SCSI devices are known to be
4315 * volatile wrt too early system reboots. While the
4316 * right place to handle this issue is the given
4317 * driver, we do want to have a safe RAID driver ...
4318 */
4319 mdelay(1000*1);
4320 }
4321 return NOTIFY_DONE;
4322}
4323
Adrian Bunk75c96f82005-05-05 16:16:09 -07004324static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 .notifier_call = md_notify_reboot,
4326 .next = NULL,
4327 .priority = INT_MAX, /* before any real devices */
4328};
4329
4330static void md_geninit(void)
4331{
4332 struct proc_dir_entry *p;
4333
4334 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4335
4336 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4337 if (p)
4338 p->proc_fops = &md_seq_fops;
4339}
4340
Adrian Bunk75c96f82005-05-05 16:16:09 -07004341static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
4343 int minor;
4344
4345 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4346 " MD_SB_DISKS=%d\n",
4347 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4348 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08004349 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07004350 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
4352 if (register_blkdev(MAJOR_NR, "md"))
4353 return -1;
4354 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4355 unregister_blkdev(MAJOR_NR, "md");
4356 return -1;
4357 }
4358 devfs_mk_dir("md");
4359 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4360 md_probe, NULL, NULL);
4361 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4362 md_probe, NULL, NULL);
4363
4364 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4365 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4366 S_IFBLK|S_IRUSR|S_IWUSR,
4367 "md/%d", minor);
4368
4369 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4370 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4371 S_IFBLK|S_IRUSR|S_IWUSR,
4372 "md/mdp%d", minor);
4373
4374
4375 register_reboot_notifier(&md_notifier);
4376 raid_table_header = register_sysctl_table(raid_root_table, 1);
4377
4378 md_geninit();
4379 return (0);
4380}
4381
4382
4383#ifndef MODULE
4384
4385/*
4386 * Searches all registered partitions for autorun RAID arrays
4387 * at boot time.
4388 */
4389static dev_t detected_devices[128];
4390static int dev_cnt;
4391
4392void md_autodetect_dev(dev_t dev)
4393{
4394 if (dev_cnt >= 0 && dev_cnt < 127)
4395 detected_devices[dev_cnt++] = dev;
4396}
4397
4398
4399static void autostart_arrays(int part)
4400{
4401 mdk_rdev_t *rdev;
4402 int i;
4403
4404 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4405
4406 for (i = 0; i < dev_cnt; i++) {
4407 dev_t dev = detected_devices[i];
4408
4409 rdev = md_import_device(dev,0, 0);
4410 if (IS_ERR(rdev))
4411 continue;
4412
NeilBrownb2d444d2005-11-08 21:39:31 -08004413 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 MD_BUG();
4415 continue;
4416 }
4417 list_add(&rdev->same_set, &pending_raid_disks);
4418 }
4419 dev_cnt = 0;
4420
4421 autorun_devices(part);
4422}
4423
4424#endif
4425
4426static __exit void md_exit(void)
4427{
4428 mddev_t *mddev;
4429 struct list_head *tmp;
4430 int i;
4431 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4432 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4433 for (i=0; i < MAX_MD_DEVS; i++)
4434 devfs_remove("md/%d", i);
4435 for (i=0; i < MAX_MD_DEVS; i++)
4436 devfs_remove("md/d%d", i);
4437
4438 devfs_remove("md");
4439
4440 unregister_blkdev(MAJOR_NR,"md");
4441 unregister_blkdev(mdp_major, "mdp");
4442 unregister_reboot_notifier(&md_notifier);
4443 unregister_sysctl_table(raid_table_header);
4444 remove_proc_entry("mdstat", NULL);
4445 ITERATE_MDDEV(mddev,tmp) {
4446 struct gendisk *disk = mddev->gendisk;
4447 if (!disk)
4448 continue;
4449 export_array(mddev);
4450 del_gendisk(disk);
4451 put_disk(disk);
4452 mddev->gendisk = NULL;
4453 mddev_put(mddev);
4454 }
4455}
4456
4457module_init(md_init)
4458module_exit(md_exit)
4459
NeilBrownf91de922005-11-08 21:39:36 -08004460static int get_ro(char *buffer, struct kernel_param *kp)
4461{
4462 return sprintf(buffer, "%d", start_readonly);
4463}
4464static int set_ro(const char *val, struct kernel_param *kp)
4465{
4466 char *e;
4467 int num = simple_strtoul(val, &e, 10);
4468 if (*val && (*e == '\0' || *e == '\n')) {
4469 start_readonly = num;
4470 return 0;;
4471 }
4472 return -EINVAL;
4473}
4474
4475module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477EXPORT_SYMBOL(register_md_personality);
4478EXPORT_SYMBOL(unregister_md_personality);
4479EXPORT_SYMBOL(md_error);
4480EXPORT_SYMBOL(md_done_sync);
4481EXPORT_SYMBOL(md_write_start);
4482EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483EXPORT_SYMBOL(md_register_thread);
4484EXPORT_SYMBOL(md_unregister_thread);
4485EXPORT_SYMBOL(md_wakeup_thread);
4486EXPORT_SYMBOL(md_print_devices);
4487EXPORT_SYMBOL(md_check_recovery);
4488MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07004489MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07004490MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);