blob: d568ab4a487f55bcb87f58b22bc9d8f7c2a53811 [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>
NeilBrownd7603b72006-01-06 00:20:30 -080045#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <linux/init.h>
48
NeilBrown32a76272005-06-21 17:17:14 -070049#include <linux/file.h>
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifdef CONFIG_KMOD
52#include <linux/kmod.h>
53#endif
54
55#include <asm/unaligned.h>
56
57#define MAJOR_NR MD_MAJOR
58#define MD_DRIVER
59
60/* 63 partitions with the alternate major number (mdp) */
61#define MdpMinorShift 6
62
63#define DEBUG 0
64#define dprintk(x...) ((void)(DEBUG && printk(x)))
65
66
67#ifndef MODULE
68static void autostart_arrays (int part);
69#endif
70
NeilBrown2604b702006-01-06 00:20:36 -080071static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static DEFINE_SPINLOCK(pers_lock);
73
74/*
75 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
76 * is 1000 KB/sec, so the extra system load does not show up that much.
77 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070078 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * subsystem is idle. There is also an 'absolute maximum' reconstruction
80 * speed limit - in case reconstruction slows down your system despite
81 * idle IO detection.
82 *
83 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
84 */
85
86static int sysctl_speed_limit_min = 1000;
87static int sysctl_speed_limit_max = 200000;
88
89static struct ctl_table_header *raid_table_header;
90
91static ctl_table raid_table[] = {
92 {
93 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
94 .procname = "speed_limit_min",
95 .data = &sysctl_speed_limit_min,
96 .maxlen = sizeof(int),
97 .mode = 0644,
98 .proc_handler = &proc_dointvec,
99 },
100 {
101 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
102 .procname = "speed_limit_max",
103 .data = &sysctl_speed_limit_max,
104 .maxlen = sizeof(int),
105 .mode = 0644,
106 .proc_handler = &proc_dointvec,
107 },
108 { .ctl_name = 0 }
109};
110
111static ctl_table raid_dir_table[] = {
112 {
113 .ctl_name = DEV_RAID,
114 .procname = "raid",
115 .maxlen = 0,
116 .mode = 0555,
117 .child = raid_table,
118 },
119 { .ctl_name = 0 }
120};
121
122static ctl_table raid_root_table[] = {
123 {
124 .ctl_name = CTL_DEV,
125 .procname = "dev",
126 .maxlen = 0,
127 .mode = 0555,
128 .child = raid_dir_table,
129 },
130 { .ctl_name = 0 }
131};
132
133static struct block_device_operations md_fops;
134
NeilBrownf91de922005-11-08 21:39:36 -0800135static int start_readonly;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
NeilBrownd7603b72006-01-06 00:20:30 -0800138 * We have a system wide 'event count' that is incremented
139 * on any 'interesting' event, and readers of /proc/mdstat
140 * can use 'poll' or 'select' to find out when the event
141 * count increases.
142 *
143 * Events are:
144 * start array, stop array, error, add device, remove device,
145 * start build, activate spare
146 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800147static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800148static atomic_t md_event_count;
Adrian Bunk07dbd372006-01-06 00:20:44 -0800149static void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800150{
151 atomic_inc(&md_event_count);
152 wake_up(&md_event_waiters);
153}
154
155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * Enables to iterate over all existing md arrays
157 * all_mddevs_lock protects this list.
158 */
159static LIST_HEAD(all_mddevs);
160static DEFINE_SPINLOCK(all_mddevs_lock);
161
162
163/*
164 * iterates through all used mddevs in the system.
165 * We take care to grab the all_mddevs_lock whenever navigating
166 * the list, and to always hold a refcount when unlocked.
167 * Any code which breaks out of this loop while own
168 * a reference to the current mddev and must mddev_put it.
169 */
170#define ITERATE_MDDEV(mddev,tmp) \
171 \
172 for (({ spin_lock(&all_mddevs_lock); \
173 tmp = all_mddevs.next; \
174 mddev = NULL;}); \
175 ({ if (tmp != &all_mddevs) \
176 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
177 spin_unlock(&all_mddevs_lock); \
178 if (mddev) mddev_put(mddev); \
179 mddev = list_entry(tmp, mddev_t, all_mddevs); \
180 tmp != &all_mddevs;}); \
181 ({ spin_lock(&all_mddevs_lock); \
182 tmp = tmp->next;}) \
183 )
184
185
186static int md_fail_request (request_queue_t *q, struct bio *bio)
187{
188 bio_io_error(bio, bio->bi_size);
189 return 0;
190}
191
192static inline mddev_t *mddev_get(mddev_t *mddev)
193{
194 atomic_inc(&mddev->active);
195 return mddev;
196}
197
198static void mddev_put(mddev_t *mddev)
199{
200 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
201 return;
202 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
203 list_del(&mddev->all_mddevs);
204 blk_put_queue(mddev->queue);
NeilBrowneae17012005-11-08 21:39:23 -0800205 kobject_unregister(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 spin_unlock(&all_mddevs_lock);
208}
209
210static mddev_t * mddev_find(dev_t unit)
211{
212 mddev_t *mddev, *new = NULL;
213
214 retry:
215 spin_lock(&all_mddevs_lock);
216 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
217 if (mddev->unit == unit) {
218 mddev_get(mddev);
219 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700220 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return mddev;
222 }
223
224 if (new) {
225 list_add(&new->all_mddevs, &all_mddevs);
226 spin_unlock(&all_mddevs_lock);
227 return new;
228 }
229 spin_unlock(&all_mddevs_lock);
230
NeilBrown9ffae0c2006-01-06 00:20:32 -0800231 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!new)
233 return NULL;
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 new->unit = unit;
236 if (MAJOR(unit) == MD_MAJOR)
237 new->md_minor = MINOR(unit);
238 else
239 new->md_minor = MINOR(unit) >> MdpMinorShift;
240
241 init_MUTEX(&new->reconfig_sem);
242 INIT_LIST_HEAD(&new->disks);
243 INIT_LIST_HEAD(&new->all_mddevs);
244 init_timer(&new->safemode_timer);
245 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700246 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700247 init_waitqueue_head(&new->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 new->queue = blk_alloc_queue(GFP_KERNEL);
250 if (!new->queue) {
251 kfree(new);
252 return NULL;
253 }
254
255 blk_queue_make_request(new->queue, md_fail_request);
256
257 goto retry;
258}
259
260static inline int mddev_lock(mddev_t * mddev)
261{
262 return down_interruptible(&mddev->reconfig_sem);
263}
264
265static inline void mddev_lock_uninterruptible(mddev_t * mddev)
266{
267 down(&mddev->reconfig_sem);
268}
269
270static inline int mddev_trylock(mddev_t * mddev)
271{
272 return down_trylock(&mddev->reconfig_sem);
273}
274
275static inline void mddev_unlock(mddev_t * mddev)
276{
277 up(&mddev->reconfig_sem);
278
NeilBrown005eca52005-08-22 13:11:08 -0700279 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
NeilBrown2989ddb2006-01-06 00:20:43 -0800282static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 mdk_rdev_t * rdev;
285 struct list_head *tmp;
286
287 ITERATE_RDEV(mddev,rdev,tmp) {
288 if (rdev->desc_nr == nr)
289 return rdev;
290 }
291 return NULL;
292}
293
294static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
295{
296 struct list_head *tmp;
297 mdk_rdev_t *rdev;
298
299 ITERATE_RDEV(mddev,rdev,tmp) {
300 if (rdev->bdev->bd_dev == dev)
301 return rdev;
302 }
303 return NULL;
304}
305
NeilBrown2604b702006-01-06 00:20:36 -0800306static struct mdk_personality *find_pers(int level)
307{
308 struct mdk_personality *pers;
309 list_for_each_entry(pers, &pers_list, list)
310 if (pers->level == level)
311 return pers;
312 return NULL;
313}
314
Jesper Juhl77933d72005-07-27 11:46:09 -0700315static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
318 return MD_NEW_SIZE_BLOCKS(size);
319}
320
321static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
322{
323 sector_t size;
324
325 size = rdev->sb_offset;
326
327 if (chunk_size)
328 size &= ~((sector_t)chunk_size/1024 - 1);
329 return size;
330}
331
332static int alloc_disk_sb(mdk_rdev_t * rdev)
333{
334 if (rdev->sb_page)
335 MD_BUG();
336
337 rdev->sb_page = alloc_page(GFP_KERNEL);
338 if (!rdev->sb_page) {
339 printk(KERN_ALERT "md: out of memory.\n");
340 return -EINVAL;
341 }
342
343 return 0;
344}
345
346static void free_disk_sb(mdk_rdev_t * rdev)
347{
348 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800349 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 rdev->sb_loaded = 0;
351 rdev->sb_page = NULL;
352 rdev->sb_offset = 0;
353 rdev->size = 0;
354 }
355}
356
357
NeilBrown7bfa19f2005-06-21 17:17:28 -0700358static int super_written(struct bio *bio, unsigned int bytes_done, int error)
359{
360 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800361 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700362 if (bio->bi_size)
363 return 1;
364
365 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
NeilBrowna9701a32005-11-08 21:39:34 -0800366 md_error(mddev, rdev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700367
NeilBrowna9701a32005-11-08 21:39:34 -0800368 if (atomic_dec_and_test(&mddev->pending_writes))
369 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700370 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700371 return 0;
372}
373
NeilBrowna9701a32005-11-08 21:39:34 -0800374static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
375{
376 struct bio *bio2 = bio->bi_private;
377 mdk_rdev_t *rdev = bio2->bi_private;
378 mddev_t *mddev = rdev->mddev;
379 if (bio->bi_size)
380 return 1;
381
382 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
383 error == -EOPNOTSUPP) {
384 unsigned long flags;
385 /* barriers don't appear to be supported :-( */
386 set_bit(BarriersNotsupp, &rdev->flags);
387 mddev->barriers_work = 0;
388 spin_lock_irqsave(&mddev->write_lock, flags);
389 bio2->bi_next = mddev->biolist;
390 mddev->biolist = bio2;
391 spin_unlock_irqrestore(&mddev->write_lock, flags);
392 wake_up(&mddev->sb_wait);
393 bio_put(bio);
394 return 0;
395 }
396 bio_put(bio2);
397 bio->bi_private = rdev;
398 return super_written(bio, bytes_done, error);
399}
400
NeilBrown7bfa19f2005-06-21 17:17:28 -0700401void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
402 sector_t sector, int size, struct page *page)
403{
404 /* write first size bytes of page to sector of rdev
405 * Increment mddev->pending_writes before returning
406 * and decrement it on completion, waking up sb_wait
407 * if zero is reached.
408 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800409 *
410 * As we might need to resubmit the request if BIO_RW_BARRIER
411 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700412 */
413 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800414 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700415
416 bio->bi_bdev = rdev->bdev;
417 bio->bi_sector = sector;
418 bio_add_page(bio, page, size, 0);
419 bio->bi_private = rdev;
420 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800421 bio->bi_rw = rw;
422
NeilBrown7bfa19f2005-06-21 17:17:28 -0700423 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800424 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
425 struct bio *rbio;
426 rw |= (1<<BIO_RW_BARRIER);
427 rbio = bio_clone(bio, GFP_NOIO);
428 rbio->bi_private = bio;
429 rbio->bi_end_io = super_written_barrier;
430 submit_bio(rw, rbio);
431 } else
432 submit_bio(rw, bio);
433}
434
435void md_super_wait(mddev_t *mddev)
436{
437 /* wait for all superblock writes that were scheduled to complete.
438 * if any had to be retried (due to BARRIER problems), retry them
439 */
440 DEFINE_WAIT(wq);
441 for(;;) {
442 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
443 if (atomic_read(&mddev->pending_writes)==0)
444 break;
445 while (mddev->biolist) {
446 struct bio *bio;
447 spin_lock_irq(&mddev->write_lock);
448 bio = mddev->biolist;
449 mddev->biolist = bio->bi_next ;
450 bio->bi_next = NULL;
451 spin_unlock_irq(&mddev->write_lock);
452 submit_bio(bio->bi_rw, bio);
453 }
454 schedule();
455 }
456 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
460{
461 if (bio->bi_size)
462 return 1;
463
464 complete((struct completion*)bio->bi_private);
465 return 0;
466}
467
NeilBrowna654b9d82005-06-21 17:17:27 -0700468int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct page *page, int rw)
470{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700471 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct completion event;
473 int ret;
474
475 rw |= (1 << BIO_RW_SYNC);
476
477 bio->bi_bdev = bdev;
478 bio->bi_sector = sector;
479 bio_add_page(bio, page, size, 0);
480 init_completion(&event);
481 bio->bi_private = &event;
482 bio->bi_end_io = bi_complete;
483 submit_bio(rw, bio);
484 wait_for_completion(&event);
485
486 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
487 bio_put(bio);
488 return ret;
489}
NeilBrowna8745db2006-01-06 00:20:34 -0800490EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
NeilBrown0002b272005-09-09 16:23:53 -0700492static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 char b[BDEVNAME_SIZE];
495 if (!rdev->sb_page) {
496 MD_BUG();
497 return -EINVAL;
498 }
499 if (rdev->sb_loaded)
500 return 0;
501
502
NeilBrown0002b272005-09-09 16:23:53 -0700503 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto fail;
505 rdev->sb_loaded = 1;
506 return 0;
507
508fail:
509 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
510 bdevname(rdev->bdev,b));
511 return -EINVAL;
512}
513
514static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
515{
516 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
517 (sb1->set_uuid1 == sb2->set_uuid1) &&
518 (sb1->set_uuid2 == sb2->set_uuid2) &&
519 (sb1->set_uuid3 == sb2->set_uuid3))
520
521 return 1;
522
523 return 0;
524}
525
526
527static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
528{
529 int ret;
530 mdp_super_t *tmp1, *tmp2;
531
532 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
533 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
534
535 if (!tmp1 || !tmp2) {
536 ret = 0;
537 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
538 goto abort;
539 }
540
541 *tmp1 = *sb1;
542 *tmp2 = *sb2;
543
544 /*
545 * nr_disks is not constant
546 */
547 tmp1->nr_disks = 0;
548 tmp2->nr_disks = 0;
549
550 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
551 ret = 0;
552 else
553 ret = 1;
554
555abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700556 kfree(tmp1);
557 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return ret;
559}
560
561static unsigned int calc_sb_csum(mdp_super_t * sb)
562{
563 unsigned int disk_csum, csum;
564
565 disk_csum = sb->sb_csum;
566 sb->sb_csum = 0;
567 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
568 sb->sb_csum = disk_csum;
569 return csum;
570}
571
572
573/*
574 * Handle superblock details.
575 * We want to be able to handle multiple superblock formats
576 * so we have a common interface to them all, and an array of
577 * different handlers.
578 * We rely on user-space to write the initial superblock, and support
579 * reading and updating of superblocks.
580 * Interface methods are:
581 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
582 * loads and validates a superblock on dev.
583 * if refdev != NULL, compare superblocks on both devices
584 * Return:
585 * 0 - dev has a superblock that is compatible with refdev
586 * 1 - dev has a superblock that is compatible and newer than refdev
587 * so dev should be used as the refdev in future
588 * -EINVAL superblock incompatible or invalid
589 * -othererror e.g. -EIO
590 *
591 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
592 * Verify that dev is acceptable into mddev.
593 * The first time, mddev->raid_disks will be 0, and data from
594 * dev should be merged in. Subsequent calls check that dev
595 * is new enough. Return 0 or -EINVAL
596 *
597 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
598 * Update the superblock for rdev with data in mddev
599 * This does not write to disc.
600 *
601 */
602
603struct super_type {
604 char *name;
605 struct module *owner;
606 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
607 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
608 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
609};
610
611/*
612 * load_super for 0.90.0
613 */
614static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
615{
616 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
617 mdp_super_t *sb;
618 int ret;
619 sector_t sb_offset;
620
621 /*
622 * Calculate the position of the superblock,
623 * it's at the end of the disk.
624 *
625 * It also happens to be a multiple of 4Kb.
626 */
627 sb_offset = calc_dev_sboffset(rdev->bdev);
628 rdev->sb_offset = sb_offset;
629
NeilBrown0002b272005-09-09 16:23:53 -0700630 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (ret) return ret;
632
633 ret = -EINVAL;
634
635 bdevname(rdev->bdev, b);
636 sb = (mdp_super_t*)page_address(rdev->sb_page);
637
638 if (sb->md_magic != MD_SB_MAGIC) {
639 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
640 b);
641 goto abort;
642 }
643
644 if (sb->major_version != 0 ||
645 sb->minor_version != 90) {
646 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
647 sb->major_version, sb->minor_version,
648 b);
649 goto abort;
650 }
651
652 if (sb->raid_disks <= 0)
653 goto abort;
654
655 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
656 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
657 b);
658 goto abort;
659 }
660
661 rdev->preferred_minor = sb->md_minor;
662 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700663 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (sb->level == LEVEL_MULTIPATH)
666 rdev->desc_nr = -1;
667 else
668 rdev->desc_nr = sb->this_disk.number;
669
670 if (refdev == 0)
671 ret = 1;
672 else {
673 __u64 ev1, ev2;
674 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
675 if (!uuid_equal(refsb, sb)) {
676 printk(KERN_WARNING "md: %s has different UUID to %s\n",
677 b, bdevname(refdev->bdev,b2));
678 goto abort;
679 }
680 if (!sb_equal(refsb, sb)) {
681 printk(KERN_WARNING "md: %s has same UUID"
682 " but different superblock to %s\n",
683 b, bdevname(refdev->bdev, b2));
684 goto abort;
685 }
686 ev1 = md_event(sb);
687 ev2 = md_event(refsb);
688 if (ev1 > ev2)
689 ret = 1;
690 else
691 ret = 0;
692 }
693 rdev->size = calc_dev_size(rdev, sb->chunk_size);
694
695 abort:
696 return ret;
697}
698
699/*
700 * validate_super for 0.90.0
701 */
702static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
703{
704 mdp_disk_t *desc;
705 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
706
NeilBrown41158c72005-06-21 17:17:25 -0700707 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800708 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (mddev->raid_disks == 0) {
710 mddev->major_version = 0;
711 mddev->minor_version = sb->minor_version;
712 mddev->patch_version = sb->patch_version;
713 mddev->persistent = ! sb->not_persistent;
714 mddev->chunk_size = sb->chunk_size;
715 mddev->ctime = sb->ctime;
716 mddev->utime = sb->utime;
717 mddev->level = sb->level;
718 mddev->layout = sb->layout;
719 mddev->raid_disks = sb->raid_disks;
720 mddev->size = sb->size;
721 mddev->events = md_event(sb);
NeilBrown92232142005-08-18 11:24:16 -0700722 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700723 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (sb->state & (1<<MD_SB_CLEAN))
726 mddev->recovery_cp = MaxSector;
727 else {
728 if (sb->events_hi == sb->cp_events_hi &&
729 sb->events_lo == sb->cp_events_lo) {
730 mddev->recovery_cp = sb->recovery_cp;
731 } else
732 mddev->recovery_cp = 0;
733 }
734
735 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
736 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
737 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
738 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
739
740 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700741
742 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
743 mddev->bitmap_file == NULL) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800744 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
745 && mddev->level != 10) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700746 /* FIXME use a better test */
NeilBrown6cce3b22006-01-06 00:20:16 -0800747 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -0700748 return -EINVAL;
749 }
NeilBrown36fa3062005-09-09 16:23:45 -0700750 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700751 }
752
NeilBrown41158c72005-06-21 17:17:25 -0700753 } else if (mddev->pers == NULL) {
754 /* Insist on good event counter while assembling */
755 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 ++ev1;
757 if (ev1 < mddev->events)
758 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700759 } else if (mddev->bitmap) {
760 /* if adding to array with a bitmap, then we can accept an
761 * older device ... but not too old.
762 */
763 __u64 ev1 = md_event(sb);
764 if (ev1 < mddev->bitmap->events_cleared)
765 return 0;
766 } else /* just a hot-add of a new device, leave raid_disk at -1 */
767 return 0;
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 desc = sb->disks + rdev->desc_nr;
771
772 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800773 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 else if (desc->state & (1<<MD_DISK_SYNC) &&
775 desc->raid_disk < mddev->raid_disks) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800776 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 rdev->raid_disk = desc->raid_disk;
778 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700779 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
780 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700781 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800782 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 0;
784}
785
786/*
787 * sync_super for 0.90.0
788 */
789static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
790{
791 mdp_super_t *sb;
792 struct list_head *tmp;
793 mdk_rdev_t *rdev2;
794 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /* make rdev->sb match mddev data..
798 *
799 * 1/ zero out disks
800 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
801 * 3/ any empty disks < next_spare become removed
802 *
803 * disks[0] gets initialised to REMOVED because
804 * we cannot be sure from other fields if it has
805 * been initialised or not.
806 */
807 int i;
808 int active=0, working=0,failed=0,spare=0,nr_disks=0;
809
NeilBrown61181562005-09-09 16:24:02 -0700810 rdev->sb_size = MD_SB_BYTES;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 sb = (mdp_super_t*)page_address(rdev->sb_page);
813
814 memset(sb, 0, sizeof(*sb));
815
816 sb->md_magic = MD_SB_MAGIC;
817 sb->major_version = mddev->major_version;
818 sb->minor_version = mddev->minor_version;
819 sb->patch_version = mddev->patch_version;
820 sb->gvalid_words = 0; /* ignored */
821 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
822 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
823 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
824 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
825
826 sb->ctime = mddev->ctime;
827 sb->level = mddev->level;
828 sb->size = mddev->size;
829 sb->raid_disks = mddev->raid_disks;
830 sb->md_minor = mddev->md_minor;
831 sb->not_persistent = !mddev->persistent;
832 sb->utime = mddev->utime;
833 sb->state = 0;
834 sb->events_hi = (mddev->events>>32);
835 sb->events_lo = (u32)mddev->events;
836
837 if (mddev->in_sync)
838 {
839 sb->recovery_cp = mddev->recovery_cp;
840 sb->cp_events_hi = (mddev->events>>32);
841 sb->cp_events_lo = (u32)mddev->events;
842 if (mddev->recovery_cp == MaxSector)
843 sb->state = (1<< MD_SB_CLEAN);
844 } else
845 sb->recovery_cp = 0;
846
847 sb->layout = mddev->layout;
848 sb->chunk_size = mddev->chunk_size;
849
NeilBrowna654b9d82005-06-21 17:17:27 -0700850 if (mddev->bitmap && mddev->bitmap_file == NULL)
851 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 sb->disks[0].state = (1<<MD_DISK_REMOVED);
854 ITERATE_RDEV(mddev,rdev2,tmp) {
855 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800856 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800857 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
858 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800859 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800861 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800862 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 d = &sb->disks[rdev2->desc_nr];
864 nr_disks++;
865 d->number = rdev2->desc_nr;
866 d->major = MAJOR(rdev2->bdev->bd_dev);
867 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800868 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
869 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 d->raid_disk = rdev2->raid_disk;
871 else
872 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrownb2d444d2005-11-08 21:39:31 -0800873 if (test_bit(Faulty, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 d->state = (1<<MD_DISK_FAULTY);
875 failed++;
NeilBrownb2d444d2005-11-08 21:39:31 -0800876 } else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 d->state = (1<<MD_DISK_ACTIVE);
878 d->state |= (1<<MD_DISK_SYNC);
879 active++;
880 working++;
881 } else {
882 d->state = 0;
883 spare++;
884 working++;
885 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700886 if (test_bit(WriteMostly, &rdev2->flags))
887 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /* now set the "removed" and "faulty" bits on any missing devices */
890 for (i=0 ; i < mddev->raid_disks ; i++) {
891 mdp_disk_t *d = &sb->disks[i];
892 if (d->state == 0 && d->number == 0) {
893 d->number = i;
894 d->raid_disk = i;
895 d->state = (1<<MD_DISK_REMOVED);
896 d->state |= (1<<MD_DISK_FAULTY);
897 failed++;
898 }
899 }
900 sb->nr_disks = nr_disks;
901 sb->active_disks = active;
902 sb->working_disks = working;
903 sb->failed_disks = failed;
904 sb->spare_disks = spare;
905
906 sb->this_disk = sb->disks[rdev->desc_nr];
907 sb->sb_csum = calc_sb_csum(sb);
908}
909
910/*
911 * version 1 superblock
912 */
913
914static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
915{
916 unsigned int disk_csum, csum;
917 unsigned long long newcsum;
918 int size = 256 + le32_to_cpu(sb->max_dev)*2;
919 unsigned int *isuper = (unsigned int*)sb;
920 int i;
921
922 disk_csum = sb->sb_csum;
923 sb->sb_csum = 0;
924 newcsum = 0;
925 for (i=0; size>=4; size -= 4 )
926 newcsum += le32_to_cpu(*isuper++);
927
928 if (size == 2)
929 newcsum += le16_to_cpu(*(unsigned short*) isuper);
930
931 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
932 sb->sb_csum = disk_csum;
933 return cpu_to_le32(csum);
934}
935
936static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
937{
938 struct mdp_superblock_1 *sb;
939 int ret;
940 sector_t sb_offset;
941 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -0700942 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /*
945 * Calculate the position of the superblock.
946 * It is always aligned to a 4K boundary and
947 * depeding on minor_version, it can be:
948 * 0: At least 8K, but less than 12K, from end of device
949 * 1: At start of device
950 * 2: 4K from start of device.
951 */
952 switch(minor_version) {
953 case 0:
954 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
955 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -0700956 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /* convert from sectors to K */
958 sb_offset /= 2;
959 break;
960 case 1:
961 sb_offset = 0;
962 break;
963 case 2:
964 sb_offset = 4;
965 break;
966 default:
967 return -EINVAL;
968 }
969 rdev->sb_offset = sb_offset;
970
NeilBrown0002b272005-09-09 16:23:53 -0700971 /* superblock is rarely larger than 1K, but it can be larger,
972 * and it is safe to read 4k, so we do that
973 */
974 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (ret) return ret;
976
977
978 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
979
980 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
981 sb->major_version != cpu_to_le32(1) ||
982 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
983 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -0700984 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return -EINVAL;
986
987 if (calc_sb_1_csum(sb) != sb->sb_csum) {
988 printk("md: invalid superblock checksum on %s\n",
989 bdevname(rdev->bdev,b));
990 return -EINVAL;
991 }
992 if (le64_to_cpu(sb->data_size) < 10) {
993 printk("md: data_size too small on %s\n",
994 bdevname(rdev->bdev,b));
995 return -EINVAL;
996 }
997 rdev->preferred_minor = 0xffff;
998 rdev->data_offset = le64_to_cpu(sb->data_offset);
999
NeilBrown0002b272005-09-09 16:23:53 -07001000 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001001 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001002 if (rdev->sb_size & bmask)
1003 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (refdev == 0)
1006 return 1;
1007 else {
1008 __u64 ev1, ev2;
1009 struct mdp_superblock_1 *refsb =
1010 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1011
1012 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1013 sb->level != refsb->level ||
1014 sb->layout != refsb->layout ||
1015 sb->chunksize != refsb->chunksize) {
1016 printk(KERN_WARNING "md: %s has strangely different"
1017 " superblock to %s\n",
1018 bdevname(rdev->bdev,b),
1019 bdevname(refdev->bdev,b2));
1020 return -EINVAL;
1021 }
1022 ev1 = le64_to_cpu(sb->events);
1023 ev2 = le64_to_cpu(refsb->events);
1024
1025 if (ev1 > ev2)
1026 return 1;
1027 }
1028 if (minor_version)
1029 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1030 else
1031 rdev->size = rdev->sb_offset;
1032 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1033 return -EINVAL;
1034 rdev->size = le64_to_cpu(sb->data_size)/2;
1035 if (le32_to_cpu(sb->chunksize))
1036 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1037 return 0;
1038}
1039
1040static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1041{
1042 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1043
NeilBrown41158c72005-06-21 17:17:25 -07001044 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001045 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (mddev->raid_disks == 0) {
1047 mddev->major_version = 1;
1048 mddev->patch_version = 0;
1049 mddev->persistent = 1;
1050 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1051 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1052 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1053 mddev->level = le32_to_cpu(sb->level);
1054 mddev->layout = le32_to_cpu(sb->layout);
1055 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1056 mddev->size = le64_to_cpu(sb->size)/2;
1057 mddev->events = le64_to_cpu(sb->events);
NeilBrown92232142005-08-18 11:24:16 -07001058 mddev->bitmap_offset = 0;
NeilBrown53e87fb2005-09-09 16:23:58 -07001059 mddev->default_bitmap_offset = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1062 memcpy(mddev->uuid, sb->set_uuid, 16);
1063
1064 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001065
NeilBrown71c08052005-09-09 16:23:51 -07001066 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -07001067 mddev->bitmap_file == NULL ) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001068 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1069 && mddev->level != 10) {
1070 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -07001071 return -EINVAL;
1072 }
1073 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1074 }
NeilBrown41158c72005-06-21 17:17:25 -07001075 } else if (mddev->pers == NULL) {
1076 /* Insist of good event counter while assembling */
1077 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 ++ev1;
1079 if (ev1 < mddev->events)
1080 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001081 } else if (mddev->bitmap) {
1082 /* If adding to array with a bitmap, then we can accept an
1083 * older device, but not too old.
1084 */
1085 __u64 ev1 = le64_to_cpu(sb->events);
1086 if (ev1 < mddev->bitmap->events_cleared)
1087 return 0;
1088 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 if (mddev->level != LEVEL_MULTIPATH) {
1092 int role;
1093 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1094 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1095 switch(role) {
1096 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 break;
1098 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001099 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 break;
1101 default:
NeilBrownb2d444d2005-11-08 21:39:31 -08001102 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 rdev->raid_disk = role;
1104 break;
1105 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001106 if (sb->devflags & WriteMostly1)
1107 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001108 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001109 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return 0;
1112}
1113
1114static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1115{
1116 struct mdp_superblock_1 *sb;
1117 struct list_head *tmp;
1118 mdk_rdev_t *rdev2;
1119 int max_dev, i;
1120 /* make rdev->sb match mddev and rdev data. */
1121
1122 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1123
1124 sb->feature_map = 0;
1125 sb->pad0 = 0;
1126 memset(sb->pad1, 0, sizeof(sb->pad1));
1127 memset(sb->pad2, 0, sizeof(sb->pad2));
1128 memset(sb->pad3, 0, sizeof(sb->pad3));
1129
1130 sb->utime = cpu_to_le64((__u64)mddev->utime);
1131 sb->events = cpu_to_le64(mddev->events);
1132 if (mddev->in_sync)
1133 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1134 else
1135 sb->resync_offset = cpu_to_le64(0);
1136
NeilBrowna654b9d82005-06-21 17:17:27 -07001137 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1138 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001139 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001140 }
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 max_dev = 0;
1143 ITERATE_RDEV(mddev,rdev2,tmp)
1144 if (rdev2->desc_nr+1 > max_dev)
1145 max_dev = rdev2->desc_nr+1;
1146
1147 sb->max_dev = cpu_to_le32(max_dev);
1148 for (i=0; i<max_dev;i++)
1149 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1150
1151 ITERATE_RDEV(mddev,rdev2,tmp) {
1152 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001153 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001155 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1157 else
1158 sb->dev_roles[i] = cpu_to_le16(0xffff);
1159 }
1160
1161 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1162 sb->sb_csum = calc_sb_1_csum(sb);
1163}
1164
1165
Adrian Bunk75c96f82005-05-05 16:16:09 -07001166static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 [0] = {
1168 .name = "0.90.0",
1169 .owner = THIS_MODULE,
1170 .load_super = super_90_load,
1171 .validate_super = super_90_validate,
1172 .sync_super = super_90_sync,
1173 },
1174 [1] = {
1175 .name = "md-1",
1176 .owner = THIS_MODULE,
1177 .load_super = super_1_load,
1178 .validate_super = super_1_validate,
1179 .sync_super = super_1_sync,
1180 },
1181};
1182
1183static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1184{
1185 struct list_head *tmp;
1186 mdk_rdev_t *rdev;
1187
1188 ITERATE_RDEV(mddev,rdev,tmp)
1189 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1190 return rdev;
1191
1192 return NULL;
1193}
1194
1195static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1196{
1197 struct list_head *tmp;
1198 mdk_rdev_t *rdev;
1199
1200 ITERATE_RDEV(mddev1,rdev,tmp)
1201 if (match_dev_unit(mddev2, rdev))
1202 return 1;
1203
1204 return 0;
1205}
1206
1207static LIST_HEAD(pending_raid_disks);
1208
1209static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1210{
1211 mdk_rdev_t *same_pdev;
1212 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001213 struct kobject *ko;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 if (rdev->mddev) {
1216 MD_BUG();
1217 return -EINVAL;
1218 }
1219 same_pdev = match_dev_unit(mddev, rdev);
1220 if (same_pdev)
1221 printk(KERN_WARNING
1222 "%s: WARNING: %s appears to be on the same physical"
1223 " disk as %s. True\n protection against single-disk"
1224 " failure might be compromised.\n",
1225 mdname(mddev), bdevname(rdev->bdev,b),
1226 bdevname(same_pdev->bdev,b2));
1227
1228 /* Verify rdev->desc_nr is unique.
1229 * If it is -1, assign a free number, else
1230 * check number is not in use
1231 */
1232 if (rdev->desc_nr < 0) {
1233 int choice = 0;
1234 if (mddev->pers) choice = mddev->raid_disks;
1235 while (find_rdev_nr(mddev, choice))
1236 choice++;
1237 rdev->desc_nr = choice;
1238 } else {
1239 if (find_rdev_nr(mddev, rdev->desc_nr))
1240 return -EBUSY;
1241 }
NeilBrown19133a42005-11-08 21:39:35 -08001242 bdevname(rdev->bdev,b);
1243 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1244 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 list_add(&rdev->same_set, &mddev->disks);
1247 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001248 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001249
NeilBrown9c791972005-11-08 21:39:28 -08001250 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001251 kobject_add(&rdev->kobj);
1252
NeilBrownf637b9f2005-11-08 21:39:37 -08001253 if (rdev->bdev->bd_part)
1254 ko = &rdev->bdev->bd_part->kobj;
1255 else
1256 ko = &rdev->bdev->bd_disk->kobj;
1257 sysfs_create_link(&rdev->kobj, ko, "block");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return 0;
1259}
1260
1261static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1262{
1263 char b[BDEVNAME_SIZE];
1264 if (!rdev->mddev) {
1265 MD_BUG();
1266 return;
1267 }
1268 list_del_init(&rdev->same_set);
1269 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1270 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001271 sysfs_remove_link(&rdev->kobj, "block");
1272 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
1275/*
1276 * prevent the device from being mounted, repartitioned or
1277 * otherwise reused by a RAID array (or any other kernel
1278 * subsystem), by bd_claiming the device.
1279 */
1280static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1281{
1282 int err = 0;
1283 struct block_device *bdev;
1284 char b[BDEVNAME_SIZE];
1285
1286 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1287 if (IS_ERR(bdev)) {
1288 printk(KERN_ERR "md: could not open %s.\n",
1289 __bdevname(dev, b));
1290 return PTR_ERR(bdev);
1291 }
1292 err = bd_claim(bdev, rdev);
1293 if (err) {
1294 printk(KERN_ERR "md: could not bd_claim %s.\n",
1295 bdevname(bdev, b));
1296 blkdev_put(bdev);
1297 return err;
1298 }
1299 rdev->bdev = bdev;
1300 return err;
1301}
1302
1303static void unlock_rdev(mdk_rdev_t *rdev)
1304{
1305 struct block_device *bdev = rdev->bdev;
1306 rdev->bdev = NULL;
1307 if (!bdev)
1308 MD_BUG();
1309 bd_release(bdev);
1310 blkdev_put(bdev);
1311}
1312
1313void md_autodetect_dev(dev_t dev);
1314
1315static void export_rdev(mdk_rdev_t * rdev)
1316{
1317 char b[BDEVNAME_SIZE];
1318 printk(KERN_INFO "md: export_rdev(%s)\n",
1319 bdevname(rdev->bdev,b));
1320 if (rdev->mddev)
1321 MD_BUG();
1322 free_disk_sb(rdev);
1323 list_del_init(&rdev->same_set);
1324#ifndef MODULE
1325 md_autodetect_dev(rdev->bdev->bd_dev);
1326#endif
1327 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001328 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331static void kick_rdev_from_array(mdk_rdev_t * rdev)
1332{
1333 unbind_rdev_from_array(rdev);
1334 export_rdev(rdev);
1335}
1336
1337static void export_array(mddev_t *mddev)
1338{
1339 struct list_head *tmp;
1340 mdk_rdev_t *rdev;
1341
1342 ITERATE_RDEV(mddev,rdev,tmp) {
1343 if (!rdev->mddev) {
1344 MD_BUG();
1345 continue;
1346 }
1347 kick_rdev_from_array(rdev);
1348 }
1349 if (!list_empty(&mddev->disks))
1350 MD_BUG();
1351 mddev->raid_disks = 0;
1352 mddev->major_version = 0;
1353}
1354
1355static void print_desc(mdp_disk_t *desc)
1356{
1357 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1358 desc->major,desc->minor,desc->raid_disk,desc->state);
1359}
1360
1361static void print_sb(mdp_super_t *sb)
1362{
1363 int i;
1364
1365 printk(KERN_INFO
1366 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1367 sb->major_version, sb->minor_version, sb->patch_version,
1368 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1369 sb->ctime);
1370 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1371 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1372 sb->md_minor, sb->layout, sb->chunk_size);
1373 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1374 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1375 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1376 sb->failed_disks, sb->spare_disks,
1377 sb->sb_csum, (unsigned long)sb->events_lo);
1378
1379 printk(KERN_INFO);
1380 for (i = 0; i < MD_SB_DISKS; i++) {
1381 mdp_disk_t *desc;
1382
1383 desc = sb->disks + i;
1384 if (desc->number || desc->major || desc->minor ||
1385 desc->raid_disk || (desc->state && (desc->state != 4))) {
1386 printk(" D %2d: ", i);
1387 print_desc(desc);
1388 }
1389 }
1390 printk(KERN_INFO "md: THIS: ");
1391 print_desc(&sb->this_disk);
1392
1393}
1394
1395static void print_rdev(mdk_rdev_t *rdev)
1396{
1397 char b[BDEVNAME_SIZE];
1398 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1399 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001400 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1401 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (rdev->sb_loaded) {
1403 printk(KERN_INFO "md: rdev superblock:\n");
1404 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1405 } else
1406 printk(KERN_INFO "md: no rdev superblock!\n");
1407}
1408
1409void md_print_devices(void)
1410{
1411 struct list_head *tmp, *tmp2;
1412 mdk_rdev_t *rdev;
1413 mddev_t *mddev;
1414 char b[BDEVNAME_SIZE];
1415
1416 printk("\n");
1417 printk("md: **********************************\n");
1418 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1419 printk("md: **********************************\n");
1420 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
NeilBrown32a76272005-06-21 17:17:14 -07001422 if (mddev->bitmap)
1423 bitmap_print_sb(mddev->bitmap);
1424 else
1425 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 ITERATE_RDEV(mddev,rdev,tmp2)
1427 printk("<%s>", bdevname(rdev->bdev,b));
1428 printk("\n");
1429
1430 ITERATE_RDEV(mddev,rdev,tmp2)
1431 print_rdev(rdev);
1432 }
1433 printk("md: **********************************\n");
1434 printk("\n");
1435}
1436
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438static void sync_sbs(mddev_t * mddev)
1439{
1440 mdk_rdev_t *rdev;
1441 struct list_head *tmp;
1442
1443 ITERATE_RDEV(mddev,rdev,tmp) {
1444 super_types[mddev->major_version].
1445 sync_super(mddev, rdev);
1446 rdev->sb_loaded = 1;
1447 }
1448}
1449
1450static void md_update_sb(mddev_t * mddev)
1451{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001452 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 struct list_head *tmp;
1454 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001455 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001458 spin_lock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001459 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 mddev->utime = get_seconds();
1461 mddev->events ++;
1462
1463 if (!mddev->events) {
1464 /*
1465 * oops, this 64-bit counter should never wrap.
1466 * Either we are in around ~1 trillion A.C., assuming
1467 * 1 reboot per second, or we have a bug:
1468 */
1469 MD_BUG();
1470 mddev->events --;
1471 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001472 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 sync_sbs(mddev);
1474
1475 /*
1476 * do not write anything to disk if using
1477 * nonpersistent superblocks
1478 */
NeilBrown06d91a52005-06-21 17:17:12 -07001479 if (!mddev->persistent) {
1480 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001481 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001482 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001484 }
NeilBrowna9701a32005-11-08 21:39:34 -08001485 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 dprintk(KERN_INFO
1488 "md: updating %s RAID superblock on device (in sync %d)\n",
1489 mdname(mddev),mddev->in_sync);
1490
NeilBrown32a76272005-06-21 17:17:14 -07001491 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 ITERATE_RDEV(mddev,rdev,tmp) {
1493 char b[BDEVNAME_SIZE];
1494 dprintk(KERN_INFO "md: ");
NeilBrownb2d444d2005-11-08 21:39:31 -08001495 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 dprintk("(skipping faulty ");
1497
1498 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001499 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001500 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001501 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001502 rdev->sb_page);
1503 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1504 bdevname(rdev->bdev,b),
1505 (unsigned long long)rdev->sb_offset);
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 } else
1508 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001509 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /* only need to write one superblock... */
1511 break;
1512 }
NeilBrowna9701a32005-11-08 21:39:34 -08001513 md_super_wait(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001514 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1515
NeilBrowna9701a32005-11-08 21:39:34 -08001516 spin_lock_irq(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001517 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001518 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001519 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001520 goto repeat;
1521 }
1522 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001523 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001524 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
NeilBrownbce74da2006-01-06 00:20:41 -08001528/* words written to sysfs files may, or my not, be \n terminated.
1529 * We want to accept with case. For this we use cmd_match.
1530 */
1531static int cmd_match(const char *cmd, const char *str)
1532{
1533 /* See if cmd, written into a sysfs file, matches
1534 * str. They must either be the same, or cmd can
1535 * have a trailing newline
1536 */
1537 while (*cmd && *str && *cmd == *str) {
1538 cmd++;
1539 str++;
1540 }
1541 if (*cmd == '\n')
1542 cmd++;
1543 if (*str || *cmd)
1544 return 0;
1545 return 1;
1546}
1547
NeilBrown86e6ffd2005-11-08 21:39:24 -08001548struct rdev_sysfs_entry {
1549 struct attribute attr;
1550 ssize_t (*show)(mdk_rdev_t *, char *);
1551 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1552};
1553
1554static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001555state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001556{
1557 char *sep = "";
1558 int len=0;
1559
NeilBrownb2d444d2005-11-08 21:39:31 -08001560 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001561 len+= sprintf(page+len, "%sfaulty",sep);
1562 sep = ",";
1563 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001564 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001565 len += sprintf(page+len, "%sin_sync",sep);
1566 sep = ",";
1567 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001568 if (!test_bit(Faulty, &rdev->flags) &&
1569 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001570 len += sprintf(page+len, "%sspare", sep);
1571 sep = ",";
1572 }
1573 return len+sprintf(page+len, "\n");
1574}
1575
NeilBrown96de1e62005-11-08 21:39:39 -08001576static struct rdev_sysfs_entry
1577rdev_state = __ATTR_RO(state);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001578
1579static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001580super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001581{
1582 if (rdev->sb_loaded && rdev->sb_size) {
1583 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1584 return rdev->sb_size;
1585 } else
1586 return 0;
1587}
NeilBrown96de1e62005-11-08 21:39:39 -08001588static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1589
NeilBrown86e6ffd2005-11-08 21:39:24 -08001590static struct attribute *rdev_default_attrs[] = {
1591 &rdev_state.attr,
1592 &rdev_super.attr,
1593 NULL,
1594};
1595static ssize_t
1596rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1597{
1598 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1599 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1600
1601 if (!entry->show)
1602 return -EIO;
1603 return entry->show(rdev, page);
1604}
1605
1606static ssize_t
1607rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1608 const char *page, size_t length)
1609{
1610 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1611 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1612
1613 if (!entry->store)
1614 return -EIO;
1615 return entry->store(rdev, page, length);
1616}
1617
1618static void rdev_free(struct kobject *ko)
1619{
1620 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1621 kfree(rdev);
1622}
1623static struct sysfs_ops rdev_sysfs_ops = {
1624 .show = rdev_attr_show,
1625 .store = rdev_attr_store,
1626};
1627static struct kobj_type rdev_ktype = {
1628 .release = rdev_free,
1629 .sysfs_ops = &rdev_sysfs_ops,
1630 .default_attrs = rdev_default_attrs,
1631};
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633/*
1634 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1635 *
1636 * mark the device faulty if:
1637 *
1638 * - the device is nonexistent (zero size)
1639 * - the device has no valid superblock
1640 *
1641 * a faulty rdev _never_ has rdev->sb set.
1642 */
1643static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1644{
1645 char b[BDEVNAME_SIZE];
1646 int err;
1647 mdk_rdev_t *rdev;
1648 sector_t size;
1649
NeilBrown9ffae0c2006-01-06 00:20:32 -08001650 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (!rdev) {
1652 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1653 return ERR_PTR(-ENOMEM);
1654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if ((err = alloc_disk_sb(rdev)))
1657 goto abort_free;
1658
1659 err = lock_rdev(rdev, newdev);
1660 if (err)
1661 goto abort_free;
1662
NeilBrown86e6ffd2005-11-08 21:39:24 -08001663 rdev->kobj.parent = NULL;
1664 rdev->kobj.ktype = &rdev_ktype;
1665 kobject_init(&rdev->kobj);
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 rdev->desc_nr = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001668 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 rdev->data_offset = 0;
1670 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001671 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1674 if (!size) {
1675 printk(KERN_WARNING
1676 "md: %s has zero or unknown size, marking faulty!\n",
1677 bdevname(rdev->bdev,b));
1678 err = -EINVAL;
1679 goto abort_free;
1680 }
1681
1682 if (super_format >= 0) {
1683 err = super_types[super_format].
1684 load_super(rdev, NULL, super_minor);
1685 if (err == -EINVAL) {
1686 printk(KERN_WARNING
1687 "md: %s has invalid sb, not importing!\n",
1688 bdevname(rdev->bdev,b));
1689 goto abort_free;
1690 }
1691 if (err < 0) {
1692 printk(KERN_WARNING
1693 "md: could not read %s's sb, not importing!\n",
1694 bdevname(rdev->bdev,b));
1695 goto abort_free;
1696 }
1697 }
1698 INIT_LIST_HEAD(&rdev->same_set);
1699
1700 return rdev;
1701
1702abort_free:
1703 if (rdev->sb_page) {
1704 if (rdev->bdev)
1705 unlock_rdev(rdev);
1706 free_disk_sb(rdev);
1707 }
1708 kfree(rdev);
1709 return ERR_PTR(err);
1710}
1711
1712/*
1713 * Check a full RAID array for plausibility
1714 */
1715
1716
NeilBrowna757e642005-04-16 15:26:42 -07001717static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
1719 int i;
1720 struct list_head *tmp;
1721 mdk_rdev_t *rdev, *freshest;
1722 char b[BDEVNAME_SIZE];
1723
1724 freshest = NULL;
1725 ITERATE_RDEV(mddev,rdev,tmp)
1726 switch (super_types[mddev->major_version].
1727 load_super(rdev, freshest, mddev->minor_version)) {
1728 case 1:
1729 freshest = rdev;
1730 break;
1731 case 0:
1732 break;
1733 default:
1734 printk( KERN_ERR \
1735 "md: fatal superblock inconsistency in %s"
1736 " -- removing from array\n",
1737 bdevname(rdev->bdev,b));
1738 kick_rdev_from_array(rdev);
1739 }
1740
1741
1742 super_types[mddev->major_version].
1743 validate_super(mddev, freshest);
1744
1745 i = 0;
1746 ITERATE_RDEV(mddev,rdev,tmp) {
1747 if (rdev != freshest)
1748 if (super_types[mddev->major_version].
1749 validate_super(mddev, rdev)) {
1750 printk(KERN_WARNING "md: kicking non-fresh %s"
1751 " from array!\n",
1752 bdevname(rdev->bdev,b));
1753 kick_rdev_from_array(rdev);
1754 continue;
1755 }
1756 if (mddev->level == LEVEL_MULTIPATH) {
1757 rdev->desc_nr = i++;
1758 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001759 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761 }
1762
1763
1764
1765 if (mddev->recovery_cp != MaxSector &&
1766 mddev->level >= 1)
1767 printk(KERN_ERR "md: %s: raid array is not clean"
1768 " -- starting background reconstruction\n",
1769 mdname(mddev));
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
NeilBrowneae17012005-11-08 21:39:23 -08001773static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001774level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08001775{
NeilBrown2604b702006-01-06 00:20:36 -08001776 struct mdk_personality *p = mddev->pers;
NeilBrownbb636542005-11-08 21:39:45 -08001777 if (p == NULL && mddev->raid_disks == 0)
NeilBrowneae17012005-11-08 21:39:23 -08001778 return 0;
1779 if (mddev->level >= 0)
Neil Brownbcb97942005-12-20 11:07:00 +11001780 return sprintf(page, "raid%d\n", mddev->level);
NeilBrowneae17012005-11-08 21:39:23 -08001781 else
1782 return sprintf(page, "%s\n", p->name);
1783}
1784
NeilBrown96de1e62005-11-08 21:39:39 -08001785static struct md_sysfs_entry md_level = __ATTR_RO(level);
NeilBrowneae17012005-11-08 21:39:23 -08001786
1787static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001788raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08001789{
NeilBrownbb636542005-11-08 21:39:45 -08001790 if (mddev->raid_disks == 0)
1791 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08001792 return sprintf(page, "%d\n", mddev->raid_disks);
1793}
1794
NeilBrown96de1e62005-11-08 21:39:39 -08001795static struct md_sysfs_entry md_raid_disks = __ATTR_RO(raid_disks);
NeilBrowneae17012005-11-08 21:39:23 -08001796
NeilBrown24dd4692005-11-08 21:39:26 -08001797static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08001798chunk_size_show(mddev_t *mddev, char *page)
1799{
1800 return sprintf(page, "%d\n", mddev->chunk_size);
1801}
1802
1803static ssize_t
1804chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
1805{
1806 /* can only set chunk_size if array is not yet active */
1807 char *e;
1808 unsigned long n = simple_strtoul(buf, &e, 10);
1809
1810 if (mddev->pers)
1811 return -EBUSY;
1812 if (!*buf || (*e && *e != '\n'))
1813 return -EINVAL;
1814
1815 mddev->chunk_size = n;
1816 return len;
1817}
1818static struct md_sysfs_entry md_chunk_size =
1819__ATTR(chunk_size, 0644, chunk_size_show, chunk_size_store);
1820
1821
1822static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08001823size_show(mddev_t *mddev, char *page)
1824{
1825 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
1826}
1827
1828static int update_size(mddev_t *mddev, unsigned long size);
1829
1830static ssize_t
1831size_store(mddev_t *mddev, const char *buf, size_t len)
1832{
1833 /* If array is inactive, we can reduce the component size, but
1834 * not increase it (except from 0).
1835 * If array is active, we can try an on-line resize
1836 */
1837 char *e;
1838 int err = 0;
1839 unsigned long long size = simple_strtoull(buf, &e, 10);
1840 if (!*buf || *buf == '\n' ||
1841 (*e && *e != '\n'))
1842 return -EINVAL;
1843
1844 if (mddev->pers) {
1845 err = update_size(mddev, size);
1846 md_update_sb(mddev);
1847 } else {
1848 if (mddev->size == 0 ||
1849 mddev->size > size)
1850 mddev->size = size;
1851 else
1852 err = -ENOSPC;
1853 }
1854 return err ? err : len;
1855}
1856
1857static struct md_sysfs_entry md_size =
1858__ATTR(component_size, 0644, size_show, size_store);
1859
1860static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08001861action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08001862{
NeilBrown7eec3142005-11-08 21:39:44 -08001863 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08001864 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1865 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
1866 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08001867 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1868 type = "resync";
1869 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1870 type = "check";
1871 else
1872 type = "repair";
1873 } else
1874 type = "recover";
1875 }
1876 return sprintf(page, "%s\n", type);
1877}
1878
1879static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08001880action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08001881{
NeilBrown7eec3142005-11-08 21:39:44 -08001882 if (!mddev->pers || !mddev->pers->sync_request)
1883 return -EINVAL;
1884
NeilBrownbce74da2006-01-06 00:20:41 -08001885 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08001886 if (mddev->sync_thread) {
1887 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1888 md_unregister_thread(mddev->sync_thread);
1889 mddev->sync_thread = NULL;
1890 mddev->recovery = 0;
1891 }
NeilBrown03c902e2006-01-06 00:20:46 -08001892 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1893 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08001894 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08001895 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08001896 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1897 else {
NeilBrownbce74da2006-01-06 00:20:41 -08001898 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08001899 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownbce74da2006-01-06 00:20:41 -08001900 else if (cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08001901 return -EINVAL;
1902 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1903 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08001904 }
NeilBrown03c902e2006-01-06 00:20:46 -08001905 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08001906 md_wakeup_thread(mddev->thread);
1907 return len;
1908}
1909
NeilBrown9d888832005-11-08 21:39:26 -08001910static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001911mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08001912{
1913 return sprintf(page, "%llu\n",
1914 (unsigned long long) mddev->resync_mismatches);
1915}
1916
NeilBrown96de1e62005-11-08 21:39:39 -08001917static struct md_sysfs_entry
NeilBrown7eec3142005-11-08 21:39:44 -08001918md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08001919
NeilBrown96de1e62005-11-08 21:39:39 -08001920
1921static struct md_sysfs_entry
1922md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08001923
NeilBrowneae17012005-11-08 21:39:23 -08001924static struct attribute *md_default_attrs[] = {
1925 &md_level.attr,
1926 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08001927 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08001928 &md_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08001929 NULL,
1930};
1931
1932static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08001933 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08001934 &md_mismatches.attr,
NeilBrowneae17012005-11-08 21:39:23 -08001935 NULL,
1936};
NeilBrown411036f2005-11-08 21:39:40 -08001937static struct attribute_group md_redundancy_group = {
1938 .name = NULL,
1939 .attrs = md_redundancy_attrs,
1940};
1941
NeilBrowneae17012005-11-08 21:39:23 -08001942
1943static ssize_t
1944md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1945{
1946 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1947 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08001948 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08001949
1950 if (!entry->show)
1951 return -EIO;
NeilBrown96de1e62005-11-08 21:39:39 -08001952 mddev_lock(mddev);
1953 rv = entry->show(mddev, page);
1954 mddev_unlock(mddev);
1955 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08001956}
1957
1958static ssize_t
1959md_attr_store(struct kobject *kobj, struct attribute *attr,
1960 const char *page, size_t length)
1961{
1962 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1963 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08001964 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08001965
1966 if (!entry->store)
1967 return -EIO;
NeilBrown96de1e62005-11-08 21:39:39 -08001968 mddev_lock(mddev);
1969 rv = entry->store(mddev, page, length);
1970 mddev_unlock(mddev);
1971 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08001972}
1973
1974static void md_free(struct kobject *ko)
1975{
1976 mddev_t *mddev = container_of(ko, mddev_t, kobj);
1977 kfree(mddev);
1978}
1979
1980static struct sysfs_ops md_sysfs_ops = {
1981 .show = md_attr_show,
1982 .store = md_attr_store,
1983};
1984static struct kobj_type md_ktype = {
1985 .release = md_free,
1986 .sysfs_ops = &md_sysfs_ops,
1987 .default_attrs = md_default_attrs,
1988};
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990int mdp_major = 0;
1991
1992static struct kobject *md_probe(dev_t dev, int *part, void *data)
1993{
1994 static DECLARE_MUTEX(disks_sem);
1995 mddev_t *mddev = mddev_find(dev);
1996 struct gendisk *disk;
1997 int partitioned = (MAJOR(dev) != MD_MAJOR);
1998 int shift = partitioned ? MdpMinorShift : 0;
1999 int unit = MINOR(dev) >> shift;
2000
2001 if (!mddev)
2002 return NULL;
2003
2004 down(&disks_sem);
2005 if (mddev->gendisk) {
2006 up(&disks_sem);
2007 mddev_put(mddev);
2008 return NULL;
2009 }
2010 disk = alloc_disk(1 << shift);
2011 if (!disk) {
2012 up(&disks_sem);
2013 mddev_put(mddev);
2014 return NULL;
2015 }
2016 disk->major = MAJOR(dev);
2017 disk->first_minor = unit << shift;
2018 if (partitioned) {
2019 sprintf(disk->disk_name, "md_d%d", unit);
2020 sprintf(disk->devfs_name, "md/d%d", unit);
2021 } else {
2022 sprintf(disk->disk_name, "md%d", unit);
2023 sprintf(disk->devfs_name, "md/%d", unit);
2024 }
2025 disk->fops = &md_fops;
2026 disk->private_data = mddev;
2027 disk->queue = mddev->queue;
2028 add_disk(disk);
2029 mddev->gendisk = disk;
2030 up(&disks_sem);
NeilBrown9c791972005-11-08 21:39:28 -08002031 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08002032 mddev->kobj.k_name = NULL;
2033 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2034 mddev->kobj.ktype = &md_ktype;
2035 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return NULL;
2037}
2038
2039void md_wakeup_thread(mdk_thread_t *thread);
2040
2041static void md_safemode_timeout(unsigned long data)
2042{
2043 mddev_t *mddev = (mddev_t *) data;
2044
2045 mddev->safemode = 1;
2046 md_wakeup_thread(mddev->thread);
2047}
2048
NeilBrown6ff8d8e2006-01-06 00:20:15 -08002049static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051static int do_md_run(mddev_t * mddev)
2052{
NeilBrown2604b702006-01-06 00:20:36 -08002053 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 int chunk_size;
2055 struct list_head *tmp;
2056 mdk_rdev_t *rdev;
2057 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08002058 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 char b[BDEVNAME_SIZE];
2060
NeilBrowna757e642005-04-16 15:26:42 -07002061 if (list_empty(&mddev->disks))
2062 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 if (mddev->pers)
2066 return -EBUSY;
2067
2068 /*
2069 * Analyze all RAID superblock(s)
2070 */
NeilBrowna757e642005-04-16 15:26:42 -07002071 if (!mddev->raid_disks)
2072 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
NeilBrown2604b702006-01-06 00:20:36 -08002076 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (chunk_size > MAX_CHUNK_SIZE) {
2078 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2079 chunk_size, MAX_CHUNK_SIZE);
2080 return -EINVAL;
2081 }
2082 /*
2083 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2084 */
2085 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07002086 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return -EINVAL;
2088 }
2089 if (chunk_size < PAGE_SIZE) {
2090 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2091 chunk_size, PAGE_SIZE);
2092 return -EINVAL;
2093 }
2094
2095 /* devices must have minimum size of one chunk */
2096 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002097 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 continue;
2099 if (rdev->size < chunk_size / 1024) {
2100 printk(KERN_WARNING
2101 "md: Dev %s smaller than chunk_size:"
2102 " %lluk < %dk\n",
2103 bdevname(rdev->bdev,b),
2104 (unsigned long long)rdev->size,
2105 chunk_size / 1024);
2106 return -EINVAL;
2107 }
2108 }
2109 }
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111#ifdef CONFIG_KMOD
NeilBrown2604b702006-01-06 00:20:36 -08002112 request_module("md-level-%d", mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113#endif
2114
2115 /*
2116 * Drop all container device buffers, from now on
2117 * the only valid external interface is through the md
2118 * device.
2119 * Also find largest hardsector size
2120 */
2121 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002122 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 continue;
2124 sync_blockdev(rdev->bdev);
2125 invalidate_bdev(rdev->bdev, 0);
2126 }
2127
2128 md_probe(mddev->unit, NULL, NULL);
2129 disk = mddev->gendisk;
2130 if (!disk)
2131 return -ENOMEM;
2132
2133 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08002134 pers = find_pers(mddev->level);
2135 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 spin_unlock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08002137 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
2138 mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return -EINVAL;
2140 }
NeilBrown2604b702006-01-06 00:20:36 -08002141 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 spin_unlock(&pers_lock);
2143
NeilBrown657390d2005-08-26 18:34:16 -07002144 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08002146 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08002147 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
NeilBrownf91de922005-11-08 21:39:36 -08002149 if (start_readonly)
2150 mddev->ro = 2; /* read-only, but switch on first write */
2151
NeilBrownb15c2e52006-01-06 00:20:16 -08002152 err = mddev->pers->run(mddev);
2153 if (!err && mddev->pers->sync_request) {
2154 err = bitmap_create(mddev);
2155 if (err) {
2156 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2157 mdname(mddev), err);
2158 mddev->pers->stop(mddev);
2159 }
2160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (err) {
2162 printk(KERN_ERR "md: pers->run() failed ...\n");
2163 module_put(mddev->pers->owner);
2164 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002165 bitmap_destroy(mddev);
2166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
NeilBrown411036f2005-11-08 21:39:40 -08002168 if (mddev->pers->sync_request)
2169 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08002170 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2171 mddev->ro = 0;
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 atomic_set(&mddev->writes_pending,0);
2174 mddev->safemode = 0;
2175 mddev->safemode_timer.function = md_safemode_timeout;
2176 mddev->safemode_timer.data = (unsigned long) mddev;
2177 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2178 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002179
2180 ITERATE_RDEV(mddev,rdev,tmp)
2181 if (rdev->raid_disk >= 0) {
2182 char nm[20];
2183 sprintf(nm, "rd%d", rdev->raid_disk);
2184 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002188 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 if (mddev->sb_dirty)
2191 md_update_sb(mddev);
2192
2193 set_capacity(disk, mddev->array_size<<1);
2194
2195 /* If we call blk_queue_make_request here, it will
2196 * re-initialise max_sectors etc which may have been
2197 * refined inside -> run. So just set the bits we need to set.
2198 * Most initialisation happended when we called
2199 * blk_queue_make_request(..., md_fail_request)
2200 * earlier.
2201 */
2202 mddev->queue->queuedata = mddev;
2203 mddev->queue->make_request_fn = mddev->pers->make_request;
2204
2205 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08002206 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return 0;
2208}
2209
2210static int restart_array(mddev_t *mddev)
2211{
2212 struct gendisk *disk = mddev->gendisk;
2213 int err;
2214
2215 /*
2216 * Complain if it has no devices
2217 */
2218 err = -ENXIO;
2219 if (list_empty(&mddev->disks))
2220 goto out;
2221
2222 if (mddev->pers) {
2223 err = -EBUSY;
2224 if (!mddev->ro)
2225 goto out;
2226
2227 mddev->safemode = 0;
2228 mddev->ro = 0;
2229 set_disk_ro(disk, 0);
2230
2231 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2232 mdname(mddev));
2233 /*
2234 * Kick recovery or resync if necessary
2235 */
2236 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2237 md_wakeup_thread(mddev->thread);
2238 err = 0;
2239 } else {
2240 printk(KERN_ERR "md: %s has no personality assigned.\n",
2241 mdname(mddev));
2242 err = -EINVAL;
2243 }
2244
2245out:
2246 return err;
2247}
2248
2249static int do_md_stop(mddev_t * mddev, int ro)
2250{
2251 int err = 0;
2252 struct gendisk *disk = mddev->gendisk;
2253
2254 if (mddev->pers) {
2255 if (atomic_read(&mddev->active)>2) {
2256 printk("md: %s still in use.\n",mdname(mddev));
2257 return -EBUSY;
2258 }
2259
2260 if (mddev->sync_thread) {
2261 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2262 md_unregister_thread(mddev->sync_thread);
2263 mddev->sync_thread = NULL;
2264 }
2265
2266 del_timer_sync(&mddev->safemode_timer);
2267
2268 invalidate_partition(disk, 0);
2269
2270 if (ro) {
2271 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08002272 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 goto out;
2274 mddev->ro = 1;
2275 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002276 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08002277 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (mddev->ro)
2279 set_disk_ro(disk, 0);
2280 blk_queue_make_request(mddev->queue, md_fail_request);
2281 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08002282 if (mddev->pers->sync_request)
2283 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 module_put(mddev->pers->owner);
2286 mddev->pers = NULL;
2287 if (mddev->ro)
2288 mddev->ro = 0;
2289 }
2290 if (!mddev->in_sync) {
2291 /* mark array as shutdown cleanly */
2292 mddev->in_sync = 1;
2293 md_update_sb(mddev);
2294 }
2295 if (ro)
2296 set_disk_ro(disk, 1);
2297 }
NeilBrown32a76272005-06-21 17:17:14 -07002298
2299 bitmap_destroy(mddev);
2300 if (mddev->bitmap_file) {
2301 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2302 fput(mddev->bitmap_file);
2303 mddev->bitmap_file = NULL;
2304 }
NeilBrown92232142005-08-18 11:24:16 -07002305 mddev->bitmap_offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07002306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 /*
2308 * Free resources if final stop
2309 */
2310 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002311 mdk_rdev_t *rdev;
2312 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 struct gendisk *disk;
2314 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2315
NeilBrown86e6ffd2005-11-08 21:39:24 -08002316 ITERATE_RDEV(mddev,rdev,tmp)
2317 if (rdev->raid_disk >= 0) {
2318 char nm[20];
2319 sprintf(nm, "rd%d", rdev->raid_disk);
2320 sysfs_remove_link(&mddev->kobj, nm);
2321 }
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 export_array(mddev);
2324
2325 mddev->array_size = 0;
2326 disk = mddev->gendisk;
2327 if (disk)
2328 set_capacity(disk, 0);
2329 mddev->changed = 1;
2330 } else
2331 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2332 mdname(mddev));
2333 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08002334 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335out:
2336 return err;
2337}
2338
2339static void autorun_array(mddev_t *mddev)
2340{
2341 mdk_rdev_t *rdev;
2342 struct list_head *tmp;
2343 int err;
2344
NeilBrowna757e642005-04-16 15:26:42 -07002345 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348 printk(KERN_INFO "md: running: ");
2349
2350 ITERATE_RDEV(mddev,rdev,tmp) {
2351 char b[BDEVNAME_SIZE];
2352 printk("<%s>", bdevname(rdev->bdev,b));
2353 }
2354 printk("\n");
2355
2356 err = do_md_run (mddev);
2357 if (err) {
2358 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2359 do_md_stop (mddev, 0);
2360 }
2361}
2362
2363/*
2364 * lets try to run arrays based on all disks that have arrived
2365 * until now. (those are in pending_raid_disks)
2366 *
2367 * the method: pick the first pending disk, collect all disks with
2368 * the same UUID, remove all from the pending list and put them into
2369 * the 'same_array' list. Then order this list based on superblock
2370 * update time (freshest comes first), kick out 'old' disks and
2371 * compare superblocks. If everything's fine then run it.
2372 *
2373 * If "unit" is allocated, then bump its reference count
2374 */
2375static void autorun_devices(int part)
2376{
2377 struct list_head candidates;
2378 struct list_head *tmp;
2379 mdk_rdev_t *rdev0, *rdev;
2380 mddev_t *mddev;
2381 char b[BDEVNAME_SIZE];
2382
2383 printk(KERN_INFO "md: autorun ...\n");
2384 while (!list_empty(&pending_raid_disks)) {
2385 dev_t dev;
2386 rdev0 = list_entry(pending_raid_disks.next,
2387 mdk_rdev_t, same_set);
2388
2389 printk(KERN_INFO "md: considering %s ...\n",
2390 bdevname(rdev0->bdev,b));
2391 INIT_LIST_HEAD(&candidates);
2392 ITERATE_RDEV_PENDING(rdev,tmp)
2393 if (super_90_load(rdev, rdev0, 0) >= 0) {
2394 printk(KERN_INFO "md: adding %s ...\n",
2395 bdevname(rdev->bdev,b));
2396 list_move(&rdev->same_set, &candidates);
2397 }
2398 /*
2399 * now we have a set of devices, with all of them having
2400 * mostly sane superblocks. It's time to allocate the
2401 * mddev.
2402 */
2403 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2404 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2405 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2406 break;
2407 }
2408 if (part)
2409 dev = MKDEV(mdp_major,
2410 rdev0->preferred_minor << MdpMinorShift);
2411 else
2412 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2413
2414 md_probe(dev, NULL, NULL);
2415 mddev = mddev_find(dev);
2416 if (!mddev) {
2417 printk(KERN_ERR
2418 "md: cannot allocate memory for md drive.\n");
2419 break;
2420 }
2421 if (mddev_lock(mddev))
2422 printk(KERN_WARNING "md: %s locked, cannot run\n",
2423 mdname(mddev));
2424 else if (mddev->raid_disks || mddev->major_version
2425 || !list_empty(&mddev->disks)) {
2426 printk(KERN_WARNING
2427 "md: %s already running, cannot run %s\n",
2428 mdname(mddev), bdevname(rdev0->bdev,b));
2429 mddev_unlock(mddev);
2430 } else {
2431 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2432 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2433 list_del_init(&rdev->same_set);
2434 if (bind_rdev_to_array(rdev, mddev))
2435 export_rdev(rdev);
2436 }
2437 autorun_array(mddev);
2438 mddev_unlock(mddev);
2439 }
2440 /* on success, candidates will be empty, on error
2441 * it won't...
2442 */
2443 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2444 export_rdev(rdev);
2445 mddev_put(mddev);
2446 }
2447 printk(KERN_INFO "md: ... autorun DONE.\n");
2448}
2449
2450/*
2451 * import RAID devices based on one partition
2452 * if possible, the array gets run as well.
2453 */
2454
2455static int autostart_array(dev_t startdev)
2456{
2457 char b[BDEVNAME_SIZE];
2458 int err = -EINVAL, i;
2459 mdp_super_t *sb = NULL;
2460 mdk_rdev_t *start_rdev = NULL, *rdev;
2461
2462 start_rdev = md_import_device(startdev, 0, 0);
2463 if (IS_ERR(start_rdev))
2464 return err;
2465
2466
2467 /* NOTE: this can only work for 0.90.0 superblocks */
2468 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2469 if (sb->major_version != 0 ||
2470 sb->minor_version != 90 ) {
2471 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2472 export_rdev(start_rdev);
2473 return err;
2474 }
2475
NeilBrownb2d444d2005-11-08 21:39:31 -08002476 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 printk(KERN_WARNING
2478 "md: can not autostart based on faulty %s!\n",
2479 bdevname(start_rdev->bdev,b));
2480 export_rdev(start_rdev);
2481 return err;
2482 }
2483 list_add(&start_rdev->same_set, &pending_raid_disks);
2484
2485 for (i = 0; i < MD_SB_DISKS; i++) {
2486 mdp_disk_t *desc = sb->disks + i;
2487 dev_t dev = MKDEV(desc->major, desc->minor);
2488
2489 if (!dev)
2490 continue;
2491 if (dev == startdev)
2492 continue;
2493 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2494 continue;
2495 rdev = md_import_device(dev, 0, 0);
2496 if (IS_ERR(rdev))
2497 continue;
2498
2499 list_add(&rdev->same_set, &pending_raid_disks);
2500 }
2501
2502 /*
2503 * possibly return codes
2504 */
2505 autorun_devices(0);
2506 return 0;
2507
2508}
2509
2510
2511static int get_version(void __user * arg)
2512{
2513 mdu_version_t ver;
2514
2515 ver.major = MD_MAJOR_VERSION;
2516 ver.minor = MD_MINOR_VERSION;
2517 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2518
2519 if (copy_to_user(arg, &ver, sizeof(ver)))
2520 return -EFAULT;
2521
2522 return 0;
2523}
2524
2525static int get_array_info(mddev_t * mddev, void __user * arg)
2526{
2527 mdu_array_info_t info;
2528 int nr,working,active,failed,spare;
2529 mdk_rdev_t *rdev;
2530 struct list_head *tmp;
2531
2532 nr=working=active=failed=spare=0;
2533 ITERATE_RDEV(mddev,rdev,tmp) {
2534 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002535 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 failed++;
2537 else {
2538 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002539 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 active++;
2541 else
2542 spare++;
2543 }
2544 }
2545
2546 info.major_version = mddev->major_version;
2547 info.minor_version = mddev->minor_version;
2548 info.patch_version = MD_PATCHLEVEL_VERSION;
2549 info.ctime = mddev->ctime;
2550 info.level = mddev->level;
2551 info.size = mddev->size;
2552 info.nr_disks = nr;
2553 info.raid_disks = mddev->raid_disks;
2554 info.md_minor = mddev->md_minor;
2555 info.not_persistent= !mddev->persistent;
2556
2557 info.utime = mddev->utime;
2558 info.state = 0;
2559 if (mddev->in_sync)
2560 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07002561 if (mddev->bitmap && mddev->bitmap_offset)
2562 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 info.active_disks = active;
2564 info.working_disks = working;
2565 info.failed_disks = failed;
2566 info.spare_disks = spare;
2567
2568 info.layout = mddev->layout;
2569 info.chunk_size = mddev->chunk_size;
2570
2571 if (copy_to_user(arg, &info, sizeof(info)))
2572 return -EFAULT;
2573
2574 return 0;
2575}
2576
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002577static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07002578{
2579 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2580 char *ptr, *buf = NULL;
2581 int err = -ENOMEM;
2582
2583 file = kmalloc(sizeof(*file), GFP_KERNEL);
2584 if (!file)
2585 goto out;
2586
2587 /* bitmap disabled, zero the first byte and copy out */
2588 if (!mddev->bitmap || !mddev->bitmap->file) {
2589 file->pathname[0] = '\0';
2590 goto copy_out;
2591 }
2592
2593 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2594 if (!buf)
2595 goto out;
2596
2597 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2598 if (!ptr)
2599 goto out;
2600
2601 strcpy(file->pathname, ptr);
2602
2603copy_out:
2604 err = 0;
2605 if (copy_to_user(arg, file, sizeof(*file)))
2606 err = -EFAULT;
2607out:
2608 kfree(buf);
2609 kfree(file);
2610 return err;
2611}
2612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613static int get_disk_info(mddev_t * mddev, void __user * arg)
2614{
2615 mdu_disk_info_t info;
2616 unsigned int nr;
2617 mdk_rdev_t *rdev;
2618
2619 if (copy_from_user(&info, arg, sizeof(info)))
2620 return -EFAULT;
2621
2622 nr = info.number;
2623
2624 rdev = find_rdev_nr(mddev, nr);
2625 if (rdev) {
2626 info.major = MAJOR(rdev->bdev->bd_dev);
2627 info.minor = MINOR(rdev->bdev->bd_dev);
2628 info.raid_disk = rdev->raid_disk;
2629 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08002630 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002632 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 info.state |= (1<<MD_DISK_ACTIVE);
2634 info.state |= (1<<MD_DISK_SYNC);
2635 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002636 if (test_bit(WriteMostly, &rdev->flags))
2637 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 } else {
2639 info.major = info.minor = 0;
2640 info.raid_disk = -1;
2641 info.state = (1<<MD_DISK_REMOVED);
2642 }
2643
2644 if (copy_to_user(arg, &info, sizeof(info)))
2645 return -EFAULT;
2646
2647 return 0;
2648}
2649
2650static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2651{
2652 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2653 mdk_rdev_t *rdev;
2654 dev_t dev = MKDEV(info->major,info->minor);
2655
2656 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2657 return -EOVERFLOW;
2658
2659 if (!mddev->raid_disks) {
2660 int err;
2661 /* expecting a device which has a superblock */
2662 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2663 if (IS_ERR(rdev)) {
2664 printk(KERN_WARNING
2665 "md: md_import_device returned %ld\n",
2666 PTR_ERR(rdev));
2667 return PTR_ERR(rdev);
2668 }
2669 if (!list_empty(&mddev->disks)) {
2670 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2671 mdk_rdev_t, same_set);
2672 int err = super_types[mddev->major_version]
2673 .load_super(rdev, rdev0, mddev->minor_version);
2674 if (err < 0) {
2675 printk(KERN_WARNING
2676 "md: %s has different UUID to %s\n",
2677 bdevname(rdev->bdev,b),
2678 bdevname(rdev0->bdev,b2));
2679 export_rdev(rdev);
2680 return -EINVAL;
2681 }
2682 }
2683 err = bind_rdev_to_array(rdev, mddev);
2684 if (err)
2685 export_rdev(rdev);
2686 return err;
2687 }
2688
2689 /*
2690 * add_new_disk can be used once the array is assembled
2691 * to add "hot spares". They must already have a superblock
2692 * written
2693 */
2694 if (mddev->pers) {
2695 int err;
2696 if (!mddev->pers->hot_add_disk) {
2697 printk(KERN_WARNING
2698 "%s: personality does not support diskops!\n",
2699 mdname(mddev));
2700 return -EINVAL;
2701 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07002702 if (mddev->persistent)
2703 rdev = md_import_device(dev, mddev->major_version,
2704 mddev->minor_version);
2705 else
2706 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (IS_ERR(rdev)) {
2708 printk(KERN_WARNING
2709 "md: md_import_device returned %ld\n",
2710 PTR_ERR(rdev));
2711 return PTR_ERR(rdev);
2712 }
NeilBrown41158c72005-06-21 17:17:25 -07002713 /* set save_raid_disk if appropriate */
2714 if (!mddev->persistent) {
2715 if (info->state & (1<<MD_DISK_SYNC) &&
2716 info->raid_disk < mddev->raid_disks)
2717 rdev->raid_disk = info->raid_disk;
2718 else
2719 rdev->raid_disk = -1;
2720 } else
2721 super_types[mddev->major_version].
2722 validate_super(mddev, rdev);
2723 rdev->saved_raid_disk = rdev->raid_disk;
2724
NeilBrownb2d444d2005-11-08 21:39:31 -08002725 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002726 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2727 set_bit(WriteMostly, &rdev->flags);
2728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 rdev->raid_disk = -1;
2730 err = bind_rdev_to_array(rdev, mddev);
2731 if (err)
2732 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07002733
2734 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002735 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 return err;
2737 }
2738
2739 /* otherwise, add_new_disk is only allowed
2740 * for major_version==0 superblocks
2741 */
2742 if (mddev->major_version != 0) {
2743 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2744 mdname(mddev));
2745 return -EINVAL;
2746 }
2747
2748 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2749 int err;
2750 rdev = md_import_device (dev, -1, 0);
2751 if (IS_ERR(rdev)) {
2752 printk(KERN_WARNING
2753 "md: error, md_import_device() returned %ld\n",
2754 PTR_ERR(rdev));
2755 return PTR_ERR(rdev);
2756 }
2757 rdev->desc_nr = info->number;
2758 if (info->raid_disk < mddev->raid_disks)
2759 rdev->raid_disk = info->raid_disk;
2760 else
2761 rdev->raid_disk = -1;
2762
NeilBrownb2d444d2005-11-08 21:39:31 -08002763 rdev->flags = 0;
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08002766 if (info->state & (1<<MD_DISK_SYNC))
2767 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002769 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2770 set_bit(WriteMostly, &rdev->flags);
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 err = bind_rdev_to_array(rdev, mddev);
2773 if (err) {
2774 export_rdev(rdev);
2775 return err;
2776 }
2777
2778 if (!mddev->persistent) {
2779 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2780 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2781 } else
2782 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2783 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2784
2785 if (!mddev->size || (mddev->size > rdev->size))
2786 mddev->size = rdev->size;
2787 }
2788
2789 return 0;
2790}
2791
2792static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2793{
2794 char b[BDEVNAME_SIZE];
2795 mdk_rdev_t *rdev;
2796
2797 if (!mddev->pers)
2798 return -ENODEV;
2799
2800 rdev = find_rdev(mddev, dev);
2801 if (!rdev)
2802 return -ENXIO;
2803
2804 if (rdev->raid_disk >= 0)
2805 goto busy;
2806
2807 kick_rdev_from_array(rdev);
2808 md_update_sb(mddev);
NeilBrownd7603b72006-01-06 00:20:30 -08002809 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 return 0;
2812busy:
2813 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2814 bdevname(rdev->bdev,b), mdname(mddev));
2815 return -EBUSY;
2816}
2817
2818static int hot_add_disk(mddev_t * mddev, dev_t dev)
2819{
2820 char b[BDEVNAME_SIZE];
2821 int err;
2822 unsigned int size;
2823 mdk_rdev_t *rdev;
2824
2825 if (!mddev->pers)
2826 return -ENODEV;
2827
2828 if (mddev->major_version != 0) {
2829 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2830 " version-0 superblocks.\n",
2831 mdname(mddev));
2832 return -EINVAL;
2833 }
2834 if (!mddev->pers->hot_add_disk) {
2835 printk(KERN_WARNING
2836 "%s: personality does not support diskops!\n",
2837 mdname(mddev));
2838 return -EINVAL;
2839 }
2840
2841 rdev = md_import_device (dev, -1, 0);
2842 if (IS_ERR(rdev)) {
2843 printk(KERN_WARNING
2844 "md: error, md_import_device() returned %ld\n",
2845 PTR_ERR(rdev));
2846 return -EINVAL;
2847 }
2848
2849 if (mddev->persistent)
2850 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2851 else
2852 rdev->sb_offset =
2853 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2854
2855 size = calc_dev_size(rdev, mddev->chunk_size);
2856 rdev->size = size;
2857
2858 if (size < mddev->size) {
2859 printk(KERN_WARNING
2860 "%s: disk size %llu blocks < array size %llu\n",
2861 mdname(mddev), (unsigned long long)size,
2862 (unsigned long long)mddev->size);
2863 err = -ENOSPC;
2864 goto abort_export;
2865 }
2866
NeilBrownb2d444d2005-11-08 21:39:31 -08002867 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 printk(KERN_WARNING
2869 "md: can not hot-add faulty %s disk to %s!\n",
2870 bdevname(rdev->bdev,b), mdname(mddev));
2871 err = -EINVAL;
2872 goto abort_export;
2873 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002874 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 rdev->desc_nr = -1;
2876 bind_rdev_to_array(rdev, mddev);
2877
2878 /*
2879 * The rest should better be atomic, we can have disk failures
2880 * noticed in interrupt contexts ...
2881 */
2882
2883 if (rdev->desc_nr == mddev->max_disks) {
2884 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2885 mdname(mddev));
2886 err = -EBUSY;
2887 goto abort_unbind_export;
2888 }
2889
2890 rdev->raid_disk = -1;
2891
2892 md_update_sb(mddev);
2893
2894 /*
2895 * Kick recovery, maybe this spare has to be added to the
2896 * array immediately.
2897 */
2898 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2899 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08002900 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return 0;
2902
2903abort_unbind_export:
2904 unbind_rdev_from_array(rdev);
2905
2906abort_export:
2907 export_rdev(rdev);
2908 return err;
2909}
2910
NeilBrown32a76272005-06-21 17:17:14 -07002911/* similar to deny_write_access, but accounts for our holding a reference
2912 * to the file ourselves */
2913static int deny_bitmap_write_access(struct file * file)
2914{
2915 struct inode *inode = file->f_mapping->host;
2916
2917 spin_lock(&inode->i_lock);
2918 if (atomic_read(&inode->i_writecount) > 1) {
2919 spin_unlock(&inode->i_lock);
2920 return -ETXTBSY;
2921 }
2922 atomic_set(&inode->i_writecount, -1);
2923 spin_unlock(&inode->i_lock);
2924
2925 return 0;
2926}
2927
2928static int set_bitmap_file(mddev_t *mddev, int fd)
2929{
2930 int err;
2931
NeilBrown36fa3062005-09-09 16:23:45 -07002932 if (mddev->pers) {
2933 if (!mddev->pers->quiesce)
2934 return -EBUSY;
2935 if (mddev->recovery || mddev->sync_thread)
2936 return -EBUSY;
2937 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07002938 }
2939
NeilBrown36fa3062005-09-09 16:23:45 -07002940
2941 if (fd >= 0) {
2942 if (mddev->bitmap)
2943 return -EEXIST; /* cannot add when bitmap is present */
2944 mddev->bitmap_file = fget(fd);
2945
2946 if (mddev->bitmap_file == NULL) {
2947 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2948 mdname(mddev));
2949 return -EBADF;
2950 }
2951
2952 err = deny_bitmap_write_access(mddev->bitmap_file);
2953 if (err) {
2954 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2955 mdname(mddev));
2956 fput(mddev->bitmap_file);
2957 mddev->bitmap_file = NULL;
2958 return err;
2959 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002960 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07002961 } else if (mddev->bitmap == NULL)
2962 return -ENOENT; /* cannot remove what isn't there */
2963 err = 0;
2964 if (mddev->pers) {
2965 mddev->pers->quiesce(mddev, 1);
2966 if (fd >= 0)
2967 err = bitmap_create(mddev);
2968 if (fd < 0 || err)
2969 bitmap_destroy(mddev);
2970 mddev->pers->quiesce(mddev, 0);
2971 } else if (fd < 0) {
2972 if (mddev->bitmap_file)
2973 fput(mddev->bitmap_file);
2974 mddev->bitmap_file = NULL;
2975 }
2976
NeilBrown32a76272005-06-21 17:17:14 -07002977 return err;
2978}
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980/*
2981 * set_array_info is used two different ways
2982 * The original usage is when creating a new array.
2983 * In this usage, raid_disks is > 0 and it together with
2984 * level, size, not_persistent,layout,chunksize determine the
2985 * shape of the array.
2986 * This will always create an array with a type-0.90.0 superblock.
2987 * The newer usage is when assembling an array.
2988 * In this case raid_disks will be 0, and the major_version field is
2989 * use to determine which style super-blocks are to be found on the devices.
2990 * The minor and patch _version numbers are also kept incase the
2991 * super_block handler wishes to interpret them.
2992 */
2993static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2994{
2995
2996 if (info->raid_disks == 0) {
2997 /* just setting version number for superblock loading */
2998 if (info->major_version < 0 ||
2999 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3000 super_types[info->major_version].name == NULL) {
3001 /* maybe try to auto-load a module? */
3002 printk(KERN_INFO
3003 "md: superblock version %d not known\n",
3004 info->major_version);
3005 return -EINVAL;
3006 }
3007 mddev->major_version = info->major_version;
3008 mddev->minor_version = info->minor_version;
3009 mddev->patch_version = info->patch_version;
3010 return 0;
3011 }
3012 mddev->major_version = MD_MAJOR_VERSION;
3013 mddev->minor_version = MD_MINOR_VERSION;
3014 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3015 mddev->ctime = get_seconds();
3016
3017 mddev->level = info->level;
3018 mddev->size = info->size;
3019 mddev->raid_disks = info->raid_disks;
3020 /* don't set md_minor, it is determined by which /dev/md* was
3021 * openned
3022 */
3023 if (info->state & (1<<MD_SB_CLEAN))
3024 mddev->recovery_cp = MaxSector;
3025 else
3026 mddev->recovery_cp = 0;
3027 mddev->persistent = ! info->not_persistent;
3028
3029 mddev->layout = info->layout;
3030 mddev->chunk_size = info->chunk_size;
3031
3032 mddev->max_disks = MD_SB_DISKS;
3033
3034 mddev->sb_dirty = 1;
3035
NeilBrownb2a27032005-11-28 13:44:12 -08003036 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
3037 mddev->bitmap_offset = 0;
3038
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 /*
3040 * Generate a 128 bit UUID
3041 */
3042 get_random_bytes(mddev->uuid, 16);
3043
3044 return 0;
3045}
3046
NeilBrowna35b0d62006-01-06 00:20:49 -08003047static int update_size(mddev_t *mddev, unsigned long size)
3048{
3049 mdk_rdev_t * rdev;
3050 int rv;
3051 struct list_head *tmp;
3052
3053 if (mddev->pers->resize == NULL)
3054 return -EINVAL;
3055 /* The "size" is the amount of each device that is used.
3056 * This can only make sense for arrays with redundancy.
3057 * linear and raid0 always use whatever space is available
3058 * We can only consider changing the size if no resync
3059 * or reconstruction is happening, and if the new size
3060 * is acceptable. It must fit before the sb_offset or,
3061 * if that is <data_offset, it must fit before the
3062 * size of each device.
3063 * If size is zero, we find the largest size that fits.
3064 */
3065 if (mddev->sync_thread)
3066 return -EBUSY;
3067 ITERATE_RDEV(mddev,rdev,tmp) {
3068 sector_t avail;
3069 int fit = (size == 0);
3070 if (rdev->sb_offset > rdev->data_offset)
3071 avail = (rdev->sb_offset*2) - rdev->data_offset;
3072 else
3073 avail = get_capacity(rdev->bdev->bd_disk)
3074 - rdev->data_offset;
3075 if (fit && (size == 0 || size > avail/2))
3076 size = avail/2;
3077 if (avail < ((sector_t)size << 1))
3078 return -ENOSPC;
3079 }
3080 rv = mddev->pers->resize(mddev, (sector_t)size *2);
3081 if (!rv) {
3082 struct block_device *bdev;
3083
3084 bdev = bdget_disk(mddev->gendisk, 0);
3085 if (bdev) {
3086 down(&bdev->bd_inode->i_sem);
3087 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3088 up(&bdev->bd_inode->i_sem);
3089 bdput(bdev);
3090 }
3091 }
3092 return rv;
3093}
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095/*
3096 * update_array_info is used to change the configuration of an
3097 * on-line array.
3098 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
3099 * fields in the info are checked against the array.
3100 * Any differences that cannot be handled will cause an error.
3101 * Normally, only one change can be managed at a time.
3102 */
3103static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
3104{
3105 int rv = 0;
3106 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07003107 int state = 0;
3108
3109 /* calculate expected state,ignoring low bits */
3110 if (mddev->bitmap && mddev->bitmap_offset)
3111 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113 if (mddev->major_version != info->major_version ||
3114 mddev->minor_version != info->minor_version ||
3115/* mddev->patch_version != info->patch_version || */
3116 mddev->ctime != info->ctime ||
3117 mddev->level != info->level ||
3118/* mddev->layout != info->layout || */
3119 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07003120 mddev->chunk_size != info->chunk_size ||
3121 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
3122 ((state^info->state) & 0xfffffe00)
3123 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return -EINVAL;
3125 /* Check there is only one change */
3126 if (mddev->size != info->size) cnt++;
3127 if (mddev->raid_disks != info->raid_disks) cnt++;
3128 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07003129 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 if (cnt == 0) return 0;
3131 if (cnt > 1) return -EINVAL;
3132
3133 if (mddev->layout != info->layout) {
3134 /* Change layout
3135 * we don't need to do anything at the md level, the
3136 * personality will take care of it all.
3137 */
3138 if (mddev->pers->reconfig == NULL)
3139 return -EINVAL;
3140 else
3141 return mddev->pers->reconfig(mddev, info->layout, -1);
3142 }
NeilBrowna35b0d62006-01-06 00:20:49 -08003143 if (mddev->size != info->size)
3144 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 if (mddev->raid_disks != info->raid_disks) {
3147 /* change the number of raid disks */
3148 if (mddev->pers->reshape == NULL)
3149 return -EINVAL;
3150 if (info->raid_disks <= 0 ||
3151 info->raid_disks >= mddev->max_disks)
3152 return -EINVAL;
3153 if (mddev->sync_thread)
3154 return -EBUSY;
3155 rv = mddev->pers->reshape(mddev, info->raid_disks);
3156 if (!rv) {
3157 struct block_device *bdev;
3158
3159 bdev = bdget_disk(mddev->gendisk, 0);
3160 if (bdev) {
3161 down(&bdev->bd_inode->i_sem);
3162 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3163 up(&bdev->bd_inode->i_sem);
3164 bdput(bdev);
3165 }
3166 }
3167 }
NeilBrown36fa3062005-09-09 16:23:45 -07003168 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3169 if (mddev->pers->quiesce == NULL)
3170 return -EINVAL;
3171 if (mddev->recovery || mddev->sync_thread)
3172 return -EBUSY;
3173 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3174 /* add the bitmap */
3175 if (mddev->bitmap)
3176 return -EEXIST;
3177 if (mddev->default_bitmap_offset == 0)
3178 return -EINVAL;
3179 mddev->bitmap_offset = mddev->default_bitmap_offset;
3180 mddev->pers->quiesce(mddev, 1);
3181 rv = bitmap_create(mddev);
3182 if (rv)
3183 bitmap_destroy(mddev);
3184 mddev->pers->quiesce(mddev, 0);
3185 } else {
3186 /* remove the bitmap */
3187 if (!mddev->bitmap)
3188 return -ENOENT;
3189 if (mddev->bitmap->file)
3190 return -EINVAL;
3191 mddev->pers->quiesce(mddev, 1);
3192 bitmap_destroy(mddev);
3193 mddev->pers->quiesce(mddev, 0);
3194 mddev->bitmap_offset = 0;
3195 }
3196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 md_update_sb(mddev);
3198 return rv;
3199}
3200
3201static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3202{
3203 mdk_rdev_t *rdev;
3204
3205 if (mddev->pers == NULL)
3206 return -ENODEV;
3207
3208 rdev = find_rdev(mddev, dev);
3209 if (!rdev)
3210 return -ENODEV;
3211
3212 md_error(mddev, rdev);
3213 return 0;
3214}
3215
3216static int md_ioctl(struct inode *inode, struct file *file,
3217 unsigned int cmd, unsigned long arg)
3218{
3219 int err = 0;
3220 void __user *argp = (void __user *)arg;
3221 struct hd_geometry __user *loc = argp;
3222 mddev_t *mddev = NULL;
3223
3224 if (!capable(CAP_SYS_ADMIN))
3225 return -EACCES;
3226
3227 /*
3228 * Commands dealing with the RAID driver but not any
3229 * particular array:
3230 */
3231 switch (cmd)
3232 {
3233 case RAID_VERSION:
3234 err = get_version(argp);
3235 goto done;
3236
3237 case PRINT_RAID_DEBUG:
3238 err = 0;
3239 md_print_devices();
3240 goto done;
3241
3242#ifndef MODULE
3243 case RAID_AUTORUN:
3244 err = 0;
3245 autostart_arrays(arg);
3246 goto done;
3247#endif
3248 default:;
3249 }
3250
3251 /*
3252 * Commands creating/starting a new array:
3253 */
3254
3255 mddev = inode->i_bdev->bd_disk->private_data;
3256
3257 if (!mddev) {
3258 BUG();
3259 goto abort;
3260 }
3261
3262
3263 if (cmd == START_ARRAY) {
3264 /* START_ARRAY doesn't need to lock the array as autostart_array
3265 * does the locking, and it could even be a different array
3266 */
3267 static int cnt = 3;
3268 if (cnt > 0 ) {
3269 printk(KERN_WARNING
3270 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08003271 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 current->comm, current->pid);
3273 cnt--;
3274 }
3275 err = autostart_array(new_decode_dev(arg));
3276 if (err) {
3277 printk(KERN_WARNING "md: autostart failed!\n");
3278 goto abort;
3279 }
3280 goto done;
3281 }
3282
3283 err = mddev_lock(mddev);
3284 if (err) {
3285 printk(KERN_INFO
3286 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3287 err, cmd);
3288 goto abort;
3289 }
3290
3291 switch (cmd)
3292 {
3293 case SET_ARRAY_INFO:
3294 {
3295 mdu_array_info_t info;
3296 if (!arg)
3297 memset(&info, 0, sizeof(info));
3298 else if (copy_from_user(&info, argp, sizeof(info))) {
3299 err = -EFAULT;
3300 goto abort_unlock;
3301 }
3302 if (mddev->pers) {
3303 err = update_array_info(mddev, &info);
3304 if (err) {
3305 printk(KERN_WARNING "md: couldn't update"
3306 " array info. %d\n", err);
3307 goto abort_unlock;
3308 }
3309 goto done_unlock;
3310 }
3311 if (!list_empty(&mddev->disks)) {
3312 printk(KERN_WARNING
3313 "md: array %s already has disks!\n",
3314 mdname(mddev));
3315 err = -EBUSY;
3316 goto abort_unlock;
3317 }
3318 if (mddev->raid_disks) {
3319 printk(KERN_WARNING
3320 "md: array %s already initialised!\n",
3321 mdname(mddev));
3322 err = -EBUSY;
3323 goto abort_unlock;
3324 }
3325 err = set_array_info(mddev, &info);
3326 if (err) {
3327 printk(KERN_WARNING "md: couldn't set"
3328 " array info. %d\n", err);
3329 goto abort_unlock;
3330 }
3331 }
3332 goto done_unlock;
3333
3334 default:;
3335 }
3336
3337 /*
3338 * Commands querying/configuring an existing array:
3339 */
NeilBrown32a76272005-06-21 17:17:14 -07003340 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3341 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3342 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3343 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 err = -ENODEV;
3345 goto abort_unlock;
3346 }
3347
3348 /*
3349 * Commands even a read-only array can execute:
3350 */
3351 switch (cmd)
3352 {
3353 case GET_ARRAY_INFO:
3354 err = get_array_info(mddev, argp);
3355 goto done_unlock;
3356
NeilBrown32a76272005-06-21 17:17:14 -07003357 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003358 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003359 goto done_unlock;
3360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 case GET_DISK_INFO:
3362 err = get_disk_info(mddev, argp);
3363 goto done_unlock;
3364
3365 case RESTART_ARRAY_RW:
3366 err = restart_array(mddev);
3367 goto done_unlock;
3368
3369 case STOP_ARRAY:
3370 err = do_md_stop (mddev, 0);
3371 goto done_unlock;
3372
3373 case STOP_ARRAY_RO:
3374 err = do_md_stop (mddev, 1);
3375 goto done_unlock;
3376
3377 /*
3378 * We have a problem here : there is no easy way to give a CHS
3379 * virtual geometry. We currently pretend that we have a 2 heads
3380 * 4 sectors (with a BIG number of cylinders...). This drives
3381 * dosfs just mad... ;-)
3382 */
3383 case HDIO_GETGEO:
3384 if (!loc) {
3385 err = -EINVAL;
3386 goto abort_unlock;
3387 }
3388 err = put_user (2, (char __user *) &loc->heads);
3389 if (err)
3390 goto abort_unlock;
3391 err = put_user (4, (char __user *) &loc->sectors);
3392 if (err)
3393 goto abort_unlock;
3394 err = put_user(get_capacity(mddev->gendisk)/8,
3395 (short __user *) &loc->cylinders);
3396 if (err)
3397 goto abort_unlock;
3398 err = put_user (get_start_sect(inode->i_bdev),
3399 (long __user *) &loc->start);
3400 goto done_unlock;
3401 }
3402
3403 /*
3404 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08003405 * superblock, so we do not allow them on read-only arrays.
3406 * However non-MD ioctls (e.g. get-size) will still come through
3407 * here and hit the 'default' below, so only disallow
3408 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 */
NeilBrownf91de922005-11-08 21:39:36 -08003410 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3411 mddev->ro && mddev->pers) {
3412 if (mddev->ro == 2) {
3413 mddev->ro = 0;
3414 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3415 md_wakeup_thread(mddev->thread);
3416
3417 } else {
3418 err = -EROFS;
3419 goto abort_unlock;
3420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 }
3422
3423 switch (cmd)
3424 {
3425 case ADD_NEW_DISK:
3426 {
3427 mdu_disk_info_t info;
3428 if (copy_from_user(&info, argp, sizeof(info)))
3429 err = -EFAULT;
3430 else
3431 err = add_new_disk(mddev, &info);
3432 goto done_unlock;
3433 }
3434
3435 case HOT_REMOVE_DISK:
3436 err = hot_remove_disk(mddev, new_decode_dev(arg));
3437 goto done_unlock;
3438
3439 case HOT_ADD_DISK:
3440 err = hot_add_disk(mddev, new_decode_dev(arg));
3441 goto done_unlock;
3442
3443 case SET_DISK_FAULTY:
3444 err = set_disk_faulty(mddev, new_decode_dev(arg));
3445 goto done_unlock;
3446
3447 case RUN_ARRAY:
3448 err = do_md_run (mddev);
3449 goto done_unlock;
3450
NeilBrown32a76272005-06-21 17:17:14 -07003451 case SET_BITMAP_FILE:
3452 err = set_bitmap_file(mddev, (int)arg);
3453 goto done_unlock;
3454
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 default:
3456 if (_IOC_TYPE(cmd) == MD_MAJOR)
3457 printk(KERN_WARNING "md: %s(pid %d) used"
3458 " obsolete MD ioctl, upgrade your"
3459 " software to use new ictls.\n",
3460 current->comm, current->pid);
3461 err = -EINVAL;
3462 goto abort_unlock;
3463 }
3464
3465done_unlock:
3466abort_unlock:
3467 mddev_unlock(mddev);
3468
3469 return err;
3470done:
3471 if (err)
3472 MD_BUG();
3473abort:
3474 return err;
3475}
3476
3477static int md_open(struct inode *inode, struct file *file)
3478{
3479 /*
3480 * Succeed if we can lock the mddev, which confirms that
3481 * it isn't being stopped right now.
3482 */
3483 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3484 int err;
3485
3486 if ((err = mddev_lock(mddev)))
3487 goto out;
3488
3489 err = 0;
3490 mddev_get(mddev);
3491 mddev_unlock(mddev);
3492
3493 check_disk_change(inode->i_bdev);
3494 out:
3495 return err;
3496}
3497
3498static int md_release(struct inode *inode, struct file * file)
3499{
3500 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3501
3502 if (!mddev)
3503 BUG();
3504 mddev_put(mddev);
3505
3506 return 0;
3507}
3508
3509static int md_media_changed(struct gendisk *disk)
3510{
3511 mddev_t *mddev = disk->private_data;
3512
3513 return mddev->changed;
3514}
3515
3516static int md_revalidate(struct gendisk *disk)
3517{
3518 mddev_t *mddev = disk->private_data;
3519
3520 mddev->changed = 0;
3521 return 0;
3522}
3523static struct block_device_operations md_fops =
3524{
3525 .owner = THIS_MODULE,
3526 .open = md_open,
3527 .release = md_release,
3528 .ioctl = md_ioctl,
3529 .media_changed = md_media_changed,
3530 .revalidate_disk= md_revalidate,
3531};
3532
Adrian Bunk75c96f82005-05-05 16:16:09 -07003533static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
3535 mdk_thread_t *thread = arg;
3536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 /*
3538 * md_thread is a 'system-thread', it's priority should be very
3539 * high. We avoid resource deadlocks individually in each
3540 * raid personality. (RAID5 does preallocation) We also use RR and
3541 * the very same RT priority as kswapd, thus we will never get
3542 * into a priority inversion deadlock.
3543 *
3544 * we definitely have to have equal or higher priority than
3545 * bdflush, otherwise bdflush will deadlock if there are too
3546 * many dirty RAID5 blocks.
3547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
NeilBrown6985c432005-10-19 21:23:47 -07003549 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07003550 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
NeilBrown93588e22005-11-15 00:09:12 -08003552 /* We need to wait INTERRUPTIBLE so that
3553 * we don't add to the load-average.
3554 * That means we need to be sure no signals are
3555 * pending
3556 */
3557 if (signal_pending(current))
3558 flush_signals(current);
3559
3560 wait_event_interruptible_timeout
3561 (thread->wqueue,
3562 test_bit(THREAD_WAKEUP, &thread->flags)
3563 || kthread_should_stop(),
3564 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003565 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
3567 clear_bit(THREAD_WAKEUP, &thread->flags);
3568
NeilBrown787453c2005-11-08 21:39:43 -08003569 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
NeilBrowna6fb0932005-09-09 16:23:56 -07003571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 return 0;
3573}
3574
3575void md_wakeup_thread(mdk_thread_t *thread)
3576{
3577 if (thread) {
3578 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3579 set_bit(THREAD_WAKEUP, &thread->flags);
3580 wake_up(&thread->wqueue);
3581 }
3582}
3583
3584mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3585 const char *name)
3586{
3587 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
NeilBrown9ffae0c2006-01-06 00:20:32 -08003589 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 if (!thread)
3591 return NULL;
3592
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 init_waitqueue_head(&thread->wqueue);
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 thread->run = run;
3596 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07003597 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07003598 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07003599 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 kfree(thread);
3601 return NULL;
3602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 return thread;
3604}
3605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606void md_unregister_thread(mdk_thread_t *thread)
3607{
NeilBrownd28446f2005-04-16 15:26:41 -07003608 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07003609
3610 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 kfree(thread);
3612}
3613
3614void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3615{
3616 if (!mddev) {
3617 MD_BUG();
3618 return;
3619 }
3620
NeilBrownb2d444d2005-11-08 21:39:31 -08003621 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 return;
NeilBrown32a76272005-06-21 17:17:14 -07003623/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3625 mdname(mddev),
3626 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3627 __builtin_return_address(0),__builtin_return_address(1),
3628 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07003629*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 if (!mddev->pers->error_handler)
3631 return;
3632 mddev->pers->error_handler(mddev,rdev);
3633 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3634 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3635 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08003636 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637}
3638
3639/* seq_file implementation /proc/mdstat */
3640
3641static void status_unused(struct seq_file *seq)
3642{
3643 int i = 0;
3644 mdk_rdev_t *rdev;
3645 struct list_head *tmp;
3646
3647 seq_printf(seq, "unused devices: ");
3648
3649 ITERATE_RDEV_PENDING(rdev,tmp) {
3650 char b[BDEVNAME_SIZE];
3651 i++;
3652 seq_printf(seq, "%s ",
3653 bdevname(rdev->bdev,b));
3654 }
3655 if (!i)
3656 seq_printf(seq, "<none>");
3657
3658 seq_printf(seq, "\n");
3659}
3660
3661
3662static void status_resync(struct seq_file *seq, mddev_t * mddev)
3663{
3664 unsigned long max_blocks, resync, res, dt, db, rt;
3665
3666 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3667
3668 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3669 max_blocks = mddev->resync_max_sectors >> 1;
3670 else
3671 max_blocks = mddev->size;
3672
3673 /*
3674 * Should not happen.
3675 */
3676 if (!max_blocks) {
3677 MD_BUG();
3678 return;
3679 }
3680 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3681 {
3682 int i, x = res/50, y = 20-x;
3683 seq_printf(seq, "[");
3684 for (i = 0; i < x; i++)
3685 seq_printf(seq, "=");
3686 seq_printf(seq, ">");
3687 for (i = 0; i < y; i++)
3688 seq_printf(seq, ".");
3689 seq_printf(seq, "] ");
3690 }
3691 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3692 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3693 "resync" : "recovery"),
3694 res/10, res % 10, resync, max_blocks);
3695
3696 /*
3697 * We do not want to overflow, so the order of operands and
3698 * the * 100 / 100 trick are important. We do a +1 to be
3699 * safe against division by zero. We only estimate anyway.
3700 *
3701 * dt: time from mark until now
3702 * db: blocks written from mark until now
3703 * rt: remaining time
3704 */
3705 dt = ((jiffies - mddev->resync_mark) / HZ);
3706 if (!dt) dt++;
3707 db = resync - (mddev->resync_mark_cnt/2);
3708 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3709
3710 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3711
3712 seq_printf(seq, " speed=%ldK/sec", db/dt);
3713}
3714
3715static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3716{
3717 struct list_head *tmp;
3718 loff_t l = *pos;
3719 mddev_t *mddev;
3720
3721 if (l >= 0x10000)
3722 return NULL;
3723 if (!l--)
3724 /* header */
3725 return (void*)1;
3726
3727 spin_lock(&all_mddevs_lock);
3728 list_for_each(tmp,&all_mddevs)
3729 if (!l--) {
3730 mddev = list_entry(tmp, mddev_t, all_mddevs);
3731 mddev_get(mddev);
3732 spin_unlock(&all_mddevs_lock);
3733 return mddev;
3734 }
3735 spin_unlock(&all_mddevs_lock);
3736 if (!l--)
3737 return (void*)2;/* tail */
3738 return NULL;
3739}
3740
3741static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3742{
3743 struct list_head *tmp;
3744 mddev_t *next_mddev, *mddev = v;
3745
3746 ++*pos;
3747 if (v == (void*)2)
3748 return NULL;
3749
3750 spin_lock(&all_mddevs_lock);
3751 if (v == (void*)1)
3752 tmp = all_mddevs.next;
3753 else
3754 tmp = mddev->all_mddevs.next;
3755 if (tmp != &all_mddevs)
3756 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3757 else {
3758 next_mddev = (void*)2;
3759 *pos = 0x10000;
3760 }
3761 spin_unlock(&all_mddevs_lock);
3762
3763 if (v != (void*)1)
3764 mddev_put(mddev);
3765 return next_mddev;
3766
3767}
3768
3769static void md_seq_stop(struct seq_file *seq, void *v)
3770{
3771 mddev_t *mddev = v;
3772
3773 if (mddev && v != (void*)1 && v != (void*)2)
3774 mddev_put(mddev);
3775}
3776
NeilBrownd7603b72006-01-06 00:20:30 -08003777struct mdstat_info {
3778 int event;
3779};
3780
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781static int md_seq_show(struct seq_file *seq, void *v)
3782{
3783 mddev_t *mddev = v;
3784 sector_t size;
3785 struct list_head *tmp2;
3786 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08003787 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07003788 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08003791 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 seq_printf(seq, "Personalities : ");
3793 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08003794 list_for_each_entry(pers, &pers_list, list)
3795 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
3797 spin_unlock(&pers_lock);
3798 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08003799 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 return 0;
3801 }
3802 if (v == (void*)2) {
3803 status_unused(seq);
3804 return 0;
3805 }
3806
3807 if (mddev_lock(mddev)!=0)
3808 return -EINTR;
3809 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3810 seq_printf(seq, "%s : %sactive", mdname(mddev),
3811 mddev->pers ? "" : "in");
3812 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08003813 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08003815 if (mddev->ro==2)
3816 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 seq_printf(seq, " %s", mddev->pers->name);
3818 }
3819
3820 size = 0;
3821 ITERATE_RDEV(mddev,rdev,tmp2) {
3822 char b[BDEVNAME_SIZE];
3823 seq_printf(seq, " %s[%d]",
3824 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003825 if (test_bit(WriteMostly, &rdev->flags))
3826 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08003827 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 seq_printf(seq, "(F)");
3829 continue;
NeilBrownb325a322005-09-09 16:24:00 -07003830 } else if (rdev->raid_disk < 0)
3831 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 size += rdev->size;
3833 }
3834
3835 if (!list_empty(&mddev->disks)) {
3836 if (mddev->pers)
3837 seq_printf(seq, "\n %llu blocks",
3838 (unsigned long long)mddev->array_size);
3839 else
3840 seq_printf(seq, "\n %llu blocks",
3841 (unsigned long long)size);
3842 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07003843 if (mddev->persistent) {
3844 if (mddev->major_version != 0 ||
3845 mddev->minor_version != 90) {
3846 seq_printf(seq," super %d.%d",
3847 mddev->major_version,
3848 mddev->minor_version);
3849 }
3850 } else
3851 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
3853 if (mddev->pers) {
3854 mddev->pers->status (seq, mddev);
3855 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08003856 if (mddev->pers->sync_request) {
3857 if (mddev->curr_resync > 2) {
3858 status_resync (seq, mddev);
3859 seq_printf(seq, "\n ");
3860 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3861 seq_printf(seq, "\tresync=DELAYED\n ");
3862 else if (mddev->recovery_cp < MaxSector)
3863 seq_printf(seq, "\tresync=PENDING\n ");
3864 }
NeilBrown32a76272005-06-21 17:17:14 -07003865 } else
3866 seq_printf(seq, "\n ");
3867
3868 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07003869 unsigned long chunk_kb;
3870 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07003871 spin_lock_irqsave(&bitmap->lock, flags);
3872 chunk_kb = bitmap->chunksize >> 10;
3873 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3874 "%lu%s chunk",
3875 bitmap->pages - bitmap->missing_pages,
3876 bitmap->pages,
3877 (bitmap->pages - bitmap->missing_pages)
3878 << (PAGE_SHIFT - 10),
3879 chunk_kb ? chunk_kb : bitmap->chunksize,
3880 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07003881 if (bitmap->file) {
3882 seq_printf(seq, ", file: ");
3883 seq_path(seq, bitmap->file->f_vfsmnt,
3884 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07003885 }
NeilBrown78d742d2005-06-21 17:17:15 -07003886
NeilBrown32a76272005-06-21 17:17:14 -07003887 seq_printf(seq, "\n");
3888 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 }
3890
3891 seq_printf(seq, "\n");
3892 }
3893 mddev_unlock(mddev);
3894
3895 return 0;
3896}
3897
3898static struct seq_operations md_seq_ops = {
3899 .start = md_seq_start,
3900 .next = md_seq_next,
3901 .stop = md_seq_stop,
3902 .show = md_seq_show,
3903};
3904
3905static int md_seq_open(struct inode *inode, struct file *file)
3906{
3907 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08003908 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
3909 if (mi == NULL)
3910 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
3912 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08003913 if (error)
3914 kfree(mi);
3915 else {
3916 struct seq_file *p = file->private_data;
3917 p->private = mi;
3918 mi->event = atomic_read(&md_event_count);
3919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 return error;
3921}
3922
NeilBrownd7603b72006-01-06 00:20:30 -08003923static int md_seq_release(struct inode *inode, struct file *file)
3924{
3925 struct seq_file *m = file->private_data;
3926 struct mdstat_info *mi = m->private;
3927 m->private = NULL;
3928 kfree(mi);
3929 return seq_release(inode, file);
3930}
3931
3932static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
3933{
3934 struct seq_file *m = filp->private_data;
3935 struct mdstat_info *mi = m->private;
3936 int mask;
3937
3938 poll_wait(filp, &md_event_waiters, wait);
3939
3940 /* always allow read */
3941 mask = POLLIN | POLLRDNORM;
3942
3943 if (mi->event != atomic_read(&md_event_count))
3944 mask |= POLLERR | POLLPRI;
3945 return mask;
3946}
3947
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948static struct file_operations md_seq_fops = {
3949 .open = md_seq_open,
3950 .read = seq_read,
3951 .llseek = seq_lseek,
NeilBrownd7603b72006-01-06 00:20:30 -08003952 .release = md_seq_release,
3953 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954};
3955
NeilBrown2604b702006-01-06 00:20:36 -08003956int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08003959 list_add_tail(&p->list, &pers_list);
3960 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 spin_unlock(&pers_lock);
3962 return 0;
3963}
3964
NeilBrown2604b702006-01-06 00:20:36 -08003965int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
NeilBrown2604b702006-01-06 00:20:36 -08003967 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08003969 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 spin_unlock(&pers_lock);
3971 return 0;
3972}
3973
3974static int is_mddev_idle(mddev_t *mddev)
3975{
3976 mdk_rdev_t * rdev;
3977 struct list_head *tmp;
3978 int idle;
3979 unsigned long curr_events;
3980
3981 idle = 1;
3982 ITERATE_RDEV(mddev,rdev,tmp) {
3983 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01003984 curr_events = disk_stat_read(disk, sectors[0]) +
3985 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08003987 /* The difference between curr_events and last_events
3988 * will be affected by any new non-sync IO (making
3989 * curr_events bigger) and any difference in the amount of
3990 * in-flight syncio (making current_events bigger or smaller)
3991 * The amount in-flight is currently limited to
3992 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
3993 * which is at most 4096 sectors.
3994 * These numbers are fairly fragile and should be made
3995 * more robust, probably by enforcing the
3996 * 'window size' that md_do_sync sort-of uses.
3997 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 * Note: the following is an unsigned comparison.
3999 */
NeilBrownc0e48522005-11-18 01:11:01 -08004000 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 rdev->last_events = curr_events;
4002 idle = 0;
4003 }
4004 }
4005 return idle;
4006}
4007
4008void md_done_sync(mddev_t *mddev, int blocks, int ok)
4009{
4010 /* another "blocks" (512byte) blocks have been synced */
4011 atomic_sub(blocks, &mddev->recovery_active);
4012 wake_up(&mddev->recovery_wait);
4013 if (!ok) {
4014 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4015 md_wakeup_thread(mddev->thread);
4016 // stop recovery, signal do_sync ....
4017 }
4018}
4019
4020
NeilBrown06d91a52005-06-21 17:17:12 -07004021/* md_write_start(mddev, bi)
4022 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07004023 * in superblock) before writing, schedule a superblock update
4024 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07004025 */
NeilBrown3d310eb2005-06-21 17:17:26 -07004026void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
NeilBrown06d91a52005-06-21 17:17:12 -07004028 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07004029 return;
NeilBrown06d91a52005-06-21 17:17:12 -07004030
NeilBrownf91de922005-11-08 21:39:36 -08004031 BUG_ON(mddev->ro == 1);
4032 if (mddev->ro == 2) {
4033 /* need to switch to read/write */
4034 mddev->ro = 0;
4035 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4036 md_wakeup_thread(mddev->thread);
4037 }
NeilBrown06d91a52005-06-21 17:17:12 -07004038 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07004039 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08004040 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07004041 if (mddev->in_sync) {
4042 mddev->in_sync = 0;
4043 mddev->sb_dirty = 1;
4044 md_wakeup_thread(mddev->thread);
4045 }
NeilBrowna9701a32005-11-08 21:39:34 -08004046 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07004047 }
NeilBrown3d310eb2005-06-21 17:17:26 -07004048 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049}
4050
4051void md_write_end(mddev_t *mddev)
4052{
4053 if (atomic_dec_and_test(&mddev->writes_pending)) {
4054 if (mddev->safemode == 2)
4055 md_wakeup_thread(mddev->thread);
4056 else
4057 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
4058 }
4059}
4060
Adrian Bunk75c96f82005-05-05 16:16:09 -07004061static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
4063#define SYNC_MARKS 10
4064#define SYNC_MARK_STEP (3*HZ)
4065static void md_do_sync(mddev_t *mddev)
4066{
4067 mddev_t *mddev2;
4068 unsigned int currspeed = 0,
4069 window;
NeilBrown57afd892005-06-21 17:17:13 -07004070 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 unsigned long mark[SYNC_MARKS];
4072 sector_t mark_cnt[SYNC_MARKS];
4073 int last_mark,m;
4074 struct list_head *tmp;
4075 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07004076 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
4078 /* just incase thread restarts... */
4079 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
4080 return;
4081
4082 /* we overload curr_resync somewhat here.
4083 * 0 == not engaged in resync at all
4084 * 2 == checking that there is no conflict with another sync
4085 * 1 == like 2, but have yielded to allow conflicting resync to
4086 * commense
4087 * other == active in resync - this many blocks
4088 *
4089 * Before starting a resync we must have set curr_resync to
4090 * 2, and then checked that every "conflicting" array has curr_resync
4091 * less than ours. When we find one that is the same or higher
4092 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
4093 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
4094 * This will mean we have to start checking from the beginning again.
4095 *
4096 */
4097
4098 do {
4099 mddev->curr_resync = 2;
4100
4101 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08004102 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07004103 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 goto skip;
4105 }
4106 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 if (mddev2 == mddev)
4108 continue;
4109 if (mddev2->curr_resync &&
4110 match_mddev_units(mddev,mddev2)) {
4111 DEFINE_WAIT(wq);
4112 if (mddev < mddev2 && mddev->curr_resync == 2) {
4113 /* arbitrarily yield */
4114 mddev->curr_resync = 1;
4115 wake_up(&resync_wait);
4116 }
4117 if (mddev > mddev2 && mddev->curr_resync == 1)
4118 /* no need to wait here, we can wait the next
4119 * time 'round when curr_resync == 2
4120 */
4121 continue;
NeilBrown787453c2005-11-08 21:39:43 -08004122 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
4123 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07004124 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 printk(KERN_INFO "md: delaying resync of %s"
4126 " until %s has finished resync (they"
4127 " share one or more physical units)\n",
4128 mdname(mddev), mdname(mddev2));
4129 mddev_put(mddev2);
4130 schedule();
4131 finish_wait(&resync_wait, &wq);
4132 goto try_again;
4133 }
4134 finish_wait(&resync_wait, &wq);
4135 }
4136 }
4137 } while (mddev->curr_resync < 2);
4138
NeilBrown9d888832005-11-08 21:39:26 -08004139 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07004141 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 */
4143 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08004144 mddev->resync_mismatches = 0;
4145 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 /* recovery follows the physical size of devices */
4147 max_sectors = mddev->size << 1;
4148
4149 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4150 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
4151 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
Adrian Bunk338cec32005-09-10 00:26:54 -07004152 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 "(but not more than %d KB/sec) for reconstruction.\n",
4154 sysctl_speed_limit_max);
4155
4156 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07004157 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08004158 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4159 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 j = mddev->recovery_cp;
4161 else
4162 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07004163 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 for (m = 0; m < SYNC_MARKS; m++) {
4165 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004166 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 }
4168 last_mark = 0;
4169 mddev->resync_mark = mark[last_mark];
4170 mddev->resync_mark_cnt = mark_cnt[last_mark];
4171
4172 /*
4173 * Tune reconstruction:
4174 */
4175 window = 32*(PAGE_SIZE/512);
4176 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4177 window/2,(unsigned long long) max_sectors/2);
4178
4179 atomic_set(&mddev->recovery_active, 0);
4180 init_waitqueue_head(&mddev->recovery_wait);
4181 last_check = 0;
4182
4183 if (j>2) {
4184 printk(KERN_INFO
4185 "md: resuming recovery of %s from checkpoint.\n",
4186 mdname(mddev));
4187 mddev->curr_resync = j;
4188 }
4189
4190 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07004191 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
NeilBrown57afd892005-06-21 17:17:13 -07004193 skipped = 0;
4194 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4195 currspeed < sysctl_speed_limit_min);
4196 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4198 goto out;
4199 }
NeilBrown57afd892005-06-21 17:17:13 -07004200
4201 if (!skipped) { /* actual IO requested */
4202 io_sectors += sectors;
4203 atomic_add(sectors, &mddev->recovery_active);
4204 }
4205
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 j += sectors;
4207 if (j>1) mddev->curr_resync = j;
NeilBrownd7603b72006-01-06 00:20:30 -08004208 if (last_check == 0)
4209 /* this is the earliers that rebuilt will be
4210 * visible in /proc/mdstat
4211 */
4212 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07004213
4214 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 continue;
4216
NeilBrown57afd892005-06-21 17:17:13 -07004217 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
4219 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4220 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4221 break;
4222
4223 repeat:
4224 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4225 /* step marks */
4226 int next = (last_mark+1) % SYNC_MARKS;
4227
4228 mddev->resync_mark = mark[next];
4229 mddev->resync_mark_cnt = mark_cnt[next];
4230 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004231 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 last_mark = next;
4233 }
4234
4235
NeilBrown787453c2005-11-08 21:39:43 -08004236 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 /*
4238 * got a signal, exit.
4239 */
4240 printk(KERN_INFO
4241 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4243 goto out;
4244 }
4245
4246 /*
4247 * this loop exits only if either when we are slower than
4248 * the 'hard' speed limit, or the system was IO-idle for
4249 * a jiffy.
4250 * the system might be non-idle CPU-wise, but we only care
4251 * about not overloading the IO subsystem. (things like an
4252 * e2fsck being done on the RAID array should execute fast)
4253 */
4254 mddev->queue->unplug_fn(mddev->queue);
4255 cond_resched();
4256
NeilBrown57afd892005-06-21 17:17:13 -07004257 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4258 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
4260 if (currspeed > sysctl_speed_limit_min) {
4261 if ((currspeed > sysctl_speed_limit_max) ||
4262 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08004263 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 goto repeat;
4265 }
4266 }
4267 }
4268 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4269 /*
4270 * this also signals 'finished resyncing' to md_stop
4271 */
4272 out:
4273 mddev->queue->unplug_fn(mddev->queue);
4274
4275 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4276
4277 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004278 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
4280 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4281 mddev->curr_resync > 2 &&
4282 mddev->curr_resync >= mddev->recovery_cp) {
4283 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4284 printk(KERN_INFO
4285 "md: checkpointing recovery of %s.\n",
4286 mdname(mddev));
4287 mddev->recovery_cp = mddev->curr_resync;
4288 } else
4289 mddev->recovery_cp = MaxSector;
4290 }
4291
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 skip:
4293 mddev->curr_resync = 0;
4294 wake_up(&resync_wait);
4295 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4296 md_wakeup_thread(mddev->thread);
4297}
4298
4299
4300/*
4301 * This routine is regularly called by all per-raid-array threads to
4302 * deal with generic issues like resync and super-block update.
4303 * Raid personalities that don't have a thread (linear/raid0) do not
4304 * need this as they never do any recovery or update the superblock.
4305 *
4306 * It does not do any resync itself, but rather "forks" off other threads
4307 * to do that as needed.
4308 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4309 * "->recovery" and create a thread at ->sync_thread.
4310 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4311 * and wakeups up this thread which will reap the thread and finish up.
4312 * This thread also removes any faulty devices (with nr_pending == 0).
4313 *
4314 * The overall approach is:
4315 * 1/ if the superblock needs updating, update it.
4316 * 2/ If a recovery thread is running, don't do anything else.
4317 * 3/ If recovery has finished, clean up, possibly marking spares active.
4318 * 4/ If there are any faulty devices, remove them.
4319 * 5/ If array is degraded, try to add spares devices
4320 * 6/ If array has spares or is not in-sync, start a resync thread.
4321 */
4322void md_check_recovery(mddev_t *mddev)
4323{
4324 mdk_rdev_t *rdev;
4325 struct list_head *rtmp;
4326
4327
NeilBrown5f404022005-06-21 17:17:16 -07004328 if (mddev->bitmap)
4329 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
4331 if (mddev->ro)
4332 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004333
4334 if (signal_pending(current)) {
4335 if (mddev->pers->sync_request) {
4336 printk(KERN_INFO "md: %s in immediate safe mode\n",
4337 mdname(mddev));
4338 mddev->safemode = 2;
4339 }
4340 flush_signals(current);
4341 }
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 if ( ! (
4344 mddev->sb_dirty ||
4345 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004346 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4347 (mddev->safemode == 1) ||
4348 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4349 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 ))
4351 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 if (mddev_trylock(mddev)==0) {
4354 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004355
NeilBrowna9701a32005-11-08 21:39:34 -08004356 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004357 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4358 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4359 mddev->in_sync = 1;
4360 mddev->sb_dirty = 1;
4361 }
4362 if (mddev->safemode == 1)
4363 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08004364 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004365
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 if (mddev->sb_dirty)
4367 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004368
NeilBrown06d91a52005-06-21 17:17:12 -07004369
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4371 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4372 /* resync/recovery still happening */
4373 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4374 goto unlock;
4375 }
4376 if (mddev->sync_thread) {
4377 /* resync has finished, collect result */
4378 md_unregister_thread(mddev->sync_thread);
4379 mddev->sync_thread = NULL;
4380 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4381 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4382 /* success...*/
4383 /* activate any spares */
4384 mddev->pers->spare_active(mddev);
4385 }
4386 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004387
4388 /* if array is no-longer degraded, then any saved_raid_disk
4389 * information must be scrapped
4390 */
4391 if (!mddev->degraded)
4392 ITERATE_RDEV(mddev,rdev,rtmp)
4393 rdev->saved_raid_disk = -1;
4394
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 mddev->recovery = 0;
4396 /* flag recovery needed just to double check */
4397 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08004398 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 goto unlock;
4400 }
NeilBrown24dd4692005-11-08 21:39:26 -08004401 /* Clear some bits that don't mean anything, but
4402 * might be left set
4403 */
4404 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4405 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4406 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4407 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
4409 /* no recovery is running.
4410 * remove any failed drives, then
4411 * add spares if possible.
4412 * Spare are also removed and re-added, to allow
4413 * the personality to fail the re-add.
4414 */
4415 ITERATE_RDEV(mddev,rdev,rtmp)
4416 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004417 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004419 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4420 char nm[20];
4421 sprintf(nm,"rd%d", rdev->raid_disk);
4422 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 }
4426
4427 if (mddev->degraded) {
4428 ITERATE_RDEV(mddev,rdev,rtmp)
4429 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08004430 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004431 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4432 char nm[20];
4433 sprintf(nm, "rd%d", rdev->raid_disk);
4434 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 spares++;
NeilBrownd7603b72006-01-06 00:20:30 -08004436 md_new_event(mddev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08004437 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 break;
4439 }
4440 }
4441
NeilBrown24dd4692005-11-08 21:39:26 -08004442 if (spares) {
4443 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4444 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4445 } else if (mddev->recovery_cp < MaxSector) {
4446 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4447 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4448 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08004450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 if (mddev->pers->sync_request) {
4452 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07004453 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4454 /* We are adding a device or devices to an array
4455 * which has the bitmap stored on all devices.
4456 * So make sure all bitmap pages get written
4457 */
4458 bitmap_write_all(mddev->bitmap);
4459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 mddev->sync_thread = md_register_thread(md_do_sync,
4461 mddev,
4462 "%s_resync");
4463 if (!mddev->sync_thread) {
4464 printk(KERN_ERR "%s: could not start resync"
4465 " thread...\n",
4466 mdname(mddev));
4467 /* leave the spares where they are, it shouldn't hurt */
4468 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08004469 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004471 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 }
4473 unlock:
4474 mddev_unlock(mddev);
4475 }
4476}
4477
Adrian Bunk75c96f82005-05-05 16:16:09 -07004478static int md_notify_reboot(struct notifier_block *this,
4479 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480{
4481 struct list_head *tmp;
4482 mddev_t *mddev;
4483
4484 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4485
4486 printk(KERN_INFO "md: stopping all md devices.\n");
4487
4488 ITERATE_MDDEV(mddev,tmp)
4489 if (mddev_trylock(mddev)==0)
4490 do_md_stop (mddev, 1);
4491 /*
4492 * certain more exotic SCSI devices are known to be
4493 * volatile wrt too early system reboots. While the
4494 * right place to handle this issue is the given
4495 * driver, we do want to have a safe RAID driver ...
4496 */
4497 mdelay(1000*1);
4498 }
4499 return NOTIFY_DONE;
4500}
4501
Adrian Bunk75c96f82005-05-05 16:16:09 -07004502static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 .notifier_call = md_notify_reboot,
4504 .next = NULL,
4505 .priority = INT_MAX, /* before any real devices */
4506};
4507
4508static void md_geninit(void)
4509{
4510 struct proc_dir_entry *p;
4511
4512 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4513
4514 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4515 if (p)
4516 p->proc_fops = &md_seq_fops;
4517}
4518
Adrian Bunk75c96f82005-05-05 16:16:09 -07004519static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520{
4521 int minor;
4522
4523 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4524 " MD_SB_DISKS=%d\n",
4525 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4526 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08004527 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07004528 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
4530 if (register_blkdev(MAJOR_NR, "md"))
4531 return -1;
4532 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4533 unregister_blkdev(MAJOR_NR, "md");
4534 return -1;
4535 }
4536 devfs_mk_dir("md");
4537 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4538 md_probe, NULL, NULL);
4539 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4540 md_probe, NULL, NULL);
4541
4542 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4543 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4544 S_IFBLK|S_IRUSR|S_IWUSR,
4545 "md/%d", minor);
4546
4547 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4548 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4549 S_IFBLK|S_IRUSR|S_IWUSR,
4550 "md/mdp%d", minor);
4551
4552
4553 register_reboot_notifier(&md_notifier);
4554 raid_table_header = register_sysctl_table(raid_root_table, 1);
4555
4556 md_geninit();
4557 return (0);
4558}
4559
4560
4561#ifndef MODULE
4562
4563/*
4564 * Searches all registered partitions for autorun RAID arrays
4565 * at boot time.
4566 */
4567static dev_t detected_devices[128];
4568static int dev_cnt;
4569
4570void md_autodetect_dev(dev_t dev)
4571{
4572 if (dev_cnt >= 0 && dev_cnt < 127)
4573 detected_devices[dev_cnt++] = dev;
4574}
4575
4576
4577static void autostart_arrays(int part)
4578{
4579 mdk_rdev_t *rdev;
4580 int i;
4581
4582 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4583
4584 for (i = 0; i < dev_cnt; i++) {
4585 dev_t dev = detected_devices[i];
4586
4587 rdev = md_import_device(dev,0, 0);
4588 if (IS_ERR(rdev))
4589 continue;
4590
NeilBrownb2d444d2005-11-08 21:39:31 -08004591 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 MD_BUG();
4593 continue;
4594 }
4595 list_add(&rdev->same_set, &pending_raid_disks);
4596 }
4597 dev_cnt = 0;
4598
4599 autorun_devices(part);
4600}
4601
4602#endif
4603
4604static __exit void md_exit(void)
4605{
4606 mddev_t *mddev;
4607 struct list_head *tmp;
4608 int i;
4609 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4610 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4611 for (i=0; i < MAX_MD_DEVS; i++)
4612 devfs_remove("md/%d", i);
4613 for (i=0; i < MAX_MD_DEVS; i++)
4614 devfs_remove("md/d%d", i);
4615
4616 devfs_remove("md");
4617
4618 unregister_blkdev(MAJOR_NR,"md");
4619 unregister_blkdev(mdp_major, "mdp");
4620 unregister_reboot_notifier(&md_notifier);
4621 unregister_sysctl_table(raid_table_header);
4622 remove_proc_entry("mdstat", NULL);
4623 ITERATE_MDDEV(mddev,tmp) {
4624 struct gendisk *disk = mddev->gendisk;
4625 if (!disk)
4626 continue;
4627 export_array(mddev);
4628 del_gendisk(disk);
4629 put_disk(disk);
4630 mddev->gendisk = NULL;
4631 mddev_put(mddev);
4632 }
4633}
4634
4635module_init(md_init)
4636module_exit(md_exit)
4637
NeilBrownf91de922005-11-08 21:39:36 -08004638static int get_ro(char *buffer, struct kernel_param *kp)
4639{
4640 return sprintf(buffer, "%d", start_readonly);
4641}
4642static int set_ro(const char *val, struct kernel_param *kp)
4643{
4644 char *e;
4645 int num = simple_strtoul(val, &e, 10);
4646 if (*val && (*e == '\0' || *e == '\n')) {
4647 start_readonly = num;
4648 return 0;;
4649 }
4650 return -EINVAL;
4651}
4652
4653module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004654module_param(start_dirty_degraded, int, 0644);
4655
NeilBrownf91de922005-11-08 21:39:36 -08004656
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657EXPORT_SYMBOL(register_md_personality);
4658EXPORT_SYMBOL(unregister_md_personality);
4659EXPORT_SYMBOL(md_error);
4660EXPORT_SYMBOL(md_done_sync);
4661EXPORT_SYMBOL(md_write_start);
4662EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663EXPORT_SYMBOL(md_register_thread);
4664EXPORT_SYMBOL(md_unregister_thread);
4665EXPORT_SYMBOL(md_wakeup_thread);
4666EXPORT_SYMBOL(md_print_devices);
4667EXPORT_SYMBOL(md_check_recovery);
4668MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07004669MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07004670MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);