blob: a68ad85473253c4452817d19d4faa7c730fbf6e4 [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
134/*
135 * Enables to iterate over all existing md arrays
136 * all_mddevs_lock protects this list.
137 */
138static LIST_HEAD(all_mddevs);
139static DEFINE_SPINLOCK(all_mddevs_lock);
140
141
142/*
143 * iterates through all used mddevs in the system.
144 * We take care to grab the all_mddevs_lock whenever navigating
145 * the list, and to always hold a refcount when unlocked.
146 * Any code which breaks out of this loop while own
147 * a reference to the current mddev and must mddev_put it.
148 */
149#define ITERATE_MDDEV(mddev,tmp) \
150 \
151 for (({ spin_lock(&all_mddevs_lock); \
152 tmp = all_mddevs.next; \
153 mddev = NULL;}); \
154 ({ if (tmp != &all_mddevs) \
155 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
156 spin_unlock(&all_mddevs_lock); \
157 if (mddev) mddev_put(mddev); \
158 mddev = list_entry(tmp, mddev_t, all_mddevs); \
159 tmp != &all_mddevs;}); \
160 ({ spin_lock(&all_mddevs_lock); \
161 tmp = tmp->next;}) \
162 )
163
164
165static int md_fail_request (request_queue_t *q, struct bio *bio)
166{
167 bio_io_error(bio, bio->bi_size);
168 return 0;
169}
170
171static inline mddev_t *mddev_get(mddev_t *mddev)
172{
173 atomic_inc(&mddev->active);
174 return mddev;
175}
176
177static void mddev_put(mddev_t *mddev)
178{
179 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
180 return;
181 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
182 list_del(&mddev->all_mddevs);
183 blk_put_queue(mddev->queue);
NeilBrowneae17012005-11-08 21:39:23 -0800184 kobject_unregister(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186 spin_unlock(&all_mddevs_lock);
187}
188
189static mddev_t * mddev_find(dev_t unit)
190{
191 mddev_t *mddev, *new = NULL;
192
193 retry:
194 spin_lock(&all_mddevs_lock);
195 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
196 if (mddev->unit == unit) {
197 mddev_get(mddev);
198 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700199 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return mddev;
201 }
202
203 if (new) {
204 list_add(&new->all_mddevs, &all_mddevs);
205 spin_unlock(&all_mddevs_lock);
206 return new;
207 }
208 spin_unlock(&all_mddevs_lock);
209
210 new = (mddev_t *) kmalloc(sizeof(*new), GFP_KERNEL);
211 if (!new)
212 return NULL;
213
214 memset(new, 0, sizeof(*new));
215
216 new->unit = unit;
217 if (MAJOR(unit) == MD_MAJOR)
218 new->md_minor = MINOR(unit);
219 else
220 new->md_minor = MINOR(unit) >> MdpMinorShift;
221
222 init_MUTEX(&new->reconfig_sem);
223 INIT_LIST_HEAD(&new->disks);
224 INIT_LIST_HEAD(&new->all_mddevs);
225 init_timer(&new->safemode_timer);
226 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700227 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700228 init_waitqueue_head(&new->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 new->queue = blk_alloc_queue(GFP_KERNEL);
231 if (!new->queue) {
232 kfree(new);
233 return NULL;
234 }
235
236 blk_queue_make_request(new->queue, md_fail_request);
237
238 goto retry;
239}
240
241static inline int mddev_lock(mddev_t * mddev)
242{
243 return down_interruptible(&mddev->reconfig_sem);
244}
245
246static inline void mddev_lock_uninterruptible(mddev_t * mddev)
247{
248 down(&mddev->reconfig_sem);
249}
250
251static inline int mddev_trylock(mddev_t * mddev)
252{
253 return down_trylock(&mddev->reconfig_sem);
254}
255
256static inline void mddev_unlock(mddev_t * mddev)
257{
258 up(&mddev->reconfig_sem);
259
NeilBrown005eca52005-08-22 13:11:08 -0700260 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
264{
265 mdk_rdev_t * rdev;
266 struct list_head *tmp;
267
268 ITERATE_RDEV(mddev,rdev,tmp) {
269 if (rdev->desc_nr == nr)
270 return rdev;
271 }
272 return NULL;
273}
274
275static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
276{
277 struct list_head *tmp;
278 mdk_rdev_t *rdev;
279
280 ITERATE_RDEV(mddev,rdev,tmp) {
281 if (rdev->bdev->bd_dev == dev)
282 return rdev;
283 }
284 return NULL;
285}
286
Jesper Juhl77933d72005-07-27 11:46:09 -0700287static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
290 return MD_NEW_SIZE_BLOCKS(size);
291}
292
293static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
294{
295 sector_t size;
296
297 size = rdev->sb_offset;
298
299 if (chunk_size)
300 size &= ~((sector_t)chunk_size/1024 - 1);
301 return size;
302}
303
304static int alloc_disk_sb(mdk_rdev_t * rdev)
305{
306 if (rdev->sb_page)
307 MD_BUG();
308
309 rdev->sb_page = alloc_page(GFP_KERNEL);
310 if (!rdev->sb_page) {
311 printk(KERN_ALERT "md: out of memory.\n");
312 return -EINVAL;
313 }
314
315 return 0;
316}
317
318static void free_disk_sb(mdk_rdev_t * rdev)
319{
320 if (rdev->sb_page) {
321 page_cache_release(rdev->sb_page);
322 rdev->sb_loaded = 0;
323 rdev->sb_page = NULL;
324 rdev->sb_offset = 0;
325 rdev->size = 0;
326 }
327}
328
329
NeilBrown7bfa19f2005-06-21 17:17:28 -0700330static int super_written(struct bio *bio, unsigned int bytes_done, int error)
331{
332 mdk_rdev_t *rdev = bio->bi_private;
333 if (bio->bi_size)
334 return 1;
335
336 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
337 md_error(rdev->mddev, rdev);
338
339 if (atomic_dec_and_test(&rdev->mddev->pending_writes))
340 wake_up(&rdev->mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700341 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700342 return 0;
343}
344
345void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
346 sector_t sector, int size, struct page *page)
347{
348 /* write first size bytes of page to sector of rdev
349 * Increment mddev->pending_writes before returning
350 * and decrement it on completion, waking up sb_wait
351 * if zero is reached.
352 * If an error occurred, call md_error
353 */
354 struct bio *bio = bio_alloc(GFP_NOIO, 1);
355
356 bio->bi_bdev = rdev->bdev;
357 bio->bi_sector = sector;
358 bio_add_page(bio, page, size, 0);
359 bio->bi_private = rdev;
360 bio->bi_end_io = super_written;
361 atomic_inc(&mddev->pending_writes);
362 submit_bio((1<<BIO_RW)|(1<<BIO_RW_SYNC), bio);
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
366{
367 if (bio->bi_size)
368 return 1;
369
370 complete((struct completion*)bio->bi_private);
371 return 0;
372}
373
NeilBrowna654b9d82005-06-21 17:17:27 -0700374int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct page *page, int rw)
376{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700377 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct completion event;
379 int ret;
380
381 rw |= (1 << BIO_RW_SYNC);
382
383 bio->bi_bdev = bdev;
384 bio->bi_sector = sector;
385 bio_add_page(bio, page, size, 0);
386 init_completion(&event);
387 bio->bi_private = &event;
388 bio->bi_end_io = bi_complete;
389 submit_bio(rw, bio);
390 wait_for_completion(&event);
391
392 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
393 bio_put(bio);
394 return ret;
395}
396
NeilBrown0002b272005-09-09 16:23:53 -0700397static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 char b[BDEVNAME_SIZE];
400 if (!rdev->sb_page) {
401 MD_BUG();
402 return -EINVAL;
403 }
404 if (rdev->sb_loaded)
405 return 0;
406
407
NeilBrown0002b272005-09-09 16:23:53 -0700408 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 goto fail;
410 rdev->sb_loaded = 1;
411 return 0;
412
413fail:
414 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
415 bdevname(rdev->bdev,b));
416 return -EINVAL;
417}
418
419static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
420{
421 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
422 (sb1->set_uuid1 == sb2->set_uuid1) &&
423 (sb1->set_uuid2 == sb2->set_uuid2) &&
424 (sb1->set_uuid3 == sb2->set_uuid3))
425
426 return 1;
427
428 return 0;
429}
430
431
432static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
433{
434 int ret;
435 mdp_super_t *tmp1, *tmp2;
436
437 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
438 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
439
440 if (!tmp1 || !tmp2) {
441 ret = 0;
442 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
443 goto abort;
444 }
445
446 *tmp1 = *sb1;
447 *tmp2 = *sb2;
448
449 /*
450 * nr_disks is not constant
451 */
452 tmp1->nr_disks = 0;
453 tmp2->nr_disks = 0;
454
455 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
456 ret = 0;
457 else
458 ret = 1;
459
460abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700461 kfree(tmp1);
462 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return ret;
464}
465
466static unsigned int calc_sb_csum(mdp_super_t * sb)
467{
468 unsigned int disk_csum, csum;
469
470 disk_csum = sb->sb_csum;
471 sb->sb_csum = 0;
472 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
473 sb->sb_csum = disk_csum;
474 return csum;
475}
476
477
478/*
479 * Handle superblock details.
480 * We want to be able to handle multiple superblock formats
481 * so we have a common interface to them all, and an array of
482 * different handlers.
483 * We rely on user-space to write the initial superblock, and support
484 * reading and updating of superblocks.
485 * Interface methods are:
486 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
487 * loads and validates a superblock on dev.
488 * if refdev != NULL, compare superblocks on both devices
489 * Return:
490 * 0 - dev has a superblock that is compatible with refdev
491 * 1 - dev has a superblock that is compatible and newer than refdev
492 * so dev should be used as the refdev in future
493 * -EINVAL superblock incompatible or invalid
494 * -othererror e.g. -EIO
495 *
496 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
497 * Verify that dev is acceptable into mddev.
498 * The first time, mddev->raid_disks will be 0, and data from
499 * dev should be merged in. Subsequent calls check that dev
500 * is new enough. Return 0 or -EINVAL
501 *
502 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
503 * Update the superblock for rdev with data in mddev
504 * This does not write to disc.
505 *
506 */
507
508struct super_type {
509 char *name;
510 struct module *owner;
511 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
512 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
513 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
514};
515
516/*
517 * load_super for 0.90.0
518 */
519static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
520{
521 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
522 mdp_super_t *sb;
523 int ret;
524 sector_t sb_offset;
525
526 /*
527 * Calculate the position of the superblock,
528 * it's at the end of the disk.
529 *
530 * It also happens to be a multiple of 4Kb.
531 */
532 sb_offset = calc_dev_sboffset(rdev->bdev);
533 rdev->sb_offset = sb_offset;
534
NeilBrown0002b272005-09-09 16:23:53 -0700535 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (ret) return ret;
537
538 ret = -EINVAL;
539
540 bdevname(rdev->bdev, b);
541 sb = (mdp_super_t*)page_address(rdev->sb_page);
542
543 if (sb->md_magic != MD_SB_MAGIC) {
544 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
545 b);
546 goto abort;
547 }
548
549 if (sb->major_version != 0 ||
550 sb->minor_version != 90) {
551 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
552 sb->major_version, sb->minor_version,
553 b);
554 goto abort;
555 }
556
557 if (sb->raid_disks <= 0)
558 goto abort;
559
560 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
561 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
562 b);
563 goto abort;
564 }
565
566 rdev->preferred_minor = sb->md_minor;
567 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700568 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 if (sb->level == LEVEL_MULTIPATH)
571 rdev->desc_nr = -1;
572 else
573 rdev->desc_nr = sb->this_disk.number;
574
575 if (refdev == 0)
576 ret = 1;
577 else {
578 __u64 ev1, ev2;
579 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
580 if (!uuid_equal(refsb, sb)) {
581 printk(KERN_WARNING "md: %s has different UUID to %s\n",
582 b, bdevname(refdev->bdev,b2));
583 goto abort;
584 }
585 if (!sb_equal(refsb, sb)) {
586 printk(KERN_WARNING "md: %s has same UUID"
587 " but different superblock to %s\n",
588 b, bdevname(refdev->bdev, b2));
589 goto abort;
590 }
591 ev1 = md_event(sb);
592 ev2 = md_event(refsb);
593 if (ev1 > ev2)
594 ret = 1;
595 else
596 ret = 0;
597 }
598 rdev->size = calc_dev_size(rdev, sb->chunk_size);
599
600 abort:
601 return ret;
602}
603
604/*
605 * validate_super for 0.90.0
606 */
607static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
608{
609 mdp_disk_t *desc;
610 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
611
NeilBrown41158c72005-06-21 17:17:25 -0700612 rdev->raid_disk = -1;
613 rdev->in_sync = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (mddev->raid_disks == 0) {
615 mddev->major_version = 0;
616 mddev->minor_version = sb->minor_version;
617 mddev->patch_version = sb->patch_version;
618 mddev->persistent = ! sb->not_persistent;
619 mddev->chunk_size = sb->chunk_size;
620 mddev->ctime = sb->ctime;
621 mddev->utime = sb->utime;
622 mddev->level = sb->level;
623 mddev->layout = sb->layout;
624 mddev->raid_disks = sb->raid_disks;
625 mddev->size = sb->size;
626 mddev->events = md_event(sb);
NeilBrown92232142005-08-18 11:24:16 -0700627 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700628 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 if (sb->state & (1<<MD_SB_CLEAN))
631 mddev->recovery_cp = MaxSector;
632 else {
633 if (sb->events_hi == sb->cp_events_hi &&
634 sb->events_lo == sb->cp_events_lo) {
635 mddev->recovery_cp = sb->recovery_cp;
636 } else
637 mddev->recovery_cp = 0;
638 }
639
640 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
641 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
642 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
643 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
644
645 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700646
647 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
648 mddev->bitmap_file == NULL) {
NeilBrown934ce7c2005-09-09 16:23:55 -0700649 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700650 /* FIXME use a better test */
651 printk(KERN_WARNING "md: bitmaps only support for raid1\n");
652 return -EINVAL;
653 }
NeilBrown36fa3062005-09-09 16:23:45 -0700654 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700655 }
656
NeilBrown41158c72005-06-21 17:17:25 -0700657 } else if (mddev->pers == NULL) {
658 /* Insist on good event counter while assembling */
659 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 ++ev1;
661 if (ev1 < mddev->events)
662 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700663 } else if (mddev->bitmap) {
664 /* if adding to array with a bitmap, then we can accept an
665 * older device ... but not too old.
666 */
667 __u64 ev1 = md_event(sb);
668 if (ev1 < mddev->bitmap->events_cleared)
669 return 0;
670 } else /* just a hot-add of a new device, leave raid_disk at -1 */
671 return 0;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (mddev->level != LEVEL_MULTIPATH) {
NeilBrown41158c72005-06-21 17:17:25 -0700674 rdev->faulty = 0;
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700675 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 desc = sb->disks + rdev->desc_nr;
677
678 if (desc->state & (1<<MD_DISK_FAULTY))
679 rdev->faulty = 1;
680 else if (desc->state & (1<<MD_DISK_SYNC) &&
681 desc->raid_disk < mddev->raid_disks) {
682 rdev->in_sync = 1;
683 rdev->raid_disk = desc->raid_disk;
684 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700685 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
686 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700687 } else /* MULTIPATH are always insync */
688 rdev->in_sync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690}
691
692/*
693 * sync_super for 0.90.0
694 */
695static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
696{
697 mdp_super_t *sb;
698 struct list_head *tmp;
699 mdk_rdev_t *rdev2;
700 int next_spare = mddev->raid_disks;
701
702 /* make rdev->sb match mddev data..
703 *
704 * 1/ zero out disks
705 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
706 * 3/ any empty disks < next_spare become removed
707 *
708 * disks[0] gets initialised to REMOVED because
709 * we cannot be sure from other fields if it has
710 * been initialised or not.
711 */
712 int i;
713 int active=0, working=0,failed=0,spare=0,nr_disks=0;
714
NeilBrown61181562005-09-09 16:24:02 -0700715 rdev->sb_size = MD_SB_BYTES;
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 sb = (mdp_super_t*)page_address(rdev->sb_page);
718
719 memset(sb, 0, sizeof(*sb));
720
721 sb->md_magic = MD_SB_MAGIC;
722 sb->major_version = mddev->major_version;
723 sb->minor_version = mddev->minor_version;
724 sb->patch_version = mddev->patch_version;
725 sb->gvalid_words = 0; /* ignored */
726 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
727 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
728 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
729 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
730
731 sb->ctime = mddev->ctime;
732 sb->level = mddev->level;
733 sb->size = mddev->size;
734 sb->raid_disks = mddev->raid_disks;
735 sb->md_minor = mddev->md_minor;
736 sb->not_persistent = !mddev->persistent;
737 sb->utime = mddev->utime;
738 sb->state = 0;
739 sb->events_hi = (mddev->events>>32);
740 sb->events_lo = (u32)mddev->events;
741
742 if (mddev->in_sync)
743 {
744 sb->recovery_cp = mddev->recovery_cp;
745 sb->cp_events_hi = (mddev->events>>32);
746 sb->cp_events_lo = (u32)mddev->events;
747 if (mddev->recovery_cp == MaxSector)
748 sb->state = (1<< MD_SB_CLEAN);
749 } else
750 sb->recovery_cp = 0;
751
752 sb->layout = mddev->layout;
753 sb->chunk_size = mddev->chunk_size;
754
NeilBrowna654b9d82005-06-21 17:17:27 -0700755 if (mddev->bitmap && mddev->bitmap_file == NULL)
756 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 sb->disks[0].state = (1<<MD_DISK_REMOVED);
759 ITERATE_RDEV(mddev,rdev2,tmp) {
760 mdp_disk_t *d;
761 if (rdev2->raid_disk >= 0 && rdev2->in_sync && !rdev2->faulty)
762 rdev2->desc_nr = rdev2->raid_disk;
763 else
764 rdev2->desc_nr = next_spare++;
765 d = &sb->disks[rdev2->desc_nr];
766 nr_disks++;
767 d->number = rdev2->desc_nr;
768 d->major = MAJOR(rdev2->bdev->bd_dev);
769 d->minor = MINOR(rdev2->bdev->bd_dev);
770 if (rdev2->raid_disk >= 0 && rdev->in_sync && !rdev2->faulty)
771 d->raid_disk = rdev2->raid_disk;
772 else
773 d->raid_disk = rdev2->desc_nr; /* compatibility */
774 if (rdev2->faulty) {
775 d->state = (1<<MD_DISK_FAULTY);
776 failed++;
777 } else if (rdev2->in_sync) {
778 d->state = (1<<MD_DISK_ACTIVE);
779 d->state |= (1<<MD_DISK_SYNC);
780 active++;
781 working++;
782 } else {
783 d->state = 0;
784 spare++;
785 working++;
786 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700787 if (test_bit(WriteMostly, &rdev2->flags))
788 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790
791 /* now set the "removed" and "faulty" bits on any missing devices */
792 for (i=0 ; i < mddev->raid_disks ; i++) {
793 mdp_disk_t *d = &sb->disks[i];
794 if (d->state == 0 && d->number == 0) {
795 d->number = i;
796 d->raid_disk = i;
797 d->state = (1<<MD_DISK_REMOVED);
798 d->state |= (1<<MD_DISK_FAULTY);
799 failed++;
800 }
801 }
802 sb->nr_disks = nr_disks;
803 sb->active_disks = active;
804 sb->working_disks = working;
805 sb->failed_disks = failed;
806 sb->spare_disks = spare;
807
808 sb->this_disk = sb->disks[rdev->desc_nr];
809 sb->sb_csum = calc_sb_csum(sb);
810}
811
812/*
813 * version 1 superblock
814 */
815
816static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
817{
818 unsigned int disk_csum, csum;
819 unsigned long long newcsum;
820 int size = 256 + le32_to_cpu(sb->max_dev)*2;
821 unsigned int *isuper = (unsigned int*)sb;
822 int i;
823
824 disk_csum = sb->sb_csum;
825 sb->sb_csum = 0;
826 newcsum = 0;
827 for (i=0; size>=4; size -= 4 )
828 newcsum += le32_to_cpu(*isuper++);
829
830 if (size == 2)
831 newcsum += le16_to_cpu(*(unsigned short*) isuper);
832
833 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
834 sb->sb_csum = disk_csum;
835 return cpu_to_le32(csum);
836}
837
838static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
839{
840 struct mdp_superblock_1 *sb;
841 int ret;
842 sector_t sb_offset;
843 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -0700844 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /*
847 * Calculate the position of the superblock.
848 * It is always aligned to a 4K boundary and
849 * depeding on minor_version, it can be:
850 * 0: At least 8K, but less than 12K, from end of device
851 * 1: At start of device
852 * 2: 4K from start of device.
853 */
854 switch(minor_version) {
855 case 0:
856 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
857 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -0700858 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 /* convert from sectors to K */
860 sb_offset /= 2;
861 break;
862 case 1:
863 sb_offset = 0;
864 break;
865 case 2:
866 sb_offset = 4;
867 break;
868 default:
869 return -EINVAL;
870 }
871 rdev->sb_offset = sb_offset;
872
NeilBrown0002b272005-09-09 16:23:53 -0700873 /* superblock is rarely larger than 1K, but it can be larger,
874 * and it is safe to read 4k, so we do that
875 */
876 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (ret) return ret;
878
879
880 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
881
882 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
883 sb->major_version != cpu_to_le32(1) ||
884 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
885 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -0700886 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -EINVAL;
888
889 if (calc_sb_1_csum(sb) != sb->sb_csum) {
890 printk("md: invalid superblock checksum on %s\n",
891 bdevname(rdev->bdev,b));
892 return -EINVAL;
893 }
894 if (le64_to_cpu(sb->data_size) < 10) {
895 printk("md: data_size too small on %s\n",
896 bdevname(rdev->bdev,b));
897 return -EINVAL;
898 }
899 rdev->preferred_minor = 0xffff;
900 rdev->data_offset = le64_to_cpu(sb->data_offset);
901
NeilBrown0002b272005-09-09 16:23:53 -0700902 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -0700903 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -0700904 if (rdev->sb_size & bmask)
905 rdev-> sb_size = (rdev->sb_size | bmask)+1;
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (refdev == 0)
908 return 1;
909 else {
910 __u64 ev1, ev2;
911 struct mdp_superblock_1 *refsb =
912 (struct mdp_superblock_1*)page_address(refdev->sb_page);
913
914 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
915 sb->level != refsb->level ||
916 sb->layout != refsb->layout ||
917 sb->chunksize != refsb->chunksize) {
918 printk(KERN_WARNING "md: %s has strangely different"
919 " superblock to %s\n",
920 bdevname(rdev->bdev,b),
921 bdevname(refdev->bdev,b2));
922 return -EINVAL;
923 }
924 ev1 = le64_to_cpu(sb->events);
925 ev2 = le64_to_cpu(refsb->events);
926
927 if (ev1 > ev2)
928 return 1;
929 }
930 if (minor_version)
931 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
932 else
933 rdev->size = rdev->sb_offset;
934 if (rdev->size < le64_to_cpu(sb->data_size)/2)
935 return -EINVAL;
936 rdev->size = le64_to_cpu(sb->data_size)/2;
937 if (le32_to_cpu(sb->chunksize))
938 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
939 return 0;
940}
941
942static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
943{
944 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
945
NeilBrown41158c72005-06-21 17:17:25 -0700946 rdev->raid_disk = -1;
947 rdev->in_sync = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (mddev->raid_disks == 0) {
949 mddev->major_version = 1;
950 mddev->patch_version = 0;
951 mddev->persistent = 1;
952 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
953 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
954 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
955 mddev->level = le32_to_cpu(sb->level);
956 mddev->layout = le32_to_cpu(sb->layout);
957 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
958 mddev->size = le64_to_cpu(sb->size)/2;
959 mddev->events = le64_to_cpu(sb->events);
NeilBrown92232142005-08-18 11:24:16 -0700960 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700961 mddev->default_bitmap_offset = 0;
NeilBrown53e87fb2005-09-09 16:23:58 -0700962 mddev->default_bitmap_offset = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
965 memcpy(mddev->uuid, sb->set_uuid, 16);
966
967 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -0700968
NeilBrown71c08052005-09-09 16:23:51 -0700969 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -0700970 mddev->bitmap_file == NULL ) {
971 if (mddev->level != 1) {
972 printk(KERN_WARNING "md: bitmaps only supported for raid1\n");
973 return -EINVAL;
974 }
975 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
976 }
NeilBrown41158c72005-06-21 17:17:25 -0700977 } else if (mddev->pers == NULL) {
978 /* Insist of good event counter while assembling */
979 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 ++ev1;
981 if (ev1 < mddev->events)
982 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700983 } else if (mddev->bitmap) {
984 /* If adding to array with a bitmap, then we can accept an
985 * older device, but not too old.
986 */
987 __u64 ev1 = le64_to_cpu(sb->events);
988 if (ev1 < mddev->bitmap->events_cleared)
989 return 0;
990 } else /* just a hot-add of a new device, leave raid_disk at -1 */
991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 if (mddev->level != LEVEL_MULTIPATH) {
994 int role;
995 rdev->desc_nr = le32_to_cpu(sb->dev_number);
996 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
997 switch(role) {
998 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 rdev->faulty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 break;
1001 case 0xfffe: /* faulty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 rdev->faulty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
1004 default:
1005 rdev->in_sync = 1;
1006 rdev->faulty = 0;
1007 rdev->raid_disk = role;
1008 break;
1009 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001010 rdev->flags = 0;
1011 if (sb->devflags & WriteMostly1)
1012 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001013 } else /* MULTIPATH are always insync */
1014 rdev->in_sync = 1;
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return 0;
1017}
1018
1019static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1020{
1021 struct mdp_superblock_1 *sb;
1022 struct list_head *tmp;
1023 mdk_rdev_t *rdev2;
1024 int max_dev, i;
1025 /* make rdev->sb match mddev and rdev data. */
1026
1027 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1028
1029 sb->feature_map = 0;
1030 sb->pad0 = 0;
1031 memset(sb->pad1, 0, sizeof(sb->pad1));
1032 memset(sb->pad2, 0, sizeof(sb->pad2));
1033 memset(sb->pad3, 0, sizeof(sb->pad3));
1034
1035 sb->utime = cpu_to_le64((__u64)mddev->utime);
1036 sb->events = cpu_to_le64(mddev->events);
1037 if (mddev->in_sync)
1038 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1039 else
1040 sb->resync_offset = cpu_to_le64(0);
1041
NeilBrowna654b9d82005-06-21 17:17:27 -07001042 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1043 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001044 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001045 }
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 max_dev = 0;
1048 ITERATE_RDEV(mddev,rdev2,tmp)
1049 if (rdev2->desc_nr+1 > max_dev)
1050 max_dev = rdev2->desc_nr+1;
1051
1052 sb->max_dev = cpu_to_le32(max_dev);
1053 for (i=0; i<max_dev;i++)
1054 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1055
1056 ITERATE_RDEV(mddev,rdev2,tmp) {
1057 i = rdev2->desc_nr;
1058 if (rdev2->faulty)
1059 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1060 else if (rdev2->in_sync)
1061 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1062 else
1063 sb->dev_roles[i] = cpu_to_le16(0xffff);
1064 }
1065
1066 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1067 sb->sb_csum = calc_sb_1_csum(sb);
1068}
1069
1070
Adrian Bunk75c96f82005-05-05 16:16:09 -07001071static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 [0] = {
1073 .name = "0.90.0",
1074 .owner = THIS_MODULE,
1075 .load_super = super_90_load,
1076 .validate_super = super_90_validate,
1077 .sync_super = super_90_sync,
1078 },
1079 [1] = {
1080 .name = "md-1",
1081 .owner = THIS_MODULE,
1082 .load_super = super_1_load,
1083 .validate_super = super_1_validate,
1084 .sync_super = super_1_sync,
1085 },
1086};
1087
1088static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1089{
1090 struct list_head *tmp;
1091 mdk_rdev_t *rdev;
1092
1093 ITERATE_RDEV(mddev,rdev,tmp)
1094 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1095 return rdev;
1096
1097 return NULL;
1098}
1099
1100static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1101{
1102 struct list_head *tmp;
1103 mdk_rdev_t *rdev;
1104
1105 ITERATE_RDEV(mddev1,rdev,tmp)
1106 if (match_dev_unit(mddev2, rdev))
1107 return 1;
1108
1109 return 0;
1110}
1111
1112static LIST_HEAD(pending_raid_disks);
1113
1114static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1115{
1116 mdk_rdev_t *same_pdev;
1117 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1118
1119 if (rdev->mddev) {
1120 MD_BUG();
1121 return -EINVAL;
1122 }
1123 same_pdev = match_dev_unit(mddev, rdev);
1124 if (same_pdev)
1125 printk(KERN_WARNING
1126 "%s: WARNING: %s appears to be on the same physical"
1127 " disk as %s. True\n protection against single-disk"
1128 " failure might be compromised.\n",
1129 mdname(mddev), bdevname(rdev->bdev,b),
1130 bdevname(same_pdev->bdev,b2));
1131
1132 /* Verify rdev->desc_nr is unique.
1133 * If it is -1, assign a free number, else
1134 * check number is not in use
1135 */
1136 if (rdev->desc_nr < 0) {
1137 int choice = 0;
1138 if (mddev->pers) choice = mddev->raid_disks;
1139 while (find_rdev_nr(mddev, choice))
1140 choice++;
1141 rdev->desc_nr = choice;
1142 } else {
1143 if (find_rdev_nr(mddev, rdev->desc_nr))
1144 return -EBUSY;
1145 }
1146
1147 list_add(&rdev->same_set, &mddev->disks);
1148 rdev->mddev = mddev;
1149 printk(KERN_INFO "md: bind<%s>\n", bdevname(rdev->bdev,b));
1150 return 0;
1151}
1152
1153static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1154{
1155 char b[BDEVNAME_SIZE];
1156 if (!rdev->mddev) {
1157 MD_BUG();
1158 return;
1159 }
1160 list_del_init(&rdev->same_set);
1161 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1162 rdev->mddev = NULL;
1163}
1164
1165/*
1166 * prevent the device from being mounted, repartitioned or
1167 * otherwise reused by a RAID array (or any other kernel
1168 * subsystem), by bd_claiming the device.
1169 */
1170static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1171{
1172 int err = 0;
1173 struct block_device *bdev;
1174 char b[BDEVNAME_SIZE];
1175
1176 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1177 if (IS_ERR(bdev)) {
1178 printk(KERN_ERR "md: could not open %s.\n",
1179 __bdevname(dev, b));
1180 return PTR_ERR(bdev);
1181 }
1182 err = bd_claim(bdev, rdev);
1183 if (err) {
1184 printk(KERN_ERR "md: could not bd_claim %s.\n",
1185 bdevname(bdev, b));
1186 blkdev_put(bdev);
1187 return err;
1188 }
1189 rdev->bdev = bdev;
1190 return err;
1191}
1192
1193static void unlock_rdev(mdk_rdev_t *rdev)
1194{
1195 struct block_device *bdev = rdev->bdev;
1196 rdev->bdev = NULL;
1197 if (!bdev)
1198 MD_BUG();
1199 bd_release(bdev);
1200 blkdev_put(bdev);
1201}
1202
1203void md_autodetect_dev(dev_t dev);
1204
1205static void export_rdev(mdk_rdev_t * rdev)
1206{
1207 char b[BDEVNAME_SIZE];
1208 printk(KERN_INFO "md: export_rdev(%s)\n",
1209 bdevname(rdev->bdev,b));
1210 if (rdev->mddev)
1211 MD_BUG();
1212 free_disk_sb(rdev);
1213 list_del_init(&rdev->same_set);
1214#ifndef MODULE
1215 md_autodetect_dev(rdev->bdev->bd_dev);
1216#endif
1217 unlock_rdev(rdev);
1218 kfree(rdev);
1219}
1220
1221static void kick_rdev_from_array(mdk_rdev_t * rdev)
1222{
1223 unbind_rdev_from_array(rdev);
1224 export_rdev(rdev);
1225}
1226
1227static void export_array(mddev_t *mddev)
1228{
1229 struct list_head *tmp;
1230 mdk_rdev_t *rdev;
1231
1232 ITERATE_RDEV(mddev,rdev,tmp) {
1233 if (!rdev->mddev) {
1234 MD_BUG();
1235 continue;
1236 }
1237 kick_rdev_from_array(rdev);
1238 }
1239 if (!list_empty(&mddev->disks))
1240 MD_BUG();
1241 mddev->raid_disks = 0;
1242 mddev->major_version = 0;
1243}
1244
1245static void print_desc(mdp_disk_t *desc)
1246{
1247 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1248 desc->major,desc->minor,desc->raid_disk,desc->state);
1249}
1250
1251static void print_sb(mdp_super_t *sb)
1252{
1253 int i;
1254
1255 printk(KERN_INFO
1256 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1257 sb->major_version, sb->minor_version, sb->patch_version,
1258 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1259 sb->ctime);
1260 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1261 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1262 sb->md_minor, sb->layout, sb->chunk_size);
1263 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1264 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1265 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1266 sb->failed_disks, sb->spare_disks,
1267 sb->sb_csum, (unsigned long)sb->events_lo);
1268
1269 printk(KERN_INFO);
1270 for (i = 0; i < MD_SB_DISKS; i++) {
1271 mdp_disk_t *desc;
1272
1273 desc = sb->disks + i;
1274 if (desc->number || desc->major || desc->minor ||
1275 desc->raid_disk || (desc->state && (desc->state != 4))) {
1276 printk(" D %2d: ", i);
1277 print_desc(desc);
1278 }
1279 }
1280 printk(KERN_INFO "md: THIS: ");
1281 print_desc(&sb->this_disk);
1282
1283}
1284
1285static void print_rdev(mdk_rdev_t *rdev)
1286{
1287 char b[BDEVNAME_SIZE];
1288 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1289 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1290 rdev->faulty, rdev->in_sync, rdev->desc_nr);
1291 if (rdev->sb_loaded) {
1292 printk(KERN_INFO "md: rdev superblock:\n");
1293 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1294 } else
1295 printk(KERN_INFO "md: no rdev superblock!\n");
1296}
1297
1298void md_print_devices(void)
1299{
1300 struct list_head *tmp, *tmp2;
1301 mdk_rdev_t *rdev;
1302 mddev_t *mddev;
1303 char b[BDEVNAME_SIZE];
1304
1305 printk("\n");
1306 printk("md: **********************************\n");
1307 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1308 printk("md: **********************************\n");
1309 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
NeilBrown32a76272005-06-21 17:17:14 -07001311 if (mddev->bitmap)
1312 bitmap_print_sb(mddev->bitmap);
1313 else
1314 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 ITERATE_RDEV(mddev,rdev,tmp2)
1316 printk("<%s>", bdevname(rdev->bdev,b));
1317 printk("\n");
1318
1319 ITERATE_RDEV(mddev,rdev,tmp2)
1320 print_rdev(rdev);
1321 }
1322 printk("md: **********************************\n");
1323 printk("\n");
1324}
1325
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327static void sync_sbs(mddev_t * mddev)
1328{
1329 mdk_rdev_t *rdev;
1330 struct list_head *tmp;
1331
1332 ITERATE_RDEV(mddev,rdev,tmp) {
1333 super_types[mddev->major_version].
1334 sync_super(mddev, rdev);
1335 rdev->sb_loaded = 1;
1336 }
1337}
1338
1339static void md_update_sb(mddev_t * mddev)
1340{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001341 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 struct list_head *tmp;
1343 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001344 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346repeat:
NeilBrown06d91a52005-06-21 17:17:12 -07001347 spin_lock(&mddev->write_lock);
1348 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 mddev->utime = get_seconds();
1350 mddev->events ++;
1351
1352 if (!mddev->events) {
1353 /*
1354 * oops, this 64-bit counter should never wrap.
1355 * Either we are in around ~1 trillion A.C., assuming
1356 * 1 reboot per second, or we have a bug:
1357 */
1358 MD_BUG();
1359 mddev->events --;
1360 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001361 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 sync_sbs(mddev);
1363
1364 /*
1365 * do not write anything to disk if using
1366 * nonpersistent superblocks
1367 */
NeilBrown06d91a52005-06-21 17:17:12 -07001368 if (!mddev->persistent) {
1369 mddev->sb_dirty = 0;
1370 spin_unlock(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001371 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001373 }
1374 spin_unlock(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 dprintk(KERN_INFO
1377 "md: updating %s RAID superblock on device (in sync %d)\n",
1378 mdname(mddev),mddev->in_sync);
1379
NeilBrown32a76272005-06-21 17:17:14 -07001380 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 ITERATE_RDEV(mddev,rdev,tmp) {
1382 char b[BDEVNAME_SIZE];
1383 dprintk(KERN_INFO "md: ");
1384 if (rdev->faulty)
1385 dprintk("(skipping faulty ");
1386
1387 dprintk("%s ", bdevname(rdev->bdev,b));
1388 if (!rdev->faulty) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001389 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001390 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001391 rdev->sb_page);
1392 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1393 bdevname(rdev->bdev,b),
1394 (unsigned long long)rdev->sb_offset);
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 } else
1397 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001398 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* only need to write one superblock... */
1400 break;
1401 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001402 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
1403 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1404
NeilBrown06d91a52005-06-21 17:17:12 -07001405 spin_lock(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001406 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001407 /* have to write it out again */
1408 spin_unlock(&mddev->write_lock);
1409 goto repeat;
1410 }
1411 mddev->sb_dirty = 0;
1412 spin_unlock(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001413 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
1417/*
1418 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1419 *
1420 * mark the device faulty if:
1421 *
1422 * - the device is nonexistent (zero size)
1423 * - the device has no valid superblock
1424 *
1425 * a faulty rdev _never_ has rdev->sb set.
1426 */
1427static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1428{
1429 char b[BDEVNAME_SIZE];
1430 int err;
1431 mdk_rdev_t *rdev;
1432 sector_t size;
1433
1434 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1435 if (!rdev) {
1436 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1437 return ERR_PTR(-ENOMEM);
1438 }
1439 memset(rdev, 0, sizeof(*rdev));
1440
1441 if ((err = alloc_disk_sb(rdev)))
1442 goto abort_free;
1443
1444 err = lock_rdev(rdev, newdev);
1445 if (err)
1446 goto abort_free;
1447
1448 rdev->desc_nr = -1;
1449 rdev->faulty = 0;
1450 rdev->in_sync = 0;
1451 rdev->data_offset = 0;
1452 atomic_set(&rdev->nr_pending, 0);
1453
1454 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1455 if (!size) {
1456 printk(KERN_WARNING
1457 "md: %s has zero or unknown size, marking faulty!\n",
1458 bdevname(rdev->bdev,b));
1459 err = -EINVAL;
1460 goto abort_free;
1461 }
1462
1463 if (super_format >= 0) {
1464 err = super_types[super_format].
1465 load_super(rdev, NULL, super_minor);
1466 if (err == -EINVAL) {
1467 printk(KERN_WARNING
1468 "md: %s has invalid sb, not importing!\n",
1469 bdevname(rdev->bdev,b));
1470 goto abort_free;
1471 }
1472 if (err < 0) {
1473 printk(KERN_WARNING
1474 "md: could not read %s's sb, not importing!\n",
1475 bdevname(rdev->bdev,b));
1476 goto abort_free;
1477 }
1478 }
1479 INIT_LIST_HEAD(&rdev->same_set);
1480
1481 return rdev;
1482
1483abort_free:
1484 if (rdev->sb_page) {
1485 if (rdev->bdev)
1486 unlock_rdev(rdev);
1487 free_disk_sb(rdev);
1488 }
1489 kfree(rdev);
1490 return ERR_PTR(err);
1491}
1492
1493/*
1494 * Check a full RAID array for plausibility
1495 */
1496
1497
NeilBrowna757e642005-04-16 15:26:42 -07001498static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 int i;
1501 struct list_head *tmp;
1502 mdk_rdev_t *rdev, *freshest;
1503 char b[BDEVNAME_SIZE];
1504
1505 freshest = NULL;
1506 ITERATE_RDEV(mddev,rdev,tmp)
1507 switch (super_types[mddev->major_version].
1508 load_super(rdev, freshest, mddev->minor_version)) {
1509 case 1:
1510 freshest = rdev;
1511 break;
1512 case 0:
1513 break;
1514 default:
1515 printk( KERN_ERR \
1516 "md: fatal superblock inconsistency in %s"
1517 " -- removing from array\n",
1518 bdevname(rdev->bdev,b));
1519 kick_rdev_from_array(rdev);
1520 }
1521
1522
1523 super_types[mddev->major_version].
1524 validate_super(mddev, freshest);
1525
1526 i = 0;
1527 ITERATE_RDEV(mddev,rdev,tmp) {
1528 if (rdev != freshest)
1529 if (super_types[mddev->major_version].
1530 validate_super(mddev, rdev)) {
1531 printk(KERN_WARNING "md: kicking non-fresh %s"
1532 " from array!\n",
1533 bdevname(rdev->bdev,b));
1534 kick_rdev_from_array(rdev);
1535 continue;
1536 }
1537 if (mddev->level == LEVEL_MULTIPATH) {
1538 rdev->desc_nr = i++;
1539 rdev->raid_disk = rdev->desc_nr;
1540 rdev->in_sync = 1;
1541 }
1542 }
1543
1544
1545
1546 if (mddev->recovery_cp != MaxSector &&
1547 mddev->level >= 1)
1548 printk(KERN_ERR "md: %s: raid array is not clean"
1549 " -- starting background reconstruction\n",
1550 mdname(mddev));
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
NeilBrowneae17012005-11-08 21:39:23 -08001554struct md_sysfs_entry {
1555 struct attribute attr;
1556 ssize_t (*show)(mddev_t *, char *);
1557 ssize_t (*store)(mddev_t *, const char *, size_t);
1558};
1559
1560static ssize_t
1561md_show_level(mddev_t *mddev, char *page)
1562{
1563 mdk_personality_t *p = mddev->pers;
1564 if (p == NULL)
1565 return 0;
1566 if (mddev->level >= 0)
1567 return sprintf(page, "RAID-%d\n", mddev->level);
1568 else
1569 return sprintf(page, "%s\n", p->name);
1570}
1571
1572static struct md_sysfs_entry md_level = {
1573 .attr = {.name = "level", .mode = S_IRUGO },
1574 .show = md_show_level,
1575};
1576
1577static ssize_t
1578md_show_rdisks(mddev_t *mddev, char *page)
1579{
1580 return sprintf(page, "%d\n", mddev->raid_disks);
1581}
1582
1583static struct md_sysfs_entry md_raid_disks = {
1584 .attr = {.name = "raid_disks", .mode = S_IRUGO },
1585 .show = md_show_rdisks,
1586};
1587
1588static struct attribute *md_default_attrs[] = {
1589 &md_level.attr,
1590 &md_raid_disks.attr,
1591 NULL,
1592};
1593
1594static ssize_t
1595md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1596{
1597 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1598 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1599
1600 if (!entry->show)
1601 return -EIO;
1602 return entry->show(mddev, page);
1603}
1604
1605static ssize_t
1606md_attr_store(struct kobject *kobj, struct attribute *attr,
1607 const char *page, size_t length)
1608{
1609 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1610 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1611
1612 if (!entry->store)
1613 return -EIO;
1614 return entry->store(mddev, page, length);
1615}
1616
1617static void md_free(struct kobject *ko)
1618{
1619 mddev_t *mddev = container_of(ko, mddev_t, kobj);
1620 kfree(mddev);
1621}
1622
1623static struct sysfs_ops md_sysfs_ops = {
1624 .show = md_attr_show,
1625 .store = md_attr_store,
1626};
1627static struct kobj_type md_ktype = {
1628 .release = md_free,
1629 .sysfs_ops = &md_sysfs_ops,
1630 .default_attrs = md_default_attrs,
1631};
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633int mdp_major = 0;
1634
1635static struct kobject *md_probe(dev_t dev, int *part, void *data)
1636{
1637 static DECLARE_MUTEX(disks_sem);
1638 mddev_t *mddev = mddev_find(dev);
1639 struct gendisk *disk;
1640 int partitioned = (MAJOR(dev) != MD_MAJOR);
1641 int shift = partitioned ? MdpMinorShift : 0;
1642 int unit = MINOR(dev) >> shift;
1643
1644 if (!mddev)
1645 return NULL;
1646
1647 down(&disks_sem);
1648 if (mddev->gendisk) {
1649 up(&disks_sem);
1650 mddev_put(mddev);
1651 return NULL;
1652 }
1653 disk = alloc_disk(1 << shift);
1654 if (!disk) {
1655 up(&disks_sem);
1656 mddev_put(mddev);
1657 return NULL;
1658 }
1659 disk->major = MAJOR(dev);
1660 disk->first_minor = unit << shift;
1661 if (partitioned) {
1662 sprintf(disk->disk_name, "md_d%d", unit);
1663 sprintf(disk->devfs_name, "md/d%d", unit);
1664 } else {
1665 sprintf(disk->disk_name, "md%d", unit);
1666 sprintf(disk->devfs_name, "md/%d", unit);
1667 }
1668 disk->fops = &md_fops;
1669 disk->private_data = mddev;
1670 disk->queue = mddev->queue;
1671 add_disk(disk);
1672 mddev->gendisk = disk;
1673 up(&disks_sem);
NeilBrowneae17012005-11-08 21:39:23 -08001674 mddev->kobj.parent = kobject_get(&disk->kobj);
1675 mddev->kobj.k_name = NULL;
1676 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
1677 mddev->kobj.ktype = &md_ktype;
1678 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return NULL;
1680}
1681
1682void md_wakeup_thread(mdk_thread_t *thread);
1683
1684static void md_safemode_timeout(unsigned long data)
1685{
1686 mddev_t *mddev = (mddev_t *) data;
1687
1688 mddev->safemode = 1;
1689 md_wakeup_thread(mddev->thread);
1690}
1691
1692
1693static int do_md_run(mddev_t * mddev)
1694{
1695 int pnum, err;
1696 int chunk_size;
1697 struct list_head *tmp;
1698 mdk_rdev_t *rdev;
1699 struct gendisk *disk;
1700 char b[BDEVNAME_SIZE];
1701
NeilBrowna757e642005-04-16 15:26:42 -07001702 if (list_empty(&mddev->disks))
1703 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 if (mddev->pers)
1707 return -EBUSY;
1708
1709 /*
1710 * Analyze all RAID superblock(s)
1711 */
NeilBrowna757e642005-04-16 15:26:42 -07001712 if (!mddev->raid_disks)
1713 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 chunk_size = mddev->chunk_size;
1716 pnum = level_to_pers(mddev->level);
1717
1718 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1719 if (!chunk_size) {
1720 /*
1721 * 'default chunksize' in the old md code used to
1722 * be PAGE_SIZE, baaad.
1723 * we abort here to be on the safe side. We don't
1724 * want to continue the bad practice.
1725 */
1726 printk(KERN_ERR
1727 "no chunksize specified, see 'man raidtab'\n");
1728 return -EINVAL;
1729 }
1730 if (chunk_size > MAX_CHUNK_SIZE) {
1731 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1732 chunk_size, MAX_CHUNK_SIZE);
1733 return -EINVAL;
1734 }
1735 /*
1736 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1737 */
1738 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07001739 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return -EINVAL;
1741 }
1742 if (chunk_size < PAGE_SIZE) {
1743 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1744 chunk_size, PAGE_SIZE);
1745 return -EINVAL;
1746 }
1747
1748 /* devices must have minimum size of one chunk */
1749 ITERATE_RDEV(mddev,rdev,tmp) {
1750 if (rdev->faulty)
1751 continue;
1752 if (rdev->size < chunk_size / 1024) {
1753 printk(KERN_WARNING
1754 "md: Dev %s smaller than chunk_size:"
1755 " %lluk < %dk\n",
1756 bdevname(rdev->bdev,b),
1757 (unsigned long long)rdev->size,
1758 chunk_size / 1024);
1759 return -EINVAL;
1760 }
1761 }
1762 }
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764#ifdef CONFIG_KMOD
1765 if (!pers[pnum])
1766 {
1767 request_module("md-personality-%d", pnum);
1768 }
1769#endif
1770
1771 /*
1772 * Drop all container device buffers, from now on
1773 * the only valid external interface is through the md
1774 * device.
1775 * Also find largest hardsector size
1776 */
1777 ITERATE_RDEV(mddev,rdev,tmp) {
1778 if (rdev->faulty)
1779 continue;
1780 sync_blockdev(rdev->bdev);
1781 invalidate_bdev(rdev->bdev, 0);
1782 }
1783
1784 md_probe(mddev->unit, NULL, NULL);
1785 disk = mddev->gendisk;
1786 if (!disk)
1787 return -ENOMEM;
1788
1789 spin_lock(&pers_lock);
1790 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
1791 spin_unlock(&pers_lock);
1792 printk(KERN_WARNING "md: personality %d is not loaded!\n",
1793 pnum);
1794 return -EINVAL;
1795 }
1796
1797 mddev->pers = pers[pnum];
1798 spin_unlock(&pers_lock);
1799
NeilBrown657390d2005-08-26 18:34:16 -07001800 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
1802
NeilBrown32a76272005-06-21 17:17:14 -07001803 /* before we start the array running, initialise the bitmap */
1804 err = bitmap_create(mddev);
1805 if (err)
1806 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
1807 mdname(mddev), err);
1808 else
1809 err = mddev->pers->run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (err) {
1811 printk(KERN_ERR "md: pers->run() failed ...\n");
1812 module_put(mddev->pers->owner);
1813 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001814 bitmap_destroy(mddev);
1815 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 atomic_set(&mddev->writes_pending,0);
1818 mddev->safemode = 0;
1819 mddev->safemode_timer.function = md_safemode_timeout;
1820 mddev->safemode_timer.data = (unsigned long) mddev;
1821 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
1822 mddev->in_sync = 1;
1823
1824 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07001825 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 if (mddev->sb_dirty)
1828 md_update_sb(mddev);
1829
1830 set_capacity(disk, mddev->array_size<<1);
1831
1832 /* If we call blk_queue_make_request here, it will
1833 * re-initialise max_sectors etc which may have been
1834 * refined inside -> run. So just set the bits we need to set.
1835 * Most initialisation happended when we called
1836 * blk_queue_make_request(..., md_fail_request)
1837 * earlier.
1838 */
1839 mddev->queue->queuedata = mddev;
1840 mddev->queue->make_request_fn = mddev->pers->make_request;
1841
1842 mddev->changed = 1;
1843 return 0;
1844}
1845
1846static int restart_array(mddev_t *mddev)
1847{
1848 struct gendisk *disk = mddev->gendisk;
1849 int err;
1850
1851 /*
1852 * Complain if it has no devices
1853 */
1854 err = -ENXIO;
1855 if (list_empty(&mddev->disks))
1856 goto out;
1857
1858 if (mddev->pers) {
1859 err = -EBUSY;
1860 if (!mddev->ro)
1861 goto out;
1862
1863 mddev->safemode = 0;
1864 mddev->ro = 0;
1865 set_disk_ro(disk, 0);
1866
1867 printk(KERN_INFO "md: %s switched to read-write mode.\n",
1868 mdname(mddev));
1869 /*
1870 * Kick recovery or resync if necessary
1871 */
1872 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1873 md_wakeup_thread(mddev->thread);
1874 err = 0;
1875 } else {
1876 printk(KERN_ERR "md: %s has no personality assigned.\n",
1877 mdname(mddev));
1878 err = -EINVAL;
1879 }
1880
1881out:
1882 return err;
1883}
1884
1885static int do_md_stop(mddev_t * mddev, int ro)
1886{
1887 int err = 0;
1888 struct gendisk *disk = mddev->gendisk;
1889
1890 if (mddev->pers) {
1891 if (atomic_read(&mddev->active)>2) {
1892 printk("md: %s still in use.\n",mdname(mddev));
1893 return -EBUSY;
1894 }
1895
1896 if (mddev->sync_thread) {
1897 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1898 md_unregister_thread(mddev->sync_thread);
1899 mddev->sync_thread = NULL;
1900 }
1901
1902 del_timer_sync(&mddev->safemode_timer);
1903
1904 invalidate_partition(disk, 0);
1905
1906 if (ro) {
1907 err = -ENXIO;
1908 if (mddev->ro)
1909 goto out;
1910 mddev->ro = 1;
1911 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07001912 bitmap_flush(mddev);
1913 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (mddev->ro)
1915 set_disk_ro(disk, 0);
1916 blk_queue_make_request(mddev->queue, md_fail_request);
1917 mddev->pers->stop(mddev);
1918 module_put(mddev->pers->owner);
1919 mddev->pers = NULL;
1920 if (mddev->ro)
1921 mddev->ro = 0;
1922 }
1923 if (!mddev->in_sync) {
1924 /* mark array as shutdown cleanly */
1925 mddev->in_sync = 1;
1926 md_update_sb(mddev);
1927 }
1928 if (ro)
1929 set_disk_ro(disk, 1);
1930 }
NeilBrown32a76272005-06-21 17:17:14 -07001931
1932 bitmap_destroy(mddev);
1933 if (mddev->bitmap_file) {
1934 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
1935 fput(mddev->bitmap_file);
1936 mddev->bitmap_file = NULL;
1937 }
NeilBrown92232142005-08-18 11:24:16 -07001938 mddev->bitmap_offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /*
1941 * Free resources if final stop
1942 */
1943 if (!ro) {
1944 struct gendisk *disk;
1945 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
1946
1947 export_array(mddev);
1948
1949 mddev->array_size = 0;
1950 disk = mddev->gendisk;
1951 if (disk)
1952 set_capacity(disk, 0);
1953 mddev->changed = 1;
1954 } else
1955 printk(KERN_INFO "md: %s switched to read-only mode.\n",
1956 mdname(mddev));
1957 err = 0;
1958out:
1959 return err;
1960}
1961
1962static void autorun_array(mddev_t *mddev)
1963{
1964 mdk_rdev_t *rdev;
1965 struct list_head *tmp;
1966 int err;
1967
NeilBrowna757e642005-04-16 15:26:42 -07001968 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 printk(KERN_INFO "md: running: ");
1972
1973 ITERATE_RDEV(mddev,rdev,tmp) {
1974 char b[BDEVNAME_SIZE];
1975 printk("<%s>", bdevname(rdev->bdev,b));
1976 }
1977 printk("\n");
1978
1979 err = do_md_run (mddev);
1980 if (err) {
1981 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
1982 do_md_stop (mddev, 0);
1983 }
1984}
1985
1986/*
1987 * lets try to run arrays based on all disks that have arrived
1988 * until now. (those are in pending_raid_disks)
1989 *
1990 * the method: pick the first pending disk, collect all disks with
1991 * the same UUID, remove all from the pending list and put them into
1992 * the 'same_array' list. Then order this list based on superblock
1993 * update time (freshest comes first), kick out 'old' disks and
1994 * compare superblocks. If everything's fine then run it.
1995 *
1996 * If "unit" is allocated, then bump its reference count
1997 */
1998static void autorun_devices(int part)
1999{
2000 struct list_head candidates;
2001 struct list_head *tmp;
2002 mdk_rdev_t *rdev0, *rdev;
2003 mddev_t *mddev;
2004 char b[BDEVNAME_SIZE];
2005
2006 printk(KERN_INFO "md: autorun ...\n");
2007 while (!list_empty(&pending_raid_disks)) {
2008 dev_t dev;
2009 rdev0 = list_entry(pending_raid_disks.next,
2010 mdk_rdev_t, same_set);
2011
2012 printk(KERN_INFO "md: considering %s ...\n",
2013 bdevname(rdev0->bdev,b));
2014 INIT_LIST_HEAD(&candidates);
2015 ITERATE_RDEV_PENDING(rdev,tmp)
2016 if (super_90_load(rdev, rdev0, 0) >= 0) {
2017 printk(KERN_INFO "md: adding %s ...\n",
2018 bdevname(rdev->bdev,b));
2019 list_move(&rdev->same_set, &candidates);
2020 }
2021 /*
2022 * now we have a set of devices, with all of them having
2023 * mostly sane superblocks. It's time to allocate the
2024 * mddev.
2025 */
2026 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2027 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2028 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2029 break;
2030 }
2031 if (part)
2032 dev = MKDEV(mdp_major,
2033 rdev0->preferred_minor << MdpMinorShift);
2034 else
2035 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2036
2037 md_probe(dev, NULL, NULL);
2038 mddev = mddev_find(dev);
2039 if (!mddev) {
2040 printk(KERN_ERR
2041 "md: cannot allocate memory for md drive.\n");
2042 break;
2043 }
2044 if (mddev_lock(mddev))
2045 printk(KERN_WARNING "md: %s locked, cannot run\n",
2046 mdname(mddev));
2047 else if (mddev->raid_disks || mddev->major_version
2048 || !list_empty(&mddev->disks)) {
2049 printk(KERN_WARNING
2050 "md: %s already running, cannot run %s\n",
2051 mdname(mddev), bdevname(rdev0->bdev,b));
2052 mddev_unlock(mddev);
2053 } else {
2054 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2055 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2056 list_del_init(&rdev->same_set);
2057 if (bind_rdev_to_array(rdev, mddev))
2058 export_rdev(rdev);
2059 }
2060 autorun_array(mddev);
2061 mddev_unlock(mddev);
2062 }
2063 /* on success, candidates will be empty, on error
2064 * it won't...
2065 */
2066 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2067 export_rdev(rdev);
2068 mddev_put(mddev);
2069 }
2070 printk(KERN_INFO "md: ... autorun DONE.\n");
2071}
2072
2073/*
2074 * import RAID devices based on one partition
2075 * if possible, the array gets run as well.
2076 */
2077
2078static int autostart_array(dev_t startdev)
2079{
2080 char b[BDEVNAME_SIZE];
2081 int err = -EINVAL, i;
2082 mdp_super_t *sb = NULL;
2083 mdk_rdev_t *start_rdev = NULL, *rdev;
2084
2085 start_rdev = md_import_device(startdev, 0, 0);
2086 if (IS_ERR(start_rdev))
2087 return err;
2088
2089
2090 /* NOTE: this can only work for 0.90.0 superblocks */
2091 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2092 if (sb->major_version != 0 ||
2093 sb->minor_version != 90 ) {
2094 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2095 export_rdev(start_rdev);
2096 return err;
2097 }
2098
2099 if (start_rdev->faulty) {
2100 printk(KERN_WARNING
2101 "md: can not autostart based on faulty %s!\n",
2102 bdevname(start_rdev->bdev,b));
2103 export_rdev(start_rdev);
2104 return err;
2105 }
2106 list_add(&start_rdev->same_set, &pending_raid_disks);
2107
2108 for (i = 0; i < MD_SB_DISKS; i++) {
2109 mdp_disk_t *desc = sb->disks + i;
2110 dev_t dev = MKDEV(desc->major, desc->minor);
2111
2112 if (!dev)
2113 continue;
2114 if (dev == startdev)
2115 continue;
2116 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2117 continue;
2118 rdev = md_import_device(dev, 0, 0);
2119 if (IS_ERR(rdev))
2120 continue;
2121
2122 list_add(&rdev->same_set, &pending_raid_disks);
2123 }
2124
2125 /*
2126 * possibly return codes
2127 */
2128 autorun_devices(0);
2129 return 0;
2130
2131}
2132
2133
2134static int get_version(void __user * arg)
2135{
2136 mdu_version_t ver;
2137
2138 ver.major = MD_MAJOR_VERSION;
2139 ver.minor = MD_MINOR_VERSION;
2140 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2141
2142 if (copy_to_user(arg, &ver, sizeof(ver)))
2143 return -EFAULT;
2144
2145 return 0;
2146}
2147
2148static int get_array_info(mddev_t * mddev, void __user * arg)
2149{
2150 mdu_array_info_t info;
2151 int nr,working,active,failed,spare;
2152 mdk_rdev_t *rdev;
2153 struct list_head *tmp;
2154
2155 nr=working=active=failed=spare=0;
2156 ITERATE_RDEV(mddev,rdev,tmp) {
2157 nr++;
2158 if (rdev->faulty)
2159 failed++;
2160 else {
2161 working++;
2162 if (rdev->in_sync)
2163 active++;
2164 else
2165 spare++;
2166 }
2167 }
2168
2169 info.major_version = mddev->major_version;
2170 info.minor_version = mddev->minor_version;
2171 info.patch_version = MD_PATCHLEVEL_VERSION;
2172 info.ctime = mddev->ctime;
2173 info.level = mddev->level;
2174 info.size = mddev->size;
2175 info.nr_disks = nr;
2176 info.raid_disks = mddev->raid_disks;
2177 info.md_minor = mddev->md_minor;
2178 info.not_persistent= !mddev->persistent;
2179
2180 info.utime = mddev->utime;
2181 info.state = 0;
2182 if (mddev->in_sync)
2183 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07002184 if (mddev->bitmap && mddev->bitmap_offset)
2185 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 info.active_disks = active;
2187 info.working_disks = working;
2188 info.failed_disks = failed;
2189 info.spare_disks = spare;
2190
2191 info.layout = mddev->layout;
2192 info.chunk_size = mddev->chunk_size;
2193
2194 if (copy_to_user(arg, &info, sizeof(info)))
2195 return -EFAULT;
2196
2197 return 0;
2198}
2199
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002200static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07002201{
2202 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2203 char *ptr, *buf = NULL;
2204 int err = -ENOMEM;
2205
2206 file = kmalloc(sizeof(*file), GFP_KERNEL);
2207 if (!file)
2208 goto out;
2209
2210 /* bitmap disabled, zero the first byte and copy out */
2211 if (!mddev->bitmap || !mddev->bitmap->file) {
2212 file->pathname[0] = '\0';
2213 goto copy_out;
2214 }
2215
2216 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2217 if (!buf)
2218 goto out;
2219
2220 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2221 if (!ptr)
2222 goto out;
2223
2224 strcpy(file->pathname, ptr);
2225
2226copy_out:
2227 err = 0;
2228 if (copy_to_user(arg, file, sizeof(*file)))
2229 err = -EFAULT;
2230out:
2231 kfree(buf);
2232 kfree(file);
2233 return err;
2234}
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236static int get_disk_info(mddev_t * mddev, void __user * arg)
2237{
2238 mdu_disk_info_t info;
2239 unsigned int nr;
2240 mdk_rdev_t *rdev;
2241
2242 if (copy_from_user(&info, arg, sizeof(info)))
2243 return -EFAULT;
2244
2245 nr = info.number;
2246
2247 rdev = find_rdev_nr(mddev, nr);
2248 if (rdev) {
2249 info.major = MAJOR(rdev->bdev->bd_dev);
2250 info.minor = MINOR(rdev->bdev->bd_dev);
2251 info.raid_disk = rdev->raid_disk;
2252 info.state = 0;
2253 if (rdev->faulty)
2254 info.state |= (1<<MD_DISK_FAULTY);
2255 else if (rdev->in_sync) {
2256 info.state |= (1<<MD_DISK_ACTIVE);
2257 info.state |= (1<<MD_DISK_SYNC);
2258 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002259 if (test_bit(WriteMostly, &rdev->flags))
2260 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 } else {
2262 info.major = info.minor = 0;
2263 info.raid_disk = -1;
2264 info.state = (1<<MD_DISK_REMOVED);
2265 }
2266
2267 if (copy_to_user(arg, &info, sizeof(info)))
2268 return -EFAULT;
2269
2270 return 0;
2271}
2272
2273static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2274{
2275 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2276 mdk_rdev_t *rdev;
2277 dev_t dev = MKDEV(info->major,info->minor);
2278
2279 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2280 return -EOVERFLOW;
2281
2282 if (!mddev->raid_disks) {
2283 int err;
2284 /* expecting a device which has a superblock */
2285 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2286 if (IS_ERR(rdev)) {
2287 printk(KERN_WARNING
2288 "md: md_import_device returned %ld\n",
2289 PTR_ERR(rdev));
2290 return PTR_ERR(rdev);
2291 }
2292 if (!list_empty(&mddev->disks)) {
2293 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2294 mdk_rdev_t, same_set);
2295 int err = super_types[mddev->major_version]
2296 .load_super(rdev, rdev0, mddev->minor_version);
2297 if (err < 0) {
2298 printk(KERN_WARNING
2299 "md: %s has different UUID to %s\n",
2300 bdevname(rdev->bdev,b),
2301 bdevname(rdev0->bdev,b2));
2302 export_rdev(rdev);
2303 return -EINVAL;
2304 }
2305 }
2306 err = bind_rdev_to_array(rdev, mddev);
2307 if (err)
2308 export_rdev(rdev);
2309 return err;
2310 }
2311
2312 /*
2313 * add_new_disk can be used once the array is assembled
2314 * to add "hot spares". They must already have a superblock
2315 * written
2316 */
2317 if (mddev->pers) {
2318 int err;
2319 if (!mddev->pers->hot_add_disk) {
2320 printk(KERN_WARNING
2321 "%s: personality does not support diskops!\n",
2322 mdname(mddev));
2323 return -EINVAL;
2324 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07002325 if (mddev->persistent)
2326 rdev = md_import_device(dev, mddev->major_version,
2327 mddev->minor_version);
2328 else
2329 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (IS_ERR(rdev)) {
2331 printk(KERN_WARNING
2332 "md: md_import_device returned %ld\n",
2333 PTR_ERR(rdev));
2334 return PTR_ERR(rdev);
2335 }
NeilBrown41158c72005-06-21 17:17:25 -07002336 /* set save_raid_disk if appropriate */
2337 if (!mddev->persistent) {
2338 if (info->state & (1<<MD_DISK_SYNC) &&
2339 info->raid_disk < mddev->raid_disks)
2340 rdev->raid_disk = info->raid_disk;
2341 else
2342 rdev->raid_disk = -1;
2343 } else
2344 super_types[mddev->major_version].
2345 validate_super(mddev, rdev);
2346 rdev->saved_raid_disk = rdev->raid_disk;
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 rdev->in_sync = 0; /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002349 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2350 set_bit(WriteMostly, &rdev->flags);
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 rdev->raid_disk = -1;
2353 err = bind_rdev_to_array(rdev, mddev);
2354 if (err)
2355 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07002356
2357 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002358 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return err;
2360 }
2361
2362 /* otherwise, add_new_disk is only allowed
2363 * for major_version==0 superblocks
2364 */
2365 if (mddev->major_version != 0) {
2366 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2367 mdname(mddev));
2368 return -EINVAL;
2369 }
2370
2371 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2372 int err;
2373 rdev = md_import_device (dev, -1, 0);
2374 if (IS_ERR(rdev)) {
2375 printk(KERN_WARNING
2376 "md: error, md_import_device() returned %ld\n",
2377 PTR_ERR(rdev));
2378 return PTR_ERR(rdev);
2379 }
2380 rdev->desc_nr = info->number;
2381 if (info->raid_disk < mddev->raid_disks)
2382 rdev->raid_disk = info->raid_disk;
2383 else
2384 rdev->raid_disk = -1;
2385
2386 rdev->faulty = 0;
2387 if (rdev->raid_disk < mddev->raid_disks)
2388 rdev->in_sync = (info->state & (1<<MD_DISK_SYNC));
2389 else
2390 rdev->in_sync = 0;
2391
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002392 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2393 set_bit(WriteMostly, &rdev->flags);
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 err = bind_rdev_to_array(rdev, mddev);
2396 if (err) {
2397 export_rdev(rdev);
2398 return err;
2399 }
2400
2401 if (!mddev->persistent) {
2402 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2403 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2404 } else
2405 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2406 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2407
2408 if (!mddev->size || (mddev->size > rdev->size))
2409 mddev->size = rdev->size;
2410 }
2411
2412 return 0;
2413}
2414
2415static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2416{
2417 char b[BDEVNAME_SIZE];
2418 mdk_rdev_t *rdev;
2419
2420 if (!mddev->pers)
2421 return -ENODEV;
2422
2423 rdev = find_rdev(mddev, dev);
2424 if (!rdev)
2425 return -ENXIO;
2426
2427 if (rdev->raid_disk >= 0)
2428 goto busy;
2429
2430 kick_rdev_from_array(rdev);
2431 md_update_sb(mddev);
2432
2433 return 0;
2434busy:
2435 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2436 bdevname(rdev->bdev,b), mdname(mddev));
2437 return -EBUSY;
2438}
2439
2440static int hot_add_disk(mddev_t * mddev, dev_t dev)
2441{
2442 char b[BDEVNAME_SIZE];
2443 int err;
2444 unsigned int size;
2445 mdk_rdev_t *rdev;
2446
2447 if (!mddev->pers)
2448 return -ENODEV;
2449
2450 if (mddev->major_version != 0) {
2451 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2452 " version-0 superblocks.\n",
2453 mdname(mddev));
2454 return -EINVAL;
2455 }
2456 if (!mddev->pers->hot_add_disk) {
2457 printk(KERN_WARNING
2458 "%s: personality does not support diskops!\n",
2459 mdname(mddev));
2460 return -EINVAL;
2461 }
2462
2463 rdev = md_import_device (dev, -1, 0);
2464 if (IS_ERR(rdev)) {
2465 printk(KERN_WARNING
2466 "md: error, md_import_device() returned %ld\n",
2467 PTR_ERR(rdev));
2468 return -EINVAL;
2469 }
2470
2471 if (mddev->persistent)
2472 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2473 else
2474 rdev->sb_offset =
2475 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2476
2477 size = calc_dev_size(rdev, mddev->chunk_size);
2478 rdev->size = size;
2479
2480 if (size < mddev->size) {
2481 printk(KERN_WARNING
2482 "%s: disk size %llu blocks < array size %llu\n",
2483 mdname(mddev), (unsigned long long)size,
2484 (unsigned long long)mddev->size);
2485 err = -ENOSPC;
2486 goto abort_export;
2487 }
2488
2489 if (rdev->faulty) {
2490 printk(KERN_WARNING
2491 "md: can not hot-add faulty %s disk to %s!\n",
2492 bdevname(rdev->bdev,b), mdname(mddev));
2493 err = -EINVAL;
2494 goto abort_export;
2495 }
2496 rdev->in_sync = 0;
2497 rdev->desc_nr = -1;
2498 bind_rdev_to_array(rdev, mddev);
2499
2500 /*
2501 * The rest should better be atomic, we can have disk failures
2502 * noticed in interrupt contexts ...
2503 */
2504
2505 if (rdev->desc_nr == mddev->max_disks) {
2506 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2507 mdname(mddev));
2508 err = -EBUSY;
2509 goto abort_unbind_export;
2510 }
2511
2512 rdev->raid_disk = -1;
2513
2514 md_update_sb(mddev);
2515
2516 /*
2517 * Kick recovery, maybe this spare has to be added to the
2518 * array immediately.
2519 */
2520 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2521 md_wakeup_thread(mddev->thread);
2522
2523 return 0;
2524
2525abort_unbind_export:
2526 unbind_rdev_from_array(rdev);
2527
2528abort_export:
2529 export_rdev(rdev);
2530 return err;
2531}
2532
NeilBrown32a76272005-06-21 17:17:14 -07002533/* similar to deny_write_access, but accounts for our holding a reference
2534 * to the file ourselves */
2535static int deny_bitmap_write_access(struct file * file)
2536{
2537 struct inode *inode = file->f_mapping->host;
2538
2539 spin_lock(&inode->i_lock);
2540 if (atomic_read(&inode->i_writecount) > 1) {
2541 spin_unlock(&inode->i_lock);
2542 return -ETXTBSY;
2543 }
2544 atomic_set(&inode->i_writecount, -1);
2545 spin_unlock(&inode->i_lock);
2546
2547 return 0;
2548}
2549
2550static int set_bitmap_file(mddev_t *mddev, int fd)
2551{
2552 int err;
2553
NeilBrown36fa3062005-09-09 16:23:45 -07002554 if (mddev->pers) {
2555 if (!mddev->pers->quiesce)
2556 return -EBUSY;
2557 if (mddev->recovery || mddev->sync_thread)
2558 return -EBUSY;
2559 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07002560 }
2561
NeilBrown36fa3062005-09-09 16:23:45 -07002562
2563 if (fd >= 0) {
2564 if (mddev->bitmap)
2565 return -EEXIST; /* cannot add when bitmap is present */
2566 mddev->bitmap_file = fget(fd);
2567
2568 if (mddev->bitmap_file == NULL) {
2569 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2570 mdname(mddev));
2571 return -EBADF;
2572 }
2573
2574 err = deny_bitmap_write_access(mddev->bitmap_file);
2575 if (err) {
2576 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2577 mdname(mddev));
2578 fput(mddev->bitmap_file);
2579 mddev->bitmap_file = NULL;
2580 return err;
2581 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002582 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07002583 } else if (mddev->bitmap == NULL)
2584 return -ENOENT; /* cannot remove what isn't there */
2585 err = 0;
2586 if (mddev->pers) {
2587 mddev->pers->quiesce(mddev, 1);
2588 if (fd >= 0)
2589 err = bitmap_create(mddev);
2590 if (fd < 0 || err)
2591 bitmap_destroy(mddev);
2592 mddev->pers->quiesce(mddev, 0);
2593 } else if (fd < 0) {
2594 if (mddev->bitmap_file)
2595 fput(mddev->bitmap_file);
2596 mddev->bitmap_file = NULL;
2597 }
2598
NeilBrown32a76272005-06-21 17:17:14 -07002599 return err;
2600}
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602/*
2603 * set_array_info is used two different ways
2604 * The original usage is when creating a new array.
2605 * In this usage, raid_disks is > 0 and it together with
2606 * level, size, not_persistent,layout,chunksize determine the
2607 * shape of the array.
2608 * This will always create an array with a type-0.90.0 superblock.
2609 * The newer usage is when assembling an array.
2610 * In this case raid_disks will be 0, and the major_version field is
2611 * use to determine which style super-blocks are to be found on the devices.
2612 * The minor and patch _version numbers are also kept incase the
2613 * super_block handler wishes to interpret them.
2614 */
2615static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2616{
2617
2618 if (info->raid_disks == 0) {
2619 /* just setting version number for superblock loading */
2620 if (info->major_version < 0 ||
2621 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2622 super_types[info->major_version].name == NULL) {
2623 /* maybe try to auto-load a module? */
2624 printk(KERN_INFO
2625 "md: superblock version %d not known\n",
2626 info->major_version);
2627 return -EINVAL;
2628 }
2629 mddev->major_version = info->major_version;
2630 mddev->minor_version = info->minor_version;
2631 mddev->patch_version = info->patch_version;
2632 return 0;
2633 }
2634 mddev->major_version = MD_MAJOR_VERSION;
2635 mddev->minor_version = MD_MINOR_VERSION;
2636 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2637 mddev->ctime = get_seconds();
2638
2639 mddev->level = info->level;
2640 mddev->size = info->size;
2641 mddev->raid_disks = info->raid_disks;
2642 /* don't set md_minor, it is determined by which /dev/md* was
2643 * openned
2644 */
2645 if (info->state & (1<<MD_SB_CLEAN))
2646 mddev->recovery_cp = MaxSector;
2647 else
2648 mddev->recovery_cp = 0;
2649 mddev->persistent = ! info->not_persistent;
2650
2651 mddev->layout = info->layout;
2652 mddev->chunk_size = info->chunk_size;
2653
2654 mddev->max_disks = MD_SB_DISKS;
2655
2656 mddev->sb_dirty = 1;
2657
2658 /*
2659 * Generate a 128 bit UUID
2660 */
2661 get_random_bytes(mddev->uuid, 16);
2662
2663 return 0;
2664}
2665
2666/*
2667 * update_array_info is used to change the configuration of an
2668 * on-line array.
2669 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2670 * fields in the info are checked against the array.
2671 * Any differences that cannot be handled will cause an error.
2672 * Normally, only one change can be managed at a time.
2673 */
2674static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2675{
2676 int rv = 0;
2677 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07002678 int state = 0;
2679
2680 /* calculate expected state,ignoring low bits */
2681 if (mddev->bitmap && mddev->bitmap_offset)
2682 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 if (mddev->major_version != info->major_version ||
2685 mddev->minor_version != info->minor_version ||
2686/* mddev->patch_version != info->patch_version || */
2687 mddev->ctime != info->ctime ||
2688 mddev->level != info->level ||
2689/* mddev->layout != info->layout || */
2690 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07002691 mddev->chunk_size != info->chunk_size ||
2692 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2693 ((state^info->state) & 0xfffffe00)
2694 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return -EINVAL;
2696 /* Check there is only one change */
2697 if (mddev->size != info->size) cnt++;
2698 if (mddev->raid_disks != info->raid_disks) cnt++;
2699 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07002700 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (cnt == 0) return 0;
2702 if (cnt > 1) return -EINVAL;
2703
2704 if (mddev->layout != info->layout) {
2705 /* Change layout
2706 * we don't need to do anything at the md level, the
2707 * personality will take care of it all.
2708 */
2709 if (mddev->pers->reconfig == NULL)
2710 return -EINVAL;
2711 else
2712 return mddev->pers->reconfig(mddev, info->layout, -1);
2713 }
2714 if (mddev->size != info->size) {
2715 mdk_rdev_t * rdev;
2716 struct list_head *tmp;
2717 if (mddev->pers->resize == NULL)
2718 return -EINVAL;
2719 /* The "size" is the amount of each device that is used.
2720 * This can only make sense for arrays with redundancy.
2721 * linear and raid0 always use whatever space is available
2722 * We can only consider changing the size if no resync
2723 * or reconstruction is happening, and if the new size
2724 * is acceptable. It must fit before the sb_offset or,
2725 * if that is <data_offset, it must fit before the
2726 * size of each device.
2727 * If size is zero, we find the largest size that fits.
2728 */
2729 if (mddev->sync_thread)
2730 return -EBUSY;
2731 ITERATE_RDEV(mddev,rdev,tmp) {
2732 sector_t avail;
2733 int fit = (info->size == 0);
2734 if (rdev->sb_offset > rdev->data_offset)
2735 avail = (rdev->sb_offset*2) - rdev->data_offset;
2736 else
2737 avail = get_capacity(rdev->bdev->bd_disk)
2738 - rdev->data_offset;
2739 if (fit && (info->size == 0 || info->size > avail/2))
2740 info->size = avail/2;
2741 if (avail < ((sector_t)info->size << 1))
2742 return -ENOSPC;
2743 }
2744 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
2745 if (!rv) {
2746 struct block_device *bdev;
2747
2748 bdev = bdget_disk(mddev->gendisk, 0);
2749 if (bdev) {
2750 down(&bdev->bd_inode->i_sem);
2751 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2752 up(&bdev->bd_inode->i_sem);
2753 bdput(bdev);
2754 }
2755 }
2756 }
2757 if (mddev->raid_disks != info->raid_disks) {
2758 /* change the number of raid disks */
2759 if (mddev->pers->reshape == NULL)
2760 return -EINVAL;
2761 if (info->raid_disks <= 0 ||
2762 info->raid_disks >= mddev->max_disks)
2763 return -EINVAL;
2764 if (mddev->sync_thread)
2765 return -EBUSY;
2766 rv = mddev->pers->reshape(mddev, info->raid_disks);
2767 if (!rv) {
2768 struct block_device *bdev;
2769
2770 bdev = bdget_disk(mddev->gendisk, 0);
2771 if (bdev) {
2772 down(&bdev->bd_inode->i_sem);
2773 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2774 up(&bdev->bd_inode->i_sem);
2775 bdput(bdev);
2776 }
2777 }
2778 }
NeilBrown36fa3062005-09-09 16:23:45 -07002779 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
2780 if (mddev->pers->quiesce == NULL)
2781 return -EINVAL;
2782 if (mddev->recovery || mddev->sync_thread)
2783 return -EBUSY;
2784 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
2785 /* add the bitmap */
2786 if (mddev->bitmap)
2787 return -EEXIST;
2788 if (mddev->default_bitmap_offset == 0)
2789 return -EINVAL;
2790 mddev->bitmap_offset = mddev->default_bitmap_offset;
2791 mddev->pers->quiesce(mddev, 1);
2792 rv = bitmap_create(mddev);
2793 if (rv)
2794 bitmap_destroy(mddev);
2795 mddev->pers->quiesce(mddev, 0);
2796 } else {
2797 /* remove the bitmap */
2798 if (!mddev->bitmap)
2799 return -ENOENT;
2800 if (mddev->bitmap->file)
2801 return -EINVAL;
2802 mddev->pers->quiesce(mddev, 1);
2803 bitmap_destroy(mddev);
2804 mddev->pers->quiesce(mddev, 0);
2805 mddev->bitmap_offset = 0;
2806 }
2807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 md_update_sb(mddev);
2809 return rv;
2810}
2811
2812static int set_disk_faulty(mddev_t *mddev, dev_t dev)
2813{
2814 mdk_rdev_t *rdev;
2815
2816 if (mddev->pers == NULL)
2817 return -ENODEV;
2818
2819 rdev = find_rdev(mddev, dev);
2820 if (!rdev)
2821 return -ENODEV;
2822
2823 md_error(mddev, rdev);
2824 return 0;
2825}
2826
2827static int md_ioctl(struct inode *inode, struct file *file,
2828 unsigned int cmd, unsigned long arg)
2829{
2830 int err = 0;
2831 void __user *argp = (void __user *)arg;
2832 struct hd_geometry __user *loc = argp;
2833 mddev_t *mddev = NULL;
2834
2835 if (!capable(CAP_SYS_ADMIN))
2836 return -EACCES;
2837
2838 /*
2839 * Commands dealing with the RAID driver but not any
2840 * particular array:
2841 */
2842 switch (cmd)
2843 {
2844 case RAID_VERSION:
2845 err = get_version(argp);
2846 goto done;
2847
2848 case PRINT_RAID_DEBUG:
2849 err = 0;
2850 md_print_devices();
2851 goto done;
2852
2853#ifndef MODULE
2854 case RAID_AUTORUN:
2855 err = 0;
2856 autostart_arrays(arg);
2857 goto done;
2858#endif
2859 default:;
2860 }
2861
2862 /*
2863 * Commands creating/starting a new array:
2864 */
2865
2866 mddev = inode->i_bdev->bd_disk->private_data;
2867
2868 if (!mddev) {
2869 BUG();
2870 goto abort;
2871 }
2872
2873
2874 if (cmd == START_ARRAY) {
2875 /* START_ARRAY doesn't need to lock the array as autostart_array
2876 * does the locking, and it could even be a different array
2877 */
2878 static int cnt = 3;
2879 if (cnt > 0 ) {
2880 printk(KERN_WARNING
2881 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
2882 "This will not be supported beyond 2.6\n",
2883 current->comm, current->pid);
2884 cnt--;
2885 }
2886 err = autostart_array(new_decode_dev(arg));
2887 if (err) {
2888 printk(KERN_WARNING "md: autostart failed!\n");
2889 goto abort;
2890 }
2891 goto done;
2892 }
2893
2894 err = mddev_lock(mddev);
2895 if (err) {
2896 printk(KERN_INFO
2897 "md: ioctl lock interrupted, reason %d, cmd %d\n",
2898 err, cmd);
2899 goto abort;
2900 }
2901
2902 switch (cmd)
2903 {
2904 case SET_ARRAY_INFO:
2905 {
2906 mdu_array_info_t info;
2907 if (!arg)
2908 memset(&info, 0, sizeof(info));
2909 else if (copy_from_user(&info, argp, sizeof(info))) {
2910 err = -EFAULT;
2911 goto abort_unlock;
2912 }
2913 if (mddev->pers) {
2914 err = update_array_info(mddev, &info);
2915 if (err) {
2916 printk(KERN_WARNING "md: couldn't update"
2917 " array info. %d\n", err);
2918 goto abort_unlock;
2919 }
2920 goto done_unlock;
2921 }
2922 if (!list_empty(&mddev->disks)) {
2923 printk(KERN_WARNING
2924 "md: array %s already has disks!\n",
2925 mdname(mddev));
2926 err = -EBUSY;
2927 goto abort_unlock;
2928 }
2929 if (mddev->raid_disks) {
2930 printk(KERN_WARNING
2931 "md: array %s already initialised!\n",
2932 mdname(mddev));
2933 err = -EBUSY;
2934 goto abort_unlock;
2935 }
2936 err = set_array_info(mddev, &info);
2937 if (err) {
2938 printk(KERN_WARNING "md: couldn't set"
2939 " array info. %d\n", err);
2940 goto abort_unlock;
2941 }
2942 }
2943 goto done_unlock;
2944
2945 default:;
2946 }
2947
2948 /*
2949 * Commands querying/configuring an existing array:
2950 */
NeilBrown32a76272005-06-21 17:17:14 -07002951 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
2952 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
2953 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
2954 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 err = -ENODEV;
2956 goto abort_unlock;
2957 }
2958
2959 /*
2960 * Commands even a read-only array can execute:
2961 */
2962 switch (cmd)
2963 {
2964 case GET_ARRAY_INFO:
2965 err = get_array_info(mddev, argp);
2966 goto done_unlock;
2967
NeilBrown32a76272005-06-21 17:17:14 -07002968 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002969 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07002970 goto done_unlock;
2971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 case GET_DISK_INFO:
2973 err = get_disk_info(mddev, argp);
2974 goto done_unlock;
2975
2976 case RESTART_ARRAY_RW:
2977 err = restart_array(mddev);
2978 goto done_unlock;
2979
2980 case STOP_ARRAY:
2981 err = do_md_stop (mddev, 0);
2982 goto done_unlock;
2983
2984 case STOP_ARRAY_RO:
2985 err = do_md_stop (mddev, 1);
2986 goto done_unlock;
2987
2988 /*
2989 * We have a problem here : there is no easy way to give a CHS
2990 * virtual geometry. We currently pretend that we have a 2 heads
2991 * 4 sectors (with a BIG number of cylinders...). This drives
2992 * dosfs just mad... ;-)
2993 */
2994 case HDIO_GETGEO:
2995 if (!loc) {
2996 err = -EINVAL;
2997 goto abort_unlock;
2998 }
2999 err = put_user (2, (char __user *) &loc->heads);
3000 if (err)
3001 goto abort_unlock;
3002 err = put_user (4, (char __user *) &loc->sectors);
3003 if (err)
3004 goto abort_unlock;
3005 err = put_user(get_capacity(mddev->gendisk)/8,
3006 (short __user *) &loc->cylinders);
3007 if (err)
3008 goto abort_unlock;
3009 err = put_user (get_start_sect(inode->i_bdev),
3010 (long __user *) &loc->start);
3011 goto done_unlock;
3012 }
3013
3014 /*
3015 * The remaining ioctls are changing the state of the
3016 * superblock, so we do not allow read-only arrays
3017 * here:
3018 */
3019 if (mddev->ro) {
3020 err = -EROFS;
3021 goto abort_unlock;
3022 }
3023
3024 switch (cmd)
3025 {
3026 case ADD_NEW_DISK:
3027 {
3028 mdu_disk_info_t info;
3029 if (copy_from_user(&info, argp, sizeof(info)))
3030 err = -EFAULT;
3031 else
3032 err = add_new_disk(mddev, &info);
3033 goto done_unlock;
3034 }
3035
3036 case HOT_REMOVE_DISK:
3037 err = hot_remove_disk(mddev, new_decode_dev(arg));
3038 goto done_unlock;
3039
3040 case HOT_ADD_DISK:
3041 err = hot_add_disk(mddev, new_decode_dev(arg));
3042 goto done_unlock;
3043
3044 case SET_DISK_FAULTY:
3045 err = set_disk_faulty(mddev, new_decode_dev(arg));
3046 goto done_unlock;
3047
3048 case RUN_ARRAY:
3049 err = do_md_run (mddev);
3050 goto done_unlock;
3051
NeilBrown32a76272005-06-21 17:17:14 -07003052 case SET_BITMAP_FILE:
3053 err = set_bitmap_file(mddev, (int)arg);
3054 goto done_unlock;
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 default:
3057 if (_IOC_TYPE(cmd) == MD_MAJOR)
3058 printk(KERN_WARNING "md: %s(pid %d) used"
3059 " obsolete MD ioctl, upgrade your"
3060 " software to use new ictls.\n",
3061 current->comm, current->pid);
3062 err = -EINVAL;
3063 goto abort_unlock;
3064 }
3065
3066done_unlock:
3067abort_unlock:
3068 mddev_unlock(mddev);
3069
3070 return err;
3071done:
3072 if (err)
3073 MD_BUG();
3074abort:
3075 return err;
3076}
3077
3078static int md_open(struct inode *inode, struct file *file)
3079{
3080 /*
3081 * Succeed if we can lock the mddev, which confirms that
3082 * it isn't being stopped right now.
3083 */
3084 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3085 int err;
3086
3087 if ((err = mddev_lock(mddev)))
3088 goto out;
3089
3090 err = 0;
3091 mddev_get(mddev);
3092 mddev_unlock(mddev);
3093
3094 check_disk_change(inode->i_bdev);
3095 out:
3096 return err;
3097}
3098
3099static int md_release(struct inode *inode, struct file * file)
3100{
3101 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3102
3103 if (!mddev)
3104 BUG();
3105 mddev_put(mddev);
3106
3107 return 0;
3108}
3109
3110static int md_media_changed(struct gendisk *disk)
3111{
3112 mddev_t *mddev = disk->private_data;
3113
3114 return mddev->changed;
3115}
3116
3117static int md_revalidate(struct gendisk *disk)
3118{
3119 mddev_t *mddev = disk->private_data;
3120
3121 mddev->changed = 0;
3122 return 0;
3123}
3124static struct block_device_operations md_fops =
3125{
3126 .owner = THIS_MODULE,
3127 .open = md_open,
3128 .release = md_release,
3129 .ioctl = md_ioctl,
3130 .media_changed = md_media_changed,
3131 .revalidate_disk= md_revalidate,
3132};
3133
Adrian Bunk75c96f82005-05-05 16:16:09 -07003134static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135{
3136 mdk_thread_t *thread = arg;
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 /*
3139 * md_thread is a 'system-thread', it's priority should be very
3140 * high. We avoid resource deadlocks individually in each
3141 * raid personality. (RAID5 does preallocation) We also use RR and
3142 * the very same RT priority as kswapd, thus we will never get
3143 * into a priority inversion deadlock.
3144 *
3145 * we definitely have to have equal or higher priority than
3146 * bdflush, otherwise bdflush will deadlock if there are too
3147 * many dirty RAID5 blocks.
3148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
NeilBrown6985c432005-10-19 21:23:47 -07003150 allow_signal(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 complete(thread->event);
NeilBrowna6fb0932005-09-09 16:23:56 -07003152 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 void (*run)(mddev_t *);
3154
NeilBrown32a76272005-06-21 17:17:14 -07003155 wait_event_interruptible_timeout(thread->wqueue,
NeilBrowna6fb0932005-09-09 16:23:56 -07003156 test_bit(THREAD_WAKEUP, &thread->flags)
3157 || kthread_should_stop(),
NeilBrown32a76272005-06-21 17:17:14 -07003158 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003159 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 clear_bit(THREAD_WAKEUP, &thread->flags);
3162
3163 run = thread->run;
3164 if (run)
3165 run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 }
NeilBrowna6fb0932005-09-09 16:23:56 -07003167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 return 0;
3169}
3170
3171void md_wakeup_thread(mdk_thread_t *thread)
3172{
3173 if (thread) {
3174 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3175 set_bit(THREAD_WAKEUP, &thread->flags);
3176 wake_up(&thread->wqueue);
3177 }
3178}
3179
3180mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3181 const char *name)
3182{
3183 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 struct completion event;
3185
NeilBrowna6fb0932005-09-09 16:23:56 -07003186 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 if (!thread)
3188 return NULL;
3189
3190 memset(thread, 0, sizeof(mdk_thread_t));
3191 init_waitqueue_head(&thread->wqueue);
3192
3193 init_completion(&event);
3194 thread->event = &event;
3195 thread->run = run;
3196 thread->mddev = mddev;
3197 thread->name = name;
NeilBrown32a76272005-06-21 17:17:14 -07003198 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07003199 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07003200 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 kfree(thread);
3202 return NULL;
3203 }
3204 wait_for_completion(&event);
3205 return thread;
3206}
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208void md_unregister_thread(mdk_thread_t *thread)
3209{
NeilBrownd28446f2005-04-16 15:26:41 -07003210 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07003211
3212 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 kfree(thread);
3214}
3215
3216void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3217{
3218 if (!mddev) {
3219 MD_BUG();
3220 return;
3221 }
3222
3223 if (!rdev || rdev->faulty)
3224 return;
NeilBrown32a76272005-06-21 17:17:14 -07003225/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3227 mdname(mddev),
3228 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3229 __builtin_return_address(0),__builtin_return_address(1),
3230 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07003231*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (!mddev->pers->error_handler)
3233 return;
3234 mddev->pers->error_handler(mddev,rdev);
3235 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3236 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3237 md_wakeup_thread(mddev->thread);
3238}
3239
3240/* seq_file implementation /proc/mdstat */
3241
3242static void status_unused(struct seq_file *seq)
3243{
3244 int i = 0;
3245 mdk_rdev_t *rdev;
3246 struct list_head *tmp;
3247
3248 seq_printf(seq, "unused devices: ");
3249
3250 ITERATE_RDEV_PENDING(rdev,tmp) {
3251 char b[BDEVNAME_SIZE];
3252 i++;
3253 seq_printf(seq, "%s ",
3254 bdevname(rdev->bdev,b));
3255 }
3256 if (!i)
3257 seq_printf(seq, "<none>");
3258
3259 seq_printf(seq, "\n");
3260}
3261
3262
3263static void status_resync(struct seq_file *seq, mddev_t * mddev)
3264{
3265 unsigned long max_blocks, resync, res, dt, db, rt;
3266
3267 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3268
3269 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3270 max_blocks = mddev->resync_max_sectors >> 1;
3271 else
3272 max_blocks = mddev->size;
3273
3274 /*
3275 * Should not happen.
3276 */
3277 if (!max_blocks) {
3278 MD_BUG();
3279 return;
3280 }
3281 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3282 {
3283 int i, x = res/50, y = 20-x;
3284 seq_printf(seq, "[");
3285 for (i = 0; i < x; i++)
3286 seq_printf(seq, "=");
3287 seq_printf(seq, ">");
3288 for (i = 0; i < y; i++)
3289 seq_printf(seq, ".");
3290 seq_printf(seq, "] ");
3291 }
3292 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3293 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3294 "resync" : "recovery"),
3295 res/10, res % 10, resync, max_blocks);
3296
3297 /*
3298 * We do not want to overflow, so the order of operands and
3299 * the * 100 / 100 trick are important. We do a +1 to be
3300 * safe against division by zero. We only estimate anyway.
3301 *
3302 * dt: time from mark until now
3303 * db: blocks written from mark until now
3304 * rt: remaining time
3305 */
3306 dt = ((jiffies - mddev->resync_mark) / HZ);
3307 if (!dt) dt++;
3308 db = resync - (mddev->resync_mark_cnt/2);
3309 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3310
3311 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3312
3313 seq_printf(seq, " speed=%ldK/sec", db/dt);
3314}
3315
3316static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3317{
3318 struct list_head *tmp;
3319 loff_t l = *pos;
3320 mddev_t *mddev;
3321
3322 if (l >= 0x10000)
3323 return NULL;
3324 if (!l--)
3325 /* header */
3326 return (void*)1;
3327
3328 spin_lock(&all_mddevs_lock);
3329 list_for_each(tmp,&all_mddevs)
3330 if (!l--) {
3331 mddev = list_entry(tmp, mddev_t, all_mddevs);
3332 mddev_get(mddev);
3333 spin_unlock(&all_mddevs_lock);
3334 return mddev;
3335 }
3336 spin_unlock(&all_mddevs_lock);
3337 if (!l--)
3338 return (void*)2;/* tail */
3339 return NULL;
3340}
3341
3342static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3343{
3344 struct list_head *tmp;
3345 mddev_t *next_mddev, *mddev = v;
3346
3347 ++*pos;
3348 if (v == (void*)2)
3349 return NULL;
3350
3351 spin_lock(&all_mddevs_lock);
3352 if (v == (void*)1)
3353 tmp = all_mddevs.next;
3354 else
3355 tmp = mddev->all_mddevs.next;
3356 if (tmp != &all_mddevs)
3357 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3358 else {
3359 next_mddev = (void*)2;
3360 *pos = 0x10000;
3361 }
3362 spin_unlock(&all_mddevs_lock);
3363
3364 if (v != (void*)1)
3365 mddev_put(mddev);
3366 return next_mddev;
3367
3368}
3369
3370static void md_seq_stop(struct seq_file *seq, void *v)
3371{
3372 mddev_t *mddev = v;
3373
3374 if (mddev && v != (void*)1 && v != (void*)2)
3375 mddev_put(mddev);
3376}
3377
3378static int md_seq_show(struct seq_file *seq, void *v)
3379{
3380 mddev_t *mddev = v;
3381 sector_t size;
3382 struct list_head *tmp2;
3383 mdk_rdev_t *rdev;
3384 int i;
NeilBrown32a76272005-06-21 17:17:14 -07003385 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
3387 if (v == (void*)1) {
3388 seq_printf(seq, "Personalities : ");
3389 spin_lock(&pers_lock);
3390 for (i = 0; i < MAX_PERSONALITY; i++)
3391 if (pers[i])
3392 seq_printf(seq, "[%s] ", pers[i]->name);
3393
3394 spin_unlock(&pers_lock);
3395 seq_printf(seq, "\n");
3396 return 0;
3397 }
3398 if (v == (void*)2) {
3399 status_unused(seq);
3400 return 0;
3401 }
3402
3403 if (mddev_lock(mddev)!=0)
3404 return -EINTR;
3405 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3406 seq_printf(seq, "%s : %sactive", mdname(mddev),
3407 mddev->pers ? "" : "in");
3408 if (mddev->pers) {
3409 if (mddev->ro)
3410 seq_printf(seq, " (read-only)");
3411 seq_printf(seq, " %s", mddev->pers->name);
3412 }
3413
3414 size = 0;
3415 ITERATE_RDEV(mddev,rdev,tmp2) {
3416 char b[BDEVNAME_SIZE];
3417 seq_printf(seq, " %s[%d]",
3418 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003419 if (test_bit(WriteMostly, &rdev->flags))
3420 seq_printf(seq, "(W)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 if (rdev->faulty) {
3422 seq_printf(seq, "(F)");
3423 continue;
NeilBrownb325a322005-09-09 16:24:00 -07003424 } else if (rdev->raid_disk < 0)
3425 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 size += rdev->size;
3427 }
3428
3429 if (!list_empty(&mddev->disks)) {
3430 if (mddev->pers)
3431 seq_printf(seq, "\n %llu blocks",
3432 (unsigned long long)mddev->array_size);
3433 else
3434 seq_printf(seq, "\n %llu blocks",
3435 (unsigned long long)size);
3436 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07003437 if (mddev->persistent) {
3438 if (mddev->major_version != 0 ||
3439 mddev->minor_version != 90) {
3440 seq_printf(seq," super %d.%d",
3441 mddev->major_version,
3442 mddev->minor_version);
3443 }
3444 } else
3445 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
3447 if (mddev->pers) {
3448 mddev->pers->status (seq, mddev);
3449 seq_printf(seq, "\n ");
NeilBrown32a76272005-06-21 17:17:14 -07003450 if (mddev->curr_resync > 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 status_resync (seq, mddev);
NeilBrown32a76272005-06-21 17:17:14 -07003452 seq_printf(seq, "\n ");
3453 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3454 seq_printf(seq, " resync=DELAYED\n ");
3455 } else
3456 seq_printf(seq, "\n ");
3457
3458 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07003459 unsigned long chunk_kb;
3460 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07003461 spin_lock_irqsave(&bitmap->lock, flags);
3462 chunk_kb = bitmap->chunksize >> 10;
3463 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3464 "%lu%s chunk",
3465 bitmap->pages - bitmap->missing_pages,
3466 bitmap->pages,
3467 (bitmap->pages - bitmap->missing_pages)
3468 << (PAGE_SHIFT - 10),
3469 chunk_kb ? chunk_kb : bitmap->chunksize,
3470 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07003471 if (bitmap->file) {
3472 seq_printf(seq, ", file: ");
3473 seq_path(seq, bitmap->file->f_vfsmnt,
3474 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07003475 }
NeilBrown78d742d2005-06-21 17:17:15 -07003476
NeilBrown32a76272005-06-21 17:17:14 -07003477 seq_printf(seq, "\n");
3478 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 }
3480
3481 seq_printf(seq, "\n");
3482 }
3483 mddev_unlock(mddev);
3484
3485 return 0;
3486}
3487
3488static struct seq_operations md_seq_ops = {
3489 .start = md_seq_start,
3490 .next = md_seq_next,
3491 .stop = md_seq_stop,
3492 .show = md_seq_show,
3493};
3494
3495static int md_seq_open(struct inode *inode, struct file *file)
3496{
3497 int error;
3498
3499 error = seq_open(file, &md_seq_ops);
3500 return error;
3501}
3502
3503static struct file_operations md_seq_fops = {
3504 .open = md_seq_open,
3505 .read = seq_read,
3506 .llseek = seq_lseek,
3507 .release = seq_release,
3508};
3509
3510int register_md_personality(int pnum, mdk_personality_t *p)
3511{
3512 if (pnum >= MAX_PERSONALITY) {
3513 printk(KERN_ERR
3514 "md: tried to install personality %s as nr %d, but max is %lu\n",
3515 p->name, pnum, MAX_PERSONALITY-1);
3516 return -EINVAL;
3517 }
3518
3519 spin_lock(&pers_lock);
3520 if (pers[pnum]) {
3521 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 return -EBUSY;
3523 }
3524
3525 pers[pnum] = p;
3526 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3527 spin_unlock(&pers_lock);
3528 return 0;
3529}
3530
3531int unregister_md_personality(int pnum)
3532{
NeilBrowna757e642005-04-16 15:26:42 -07003533 if (pnum >= MAX_PERSONALITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
3536 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3537 spin_lock(&pers_lock);
3538 pers[pnum] = NULL;
3539 spin_unlock(&pers_lock);
3540 return 0;
3541}
3542
3543static int is_mddev_idle(mddev_t *mddev)
3544{
3545 mdk_rdev_t * rdev;
3546 struct list_head *tmp;
3547 int idle;
3548 unsigned long curr_events;
3549
3550 idle = 1;
3551 ITERATE_RDEV(mddev,rdev,tmp) {
3552 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01003553 curr_events = disk_stat_read(disk, sectors[0]) +
3554 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 atomic_read(&disk->sync_io);
3556 /* Allow some slack between valud of curr_events and last_events,
3557 * as there are some uninteresting races.
3558 * Note: the following is an unsigned comparison.
3559 */
3560 if ((curr_events - rdev->last_events + 32) > 64) {
3561 rdev->last_events = curr_events;
3562 idle = 0;
3563 }
3564 }
3565 return idle;
3566}
3567
3568void md_done_sync(mddev_t *mddev, int blocks, int ok)
3569{
3570 /* another "blocks" (512byte) blocks have been synced */
3571 atomic_sub(blocks, &mddev->recovery_active);
3572 wake_up(&mddev->recovery_wait);
3573 if (!ok) {
3574 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3575 md_wakeup_thread(mddev->thread);
3576 // stop recovery, signal do_sync ....
3577 }
3578}
3579
3580
NeilBrown06d91a52005-06-21 17:17:12 -07003581/* md_write_start(mddev, bi)
3582 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07003583 * in superblock) before writing, schedule a superblock update
3584 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07003585 */
NeilBrown3d310eb2005-06-21 17:17:26 -07003586void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
NeilBrown06d91a52005-06-21 17:17:12 -07003588 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07003589 return;
NeilBrown06d91a52005-06-21 17:17:12 -07003590
3591 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07003592 if (mddev->in_sync) {
NeilBrown3d310eb2005-06-21 17:17:26 -07003593 spin_lock(&mddev->write_lock);
3594 if (mddev->in_sync) {
3595 mddev->in_sync = 0;
3596 mddev->sb_dirty = 1;
3597 md_wakeup_thread(mddev->thread);
3598 }
3599 spin_unlock(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07003600 }
NeilBrown3d310eb2005-06-21 17:17:26 -07003601 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
3604void md_write_end(mddev_t *mddev)
3605{
3606 if (atomic_dec_and_test(&mddev->writes_pending)) {
3607 if (mddev->safemode == 2)
3608 md_wakeup_thread(mddev->thread);
3609 else
3610 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3611 }
3612}
3613
Adrian Bunk75c96f82005-05-05 16:16:09 -07003614static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
3616#define SYNC_MARKS 10
3617#define SYNC_MARK_STEP (3*HZ)
3618static void md_do_sync(mddev_t *mddev)
3619{
3620 mddev_t *mddev2;
3621 unsigned int currspeed = 0,
3622 window;
NeilBrown57afd892005-06-21 17:17:13 -07003623 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 unsigned long mark[SYNC_MARKS];
3625 sector_t mark_cnt[SYNC_MARKS];
3626 int last_mark,m;
3627 struct list_head *tmp;
3628 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07003629 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
3631 /* just incase thread restarts... */
3632 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3633 return;
3634
3635 /* we overload curr_resync somewhat here.
3636 * 0 == not engaged in resync at all
3637 * 2 == checking that there is no conflict with another sync
3638 * 1 == like 2, but have yielded to allow conflicting resync to
3639 * commense
3640 * other == active in resync - this many blocks
3641 *
3642 * Before starting a resync we must have set curr_resync to
3643 * 2, and then checked that every "conflicting" array has curr_resync
3644 * less than ours. When we find one that is the same or higher
3645 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3646 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3647 * This will mean we have to start checking from the beginning again.
3648 *
3649 */
3650
3651 do {
3652 mddev->curr_resync = 2;
3653
3654 try_again:
NeilBrown8712e552005-10-26 01:58:58 -07003655 if (signal_pending(current) ||
3656 kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 flush_signals(current);
NeilBrown6985c432005-10-19 21:23:47 -07003658 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 goto skip;
3660 }
3661 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 if (mddev2 == mddev)
3663 continue;
3664 if (mddev2->curr_resync &&
3665 match_mddev_units(mddev,mddev2)) {
3666 DEFINE_WAIT(wq);
3667 if (mddev < mddev2 && mddev->curr_resync == 2) {
3668 /* arbitrarily yield */
3669 mddev->curr_resync = 1;
3670 wake_up(&resync_wait);
3671 }
3672 if (mddev > mddev2 && mddev->curr_resync == 1)
3673 /* no need to wait here, we can wait the next
3674 * time 'round when curr_resync == 2
3675 */
3676 continue;
3677 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown8712e552005-10-26 01:58:58 -07003678 if (!signal_pending(current) &&
3679 !kthread_should_stop() &&
3680 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 printk(KERN_INFO "md: delaying resync of %s"
3682 " until %s has finished resync (they"
3683 " share one or more physical units)\n",
3684 mdname(mddev), mdname(mddev2));
3685 mddev_put(mddev2);
3686 schedule();
3687 finish_wait(&resync_wait, &wq);
3688 goto try_again;
3689 }
3690 finish_wait(&resync_wait, &wq);
3691 }
3692 }
3693 } while (mddev->curr_resync < 2);
3694
3695 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3696 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07003697 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 */
3699 max_sectors = mddev->resync_max_sectors;
3700 else
3701 /* recovery follows the physical size of devices */
3702 max_sectors = mddev->size << 1;
3703
3704 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
3705 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
3706 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
Adrian Bunk338cec32005-09-10 00:26:54 -07003707 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 "(but not more than %d KB/sec) for reconstruction.\n",
3709 sysctl_speed_limit_max);
3710
3711 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07003712 /* we don't use the checkpoint if there's a bitmap */
3713 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 j = mddev->recovery_cp;
3715 else
3716 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07003717 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 for (m = 0; m < SYNC_MARKS; m++) {
3719 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07003720 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 }
3722 last_mark = 0;
3723 mddev->resync_mark = mark[last_mark];
3724 mddev->resync_mark_cnt = mark_cnt[last_mark];
3725
3726 /*
3727 * Tune reconstruction:
3728 */
3729 window = 32*(PAGE_SIZE/512);
3730 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
3731 window/2,(unsigned long long) max_sectors/2);
3732
3733 atomic_set(&mddev->recovery_active, 0);
3734 init_waitqueue_head(&mddev->recovery_wait);
3735 last_check = 0;
3736
3737 if (j>2) {
3738 printk(KERN_INFO
3739 "md: resuming recovery of %s from checkpoint.\n",
3740 mdname(mddev));
3741 mddev->curr_resync = j;
3742 }
3743
3744 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07003745 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
NeilBrown57afd892005-06-21 17:17:13 -07003747 skipped = 0;
3748 sectors = mddev->pers->sync_request(mddev, j, &skipped,
3749 currspeed < sysctl_speed_limit_min);
3750 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3752 goto out;
3753 }
NeilBrown57afd892005-06-21 17:17:13 -07003754
3755 if (!skipped) { /* actual IO requested */
3756 io_sectors += sectors;
3757 atomic_add(sectors, &mddev->recovery_active);
3758 }
3759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 j += sectors;
3761 if (j>1) mddev->curr_resync = j;
3762
NeilBrown57afd892005-06-21 17:17:13 -07003763
3764 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 continue;
3766
NeilBrown57afd892005-06-21 17:17:13 -07003767 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
3769 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
3770 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
3771 break;
3772
3773 repeat:
3774 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
3775 /* step marks */
3776 int next = (last_mark+1) % SYNC_MARKS;
3777
3778 mddev->resync_mark = mark[next];
3779 mddev->resync_mark_cnt = mark_cnt[next];
3780 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07003781 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 last_mark = next;
3783 }
3784
3785
NeilBrown8712e552005-10-26 01:58:58 -07003786 if (signal_pending(current) || kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 /*
3788 * got a signal, exit.
3789 */
3790 printk(KERN_INFO
3791 "md: md_do_sync() got signal ... exiting\n");
3792 flush_signals(current);
3793 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3794 goto out;
3795 }
3796
3797 /*
3798 * this loop exits only if either when we are slower than
3799 * the 'hard' speed limit, or the system was IO-idle for
3800 * a jiffy.
3801 * the system might be non-idle CPU-wise, but we only care
3802 * about not overloading the IO subsystem. (things like an
3803 * e2fsck being done on the RAID array should execute fast)
3804 */
3805 mddev->queue->unplug_fn(mddev->queue);
3806 cond_resched();
3807
NeilBrown57afd892005-06-21 17:17:13 -07003808 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
3809 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
3811 if (currspeed > sysctl_speed_limit_min) {
3812 if ((currspeed > sysctl_speed_limit_max) ||
3813 !is_mddev_idle(mddev)) {
3814 msleep_interruptible(250);
3815 goto repeat;
3816 }
3817 }
3818 }
3819 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
3820 /*
3821 * this also signals 'finished resyncing' to md_stop
3822 */
3823 out:
3824 mddev->queue->unplug_fn(mddev->queue);
3825
3826 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
3827
3828 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07003829 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
3831 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
3832 mddev->curr_resync > 2 &&
3833 mddev->curr_resync >= mddev->recovery_cp) {
3834 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
3835 printk(KERN_INFO
3836 "md: checkpointing recovery of %s.\n",
3837 mdname(mddev));
3838 mddev->recovery_cp = mddev->curr_resync;
3839 } else
3840 mddev->recovery_cp = MaxSector;
3841 }
3842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 skip:
3844 mddev->curr_resync = 0;
3845 wake_up(&resync_wait);
3846 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
3847 md_wakeup_thread(mddev->thread);
3848}
3849
3850
3851/*
3852 * This routine is regularly called by all per-raid-array threads to
3853 * deal with generic issues like resync and super-block update.
3854 * Raid personalities that don't have a thread (linear/raid0) do not
3855 * need this as they never do any recovery or update the superblock.
3856 *
3857 * It does not do any resync itself, but rather "forks" off other threads
3858 * to do that as needed.
3859 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
3860 * "->recovery" and create a thread at ->sync_thread.
3861 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
3862 * and wakeups up this thread which will reap the thread and finish up.
3863 * This thread also removes any faulty devices (with nr_pending == 0).
3864 *
3865 * The overall approach is:
3866 * 1/ if the superblock needs updating, update it.
3867 * 2/ If a recovery thread is running, don't do anything else.
3868 * 3/ If recovery has finished, clean up, possibly marking spares active.
3869 * 4/ If there are any faulty devices, remove them.
3870 * 5/ If array is degraded, try to add spares devices
3871 * 6/ If array has spares or is not in-sync, start a resync thread.
3872 */
3873void md_check_recovery(mddev_t *mddev)
3874{
3875 mdk_rdev_t *rdev;
3876 struct list_head *rtmp;
3877
3878
NeilBrown5f404022005-06-21 17:17:16 -07003879 if (mddev->bitmap)
3880 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
3882 if (mddev->ro)
3883 return;
NeilBrownfca4d842005-06-21 17:17:11 -07003884
3885 if (signal_pending(current)) {
3886 if (mddev->pers->sync_request) {
3887 printk(KERN_INFO "md: %s in immediate safe mode\n",
3888 mdname(mddev));
3889 mddev->safemode = 2;
3890 }
3891 flush_signals(current);
3892 }
3893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 if ( ! (
3895 mddev->sb_dirty ||
3896 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07003897 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
3898 (mddev->safemode == 1) ||
3899 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
3900 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 ))
3902 return;
NeilBrownfca4d842005-06-21 17:17:11 -07003903
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 if (mddev_trylock(mddev)==0) {
3905 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07003906
NeilBrown06d91a52005-06-21 17:17:12 -07003907 spin_lock(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07003908 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
3909 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
3910 mddev->in_sync = 1;
3911 mddev->sb_dirty = 1;
3912 }
3913 if (mddev->safemode == 1)
3914 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07003915 spin_unlock(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07003916
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 if (mddev->sb_dirty)
3918 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07003919
NeilBrown06d91a52005-06-21 17:17:12 -07003920
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
3922 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
3923 /* resync/recovery still happening */
3924 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3925 goto unlock;
3926 }
3927 if (mddev->sync_thread) {
3928 /* resync has finished, collect result */
3929 md_unregister_thread(mddev->sync_thread);
3930 mddev->sync_thread = NULL;
3931 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
3932 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
3933 /* success...*/
3934 /* activate any spares */
3935 mddev->pers->spare_active(mddev);
3936 }
3937 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07003938
3939 /* if array is no-longer degraded, then any saved_raid_disk
3940 * information must be scrapped
3941 */
3942 if (!mddev->degraded)
3943 ITERATE_RDEV(mddev,rdev,rtmp)
3944 rdev->saved_raid_disk = -1;
3945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 mddev->recovery = 0;
3947 /* flag recovery needed just to double check */
3948 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3949 goto unlock;
3950 }
3951 if (mddev->recovery)
3952 /* probably just the RECOVERY_NEEDED flag */
3953 mddev->recovery = 0;
3954
3955 /* no recovery is running.
3956 * remove any failed drives, then
3957 * add spares if possible.
3958 * Spare are also removed and re-added, to allow
3959 * the personality to fail the re-add.
3960 */
3961 ITERATE_RDEV(mddev,rdev,rtmp)
3962 if (rdev->raid_disk >= 0 &&
3963 (rdev->faulty || ! rdev->in_sync) &&
3964 atomic_read(&rdev->nr_pending)==0) {
3965 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0)
3966 rdev->raid_disk = -1;
3967 }
3968
3969 if (mddev->degraded) {
3970 ITERATE_RDEV(mddev,rdev,rtmp)
3971 if (rdev->raid_disk < 0
3972 && !rdev->faulty) {
3973 if (mddev->pers->hot_add_disk(mddev,rdev))
3974 spares++;
3975 else
3976 break;
3977 }
3978 }
3979
3980 if (!spares && (mddev->recovery_cp == MaxSector )) {
3981 /* nothing we can do ... */
3982 goto unlock;
3983 }
3984 if (mddev->pers->sync_request) {
3985 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3986 if (!spares)
3987 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07003988 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
3989 /* We are adding a device or devices to an array
3990 * which has the bitmap stored on all devices.
3991 * So make sure all bitmap pages get written
3992 */
3993 bitmap_write_all(mddev->bitmap);
3994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 mddev->sync_thread = md_register_thread(md_do_sync,
3996 mddev,
3997 "%s_resync");
3998 if (!mddev->sync_thread) {
3999 printk(KERN_ERR "%s: could not start resync"
4000 " thread...\n",
4001 mdname(mddev));
4002 /* leave the spares where they are, it shouldn't hurt */
4003 mddev->recovery = 0;
4004 } else {
4005 md_wakeup_thread(mddev->sync_thread);
4006 }
4007 }
4008 unlock:
4009 mddev_unlock(mddev);
4010 }
4011}
4012
Adrian Bunk75c96f82005-05-05 16:16:09 -07004013static int md_notify_reboot(struct notifier_block *this,
4014 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015{
4016 struct list_head *tmp;
4017 mddev_t *mddev;
4018
4019 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4020
4021 printk(KERN_INFO "md: stopping all md devices.\n");
4022
4023 ITERATE_MDDEV(mddev,tmp)
4024 if (mddev_trylock(mddev)==0)
4025 do_md_stop (mddev, 1);
4026 /*
4027 * certain more exotic SCSI devices are known to be
4028 * volatile wrt too early system reboots. While the
4029 * right place to handle this issue is the given
4030 * driver, we do want to have a safe RAID driver ...
4031 */
4032 mdelay(1000*1);
4033 }
4034 return NOTIFY_DONE;
4035}
4036
Adrian Bunk75c96f82005-05-05 16:16:09 -07004037static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 .notifier_call = md_notify_reboot,
4039 .next = NULL,
4040 .priority = INT_MAX, /* before any real devices */
4041};
4042
4043static void md_geninit(void)
4044{
4045 struct proc_dir_entry *p;
4046
4047 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4048
4049 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4050 if (p)
4051 p->proc_fops = &md_seq_fops;
4052}
4053
Adrian Bunk75c96f82005-05-05 16:16:09 -07004054static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
4056 int minor;
4057
4058 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4059 " MD_SB_DISKS=%d\n",
4060 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4061 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrown32a76272005-06-21 17:17:14 -07004062 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR,
4063 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
4065 if (register_blkdev(MAJOR_NR, "md"))
4066 return -1;
4067 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4068 unregister_blkdev(MAJOR_NR, "md");
4069 return -1;
4070 }
4071 devfs_mk_dir("md");
4072 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4073 md_probe, NULL, NULL);
4074 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4075 md_probe, NULL, NULL);
4076
4077 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4078 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4079 S_IFBLK|S_IRUSR|S_IWUSR,
4080 "md/%d", minor);
4081
4082 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4083 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4084 S_IFBLK|S_IRUSR|S_IWUSR,
4085 "md/mdp%d", minor);
4086
4087
4088 register_reboot_notifier(&md_notifier);
4089 raid_table_header = register_sysctl_table(raid_root_table, 1);
4090
4091 md_geninit();
4092 return (0);
4093}
4094
4095
4096#ifndef MODULE
4097
4098/*
4099 * Searches all registered partitions for autorun RAID arrays
4100 * at boot time.
4101 */
4102static dev_t detected_devices[128];
4103static int dev_cnt;
4104
4105void md_autodetect_dev(dev_t dev)
4106{
4107 if (dev_cnt >= 0 && dev_cnt < 127)
4108 detected_devices[dev_cnt++] = dev;
4109}
4110
4111
4112static void autostart_arrays(int part)
4113{
4114 mdk_rdev_t *rdev;
4115 int i;
4116
4117 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4118
4119 for (i = 0; i < dev_cnt; i++) {
4120 dev_t dev = detected_devices[i];
4121
4122 rdev = md_import_device(dev,0, 0);
4123 if (IS_ERR(rdev))
4124 continue;
4125
4126 if (rdev->faulty) {
4127 MD_BUG();
4128 continue;
4129 }
4130 list_add(&rdev->same_set, &pending_raid_disks);
4131 }
4132 dev_cnt = 0;
4133
4134 autorun_devices(part);
4135}
4136
4137#endif
4138
4139static __exit void md_exit(void)
4140{
4141 mddev_t *mddev;
4142 struct list_head *tmp;
4143 int i;
4144 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4145 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4146 for (i=0; i < MAX_MD_DEVS; i++)
4147 devfs_remove("md/%d", i);
4148 for (i=0; i < MAX_MD_DEVS; i++)
4149 devfs_remove("md/d%d", i);
4150
4151 devfs_remove("md");
4152
4153 unregister_blkdev(MAJOR_NR,"md");
4154 unregister_blkdev(mdp_major, "mdp");
4155 unregister_reboot_notifier(&md_notifier);
4156 unregister_sysctl_table(raid_table_header);
4157 remove_proc_entry("mdstat", NULL);
4158 ITERATE_MDDEV(mddev,tmp) {
4159 struct gendisk *disk = mddev->gendisk;
4160 if (!disk)
4161 continue;
4162 export_array(mddev);
4163 del_gendisk(disk);
4164 put_disk(disk);
4165 mddev->gendisk = NULL;
4166 mddev_put(mddev);
4167 }
4168}
4169
4170module_init(md_init)
4171module_exit(md_exit)
4172
4173EXPORT_SYMBOL(register_md_personality);
4174EXPORT_SYMBOL(unregister_md_personality);
4175EXPORT_SYMBOL(md_error);
4176EXPORT_SYMBOL(md_done_sync);
4177EXPORT_SYMBOL(md_write_start);
4178EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179EXPORT_SYMBOL(md_register_thread);
4180EXPORT_SYMBOL(md_unregister_thread);
4181EXPORT_SYMBOL(md_wakeup_thread);
4182EXPORT_SYMBOL(md_print_devices);
4183EXPORT_SYMBOL(md_check_recovery);
4184MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07004185MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07004186MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);