blob: 1778104e106cf4b331b44e09da51dd02aaf415fc [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.
NeilBrown88202a02006-01-06 00:21:36 -080084 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
86
87static int sysctl_speed_limit_min = 1000;
88static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080089static inline int speed_min(mddev_t *mddev)
90{
91 return mddev->sync_speed_min ?
92 mddev->sync_speed_min : sysctl_speed_limit_min;
93}
94
95static inline int speed_max(mddev_t *mddev)
96{
97 return mddev->sync_speed_max ?
98 mddev->sync_speed_max : sysctl_speed_limit_max;
99}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101static struct ctl_table_header *raid_table_header;
102
103static ctl_table raid_table[] = {
104 {
105 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
106 .procname = "speed_limit_min",
107 .data = &sysctl_speed_limit_min,
108 .maxlen = sizeof(int),
109 .mode = 0644,
110 .proc_handler = &proc_dointvec,
111 },
112 {
113 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
114 .procname = "speed_limit_max",
115 .data = &sysctl_speed_limit_max,
116 .maxlen = sizeof(int),
117 .mode = 0644,
118 .proc_handler = &proc_dointvec,
119 },
120 { .ctl_name = 0 }
121};
122
123static ctl_table raid_dir_table[] = {
124 {
125 .ctl_name = DEV_RAID,
126 .procname = "raid",
127 .maxlen = 0,
128 .mode = 0555,
129 .child = raid_table,
130 },
131 { .ctl_name = 0 }
132};
133
134static ctl_table raid_root_table[] = {
135 {
136 .ctl_name = CTL_DEV,
137 .procname = "dev",
138 .maxlen = 0,
139 .mode = 0555,
140 .child = raid_dir_table,
141 },
142 { .ctl_name = 0 }
143};
144
145static struct block_device_operations md_fops;
146
NeilBrownf91de922005-11-08 21:39:36 -0800147static int start_readonly;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
NeilBrownd7603b72006-01-06 00:20:30 -0800150 * We have a system wide 'event count' that is incremented
151 * on any 'interesting' event, and readers of /proc/mdstat
152 * can use 'poll' or 'select' to find out when the event
153 * count increases.
154 *
155 * Events are:
156 * start array, stop array, error, add device, remove device,
157 * start build, activate spare
158 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800159static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800160static atomic_t md_event_count;
Adrian Bunk07dbd372006-01-06 00:20:44 -0800161static void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800162{
163 atomic_inc(&md_event_count);
164 wake_up(&md_event_waiters);
165}
166
167/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * Enables to iterate over all existing md arrays
169 * all_mddevs_lock protects this list.
170 */
171static LIST_HEAD(all_mddevs);
172static DEFINE_SPINLOCK(all_mddevs_lock);
173
174
175/*
176 * iterates through all used mddevs in the system.
177 * We take care to grab the all_mddevs_lock whenever navigating
178 * the list, and to always hold a refcount when unlocked.
179 * Any code which breaks out of this loop while own
180 * a reference to the current mddev and must mddev_put it.
181 */
182#define ITERATE_MDDEV(mddev,tmp) \
183 \
184 for (({ spin_lock(&all_mddevs_lock); \
185 tmp = all_mddevs.next; \
186 mddev = NULL;}); \
187 ({ if (tmp != &all_mddevs) \
188 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
189 spin_unlock(&all_mddevs_lock); \
190 if (mddev) mddev_put(mddev); \
191 mddev = list_entry(tmp, mddev_t, all_mddevs); \
192 tmp != &all_mddevs;}); \
193 ({ spin_lock(&all_mddevs_lock); \
194 tmp = tmp->next;}) \
195 )
196
197
198static int md_fail_request (request_queue_t *q, struct bio *bio)
199{
200 bio_io_error(bio, bio->bi_size);
201 return 0;
202}
203
204static inline mddev_t *mddev_get(mddev_t *mddev)
205{
206 atomic_inc(&mddev->active);
207 return mddev;
208}
209
210static void mddev_put(mddev_t *mddev)
211{
212 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
213 return;
214 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
215 list_del(&mddev->all_mddevs);
216 blk_put_queue(mddev->queue);
NeilBrowneae17012005-11-08 21:39:23 -0800217 kobject_unregister(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219 spin_unlock(&all_mddevs_lock);
220}
221
222static mddev_t * mddev_find(dev_t unit)
223{
224 mddev_t *mddev, *new = NULL;
225
226 retry:
227 spin_lock(&all_mddevs_lock);
228 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
229 if (mddev->unit == unit) {
230 mddev_get(mddev);
231 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700232 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return mddev;
234 }
235
236 if (new) {
237 list_add(&new->all_mddevs, &all_mddevs);
238 spin_unlock(&all_mddevs_lock);
239 return new;
240 }
241 spin_unlock(&all_mddevs_lock);
242
NeilBrown9ffae0c2006-01-06 00:20:32 -0800243 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (!new)
245 return NULL;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 new->unit = unit;
248 if (MAJOR(unit) == MD_MAJOR)
249 new->md_minor = MINOR(unit);
250 else
251 new->md_minor = MINOR(unit) >> MdpMinorShift;
252
253 init_MUTEX(&new->reconfig_sem);
254 INIT_LIST_HEAD(&new->disks);
255 INIT_LIST_HEAD(&new->all_mddevs);
256 init_timer(&new->safemode_timer);
257 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700258 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700259 init_waitqueue_head(&new->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 new->queue = blk_alloc_queue(GFP_KERNEL);
262 if (!new->queue) {
263 kfree(new);
264 return NULL;
265 }
266
267 blk_queue_make_request(new->queue, md_fail_request);
268
269 goto retry;
270}
271
272static inline int mddev_lock(mddev_t * mddev)
273{
274 return down_interruptible(&mddev->reconfig_sem);
275}
276
277static inline void mddev_lock_uninterruptible(mddev_t * mddev)
278{
279 down(&mddev->reconfig_sem);
280}
281
282static inline int mddev_trylock(mddev_t * mddev)
283{
284 return down_trylock(&mddev->reconfig_sem);
285}
286
287static inline void mddev_unlock(mddev_t * mddev)
288{
289 up(&mddev->reconfig_sem);
290
NeilBrown005eca52005-08-22 13:11:08 -0700291 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
NeilBrown2989ddb2006-01-06 00:20:43 -0800294static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 mdk_rdev_t * rdev;
297 struct list_head *tmp;
298
299 ITERATE_RDEV(mddev,rdev,tmp) {
300 if (rdev->desc_nr == nr)
301 return rdev;
302 }
303 return NULL;
304}
305
306static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
307{
308 struct list_head *tmp;
309 mdk_rdev_t *rdev;
310
311 ITERATE_RDEV(mddev,rdev,tmp) {
312 if (rdev->bdev->bd_dev == dev)
313 return rdev;
314 }
315 return NULL;
316}
317
NeilBrownd9d166c2006-01-06 00:20:51 -0800318static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800319{
320 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800321 list_for_each_entry(pers, &pers_list, list) {
322 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800323 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800324 if (strcmp(pers->name, clevel)==0)
325 return pers;
326 }
NeilBrown2604b702006-01-06 00:20:36 -0800327 return NULL;
328}
329
Jesper Juhl77933d72005-07-27 11:46:09 -0700330static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
333 return MD_NEW_SIZE_BLOCKS(size);
334}
335
336static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
337{
338 sector_t size;
339
340 size = rdev->sb_offset;
341
342 if (chunk_size)
343 size &= ~((sector_t)chunk_size/1024 - 1);
344 return size;
345}
346
347static int alloc_disk_sb(mdk_rdev_t * rdev)
348{
349 if (rdev->sb_page)
350 MD_BUG();
351
352 rdev->sb_page = alloc_page(GFP_KERNEL);
353 if (!rdev->sb_page) {
354 printk(KERN_ALERT "md: out of memory.\n");
355 return -EINVAL;
356 }
357
358 return 0;
359}
360
361static void free_disk_sb(mdk_rdev_t * rdev)
362{
363 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800364 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 rdev->sb_loaded = 0;
366 rdev->sb_page = NULL;
367 rdev->sb_offset = 0;
368 rdev->size = 0;
369 }
370}
371
372
NeilBrown7bfa19f2005-06-21 17:17:28 -0700373static int super_written(struct bio *bio, unsigned int bytes_done, int error)
374{
375 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800376 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700377 if (bio->bi_size)
378 return 1;
379
380 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
NeilBrowna9701a32005-11-08 21:39:34 -0800381 md_error(mddev, rdev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700382
NeilBrowna9701a32005-11-08 21:39:34 -0800383 if (atomic_dec_and_test(&mddev->pending_writes))
384 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700385 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700386 return 0;
387}
388
NeilBrowna9701a32005-11-08 21:39:34 -0800389static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
390{
391 struct bio *bio2 = bio->bi_private;
392 mdk_rdev_t *rdev = bio2->bi_private;
393 mddev_t *mddev = rdev->mddev;
394 if (bio->bi_size)
395 return 1;
396
397 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
398 error == -EOPNOTSUPP) {
399 unsigned long flags;
400 /* barriers don't appear to be supported :-( */
401 set_bit(BarriersNotsupp, &rdev->flags);
402 mddev->barriers_work = 0;
403 spin_lock_irqsave(&mddev->write_lock, flags);
404 bio2->bi_next = mddev->biolist;
405 mddev->biolist = bio2;
406 spin_unlock_irqrestore(&mddev->write_lock, flags);
407 wake_up(&mddev->sb_wait);
408 bio_put(bio);
409 return 0;
410 }
411 bio_put(bio2);
412 bio->bi_private = rdev;
413 return super_written(bio, bytes_done, error);
414}
415
NeilBrown7bfa19f2005-06-21 17:17:28 -0700416void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
417 sector_t sector, int size, struct page *page)
418{
419 /* write first size bytes of page to sector of rdev
420 * Increment mddev->pending_writes before returning
421 * and decrement it on completion, waking up sb_wait
422 * if zero is reached.
423 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800424 *
425 * As we might need to resubmit the request if BIO_RW_BARRIER
426 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700427 */
428 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800429 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700430
431 bio->bi_bdev = rdev->bdev;
432 bio->bi_sector = sector;
433 bio_add_page(bio, page, size, 0);
434 bio->bi_private = rdev;
435 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800436 bio->bi_rw = rw;
437
NeilBrown7bfa19f2005-06-21 17:17:28 -0700438 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800439 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
440 struct bio *rbio;
441 rw |= (1<<BIO_RW_BARRIER);
442 rbio = bio_clone(bio, GFP_NOIO);
443 rbio->bi_private = bio;
444 rbio->bi_end_io = super_written_barrier;
445 submit_bio(rw, rbio);
446 } else
447 submit_bio(rw, bio);
448}
449
450void md_super_wait(mddev_t *mddev)
451{
452 /* wait for all superblock writes that were scheduled to complete.
453 * if any had to be retried (due to BARRIER problems), retry them
454 */
455 DEFINE_WAIT(wq);
456 for(;;) {
457 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
458 if (atomic_read(&mddev->pending_writes)==0)
459 break;
460 while (mddev->biolist) {
461 struct bio *bio;
462 spin_lock_irq(&mddev->write_lock);
463 bio = mddev->biolist;
464 mddev->biolist = bio->bi_next ;
465 bio->bi_next = NULL;
466 spin_unlock_irq(&mddev->write_lock);
467 submit_bio(bio->bi_rw, bio);
468 }
469 schedule();
470 }
471 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
475{
476 if (bio->bi_size)
477 return 1;
478
479 complete((struct completion*)bio->bi_private);
480 return 0;
481}
482
NeilBrowna654b9d82005-06-21 17:17:27 -0700483int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct page *page, int rw)
485{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700486 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct completion event;
488 int ret;
489
490 rw |= (1 << BIO_RW_SYNC);
491
492 bio->bi_bdev = bdev;
493 bio->bi_sector = sector;
494 bio_add_page(bio, page, size, 0);
495 init_completion(&event);
496 bio->bi_private = &event;
497 bio->bi_end_io = bi_complete;
498 submit_bio(rw, bio);
499 wait_for_completion(&event);
500
501 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
502 bio_put(bio);
503 return ret;
504}
NeilBrowna8745db2006-01-06 00:20:34 -0800505EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
NeilBrown0002b272005-09-09 16:23:53 -0700507static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 char b[BDEVNAME_SIZE];
510 if (!rdev->sb_page) {
511 MD_BUG();
512 return -EINVAL;
513 }
514 if (rdev->sb_loaded)
515 return 0;
516
517
NeilBrown0002b272005-09-09 16:23:53 -0700518 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto fail;
520 rdev->sb_loaded = 1;
521 return 0;
522
523fail:
524 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
525 bdevname(rdev->bdev,b));
526 return -EINVAL;
527}
528
529static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
530{
531 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
532 (sb1->set_uuid1 == sb2->set_uuid1) &&
533 (sb1->set_uuid2 == sb2->set_uuid2) &&
534 (sb1->set_uuid3 == sb2->set_uuid3))
535
536 return 1;
537
538 return 0;
539}
540
541
542static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
543{
544 int ret;
545 mdp_super_t *tmp1, *tmp2;
546
547 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
548 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
549
550 if (!tmp1 || !tmp2) {
551 ret = 0;
552 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
553 goto abort;
554 }
555
556 *tmp1 = *sb1;
557 *tmp2 = *sb2;
558
559 /*
560 * nr_disks is not constant
561 */
562 tmp1->nr_disks = 0;
563 tmp2->nr_disks = 0;
564
565 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
566 ret = 0;
567 else
568 ret = 1;
569
570abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700571 kfree(tmp1);
572 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return ret;
574}
575
576static unsigned int calc_sb_csum(mdp_super_t * sb)
577{
578 unsigned int disk_csum, csum;
579
580 disk_csum = sb->sb_csum;
581 sb->sb_csum = 0;
582 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
583 sb->sb_csum = disk_csum;
584 return csum;
585}
586
587
588/*
589 * Handle superblock details.
590 * We want to be able to handle multiple superblock formats
591 * so we have a common interface to them all, and an array of
592 * different handlers.
593 * We rely on user-space to write the initial superblock, and support
594 * reading and updating of superblocks.
595 * Interface methods are:
596 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
597 * loads and validates a superblock on dev.
598 * if refdev != NULL, compare superblocks on both devices
599 * Return:
600 * 0 - dev has a superblock that is compatible with refdev
601 * 1 - dev has a superblock that is compatible and newer than refdev
602 * so dev should be used as the refdev in future
603 * -EINVAL superblock incompatible or invalid
604 * -othererror e.g. -EIO
605 *
606 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
607 * Verify that dev is acceptable into mddev.
608 * The first time, mddev->raid_disks will be 0, and data from
609 * dev should be merged in. Subsequent calls check that dev
610 * is new enough. Return 0 or -EINVAL
611 *
612 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
613 * Update the superblock for rdev with data in mddev
614 * This does not write to disc.
615 *
616 */
617
618struct super_type {
619 char *name;
620 struct module *owner;
621 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
622 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
623 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
624};
625
626/*
627 * load_super for 0.90.0
628 */
629static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
630{
631 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
632 mdp_super_t *sb;
633 int ret;
634 sector_t sb_offset;
635
636 /*
637 * Calculate the position of the superblock,
638 * it's at the end of the disk.
639 *
640 * It also happens to be a multiple of 4Kb.
641 */
642 sb_offset = calc_dev_sboffset(rdev->bdev);
643 rdev->sb_offset = sb_offset;
644
NeilBrown0002b272005-09-09 16:23:53 -0700645 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (ret) return ret;
647
648 ret = -EINVAL;
649
650 bdevname(rdev->bdev, b);
651 sb = (mdp_super_t*)page_address(rdev->sb_page);
652
653 if (sb->md_magic != MD_SB_MAGIC) {
654 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
655 b);
656 goto abort;
657 }
658
659 if (sb->major_version != 0 ||
660 sb->minor_version != 90) {
661 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
662 sb->major_version, sb->minor_version,
663 b);
664 goto abort;
665 }
666
667 if (sb->raid_disks <= 0)
668 goto abort;
669
670 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
671 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
672 b);
673 goto abort;
674 }
675
676 rdev->preferred_minor = sb->md_minor;
677 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700678 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 if (sb->level == LEVEL_MULTIPATH)
681 rdev->desc_nr = -1;
682 else
683 rdev->desc_nr = sb->this_disk.number;
684
685 if (refdev == 0)
686 ret = 1;
687 else {
688 __u64 ev1, ev2;
689 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
690 if (!uuid_equal(refsb, sb)) {
691 printk(KERN_WARNING "md: %s has different UUID to %s\n",
692 b, bdevname(refdev->bdev,b2));
693 goto abort;
694 }
695 if (!sb_equal(refsb, sb)) {
696 printk(KERN_WARNING "md: %s has same UUID"
697 " but different superblock to %s\n",
698 b, bdevname(refdev->bdev, b2));
699 goto abort;
700 }
701 ev1 = md_event(sb);
702 ev2 = md_event(refsb);
703 if (ev1 > ev2)
704 ret = 1;
705 else
706 ret = 0;
707 }
708 rdev->size = calc_dev_size(rdev, sb->chunk_size);
709
NeilBrown2bf071b2006-01-06 00:20:55 -0800710 if (rdev->size < sb->size && sb->level > 1)
711 /* "this cannot possibly happen" ... */
712 ret = -EINVAL;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 abort:
715 return ret;
716}
717
718/*
719 * validate_super for 0.90.0
720 */
721static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
722{
723 mdp_disk_t *desc;
724 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
725
NeilBrown41158c72005-06-21 17:17:25 -0700726 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800727 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (mddev->raid_disks == 0) {
729 mddev->major_version = 0;
730 mddev->minor_version = sb->minor_version;
731 mddev->patch_version = sb->patch_version;
732 mddev->persistent = ! sb->not_persistent;
733 mddev->chunk_size = sb->chunk_size;
734 mddev->ctime = sb->ctime;
735 mddev->utime = sb->utime;
736 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800737 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 mddev->layout = sb->layout;
739 mddev->raid_disks = sb->raid_disks;
740 mddev->size = sb->size;
741 mddev->events = md_event(sb);
NeilBrown92232142005-08-18 11:24:16 -0700742 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700743 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (sb->state & (1<<MD_SB_CLEAN))
746 mddev->recovery_cp = MaxSector;
747 else {
748 if (sb->events_hi == sb->cp_events_hi &&
749 sb->events_lo == sb->cp_events_lo) {
750 mddev->recovery_cp = sb->recovery_cp;
751 } else
752 mddev->recovery_cp = 0;
753 }
754
755 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
756 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
757 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
758 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
759
760 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700761
762 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
763 mddev->bitmap_file == NULL) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800764 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
765 && mddev->level != 10) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700766 /* FIXME use a better test */
NeilBrown6cce3b22006-01-06 00:20:16 -0800767 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -0700768 return -EINVAL;
769 }
NeilBrown36fa3062005-09-09 16:23:45 -0700770 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700771 }
772
NeilBrown41158c72005-06-21 17:17:25 -0700773 } else if (mddev->pers == NULL) {
774 /* Insist on good event counter while assembling */
775 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 ++ev1;
777 if (ev1 < mddev->events)
778 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700779 } else if (mddev->bitmap) {
780 /* if adding to array with a bitmap, then we can accept an
781 * older device ... but not too old.
782 */
783 __u64 ev1 = md_event(sb);
784 if (ev1 < mddev->bitmap->events_cleared)
785 return 0;
786 } else /* just a hot-add of a new device, leave raid_disk at -1 */
787 return 0;
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 desc = sb->disks + rdev->desc_nr;
791
792 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800793 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 else if (desc->state & (1<<MD_DISK_SYNC) &&
795 desc->raid_disk < mddev->raid_disks) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800796 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 rdev->raid_disk = desc->raid_disk;
798 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700799 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
800 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700801 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800802 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return 0;
804}
805
806/*
807 * sync_super for 0.90.0
808 */
809static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
810{
811 mdp_super_t *sb;
812 struct list_head *tmp;
813 mdk_rdev_t *rdev2;
814 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* make rdev->sb match mddev data..
818 *
819 * 1/ zero out disks
820 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
821 * 3/ any empty disks < next_spare become removed
822 *
823 * disks[0] gets initialised to REMOVED because
824 * we cannot be sure from other fields if it has
825 * been initialised or not.
826 */
827 int i;
828 int active=0, working=0,failed=0,spare=0,nr_disks=0;
829
NeilBrown61181562005-09-09 16:24:02 -0700830 rdev->sb_size = MD_SB_BYTES;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 sb = (mdp_super_t*)page_address(rdev->sb_page);
833
834 memset(sb, 0, sizeof(*sb));
835
836 sb->md_magic = MD_SB_MAGIC;
837 sb->major_version = mddev->major_version;
838 sb->minor_version = mddev->minor_version;
839 sb->patch_version = mddev->patch_version;
840 sb->gvalid_words = 0; /* ignored */
841 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
842 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
843 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
844 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
845
846 sb->ctime = mddev->ctime;
847 sb->level = mddev->level;
848 sb->size = mddev->size;
849 sb->raid_disks = mddev->raid_disks;
850 sb->md_minor = mddev->md_minor;
851 sb->not_persistent = !mddev->persistent;
852 sb->utime = mddev->utime;
853 sb->state = 0;
854 sb->events_hi = (mddev->events>>32);
855 sb->events_lo = (u32)mddev->events;
856
857 if (mddev->in_sync)
858 {
859 sb->recovery_cp = mddev->recovery_cp;
860 sb->cp_events_hi = (mddev->events>>32);
861 sb->cp_events_lo = (u32)mddev->events;
862 if (mddev->recovery_cp == MaxSector)
863 sb->state = (1<< MD_SB_CLEAN);
864 } else
865 sb->recovery_cp = 0;
866
867 sb->layout = mddev->layout;
868 sb->chunk_size = mddev->chunk_size;
869
NeilBrowna654b9d82005-06-21 17:17:27 -0700870 if (mddev->bitmap && mddev->bitmap_file == NULL)
871 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 sb->disks[0].state = (1<<MD_DISK_REMOVED);
874 ITERATE_RDEV(mddev,rdev2,tmp) {
875 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800876 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800877 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
878 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800879 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800881 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800882 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 d = &sb->disks[rdev2->desc_nr];
884 nr_disks++;
885 d->number = rdev2->desc_nr;
886 d->major = MAJOR(rdev2->bdev->bd_dev);
887 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800888 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
889 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 d->raid_disk = rdev2->raid_disk;
891 else
892 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrownb2d444d2005-11-08 21:39:31 -0800893 if (test_bit(Faulty, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 d->state = (1<<MD_DISK_FAULTY);
895 failed++;
NeilBrownb2d444d2005-11-08 21:39:31 -0800896 } else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 d->state = (1<<MD_DISK_ACTIVE);
898 d->state |= (1<<MD_DISK_SYNC);
899 active++;
900 working++;
901 } else {
902 d->state = 0;
903 spare++;
904 working++;
905 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700906 if (test_bit(WriteMostly, &rdev2->flags))
907 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /* now set the "removed" and "faulty" bits on any missing devices */
910 for (i=0 ; i < mddev->raid_disks ; i++) {
911 mdp_disk_t *d = &sb->disks[i];
912 if (d->state == 0 && d->number == 0) {
913 d->number = i;
914 d->raid_disk = i;
915 d->state = (1<<MD_DISK_REMOVED);
916 d->state |= (1<<MD_DISK_FAULTY);
917 failed++;
918 }
919 }
920 sb->nr_disks = nr_disks;
921 sb->active_disks = active;
922 sb->working_disks = working;
923 sb->failed_disks = failed;
924 sb->spare_disks = spare;
925
926 sb->this_disk = sb->disks[rdev->desc_nr];
927 sb->sb_csum = calc_sb_csum(sb);
928}
929
930/*
931 * version 1 superblock
932 */
933
934static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
935{
936 unsigned int disk_csum, csum;
937 unsigned long long newcsum;
938 int size = 256 + le32_to_cpu(sb->max_dev)*2;
939 unsigned int *isuper = (unsigned int*)sb;
940 int i;
941
942 disk_csum = sb->sb_csum;
943 sb->sb_csum = 0;
944 newcsum = 0;
945 for (i=0; size>=4; size -= 4 )
946 newcsum += le32_to_cpu(*isuper++);
947
948 if (size == 2)
949 newcsum += le16_to_cpu(*(unsigned short*) isuper);
950
951 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
952 sb->sb_csum = disk_csum;
953 return cpu_to_le32(csum);
954}
955
956static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
957{
958 struct mdp_superblock_1 *sb;
959 int ret;
960 sector_t sb_offset;
961 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -0700962 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /*
965 * Calculate the position of the superblock.
966 * It is always aligned to a 4K boundary and
967 * depeding on minor_version, it can be:
968 * 0: At least 8K, but less than 12K, from end of device
969 * 1: At start of device
970 * 2: 4K from start of device.
971 */
972 switch(minor_version) {
973 case 0:
974 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
975 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -0700976 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* convert from sectors to K */
978 sb_offset /= 2;
979 break;
980 case 1:
981 sb_offset = 0;
982 break;
983 case 2:
984 sb_offset = 4;
985 break;
986 default:
987 return -EINVAL;
988 }
989 rdev->sb_offset = sb_offset;
990
NeilBrown0002b272005-09-09 16:23:53 -0700991 /* superblock is rarely larger than 1K, but it can be larger,
992 * and it is safe to read 4k, so we do that
993 */
994 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (ret) return ret;
996
997
998 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
999
1000 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1001 sb->major_version != cpu_to_le32(1) ||
1002 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1003 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001004 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -EINVAL;
1006
1007 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1008 printk("md: invalid superblock checksum on %s\n",
1009 bdevname(rdev->bdev,b));
1010 return -EINVAL;
1011 }
1012 if (le64_to_cpu(sb->data_size) < 10) {
1013 printk("md: data_size too small on %s\n",
1014 bdevname(rdev->bdev,b));
1015 return -EINVAL;
1016 }
1017 rdev->preferred_minor = 0xffff;
1018 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001019 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
NeilBrown0002b272005-09-09 16:23:53 -07001021 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001022 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001023 if (rdev->sb_size & bmask)
1024 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (refdev == 0)
1027 return 1;
1028 else {
1029 __u64 ev1, ev2;
1030 struct mdp_superblock_1 *refsb =
1031 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1032
1033 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1034 sb->level != refsb->level ||
1035 sb->layout != refsb->layout ||
1036 sb->chunksize != refsb->chunksize) {
1037 printk(KERN_WARNING "md: %s has strangely different"
1038 " superblock to %s\n",
1039 bdevname(rdev->bdev,b),
1040 bdevname(refdev->bdev,b2));
1041 return -EINVAL;
1042 }
1043 ev1 = le64_to_cpu(sb->events);
1044 ev2 = le64_to_cpu(refsb->events);
1045
1046 if (ev1 > ev2)
1047 return 1;
1048 }
1049 if (minor_version)
1050 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1051 else
1052 rdev->size = rdev->sb_offset;
1053 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1054 return -EINVAL;
1055 rdev->size = le64_to_cpu(sb->data_size)/2;
1056 if (le32_to_cpu(sb->chunksize))
1057 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001058
1059 if (le32_to_cpu(sb->size) > rdev->size*2)
1060 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return 0;
1062}
1063
1064static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1065{
1066 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1067
NeilBrown41158c72005-06-21 17:17:25 -07001068 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001069 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (mddev->raid_disks == 0) {
1071 mddev->major_version = 1;
1072 mddev->patch_version = 0;
1073 mddev->persistent = 1;
1074 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1075 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1076 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1077 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001078 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 mddev->layout = le32_to_cpu(sb->layout);
1080 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1081 mddev->size = le64_to_cpu(sb->size)/2;
1082 mddev->events = le64_to_cpu(sb->events);
NeilBrown92232142005-08-18 11:24:16 -07001083 mddev->bitmap_offset = 0;
NeilBrown53e87fb2005-09-09 16:23:58 -07001084 mddev->default_bitmap_offset = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1087 memcpy(mddev->uuid, sb->set_uuid, 16);
1088
1089 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001090
NeilBrown71c08052005-09-09 16:23:51 -07001091 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -07001092 mddev->bitmap_file == NULL ) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001093 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1094 && mddev->level != 10) {
1095 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -07001096 return -EINVAL;
1097 }
1098 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1099 }
NeilBrown41158c72005-06-21 17:17:25 -07001100 } else if (mddev->pers == NULL) {
1101 /* Insist of good event counter while assembling */
1102 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ++ev1;
1104 if (ev1 < mddev->events)
1105 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001106 } else if (mddev->bitmap) {
1107 /* If adding to array with a bitmap, then we can accept an
1108 * older device, but not too old.
1109 */
1110 __u64 ev1 = le64_to_cpu(sb->events);
1111 if (ev1 < mddev->bitmap->events_cleared)
1112 return 0;
1113 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if (mddev->level != LEVEL_MULTIPATH) {
1117 int role;
1118 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1119 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1120 switch(role) {
1121 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 break;
1123 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001124 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 break;
1126 default:
NeilBrownb2d444d2005-11-08 21:39:31 -08001127 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 rdev->raid_disk = role;
1129 break;
1130 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001131 if (sb->devflags & WriteMostly1)
1132 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001133 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001134 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return 0;
1137}
1138
1139static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1140{
1141 struct mdp_superblock_1 *sb;
1142 struct list_head *tmp;
1143 mdk_rdev_t *rdev2;
1144 int max_dev, i;
1145 /* make rdev->sb match mddev and rdev data. */
1146
1147 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1148
1149 sb->feature_map = 0;
1150 sb->pad0 = 0;
1151 memset(sb->pad1, 0, sizeof(sb->pad1));
1152 memset(sb->pad2, 0, sizeof(sb->pad2));
1153 memset(sb->pad3, 0, sizeof(sb->pad3));
1154
1155 sb->utime = cpu_to_le64((__u64)mddev->utime);
1156 sb->events = cpu_to_le64(mddev->events);
1157 if (mddev->in_sync)
1158 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1159 else
1160 sb->resync_offset = cpu_to_le64(0);
1161
NeilBrown4dbcdc72006-01-06 00:20:52 -08001162 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1163
NeilBrowna654b9d82005-06-21 17:17:27 -07001164 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1165 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001166 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001167 }
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 max_dev = 0;
1170 ITERATE_RDEV(mddev,rdev2,tmp)
1171 if (rdev2->desc_nr+1 > max_dev)
1172 max_dev = rdev2->desc_nr+1;
1173
1174 sb->max_dev = cpu_to_le32(max_dev);
1175 for (i=0; i<max_dev;i++)
1176 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1177
1178 ITERATE_RDEV(mddev,rdev2,tmp) {
1179 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001180 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001182 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1184 else
1185 sb->dev_roles[i] = cpu_to_le16(0xffff);
1186 }
1187
1188 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1189 sb->sb_csum = calc_sb_1_csum(sb);
1190}
1191
1192
Adrian Bunk75c96f82005-05-05 16:16:09 -07001193static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 [0] = {
1195 .name = "0.90.0",
1196 .owner = THIS_MODULE,
1197 .load_super = super_90_load,
1198 .validate_super = super_90_validate,
1199 .sync_super = super_90_sync,
1200 },
1201 [1] = {
1202 .name = "md-1",
1203 .owner = THIS_MODULE,
1204 .load_super = super_1_load,
1205 .validate_super = super_1_validate,
1206 .sync_super = super_1_sync,
1207 },
1208};
1209
1210static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1211{
1212 struct list_head *tmp;
1213 mdk_rdev_t *rdev;
1214
1215 ITERATE_RDEV(mddev,rdev,tmp)
1216 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1217 return rdev;
1218
1219 return NULL;
1220}
1221
1222static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1223{
1224 struct list_head *tmp;
1225 mdk_rdev_t *rdev;
1226
1227 ITERATE_RDEV(mddev1,rdev,tmp)
1228 if (match_dev_unit(mddev2, rdev))
1229 return 1;
1230
1231 return 0;
1232}
1233
1234static LIST_HEAD(pending_raid_disks);
1235
1236static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1237{
1238 mdk_rdev_t *same_pdev;
1239 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001240 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001241 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 if (rdev->mddev) {
1244 MD_BUG();
1245 return -EINVAL;
1246 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001247 /* make sure rdev->size exceeds mddev->size */
1248 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1249 if (mddev->pers)
1250 /* Cannot change size, so fail */
1251 return -ENOSPC;
1252 else
1253 mddev->size = rdev->size;
1254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 same_pdev = match_dev_unit(mddev, rdev);
1256 if (same_pdev)
1257 printk(KERN_WARNING
1258 "%s: WARNING: %s appears to be on the same physical"
1259 " disk as %s. True\n protection against single-disk"
1260 " failure might be compromised.\n",
1261 mdname(mddev), bdevname(rdev->bdev,b),
1262 bdevname(same_pdev->bdev,b2));
1263
1264 /* Verify rdev->desc_nr is unique.
1265 * If it is -1, assign a free number, else
1266 * check number is not in use
1267 */
1268 if (rdev->desc_nr < 0) {
1269 int choice = 0;
1270 if (mddev->pers) choice = mddev->raid_disks;
1271 while (find_rdev_nr(mddev, choice))
1272 choice++;
1273 rdev->desc_nr = choice;
1274 } else {
1275 if (find_rdev_nr(mddev, rdev->desc_nr))
1276 return -EBUSY;
1277 }
NeilBrown19133a42005-11-08 21:39:35 -08001278 bdevname(rdev->bdev,b);
1279 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1280 return -ENOMEM;
Neil Brown1edf80d2006-01-12 01:05:23 -08001281 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1282 *s = '!';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 list_add(&rdev->same_set, &mddev->disks);
1285 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001286 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001287
NeilBrown9c791972005-11-08 21:39:28 -08001288 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001289 kobject_add(&rdev->kobj);
1290
NeilBrownf637b9f2005-11-08 21:39:37 -08001291 if (rdev->bdev->bd_part)
1292 ko = &rdev->bdev->bd_part->kobj;
1293 else
1294 ko = &rdev->bdev->bd_disk->kobj;
1295 sysfs_create_link(&rdev->kobj, ko, "block");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 0;
1297}
1298
1299static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1300{
1301 char b[BDEVNAME_SIZE];
1302 if (!rdev->mddev) {
1303 MD_BUG();
1304 return;
1305 }
1306 list_del_init(&rdev->same_set);
1307 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1308 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001309 sysfs_remove_link(&rdev->kobj, "block");
1310 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313/*
1314 * prevent the device from being mounted, repartitioned or
1315 * otherwise reused by a RAID array (or any other kernel
1316 * subsystem), by bd_claiming the device.
1317 */
1318static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1319{
1320 int err = 0;
1321 struct block_device *bdev;
1322 char b[BDEVNAME_SIZE];
1323
1324 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1325 if (IS_ERR(bdev)) {
1326 printk(KERN_ERR "md: could not open %s.\n",
1327 __bdevname(dev, b));
1328 return PTR_ERR(bdev);
1329 }
1330 err = bd_claim(bdev, rdev);
1331 if (err) {
1332 printk(KERN_ERR "md: could not bd_claim %s.\n",
1333 bdevname(bdev, b));
1334 blkdev_put(bdev);
1335 return err;
1336 }
1337 rdev->bdev = bdev;
1338 return err;
1339}
1340
1341static void unlock_rdev(mdk_rdev_t *rdev)
1342{
1343 struct block_device *bdev = rdev->bdev;
1344 rdev->bdev = NULL;
1345 if (!bdev)
1346 MD_BUG();
1347 bd_release(bdev);
1348 blkdev_put(bdev);
1349}
1350
1351void md_autodetect_dev(dev_t dev);
1352
1353static void export_rdev(mdk_rdev_t * rdev)
1354{
1355 char b[BDEVNAME_SIZE];
1356 printk(KERN_INFO "md: export_rdev(%s)\n",
1357 bdevname(rdev->bdev,b));
1358 if (rdev->mddev)
1359 MD_BUG();
1360 free_disk_sb(rdev);
1361 list_del_init(&rdev->same_set);
1362#ifndef MODULE
1363 md_autodetect_dev(rdev->bdev->bd_dev);
1364#endif
1365 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001366 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
1369static void kick_rdev_from_array(mdk_rdev_t * rdev)
1370{
1371 unbind_rdev_from_array(rdev);
1372 export_rdev(rdev);
1373}
1374
1375static void export_array(mddev_t *mddev)
1376{
1377 struct list_head *tmp;
1378 mdk_rdev_t *rdev;
1379
1380 ITERATE_RDEV(mddev,rdev,tmp) {
1381 if (!rdev->mddev) {
1382 MD_BUG();
1383 continue;
1384 }
1385 kick_rdev_from_array(rdev);
1386 }
1387 if (!list_empty(&mddev->disks))
1388 MD_BUG();
1389 mddev->raid_disks = 0;
1390 mddev->major_version = 0;
1391}
1392
1393static void print_desc(mdp_disk_t *desc)
1394{
1395 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1396 desc->major,desc->minor,desc->raid_disk,desc->state);
1397}
1398
1399static void print_sb(mdp_super_t *sb)
1400{
1401 int i;
1402
1403 printk(KERN_INFO
1404 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1405 sb->major_version, sb->minor_version, sb->patch_version,
1406 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1407 sb->ctime);
1408 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1409 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1410 sb->md_minor, sb->layout, sb->chunk_size);
1411 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1412 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1413 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1414 sb->failed_disks, sb->spare_disks,
1415 sb->sb_csum, (unsigned long)sb->events_lo);
1416
1417 printk(KERN_INFO);
1418 for (i = 0; i < MD_SB_DISKS; i++) {
1419 mdp_disk_t *desc;
1420
1421 desc = sb->disks + i;
1422 if (desc->number || desc->major || desc->minor ||
1423 desc->raid_disk || (desc->state && (desc->state != 4))) {
1424 printk(" D %2d: ", i);
1425 print_desc(desc);
1426 }
1427 }
1428 printk(KERN_INFO "md: THIS: ");
1429 print_desc(&sb->this_disk);
1430
1431}
1432
1433static void print_rdev(mdk_rdev_t *rdev)
1434{
1435 char b[BDEVNAME_SIZE];
1436 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1437 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001438 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1439 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (rdev->sb_loaded) {
1441 printk(KERN_INFO "md: rdev superblock:\n");
1442 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1443 } else
1444 printk(KERN_INFO "md: no rdev superblock!\n");
1445}
1446
1447void md_print_devices(void)
1448{
1449 struct list_head *tmp, *tmp2;
1450 mdk_rdev_t *rdev;
1451 mddev_t *mddev;
1452 char b[BDEVNAME_SIZE];
1453
1454 printk("\n");
1455 printk("md: **********************************\n");
1456 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1457 printk("md: **********************************\n");
1458 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
NeilBrown32a76272005-06-21 17:17:14 -07001460 if (mddev->bitmap)
1461 bitmap_print_sb(mddev->bitmap);
1462 else
1463 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 ITERATE_RDEV(mddev,rdev,tmp2)
1465 printk("<%s>", bdevname(rdev->bdev,b));
1466 printk("\n");
1467
1468 ITERATE_RDEV(mddev,rdev,tmp2)
1469 print_rdev(rdev);
1470 }
1471 printk("md: **********************************\n");
1472 printk("\n");
1473}
1474
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476static void sync_sbs(mddev_t * mddev)
1477{
1478 mdk_rdev_t *rdev;
1479 struct list_head *tmp;
1480
1481 ITERATE_RDEV(mddev,rdev,tmp) {
1482 super_types[mddev->major_version].
1483 sync_super(mddev, rdev);
1484 rdev->sb_loaded = 1;
1485 }
1486}
1487
1488static void md_update_sb(mddev_t * mddev)
1489{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001490 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 struct list_head *tmp;
1492 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001493 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001496 spin_lock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001497 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 mddev->utime = get_seconds();
1499 mddev->events ++;
1500
1501 if (!mddev->events) {
1502 /*
1503 * oops, this 64-bit counter should never wrap.
1504 * Either we are in around ~1 trillion A.C., assuming
1505 * 1 reboot per second, or we have a bug:
1506 */
1507 MD_BUG();
1508 mddev->events --;
1509 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001510 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 sync_sbs(mddev);
1512
1513 /*
1514 * do not write anything to disk if using
1515 * nonpersistent superblocks
1516 */
NeilBrown06d91a52005-06-21 17:17:12 -07001517 if (!mddev->persistent) {
1518 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001519 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001520 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001522 }
NeilBrowna9701a32005-11-08 21:39:34 -08001523 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 dprintk(KERN_INFO
1526 "md: updating %s RAID superblock on device (in sync %d)\n",
1527 mdname(mddev),mddev->in_sync);
1528
NeilBrown32a76272005-06-21 17:17:14 -07001529 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 ITERATE_RDEV(mddev,rdev,tmp) {
1531 char b[BDEVNAME_SIZE];
1532 dprintk(KERN_INFO "md: ");
NeilBrownb2d444d2005-11-08 21:39:31 -08001533 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 dprintk("(skipping faulty ");
1535
1536 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001537 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001538 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001539 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001540 rdev->sb_page);
1541 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1542 bdevname(rdev->bdev,b),
1543 (unsigned long long)rdev->sb_offset);
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 } else
1546 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001547 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 /* only need to write one superblock... */
1549 break;
1550 }
NeilBrowna9701a32005-11-08 21:39:34 -08001551 md_super_wait(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001552 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1553
NeilBrowna9701a32005-11-08 21:39:34 -08001554 spin_lock_irq(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001555 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001556 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001557 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001558 goto repeat;
1559 }
1560 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001561 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001562 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
NeilBrownbce74da2006-01-06 00:20:41 -08001566/* words written to sysfs files may, or my not, be \n terminated.
1567 * We want to accept with case. For this we use cmd_match.
1568 */
1569static int cmd_match(const char *cmd, const char *str)
1570{
1571 /* See if cmd, written into a sysfs file, matches
1572 * str. They must either be the same, or cmd can
1573 * have a trailing newline
1574 */
1575 while (*cmd && *str && *cmd == *str) {
1576 cmd++;
1577 str++;
1578 }
1579 if (*cmd == '\n')
1580 cmd++;
1581 if (*str || *cmd)
1582 return 0;
1583 return 1;
1584}
1585
NeilBrown86e6ffd2005-11-08 21:39:24 -08001586struct rdev_sysfs_entry {
1587 struct attribute attr;
1588 ssize_t (*show)(mdk_rdev_t *, char *);
1589 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1590};
1591
1592static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001593state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001594{
1595 char *sep = "";
1596 int len=0;
1597
NeilBrownb2d444d2005-11-08 21:39:31 -08001598 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001599 len+= sprintf(page+len, "%sfaulty",sep);
1600 sep = ",";
1601 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001602 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001603 len += sprintf(page+len, "%sin_sync",sep);
1604 sep = ",";
1605 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001606 if (!test_bit(Faulty, &rdev->flags) &&
1607 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001608 len += sprintf(page+len, "%sspare", sep);
1609 sep = ",";
1610 }
1611 return len+sprintf(page+len, "\n");
1612}
1613
NeilBrown96de1e62005-11-08 21:39:39 -08001614static struct rdev_sysfs_entry
1615rdev_state = __ATTR_RO(state);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001616
1617static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001618super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001619{
1620 if (rdev->sb_loaded && rdev->sb_size) {
1621 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1622 return rdev->sb_size;
1623 } else
1624 return 0;
1625}
NeilBrown96de1e62005-11-08 21:39:39 -08001626static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1627
NeilBrown4dbcdc72006-01-06 00:20:52 -08001628static ssize_t
1629errors_show(mdk_rdev_t *rdev, char *page)
1630{
1631 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1632}
1633
1634static ssize_t
1635errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1636{
1637 char *e;
1638 unsigned long n = simple_strtoul(buf, &e, 10);
1639 if (*buf && (*e == 0 || *e == '\n')) {
1640 atomic_set(&rdev->corrected_errors, n);
1641 return len;
1642 }
1643 return -EINVAL;
1644}
1645static struct rdev_sysfs_entry rdev_errors =
1646__ATTR(errors, 0644, errors_show, errors_store);
1647
NeilBrown014236d2006-01-06 00:20:55 -08001648static ssize_t
1649slot_show(mdk_rdev_t *rdev, char *page)
1650{
1651 if (rdev->raid_disk < 0)
1652 return sprintf(page, "none\n");
1653 else
1654 return sprintf(page, "%d\n", rdev->raid_disk);
1655}
1656
1657static ssize_t
1658slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1659{
1660 char *e;
1661 int slot = simple_strtoul(buf, &e, 10);
1662 if (strncmp(buf, "none", 4)==0)
1663 slot = -1;
1664 else if (e==buf || (*e && *e!= '\n'))
1665 return -EINVAL;
1666 if (rdev->mddev->pers)
1667 /* Cannot set slot in active array (yet) */
1668 return -EBUSY;
1669 if (slot >= rdev->mddev->raid_disks)
1670 return -ENOSPC;
1671 rdev->raid_disk = slot;
1672 /* assume it is working */
1673 rdev->flags = 0;
1674 set_bit(In_sync, &rdev->flags);
1675 return len;
1676}
1677
1678
1679static struct rdev_sysfs_entry rdev_slot =
1680__ATTR(slot, 0644, slot_show, slot_store);
1681
NeilBrown93c8cad2006-01-06 00:20:56 -08001682static ssize_t
1683offset_show(mdk_rdev_t *rdev, char *page)
1684{
Andrew Morton6961ece2006-01-06 00:20:59 -08001685 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001686}
1687
1688static ssize_t
1689offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1690{
1691 char *e;
1692 unsigned long long offset = simple_strtoull(buf, &e, 10);
1693 if (e==buf || (*e && *e != '\n'))
1694 return -EINVAL;
1695 if (rdev->mddev->pers)
1696 return -EBUSY;
1697 rdev->data_offset = offset;
1698 return len;
1699}
1700
1701static struct rdev_sysfs_entry rdev_offset =
1702__ATTR(offset, 0644, offset_show, offset_store);
1703
NeilBrown83303b62006-01-06 00:21:06 -08001704static ssize_t
1705rdev_size_show(mdk_rdev_t *rdev, char *page)
1706{
1707 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1708}
1709
1710static ssize_t
1711rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1712{
1713 char *e;
1714 unsigned long long size = simple_strtoull(buf, &e, 10);
1715 if (e==buf || (*e && *e != '\n'))
1716 return -EINVAL;
1717 if (rdev->mddev->pers)
1718 return -EBUSY;
1719 rdev->size = size;
1720 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1721 rdev->mddev->size = size;
1722 return len;
1723}
1724
1725static struct rdev_sysfs_entry rdev_size =
1726__ATTR(size, 0644, rdev_size_show, rdev_size_store);
1727
NeilBrown86e6ffd2005-11-08 21:39:24 -08001728static struct attribute *rdev_default_attrs[] = {
1729 &rdev_state.attr,
1730 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001731 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001732 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001733 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001734 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001735 NULL,
1736};
1737static ssize_t
1738rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1739{
1740 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1741 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1742
1743 if (!entry->show)
1744 return -EIO;
1745 return entry->show(rdev, page);
1746}
1747
1748static ssize_t
1749rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1750 const char *page, size_t length)
1751{
1752 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1753 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1754
1755 if (!entry->store)
1756 return -EIO;
1757 return entry->store(rdev, page, length);
1758}
1759
1760static void rdev_free(struct kobject *ko)
1761{
1762 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1763 kfree(rdev);
1764}
1765static struct sysfs_ops rdev_sysfs_ops = {
1766 .show = rdev_attr_show,
1767 .store = rdev_attr_store,
1768};
1769static struct kobj_type rdev_ktype = {
1770 .release = rdev_free,
1771 .sysfs_ops = &rdev_sysfs_ops,
1772 .default_attrs = rdev_default_attrs,
1773};
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775/*
1776 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1777 *
1778 * mark the device faulty if:
1779 *
1780 * - the device is nonexistent (zero size)
1781 * - the device has no valid superblock
1782 *
1783 * a faulty rdev _never_ has rdev->sb set.
1784 */
1785static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1786{
1787 char b[BDEVNAME_SIZE];
1788 int err;
1789 mdk_rdev_t *rdev;
1790 sector_t size;
1791
NeilBrown9ffae0c2006-01-06 00:20:32 -08001792 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (!rdev) {
1794 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1795 return ERR_PTR(-ENOMEM);
1796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 if ((err = alloc_disk_sb(rdev)))
1799 goto abort_free;
1800
1801 err = lock_rdev(rdev, newdev);
1802 if (err)
1803 goto abort_free;
1804
NeilBrown86e6ffd2005-11-08 21:39:24 -08001805 rdev->kobj.parent = NULL;
1806 rdev->kobj.ktype = &rdev_ktype;
1807 kobject_init(&rdev->kobj);
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 rdev->desc_nr = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001810 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 rdev->data_offset = 0;
1812 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001813 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001814 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1817 if (!size) {
1818 printk(KERN_WARNING
1819 "md: %s has zero or unknown size, marking faulty!\n",
1820 bdevname(rdev->bdev,b));
1821 err = -EINVAL;
1822 goto abort_free;
1823 }
1824
1825 if (super_format >= 0) {
1826 err = super_types[super_format].
1827 load_super(rdev, NULL, super_minor);
1828 if (err == -EINVAL) {
1829 printk(KERN_WARNING
1830 "md: %s has invalid sb, not importing!\n",
1831 bdevname(rdev->bdev,b));
1832 goto abort_free;
1833 }
1834 if (err < 0) {
1835 printk(KERN_WARNING
1836 "md: could not read %s's sb, not importing!\n",
1837 bdevname(rdev->bdev,b));
1838 goto abort_free;
1839 }
1840 }
1841 INIT_LIST_HEAD(&rdev->same_set);
1842
1843 return rdev;
1844
1845abort_free:
1846 if (rdev->sb_page) {
1847 if (rdev->bdev)
1848 unlock_rdev(rdev);
1849 free_disk_sb(rdev);
1850 }
1851 kfree(rdev);
1852 return ERR_PTR(err);
1853}
1854
1855/*
1856 * Check a full RAID array for plausibility
1857 */
1858
1859
NeilBrowna757e642005-04-16 15:26:42 -07001860static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 int i;
1863 struct list_head *tmp;
1864 mdk_rdev_t *rdev, *freshest;
1865 char b[BDEVNAME_SIZE];
1866
1867 freshest = NULL;
1868 ITERATE_RDEV(mddev,rdev,tmp)
1869 switch (super_types[mddev->major_version].
1870 load_super(rdev, freshest, mddev->minor_version)) {
1871 case 1:
1872 freshest = rdev;
1873 break;
1874 case 0:
1875 break;
1876 default:
1877 printk( KERN_ERR \
1878 "md: fatal superblock inconsistency in %s"
1879 " -- removing from array\n",
1880 bdevname(rdev->bdev,b));
1881 kick_rdev_from_array(rdev);
1882 }
1883
1884
1885 super_types[mddev->major_version].
1886 validate_super(mddev, freshest);
1887
1888 i = 0;
1889 ITERATE_RDEV(mddev,rdev,tmp) {
1890 if (rdev != freshest)
1891 if (super_types[mddev->major_version].
1892 validate_super(mddev, rdev)) {
1893 printk(KERN_WARNING "md: kicking non-fresh %s"
1894 " from array!\n",
1895 bdevname(rdev->bdev,b));
1896 kick_rdev_from_array(rdev);
1897 continue;
1898 }
1899 if (mddev->level == LEVEL_MULTIPATH) {
1900 rdev->desc_nr = i++;
1901 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001902 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904 }
1905
1906
1907
1908 if (mddev->recovery_cp != MaxSector &&
1909 mddev->level >= 1)
1910 printk(KERN_ERR "md: %s: raid array is not clean"
1911 " -- starting background reconstruction\n",
1912 mdname(mddev));
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
NeilBrowneae17012005-11-08 21:39:23 -08001916static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001917level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08001918{
NeilBrown2604b702006-01-06 00:20:36 -08001919 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08001920 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08001921 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08001922 else if (mddev->clevel[0])
1923 return sprintf(page, "%s\n", mddev->clevel);
1924 else if (mddev->level != LEVEL_NONE)
1925 return sprintf(page, "%d\n", mddev->level);
1926 else
1927 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08001928}
1929
NeilBrownd9d166c2006-01-06 00:20:51 -08001930static ssize_t
1931level_store(mddev_t *mddev, const char *buf, size_t len)
1932{
1933 int rv = len;
1934 if (mddev->pers)
1935 return -EBUSY;
1936 if (len == 0)
1937 return 0;
1938 if (len >= sizeof(mddev->clevel))
1939 return -ENOSPC;
1940 strncpy(mddev->clevel, buf, len);
1941 if (mddev->clevel[len-1] == '\n')
1942 len--;
1943 mddev->clevel[len] = 0;
1944 mddev->level = LEVEL_NONE;
1945 return rv;
1946}
1947
1948static struct md_sysfs_entry md_level =
1949__ATTR(level, 0644, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08001950
1951static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001952raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08001953{
NeilBrownbb636542005-11-08 21:39:45 -08001954 if (mddev->raid_disks == 0)
1955 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08001956 return sprintf(page, "%d\n", mddev->raid_disks);
1957}
1958
NeilBrownda943b992006-01-06 00:20:54 -08001959static int update_raid_disks(mddev_t *mddev, int raid_disks);
1960
1961static ssize_t
1962raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
1963{
1964 /* can only set raid_disks if array is not yet active */
1965 char *e;
1966 int rv = 0;
1967 unsigned long n = simple_strtoul(buf, &e, 10);
1968
1969 if (!*buf || (*e && *e != '\n'))
1970 return -EINVAL;
1971
1972 if (mddev->pers)
1973 rv = update_raid_disks(mddev, n);
1974 else
1975 mddev->raid_disks = n;
1976 return rv ? rv : len;
1977}
1978static struct md_sysfs_entry md_raid_disks =
1979__ATTR(raid_disks, 0644, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08001980
NeilBrown24dd4692005-11-08 21:39:26 -08001981static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08001982chunk_size_show(mddev_t *mddev, char *page)
1983{
1984 return sprintf(page, "%d\n", mddev->chunk_size);
1985}
1986
1987static ssize_t
1988chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
1989{
1990 /* can only set chunk_size if array is not yet active */
1991 char *e;
1992 unsigned long n = simple_strtoul(buf, &e, 10);
1993
1994 if (mddev->pers)
1995 return -EBUSY;
1996 if (!*buf || (*e && *e != '\n'))
1997 return -EINVAL;
1998
1999 mddev->chunk_size = n;
2000 return len;
2001}
2002static struct md_sysfs_entry md_chunk_size =
2003__ATTR(chunk_size, 0644, chunk_size_show, chunk_size_store);
2004
NeilBrown6d7ff732006-01-06 00:21:16 -08002005static ssize_t
2006null_show(mddev_t *mddev, char *page)
2007{
2008 return -EINVAL;
2009}
2010
2011static ssize_t
2012new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2013{
2014 /* buf must be %d:%d\n? giving major and minor numbers */
2015 /* The new device is added to the array.
2016 * If the array has a persistent superblock, we read the
2017 * superblock to initialise info and check validity.
2018 * Otherwise, only checking done is that in bind_rdev_to_array,
2019 * which mainly checks size.
2020 */
2021 char *e;
2022 int major = simple_strtoul(buf, &e, 10);
2023 int minor;
2024 dev_t dev;
2025 mdk_rdev_t *rdev;
2026 int err;
2027
2028 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2029 return -EINVAL;
2030 minor = simple_strtoul(e+1, &e, 10);
2031 if (*e && *e != '\n')
2032 return -EINVAL;
2033 dev = MKDEV(major, minor);
2034 if (major != MAJOR(dev) ||
2035 minor != MINOR(dev))
2036 return -EOVERFLOW;
2037
2038
2039 if (mddev->persistent) {
2040 rdev = md_import_device(dev, mddev->major_version,
2041 mddev->minor_version);
2042 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2043 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2044 mdk_rdev_t, same_set);
2045 err = super_types[mddev->major_version]
2046 .load_super(rdev, rdev0, mddev->minor_version);
2047 if (err < 0)
2048 goto out;
2049 }
2050 } else
2051 rdev = md_import_device(dev, -1, -1);
2052
2053 if (IS_ERR(rdev))
2054 return PTR_ERR(rdev);
2055 err = bind_rdev_to_array(rdev, mddev);
2056 out:
2057 if (err)
2058 export_rdev(rdev);
2059 return err ? err : len;
2060}
2061
2062static struct md_sysfs_entry md_new_device =
2063__ATTR(new_dev, 0200, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002064
2065static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002066size_show(mddev_t *mddev, char *page)
2067{
2068 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2069}
2070
2071static int update_size(mddev_t *mddev, unsigned long size);
2072
2073static ssize_t
2074size_store(mddev_t *mddev, const char *buf, size_t len)
2075{
2076 /* If array is inactive, we can reduce the component size, but
2077 * not increase it (except from 0).
2078 * If array is active, we can try an on-line resize
2079 */
2080 char *e;
2081 int err = 0;
2082 unsigned long long size = simple_strtoull(buf, &e, 10);
2083 if (!*buf || *buf == '\n' ||
2084 (*e && *e != '\n'))
2085 return -EINVAL;
2086
2087 if (mddev->pers) {
2088 err = update_size(mddev, size);
2089 md_update_sb(mddev);
2090 } else {
2091 if (mddev->size == 0 ||
2092 mddev->size > size)
2093 mddev->size = size;
2094 else
2095 err = -ENOSPC;
2096 }
2097 return err ? err : len;
2098}
2099
2100static struct md_sysfs_entry md_size =
2101__ATTR(component_size, 0644, size_show, size_store);
2102
NeilBrown8bb93aa2006-01-06 00:20:50 -08002103
2104/* Metdata version.
2105 * This is either 'none' for arrays with externally managed metadata,
2106 * or N.M for internally known formats
2107 */
2108static ssize_t
2109metadata_show(mddev_t *mddev, char *page)
2110{
2111 if (mddev->persistent)
2112 return sprintf(page, "%d.%d\n",
2113 mddev->major_version, mddev->minor_version);
2114 else
2115 return sprintf(page, "none\n");
2116}
2117
2118static ssize_t
2119metadata_store(mddev_t *mddev, const char *buf, size_t len)
2120{
2121 int major, minor;
2122 char *e;
2123 if (!list_empty(&mddev->disks))
2124 return -EBUSY;
2125
2126 if (cmd_match(buf, "none")) {
2127 mddev->persistent = 0;
2128 mddev->major_version = 0;
2129 mddev->minor_version = 90;
2130 return len;
2131 }
2132 major = simple_strtoul(buf, &e, 10);
2133 if (e==buf || *e != '.')
2134 return -EINVAL;
2135 buf = e+1;
2136 minor = simple_strtoul(buf, &e, 10);
2137 if (e==buf || *e != '\n')
2138 return -EINVAL;
2139 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2140 super_types[major].name == NULL)
2141 return -ENOENT;
2142 mddev->major_version = major;
2143 mddev->minor_version = minor;
2144 mddev->persistent = 1;
2145 return len;
2146}
2147
2148static struct md_sysfs_entry md_metadata =
2149__ATTR(metadata_version, 0644, metadata_show, metadata_store);
2150
NeilBrowna35b0d62006-01-06 00:20:49 -08002151static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002152action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002153{
NeilBrown7eec3142005-11-08 21:39:44 -08002154 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002155 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2156 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
2157 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002158 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2159 type = "resync";
2160 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2161 type = "check";
2162 else
2163 type = "repair";
2164 } else
2165 type = "recover";
2166 }
2167 return sprintf(page, "%s\n", type);
2168}
2169
2170static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002171action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002172{
NeilBrown7eec3142005-11-08 21:39:44 -08002173 if (!mddev->pers || !mddev->pers->sync_request)
2174 return -EINVAL;
2175
NeilBrownbce74da2006-01-06 00:20:41 -08002176 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002177 if (mddev->sync_thread) {
2178 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2179 md_unregister_thread(mddev->sync_thread);
2180 mddev->sync_thread = NULL;
2181 mddev->recovery = 0;
2182 }
NeilBrown03c902e2006-01-06 00:20:46 -08002183 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2184 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002185 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002186 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002187 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2188 else {
NeilBrownbce74da2006-01-06 00:20:41 -08002189 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002190 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownbce74da2006-01-06 00:20:41 -08002191 else if (cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002192 return -EINVAL;
2193 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2194 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002195 }
NeilBrown03c902e2006-01-06 00:20:46 -08002196 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002197 md_wakeup_thread(mddev->thread);
2198 return len;
2199}
2200
NeilBrown9d888832005-11-08 21:39:26 -08002201static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002202mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002203{
2204 return sprintf(page, "%llu\n",
2205 (unsigned long long) mddev->resync_mismatches);
2206}
2207
NeilBrown96de1e62005-11-08 21:39:39 -08002208static struct md_sysfs_entry
NeilBrown7eec3142005-11-08 21:39:44 -08002209md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002210
NeilBrown96de1e62005-11-08 21:39:39 -08002211
2212static struct md_sysfs_entry
2213md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002214
NeilBrown88202a02006-01-06 00:21:36 -08002215static ssize_t
2216sync_min_show(mddev_t *mddev, char *page)
2217{
2218 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2219 mddev->sync_speed_min ? "local": "system");
2220}
2221
2222static ssize_t
2223sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2224{
2225 int min;
2226 char *e;
2227 if (strncmp(buf, "system", 6)==0) {
2228 mddev->sync_speed_min = 0;
2229 return len;
2230 }
2231 min = simple_strtoul(buf, &e, 10);
2232 if (buf == e || (*e && *e != '\n') || min <= 0)
2233 return -EINVAL;
2234 mddev->sync_speed_min = min;
2235 return len;
2236}
2237
2238static struct md_sysfs_entry md_sync_min =
2239__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2240
2241static ssize_t
2242sync_max_show(mddev_t *mddev, char *page)
2243{
2244 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2245 mddev->sync_speed_max ? "local": "system");
2246}
2247
2248static ssize_t
2249sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2250{
2251 int max;
2252 char *e;
2253 if (strncmp(buf, "system", 6)==0) {
2254 mddev->sync_speed_max = 0;
2255 return len;
2256 }
2257 max = simple_strtoul(buf, &e, 10);
2258 if (buf == e || (*e && *e != '\n') || max <= 0)
2259 return -EINVAL;
2260 mddev->sync_speed_max = max;
2261 return len;
2262}
2263
2264static struct md_sysfs_entry md_sync_max =
2265__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2266
2267
2268static ssize_t
2269sync_speed_show(mddev_t *mddev, char *page)
2270{
2271 unsigned long resync, dt, db;
2272 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2273 dt = ((jiffies - mddev->resync_mark) / HZ);
2274 if (!dt) dt++;
2275 db = resync - (mddev->resync_mark_cnt);
2276 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2277}
2278
2279static struct md_sysfs_entry
2280md_sync_speed = __ATTR_RO(sync_speed);
2281
2282static ssize_t
2283sync_completed_show(mddev_t *mddev, char *page)
2284{
2285 unsigned long max_blocks, resync;
2286
2287 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2288 max_blocks = mddev->resync_max_sectors;
2289 else
2290 max_blocks = mddev->size << 1;
2291
2292 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2293 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2294}
2295
2296static struct md_sysfs_entry
2297md_sync_completed = __ATTR_RO(sync_completed);
2298
NeilBrowneae17012005-11-08 21:39:23 -08002299static struct attribute *md_default_attrs[] = {
2300 &md_level.attr,
2301 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002302 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002303 &md_size.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002304 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08002305 &md_new_device.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002306 NULL,
2307};
2308
2309static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002310 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002311 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002312 &md_sync_min.attr,
2313 &md_sync_max.attr,
2314 &md_sync_speed.attr,
2315 &md_sync_completed.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002316 NULL,
2317};
NeilBrown411036f2005-11-08 21:39:40 -08002318static struct attribute_group md_redundancy_group = {
2319 .name = NULL,
2320 .attrs = md_redundancy_attrs,
2321};
2322
NeilBrowneae17012005-11-08 21:39:23 -08002323
2324static ssize_t
2325md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2326{
2327 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2328 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002329 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002330
2331 if (!entry->show)
2332 return -EIO;
NeilBrown96de1e62005-11-08 21:39:39 -08002333 mddev_lock(mddev);
2334 rv = entry->show(mddev, page);
2335 mddev_unlock(mddev);
2336 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002337}
2338
2339static ssize_t
2340md_attr_store(struct kobject *kobj, struct attribute *attr,
2341 const char *page, size_t length)
2342{
2343 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2344 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002345 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002346
2347 if (!entry->store)
2348 return -EIO;
NeilBrown96de1e62005-11-08 21:39:39 -08002349 mddev_lock(mddev);
2350 rv = entry->store(mddev, page, length);
2351 mddev_unlock(mddev);
2352 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002353}
2354
2355static void md_free(struct kobject *ko)
2356{
2357 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2358 kfree(mddev);
2359}
2360
2361static struct sysfs_ops md_sysfs_ops = {
2362 .show = md_attr_show,
2363 .store = md_attr_store,
2364};
2365static struct kobj_type md_ktype = {
2366 .release = md_free,
2367 .sysfs_ops = &md_sysfs_ops,
2368 .default_attrs = md_default_attrs,
2369};
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371int mdp_major = 0;
2372
2373static struct kobject *md_probe(dev_t dev, int *part, void *data)
2374{
2375 static DECLARE_MUTEX(disks_sem);
2376 mddev_t *mddev = mddev_find(dev);
2377 struct gendisk *disk;
2378 int partitioned = (MAJOR(dev) != MD_MAJOR);
2379 int shift = partitioned ? MdpMinorShift : 0;
2380 int unit = MINOR(dev) >> shift;
2381
2382 if (!mddev)
2383 return NULL;
2384
2385 down(&disks_sem);
2386 if (mddev->gendisk) {
2387 up(&disks_sem);
2388 mddev_put(mddev);
2389 return NULL;
2390 }
2391 disk = alloc_disk(1 << shift);
2392 if (!disk) {
2393 up(&disks_sem);
2394 mddev_put(mddev);
2395 return NULL;
2396 }
2397 disk->major = MAJOR(dev);
2398 disk->first_minor = unit << shift;
2399 if (partitioned) {
2400 sprintf(disk->disk_name, "md_d%d", unit);
2401 sprintf(disk->devfs_name, "md/d%d", unit);
2402 } else {
2403 sprintf(disk->disk_name, "md%d", unit);
2404 sprintf(disk->devfs_name, "md/%d", unit);
2405 }
2406 disk->fops = &md_fops;
2407 disk->private_data = mddev;
2408 disk->queue = mddev->queue;
2409 add_disk(disk);
2410 mddev->gendisk = disk;
2411 up(&disks_sem);
NeilBrown9c791972005-11-08 21:39:28 -08002412 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08002413 mddev->kobj.k_name = NULL;
2414 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2415 mddev->kobj.ktype = &md_ktype;
2416 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return NULL;
2418}
2419
2420void md_wakeup_thread(mdk_thread_t *thread);
2421
2422static void md_safemode_timeout(unsigned long data)
2423{
2424 mddev_t *mddev = (mddev_t *) data;
2425
2426 mddev->safemode = 1;
2427 md_wakeup_thread(mddev->thread);
2428}
2429
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002430static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432static int do_md_run(mddev_t * mddev)
2433{
NeilBrown2604b702006-01-06 00:20:36 -08002434 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 int chunk_size;
2436 struct list_head *tmp;
2437 mdk_rdev_t *rdev;
2438 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08002439 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 char b[BDEVNAME_SIZE];
2441
NeilBrowna757e642005-04-16 15:26:42 -07002442 if (list_empty(&mddev->disks))
2443 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 if (mddev->pers)
2447 return -EBUSY;
2448
2449 /*
2450 * Analyze all RAID superblock(s)
2451 */
NeilBrowna757e642005-04-16 15:26:42 -07002452 if (!mddev->raid_disks)
2453 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
NeilBrown2604b702006-01-06 00:20:36 -08002457 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (chunk_size > MAX_CHUNK_SIZE) {
2459 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2460 chunk_size, MAX_CHUNK_SIZE);
2461 return -EINVAL;
2462 }
2463 /*
2464 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2465 */
2466 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07002467 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return -EINVAL;
2469 }
2470 if (chunk_size < PAGE_SIZE) {
2471 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2472 chunk_size, PAGE_SIZE);
2473 return -EINVAL;
2474 }
2475
2476 /* devices must have minimum size of one chunk */
2477 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002478 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 continue;
2480 if (rdev->size < chunk_size / 1024) {
2481 printk(KERN_WARNING
2482 "md: Dev %s smaller than chunk_size:"
2483 " %lluk < %dk\n",
2484 bdevname(rdev->bdev,b),
2485 (unsigned long long)rdev->size,
2486 chunk_size / 1024);
2487 return -EINVAL;
2488 }
2489 }
2490 }
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08002493 if (mddev->level != LEVEL_NONE)
2494 request_module("md-level-%d", mddev->level);
2495 else if (mddev->clevel[0])
2496 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497#endif
2498
2499 /*
2500 * Drop all container device buffers, from now on
2501 * the only valid external interface is through the md
2502 * device.
2503 * Also find largest hardsector size
2504 */
2505 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002506 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 continue;
2508 sync_blockdev(rdev->bdev);
2509 invalidate_bdev(rdev->bdev, 0);
2510 }
2511
2512 md_probe(mddev->unit, NULL, NULL);
2513 disk = mddev->gendisk;
2514 if (!disk)
2515 return -ENOMEM;
2516
2517 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002518 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08002519 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002521 if (mddev->level != LEVEL_NONE)
2522 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
2523 mddev->level);
2524 else
2525 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
2526 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return -EINVAL;
2528 }
NeilBrown2604b702006-01-06 00:20:36 -08002529 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002531 mddev->level = pers->level;
2532 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
NeilBrown657390d2005-08-26 18:34:16 -07002534 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08002536 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002537 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
NeilBrownf91de922005-11-08 21:39:36 -08002539 if (start_readonly)
2540 mddev->ro = 2; /* read-only, but switch on first write */
2541
NeilBrownb15c2e52006-01-06 00:20:16 -08002542 err = mddev->pers->run(mddev);
2543 if (!err && mddev->pers->sync_request) {
2544 err = bitmap_create(mddev);
2545 if (err) {
2546 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2547 mdname(mddev), err);
2548 mddev->pers->stop(mddev);
2549 }
2550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (err) {
2552 printk(KERN_ERR "md: pers->run() failed ...\n");
2553 module_put(mddev->pers->owner);
2554 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002555 bitmap_destroy(mddev);
2556 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
NeilBrown411036f2005-11-08 21:39:40 -08002558 if (mddev->pers->sync_request)
2559 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08002560 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2561 mddev->ro = 0;
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 atomic_set(&mddev->writes_pending,0);
2564 mddev->safemode = 0;
2565 mddev->safemode_timer.function = md_safemode_timeout;
2566 mddev->safemode_timer.data = (unsigned long) mddev;
2567 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2568 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002569
2570 ITERATE_RDEV(mddev,rdev,tmp)
2571 if (rdev->raid_disk >= 0) {
2572 char nm[20];
2573 sprintf(nm, "rd%d", rdev->raid_disk);
2574 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002578 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 if (mddev->sb_dirty)
2581 md_update_sb(mddev);
2582
2583 set_capacity(disk, mddev->array_size<<1);
2584
2585 /* If we call blk_queue_make_request here, it will
2586 * re-initialise max_sectors etc which may have been
2587 * refined inside -> run. So just set the bits we need to set.
2588 * Most initialisation happended when we called
2589 * blk_queue_make_request(..., md_fail_request)
2590 * earlier.
2591 */
2592 mddev->queue->queuedata = mddev;
2593 mddev->queue->make_request_fn = mddev->pers->make_request;
2594
2595 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08002596 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return 0;
2598}
2599
2600static int restart_array(mddev_t *mddev)
2601{
2602 struct gendisk *disk = mddev->gendisk;
2603 int err;
2604
2605 /*
2606 * Complain if it has no devices
2607 */
2608 err = -ENXIO;
2609 if (list_empty(&mddev->disks))
2610 goto out;
2611
2612 if (mddev->pers) {
2613 err = -EBUSY;
2614 if (!mddev->ro)
2615 goto out;
2616
2617 mddev->safemode = 0;
2618 mddev->ro = 0;
2619 set_disk_ro(disk, 0);
2620
2621 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2622 mdname(mddev));
2623 /*
2624 * Kick recovery or resync if necessary
2625 */
2626 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2627 md_wakeup_thread(mddev->thread);
2628 err = 0;
2629 } else {
2630 printk(KERN_ERR "md: %s has no personality assigned.\n",
2631 mdname(mddev));
2632 err = -EINVAL;
2633 }
2634
2635out:
2636 return err;
2637}
2638
2639static int do_md_stop(mddev_t * mddev, int ro)
2640{
2641 int err = 0;
2642 struct gendisk *disk = mddev->gendisk;
2643
2644 if (mddev->pers) {
2645 if (atomic_read(&mddev->active)>2) {
2646 printk("md: %s still in use.\n",mdname(mddev));
2647 return -EBUSY;
2648 }
2649
2650 if (mddev->sync_thread) {
2651 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2652 md_unregister_thread(mddev->sync_thread);
2653 mddev->sync_thread = NULL;
2654 }
2655
2656 del_timer_sync(&mddev->safemode_timer);
2657
2658 invalidate_partition(disk, 0);
2659
2660 if (ro) {
2661 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08002662 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 goto out;
2664 mddev->ro = 1;
2665 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002666 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08002667 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (mddev->ro)
2669 set_disk_ro(disk, 0);
2670 blk_queue_make_request(mddev->queue, md_fail_request);
2671 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08002672 if (mddev->pers->sync_request)
2673 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 module_put(mddev->pers->owner);
2676 mddev->pers = NULL;
2677 if (mddev->ro)
2678 mddev->ro = 0;
2679 }
2680 if (!mddev->in_sync) {
2681 /* mark array as shutdown cleanly */
2682 mddev->in_sync = 1;
2683 md_update_sb(mddev);
2684 }
2685 if (ro)
2686 set_disk_ro(disk, 1);
2687 }
NeilBrown32a76272005-06-21 17:17:14 -07002688
2689 bitmap_destroy(mddev);
2690 if (mddev->bitmap_file) {
2691 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2692 fput(mddev->bitmap_file);
2693 mddev->bitmap_file = NULL;
2694 }
NeilBrown92232142005-08-18 11:24:16 -07002695 mddev->bitmap_offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07002696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 /*
2698 * Free resources if final stop
2699 */
2700 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002701 mdk_rdev_t *rdev;
2702 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 struct gendisk *disk;
2704 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2705
NeilBrown86e6ffd2005-11-08 21:39:24 -08002706 ITERATE_RDEV(mddev,rdev,tmp)
2707 if (rdev->raid_disk >= 0) {
2708 char nm[20];
2709 sprintf(nm, "rd%d", rdev->raid_disk);
2710 sysfs_remove_link(&mddev->kobj, nm);
2711 }
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 export_array(mddev);
2714
2715 mddev->array_size = 0;
2716 disk = mddev->gendisk;
2717 if (disk)
2718 set_capacity(disk, 0);
2719 mddev->changed = 1;
2720 } else
2721 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2722 mdname(mddev));
2723 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08002724 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725out:
2726 return err;
2727}
2728
2729static void autorun_array(mddev_t *mddev)
2730{
2731 mdk_rdev_t *rdev;
2732 struct list_head *tmp;
2733 int err;
2734
NeilBrowna757e642005-04-16 15:26:42 -07002735 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 printk(KERN_INFO "md: running: ");
2739
2740 ITERATE_RDEV(mddev,rdev,tmp) {
2741 char b[BDEVNAME_SIZE];
2742 printk("<%s>", bdevname(rdev->bdev,b));
2743 }
2744 printk("\n");
2745
2746 err = do_md_run (mddev);
2747 if (err) {
2748 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2749 do_md_stop (mddev, 0);
2750 }
2751}
2752
2753/*
2754 * lets try to run arrays based on all disks that have arrived
2755 * until now. (those are in pending_raid_disks)
2756 *
2757 * the method: pick the first pending disk, collect all disks with
2758 * the same UUID, remove all from the pending list and put them into
2759 * the 'same_array' list. Then order this list based on superblock
2760 * update time (freshest comes first), kick out 'old' disks and
2761 * compare superblocks. If everything's fine then run it.
2762 *
2763 * If "unit" is allocated, then bump its reference count
2764 */
2765static void autorun_devices(int part)
2766{
2767 struct list_head candidates;
2768 struct list_head *tmp;
2769 mdk_rdev_t *rdev0, *rdev;
2770 mddev_t *mddev;
2771 char b[BDEVNAME_SIZE];
2772
2773 printk(KERN_INFO "md: autorun ...\n");
2774 while (!list_empty(&pending_raid_disks)) {
2775 dev_t dev;
2776 rdev0 = list_entry(pending_raid_disks.next,
2777 mdk_rdev_t, same_set);
2778
2779 printk(KERN_INFO "md: considering %s ...\n",
2780 bdevname(rdev0->bdev,b));
2781 INIT_LIST_HEAD(&candidates);
2782 ITERATE_RDEV_PENDING(rdev,tmp)
2783 if (super_90_load(rdev, rdev0, 0) >= 0) {
2784 printk(KERN_INFO "md: adding %s ...\n",
2785 bdevname(rdev->bdev,b));
2786 list_move(&rdev->same_set, &candidates);
2787 }
2788 /*
2789 * now we have a set of devices, with all of them having
2790 * mostly sane superblocks. It's time to allocate the
2791 * mddev.
2792 */
2793 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2794 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2795 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2796 break;
2797 }
2798 if (part)
2799 dev = MKDEV(mdp_major,
2800 rdev0->preferred_minor << MdpMinorShift);
2801 else
2802 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2803
2804 md_probe(dev, NULL, NULL);
2805 mddev = mddev_find(dev);
2806 if (!mddev) {
2807 printk(KERN_ERR
2808 "md: cannot allocate memory for md drive.\n");
2809 break;
2810 }
2811 if (mddev_lock(mddev))
2812 printk(KERN_WARNING "md: %s locked, cannot run\n",
2813 mdname(mddev));
2814 else if (mddev->raid_disks || mddev->major_version
2815 || !list_empty(&mddev->disks)) {
2816 printk(KERN_WARNING
2817 "md: %s already running, cannot run %s\n",
2818 mdname(mddev), bdevname(rdev0->bdev,b));
2819 mddev_unlock(mddev);
2820 } else {
2821 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2822 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2823 list_del_init(&rdev->same_set);
2824 if (bind_rdev_to_array(rdev, mddev))
2825 export_rdev(rdev);
2826 }
2827 autorun_array(mddev);
2828 mddev_unlock(mddev);
2829 }
2830 /* on success, candidates will be empty, on error
2831 * it won't...
2832 */
2833 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2834 export_rdev(rdev);
2835 mddev_put(mddev);
2836 }
2837 printk(KERN_INFO "md: ... autorun DONE.\n");
2838}
2839
2840/*
2841 * import RAID devices based on one partition
2842 * if possible, the array gets run as well.
2843 */
2844
2845static int autostart_array(dev_t startdev)
2846{
2847 char b[BDEVNAME_SIZE];
2848 int err = -EINVAL, i;
2849 mdp_super_t *sb = NULL;
2850 mdk_rdev_t *start_rdev = NULL, *rdev;
2851
2852 start_rdev = md_import_device(startdev, 0, 0);
2853 if (IS_ERR(start_rdev))
2854 return err;
2855
2856
2857 /* NOTE: this can only work for 0.90.0 superblocks */
2858 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2859 if (sb->major_version != 0 ||
2860 sb->minor_version != 90 ) {
2861 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2862 export_rdev(start_rdev);
2863 return err;
2864 }
2865
NeilBrownb2d444d2005-11-08 21:39:31 -08002866 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 printk(KERN_WARNING
2868 "md: can not autostart based on faulty %s!\n",
2869 bdevname(start_rdev->bdev,b));
2870 export_rdev(start_rdev);
2871 return err;
2872 }
2873 list_add(&start_rdev->same_set, &pending_raid_disks);
2874
2875 for (i = 0; i < MD_SB_DISKS; i++) {
2876 mdp_disk_t *desc = sb->disks + i;
2877 dev_t dev = MKDEV(desc->major, desc->minor);
2878
2879 if (!dev)
2880 continue;
2881 if (dev == startdev)
2882 continue;
2883 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2884 continue;
2885 rdev = md_import_device(dev, 0, 0);
2886 if (IS_ERR(rdev))
2887 continue;
2888
2889 list_add(&rdev->same_set, &pending_raid_disks);
2890 }
2891
2892 /*
2893 * possibly return codes
2894 */
2895 autorun_devices(0);
2896 return 0;
2897
2898}
2899
2900
2901static int get_version(void __user * arg)
2902{
2903 mdu_version_t ver;
2904
2905 ver.major = MD_MAJOR_VERSION;
2906 ver.minor = MD_MINOR_VERSION;
2907 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2908
2909 if (copy_to_user(arg, &ver, sizeof(ver)))
2910 return -EFAULT;
2911
2912 return 0;
2913}
2914
2915static int get_array_info(mddev_t * mddev, void __user * arg)
2916{
2917 mdu_array_info_t info;
2918 int nr,working,active,failed,spare;
2919 mdk_rdev_t *rdev;
2920 struct list_head *tmp;
2921
2922 nr=working=active=failed=spare=0;
2923 ITERATE_RDEV(mddev,rdev,tmp) {
2924 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002925 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 failed++;
2927 else {
2928 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002929 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 active++;
2931 else
2932 spare++;
2933 }
2934 }
2935
2936 info.major_version = mddev->major_version;
2937 info.minor_version = mddev->minor_version;
2938 info.patch_version = MD_PATCHLEVEL_VERSION;
2939 info.ctime = mddev->ctime;
2940 info.level = mddev->level;
2941 info.size = mddev->size;
2942 info.nr_disks = nr;
2943 info.raid_disks = mddev->raid_disks;
2944 info.md_minor = mddev->md_minor;
2945 info.not_persistent= !mddev->persistent;
2946
2947 info.utime = mddev->utime;
2948 info.state = 0;
2949 if (mddev->in_sync)
2950 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07002951 if (mddev->bitmap && mddev->bitmap_offset)
2952 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 info.active_disks = active;
2954 info.working_disks = working;
2955 info.failed_disks = failed;
2956 info.spare_disks = spare;
2957
2958 info.layout = mddev->layout;
2959 info.chunk_size = mddev->chunk_size;
2960
2961 if (copy_to_user(arg, &info, sizeof(info)))
2962 return -EFAULT;
2963
2964 return 0;
2965}
2966
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01002967static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07002968{
2969 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2970 char *ptr, *buf = NULL;
2971 int err = -ENOMEM;
2972
2973 file = kmalloc(sizeof(*file), GFP_KERNEL);
2974 if (!file)
2975 goto out;
2976
2977 /* bitmap disabled, zero the first byte and copy out */
2978 if (!mddev->bitmap || !mddev->bitmap->file) {
2979 file->pathname[0] = '\0';
2980 goto copy_out;
2981 }
2982
2983 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2984 if (!buf)
2985 goto out;
2986
2987 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2988 if (!ptr)
2989 goto out;
2990
2991 strcpy(file->pathname, ptr);
2992
2993copy_out:
2994 err = 0;
2995 if (copy_to_user(arg, file, sizeof(*file)))
2996 err = -EFAULT;
2997out:
2998 kfree(buf);
2999 kfree(file);
3000 return err;
3001}
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003static int get_disk_info(mddev_t * mddev, void __user * arg)
3004{
3005 mdu_disk_info_t info;
3006 unsigned int nr;
3007 mdk_rdev_t *rdev;
3008
3009 if (copy_from_user(&info, arg, sizeof(info)))
3010 return -EFAULT;
3011
3012 nr = info.number;
3013
3014 rdev = find_rdev_nr(mddev, nr);
3015 if (rdev) {
3016 info.major = MAJOR(rdev->bdev->bd_dev);
3017 info.minor = MINOR(rdev->bdev->bd_dev);
3018 info.raid_disk = rdev->raid_disk;
3019 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003020 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003022 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 info.state |= (1<<MD_DISK_ACTIVE);
3024 info.state |= (1<<MD_DISK_SYNC);
3025 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003026 if (test_bit(WriteMostly, &rdev->flags))
3027 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 } else {
3029 info.major = info.minor = 0;
3030 info.raid_disk = -1;
3031 info.state = (1<<MD_DISK_REMOVED);
3032 }
3033
3034 if (copy_to_user(arg, &info, sizeof(info)))
3035 return -EFAULT;
3036
3037 return 0;
3038}
3039
3040static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3041{
3042 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3043 mdk_rdev_t *rdev;
3044 dev_t dev = MKDEV(info->major,info->minor);
3045
3046 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3047 return -EOVERFLOW;
3048
3049 if (!mddev->raid_disks) {
3050 int err;
3051 /* expecting a device which has a superblock */
3052 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3053 if (IS_ERR(rdev)) {
3054 printk(KERN_WARNING
3055 "md: md_import_device returned %ld\n",
3056 PTR_ERR(rdev));
3057 return PTR_ERR(rdev);
3058 }
3059 if (!list_empty(&mddev->disks)) {
3060 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3061 mdk_rdev_t, same_set);
3062 int err = super_types[mddev->major_version]
3063 .load_super(rdev, rdev0, mddev->minor_version);
3064 if (err < 0) {
3065 printk(KERN_WARNING
3066 "md: %s has different UUID to %s\n",
3067 bdevname(rdev->bdev,b),
3068 bdevname(rdev0->bdev,b2));
3069 export_rdev(rdev);
3070 return -EINVAL;
3071 }
3072 }
3073 err = bind_rdev_to_array(rdev, mddev);
3074 if (err)
3075 export_rdev(rdev);
3076 return err;
3077 }
3078
3079 /*
3080 * add_new_disk can be used once the array is assembled
3081 * to add "hot spares". They must already have a superblock
3082 * written
3083 */
3084 if (mddev->pers) {
3085 int err;
3086 if (!mddev->pers->hot_add_disk) {
3087 printk(KERN_WARNING
3088 "%s: personality does not support diskops!\n",
3089 mdname(mddev));
3090 return -EINVAL;
3091 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003092 if (mddev->persistent)
3093 rdev = md_import_device(dev, mddev->major_version,
3094 mddev->minor_version);
3095 else
3096 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 if (IS_ERR(rdev)) {
3098 printk(KERN_WARNING
3099 "md: md_import_device returned %ld\n",
3100 PTR_ERR(rdev));
3101 return PTR_ERR(rdev);
3102 }
NeilBrown41158c72005-06-21 17:17:25 -07003103 /* set save_raid_disk if appropriate */
3104 if (!mddev->persistent) {
3105 if (info->state & (1<<MD_DISK_SYNC) &&
3106 info->raid_disk < mddev->raid_disks)
3107 rdev->raid_disk = info->raid_disk;
3108 else
3109 rdev->raid_disk = -1;
3110 } else
3111 super_types[mddev->major_version].
3112 validate_super(mddev, rdev);
3113 rdev->saved_raid_disk = rdev->raid_disk;
3114
NeilBrownb2d444d2005-11-08 21:39:31 -08003115 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003116 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3117 set_bit(WriteMostly, &rdev->flags);
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 rdev->raid_disk = -1;
3120 err = bind_rdev_to_array(rdev, mddev);
3121 if (err)
3122 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003123
3124 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003125 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 return err;
3127 }
3128
3129 /* otherwise, add_new_disk is only allowed
3130 * for major_version==0 superblocks
3131 */
3132 if (mddev->major_version != 0) {
3133 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3134 mdname(mddev));
3135 return -EINVAL;
3136 }
3137
3138 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3139 int err;
3140 rdev = md_import_device (dev, -1, 0);
3141 if (IS_ERR(rdev)) {
3142 printk(KERN_WARNING
3143 "md: error, md_import_device() returned %ld\n",
3144 PTR_ERR(rdev));
3145 return PTR_ERR(rdev);
3146 }
3147 rdev->desc_nr = info->number;
3148 if (info->raid_disk < mddev->raid_disks)
3149 rdev->raid_disk = info->raid_disk;
3150 else
3151 rdev->raid_disk = -1;
3152
NeilBrownb2d444d2005-11-08 21:39:31 -08003153 rdev->flags = 0;
3154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003156 if (info->state & (1<<MD_DISK_SYNC))
3157 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003159 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3160 set_bit(WriteMostly, &rdev->flags);
3161
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if (!mddev->persistent) {
3163 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3164 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3165 } else
3166 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3167 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3168
NeilBrown2bf071b2006-01-06 00:20:55 -08003169 err = bind_rdev_to_array(rdev, mddev);
3170 if (err) {
3171 export_rdev(rdev);
3172 return err;
3173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
3175
3176 return 0;
3177}
3178
3179static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3180{
3181 char b[BDEVNAME_SIZE];
3182 mdk_rdev_t *rdev;
3183
3184 if (!mddev->pers)
3185 return -ENODEV;
3186
3187 rdev = find_rdev(mddev, dev);
3188 if (!rdev)
3189 return -ENXIO;
3190
3191 if (rdev->raid_disk >= 0)
3192 goto busy;
3193
3194 kick_rdev_from_array(rdev);
3195 md_update_sb(mddev);
NeilBrownd7603b72006-01-06 00:20:30 -08003196 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 return 0;
3199busy:
3200 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3201 bdevname(rdev->bdev,b), mdname(mddev));
3202 return -EBUSY;
3203}
3204
3205static int hot_add_disk(mddev_t * mddev, dev_t dev)
3206{
3207 char b[BDEVNAME_SIZE];
3208 int err;
3209 unsigned int size;
3210 mdk_rdev_t *rdev;
3211
3212 if (!mddev->pers)
3213 return -ENODEV;
3214
3215 if (mddev->major_version != 0) {
3216 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3217 " version-0 superblocks.\n",
3218 mdname(mddev));
3219 return -EINVAL;
3220 }
3221 if (!mddev->pers->hot_add_disk) {
3222 printk(KERN_WARNING
3223 "%s: personality does not support diskops!\n",
3224 mdname(mddev));
3225 return -EINVAL;
3226 }
3227
3228 rdev = md_import_device (dev, -1, 0);
3229 if (IS_ERR(rdev)) {
3230 printk(KERN_WARNING
3231 "md: error, md_import_device() returned %ld\n",
3232 PTR_ERR(rdev));
3233 return -EINVAL;
3234 }
3235
3236 if (mddev->persistent)
3237 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3238 else
3239 rdev->sb_offset =
3240 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3241
3242 size = calc_dev_size(rdev, mddev->chunk_size);
3243 rdev->size = size;
3244
NeilBrownb2d444d2005-11-08 21:39:31 -08003245 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 printk(KERN_WARNING
3247 "md: can not hot-add faulty %s disk to %s!\n",
3248 bdevname(rdev->bdev,b), mdname(mddev));
3249 err = -EINVAL;
3250 goto abort_export;
3251 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003252 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 rdev->desc_nr = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08003254 err = bind_rdev_to_array(rdev, mddev);
3255 if (err)
3256 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
3258 /*
3259 * The rest should better be atomic, we can have disk failures
3260 * noticed in interrupt contexts ...
3261 */
3262
3263 if (rdev->desc_nr == mddev->max_disks) {
3264 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3265 mdname(mddev));
3266 err = -EBUSY;
3267 goto abort_unbind_export;
3268 }
3269
3270 rdev->raid_disk = -1;
3271
3272 md_update_sb(mddev);
3273
3274 /*
3275 * Kick recovery, maybe this spare has to be added to the
3276 * array immediately.
3277 */
3278 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3279 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08003280 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return 0;
3282
3283abort_unbind_export:
3284 unbind_rdev_from_array(rdev);
3285
3286abort_export:
3287 export_rdev(rdev);
3288 return err;
3289}
3290
NeilBrown32a76272005-06-21 17:17:14 -07003291/* similar to deny_write_access, but accounts for our holding a reference
3292 * to the file ourselves */
3293static int deny_bitmap_write_access(struct file * file)
3294{
3295 struct inode *inode = file->f_mapping->host;
3296
3297 spin_lock(&inode->i_lock);
3298 if (atomic_read(&inode->i_writecount) > 1) {
3299 spin_unlock(&inode->i_lock);
3300 return -ETXTBSY;
3301 }
3302 atomic_set(&inode->i_writecount, -1);
3303 spin_unlock(&inode->i_lock);
3304
3305 return 0;
3306}
3307
3308static int set_bitmap_file(mddev_t *mddev, int fd)
3309{
3310 int err;
3311
NeilBrown36fa3062005-09-09 16:23:45 -07003312 if (mddev->pers) {
3313 if (!mddev->pers->quiesce)
3314 return -EBUSY;
3315 if (mddev->recovery || mddev->sync_thread)
3316 return -EBUSY;
3317 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07003318 }
3319
NeilBrown36fa3062005-09-09 16:23:45 -07003320
3321 if (fd >= 0) {
3322 if (mddev->bitmap)
3323 return -EEXIST; /* cannot add when bitmap is present */
3324 mddev->bitmap_file = fget(fd);
3325
3326 if (mddev->bitmap_file == NULL) {
3327 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3328 mdname(mddev));
3329 return -EBADF;
3330 }
3331
3332 err = deny_bitmap_write_access(mddev->bitmap_file);
3333 if (err) {
3334 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3335 mdname(mddev));
3336 fput(mddev->bitmap_file);
3337 mddev->bitmap_file = NULL;
3338 return err;
3339 }
NeilBrowna654b9d82005-06-21 17:17:27 -07003340 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07003341 } else if (mddev->bitmap == NULL)
3342 return -ENOENT; /* cannot remove what isn't there */
3343 err = 0;
3344 if (mddev->pers) {
3345 mddev->pers->quiesce(mddev, 1);
3346 if (fd >= 0)
3347 err = bitmap_create(mddev);
3348 if (fd < 0 || err)
3349 bitmap_destroy(mddev);
3350 mddev->pers->quiesce(mddev, 0);
3351 } else if (fd < 0) {
3352 if (mddev->bitmap_file)
3353 fput(mddev->bitmap_file);
3354 mddev->bitmap_file = NULL;
3355 }
3356
NeilBrown32a76272005-06-21 17:17:14 -07003357 return err;
3358}
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360/*
3361 * set_array_info is used two different ways
3362 * The original usage is when creating a new array.
3363 * In this usage, raid_disks is > 0 and it together with
3364 * level, size, not_persistent,layout,chunksize determine the
3365 * shape of the array.
3366 * This will always create an array with a type-0.90.0 superblock.
3367 * The newer usage is when assembling an array.
3368 * In this case raid_disks will be 0, and the major_version field is
3369 * use to determine which style super-blocks are to be found on the devices.
3370 * The minor and patch _version numbers are also kept incase the
3371 * super_block handler wishes to interpret them.
3372 */
3373static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3374{
3375
3376 if (info->raid_disks == 0) {
3377 /* just setting version number for superblock loading */
3378 if (info->major_version < 0 ||
3379 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3380 super_types[info->major_version].name == NULL) {
3381 /* maybe try to auto-load a module? */
3382 printk(KERN_INFO
3383 "md: superblock version %d not known\n",
3384 info->major_version);
3385 return -EINVAL;
3386 }
3387 mddev->major_version = info->major_version;
3388 mddev->minor_version = info->minor_version;
3389 mddev->patch_version = info->patch_version;
3390 return 0;
3391 }
3392 mddev->major_version = MD_MAJOR_VERSION;
3393 mddev->minor_version = MD_MINOR_VERSION;
3394 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3395 mddev->ctime = get_seconds();
3396
3397 mddev->level = info->level;
3398 mddev->size = info->size;
3399 mddev->raid_disks = info->raid_disks;
3400 /* don't set md_minor, it is determined by which /dev/md* was
3401 * openned
3402 */
3403 if (info->state & (1<<MD_SB_CLEAN))
3404 mddev->recovery_cp = MaxSector;
3405 else
3406 mddev->recovery_cp = 0;
3407 mddev->persistent = ! info->not_persistent;
3408
3409 mddev->layout = info->layout;
3410 mddev->chunk_size = info->chunk_size;
3411
3412 mddev->max_disks = MD_SB_DISKS;
3413
3414 mddev->sb_dirty = 1;
3415
NeilBrownb2a27032005-11-28 13:44:12 -08003416 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
3417 mddev->bitmap_offset = 0;
3418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 /*
3420 * Generate a 128 bit UUID
3421 */
3422 get_random_bytes(mddev->uuid, 16);
3423
3424 return 0;
3425}
3426
NeilBrowna35b0d62006-01-06 00:20:49 -08003427static int update_size(mddev_t *mddev, unsigned long size)
3428{
3429 mdk_rdev_t * rdev;
3430 int rv;
3431 struct list_head *tmp;
3432
3433 if (mddev->pers->resize == NULL)
3434 return -EINVAL;
3435 /* The "size" is the amount of each device that is used.
3436 * This can only make sense for arrays with redundancy.
3437 * linear and raid0 always use whatever space is available
3438 * We can only consider changing the size if no resync
3439 * or reconstruction is happening, and if the new size
3440 * is acceptable. It must fit before the sb_offset or,
3441 * if that is <data_offset, it must fit before the
3442 * size of each device.
3443 * If size is zero, we find the largest size that fits.
3444 */
3445 if (mddev->sync_thread)
3446 return -EBUSY;
3447 ITERATE_RDEV(mddev,rdev,tmp) {
3448 sector_t avail;
3449 int fit = (size == 0);
3450 if (rdev->sb_offset > rdev->data_offset)
3451 avail = (rdev->sb_offset*2) - rdev->data_offset;
3452 else
3453 avail = get_capacity(rdev->bdev->bd_disk)
3454 - rdev->data_offset;
3455 if (fit && (size == 0 || size > avail/2))
3456 size = avail/2;
3457 if (avail < ((sector_t)size << 1))
3458 return -ENOSPC;
3459 }
3460 rv = mddev->pers->resize(mddev, (sector_t)size *2);
3461 if (!rv) {
3462 struct block_device *bdev;
3463
3464 bdev = bdget_disk(mddev->gendisk, 0);
3465 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003466 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08003467 i_size_write(bdev->bd_inode, mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003468 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08003469 bdput(bdev);
3470 }
3471 }
3472 return rv;
3473}
3474
NeilBrownda943b992006-01-06 00:20:54 -08003475static int update_raid_disks(mddev_t *mddev, int raid_disks)
3476{
3477 int rv;
3478 /* change the number of raid disks */
3479 if (mddev->pers->reshape == NULL)
3480 return -EINVAL;
3481 if (raid_disks <= 0 ||
3482 raid_disks >= mddev->max_disks)
3483 return -EINVAL;
3484 if (mddev->sync_thread)
3485 return -EBUSY;
3486 rv = mddev->pers->reshape(mddev, raid_disks);
3487 if (!rv) {
3488 struct block_device *bdev;
3489
3490 bdev = bdget_disk(mddev->gendisk, 0);
3491 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003492 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrownda943b992006-01-06 00:20:54 -08003493 i_size_write(bdev->bd_inode, mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003494 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrownda943b992006-01-06 00:20:54 -08003495 bdput(bdev);
3496 }
3497 }
3498 return rv;
3499}
3500
3501
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502/*
3503 * update_array_info is used to change the configuration of an
3504 * on-line array.
3505 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
3506 * fields in the info are checked against the array.
3507 * Any differences that cannot be handled will cause an error.
3508 * Normally, only one change can be managed at a time.
3509 */
3510static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
3511{
3512 int rv = 0;
3513 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07003514 int state = 0;
3515
3516 /* calculate expected state,ignoring low bits */
3517 if (mddev->bitmap && mddev->bitmap_offset)
3518 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
3520 if (mddev->major_version != info->major_version ||
3521 mddev->minor_version != info->minor_version ||
3522/* mddev->patch_version != info->patch_version || */
3523 mddev->ctime != info->ctime ||
3524 mddev->level != info->level ||
3525/* mddev->layout != info->layout || */
3526 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07003527 mddev->chunk_size != info->chunk_size ||
3528 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
3529 ((state^info->state) & 0xfffffe00)
3530 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return -EINVAL;
3532 /* Check there is only one change */
3533 if (mddev->size != info->size) cnt++;
3534 if (mddev->raid_disks != info->raid_disks) cnt++;
3535 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07003536 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 if (cnt == 0) return 0;
3538 if (cnt > 1) return -EINVAL;
3539
3540 if (mddev->layout != info->layout) {
3541 /* Change layout
3542 * we don't need to do anything at the md level, the
3543 * personality will take care of it all.
3544 */
3545 if (mddev->pers->reconfig == NULL)
3546 return -EINVAL;
3547 else
3548 return mddev->pers->reconfig(mddev, info->layout, -1);
3549 }
NeilBrowna35b0d62006-01-06 00:20:49 -08003550 if (mddev->size != info->size)
3551 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
NeilBrownda943b992006-01-06 00:20:54 -08003553 if (mddev->raid_disks != info->raid_disks)
3554 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
NeilBrown36fa3062005-09-09 16:23:45 -07003556 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3557 if (mddev->pers->quiesce == NULL)
3558 return -EINVAL;
3559 if (mddev->recovery || mddev->sync_thread)
3560 return -EBUSY;
3561 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3562 /* add the bitmap */
3563 if (mddev->bitmap)
3564 return -EEXIST;
3565 if (mddev->default_bitmap_offset == 0)
3566 return -EINVAL;
3567 mddev->bitmap_offset = mddev->default_bitmap_offset;
3568 mddev->pers->quiesce(mddev, 1);
3569 rv = bitmap_create(mddev);
3570 if (rv)
3571 bitmap_destroy(mddev);
3572 mddev->pers->quiesce(mddev, 0);
3573 } else {
3574 /* remove the bitmap */
3575 if (!mddev->bitmap)
3576 return -ENOENT;
3577 if (mddev->bitmap->file)
3578 return -EINVAL;
3579 mddev->pers->quiesce(mddev, 1);
3580 bitmap_destroy(mddev);
3581 mddev->pers->quiesce(mddev, 0);
3582 mddev->bitmap_offset = 0;
3583 }
3584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 md_update_sb(mddev);
3586 return rv;
3587}
3588
3589static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3590{
3591 mdk_rdev_t *rdev;
3592
3593 if (mddev->pers == NULL)
3594 return -ENODEV;
3595
3596 rdev = find_rdev(mddev, dev);
3597 if (!rdev)
3598 return -ENODEV;
3599
3600 md_error(mddev, rdev);
3601 return 0;
3602}
3603
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003604static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3605{
3606 mddev_t *mddev = bdev->bd_disk->private_data;
3607
3608 geo->heads = 2;
3609 geo->sectors = 4;
3610 geo->cylinders = get_capacity(mddev->gendisk) / 8;
3611 return 0;
3612}
3613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614static int md_ioctl(struct inode *inode, struct file *file,
3615 unsigned int cmd, unsigned long arg)
3616{
3617 int err = 0;
3618 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 mddev_t *mddev = NULL;
3620
3621 if (!capable(CAP_SYS_ADMIN))
3622 return -EACCES;
3623
3624 /*
3625 * Commands dealing with the RAID driver but not any
3626 * particular array:
3627 */
3628 switch (cmd)
3629 {
3630 case RAID_VERSION:
3631 err = get_version(argp);
3632 goto done;
3633
3634 case PRINT_RAID_DEBUG:
3635 err = 0;
3636 md_print_devices();
3637 goto done;
3638
3639#ifndef MODULE
3640 case RAID_AUTORUN:
3641 err = 0;
3642 autostart_arrays(arg);
3643 goto done;
3644#endif
3645 default:;
3646 }
3647
3648 /*
3649 * Commands creating/starting a new array:
3650 */
3651
3652 mddev = inode->i_bdev->bd_disk->private_data;
3653
3654 if (!mddev) {
3655 BUG();
3656 goto abort;
3657 }
3658
3659
3660 if (cmd == START_ARRAY) {
3661 /* START_ARRAY doesn't need to lock the array as autostart_array
3662 * does the locking, and it could even be a different array
3663 */
3664 static int cnt = 3;
3665 if (cnt > 0 ) {
3666 printk(KERN_WARNING
3667 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08003668 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 current->comm, current->pid);
3670 cnt--;
3671 }
3672 err = autostart_array(new_decode_dev(arg));
3673 if (err) {
3674 printk(KERN_WARNING "md: autostart failed!\n");
3675 goto abort;
3676 }
3677 goto done;
3678 }
3679
3680 err = mddev_lock(mddev);
3681 if (err) {
3682 printk(KERN_INFO
3683 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3684 err, cmd);
3685 goto abort;
3686 }
3687
3688 switch (cmd)
3689 {
3690 case SET_ARRAY_INFO:
3691 {
3692 mdu_array_info_t info;
3693 if (!arg)
3694 memset(&info, 0, sizeof(info));
3695 else if (copy_from_user(&info, argp, sizeof(info))) {
3696 err = -EFAULT;
3697 goto abort_unlock;
3698 }
3699 if (mddev->pers) {
3700 err = update_array_info(mddev, &info);
3701 if (err) {
3702 printk(KERN_WARNING "md: couldn't update"
3703 " array info. %d\n", err);
3704 goto abort_unlock;
3705 }
3706 goto done_unlock;
3707 }
3708 if (!list_empty(&mddev->disks)) {
3709 printk(KERN_WARNING
3710 "md: array %s already has disks!\n",
3711 mdname(mddev));
3712 err = -EBUSY;
3713 goto abort_unlock;
3714 }
3715 if (mddev->raid_disks) {
3716 printk(KERN_WARNING
3717 "md: array %s already initialised!\n",
3718 mdname(mddev));
3719 err = -EBUSY;
3720 goto abort_unlock;
3721 }
3722 err = set_array_info(mddev, &info);
3723 if (err) {
3724 printk(KERN_WARNING "md: couldn't set"
3725 " array info. %d\n", err);
3726 goto abort_unlock;
3727 }
3728 }
3729 goto done_unlock;
3730
3731 default:;
3732 }
3733
3734 /*
3735 * Commands querying/configuring an existing array:
3736 */
NeilBrown32a76272005-06-21 17:17:14 -07003737 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3738 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3739 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3740 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 err = -ENODEV;
3742 goto abort_unlock;
3743 }
3744
3745 /*
3746 * Commands even a read-only array can execute:
3747 */
3748 switch (cmd)
3749 {
3750 case GET_ARRAY_INFO:
3751 err = get_array_info(mddev, argp);
3752 goto done_unlock;
3753
NeilBrown32a76272005-06-21 17:17:14 -07003754 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003755 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003756 goto done_unlock;
3757
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 case GET_DISK_INFO:
3759 err = get_disk_info(mddev, argp);
3760 goto done_unlock;
3761
3762 case RESTART_ARRAY_RW:
3763 err = restart_array(mddev);
3764 goto done_unlock;
3765
3766 case STOP_ARRAY:
3767 err = do_md_stop (mddev, 0);
3768 goto done_unlock;
3769
3770 case STOP_ARRAY_RO:
3771 err = do_md_stop (mddev, 1);
3772 goto done_unlock;
3773
3774 /*
3775 * We have a problem here : there is no easy way to give a CHS
3776 * virtual geometry. We currently pretend that we have a 2 heads
3777 * 4 sectors (with a BIG number of cylinders...). This drives
3778 * dosfs just mad... ;-)
3779 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 }
3781
3782 /*
3783 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08003784 * superblock, so we do not allow them on read-only arrays.
3785 * However non-MD ioctls (e.g. get-size) will still come through
3786 * here and hit the 'default' below, so only disallow
3787 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 */
NeilBrownf91de922005-11-08 21:39:36 -08003789 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3790 mddev->ro && mddev->pers) {
3791 if (mddev->ro == 2) {
3792 mddev->ro = 0;
3793 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3794 md_wakeup_thread(mddev->thread);
3795
3796 } else {
3797 err = -EROFS;
3798 goto abort_unlock;
3799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 }
3801
3802 switch (cmd)
3803 {
3804 case ADD_NEW_DISK:
3805 {
3806 mdu_disk_info_t info;
3807 if (copy_from_user(&info, argp, sizeof(info)))
3808 err = -EFAULT;
3809 else
3810 err = add_new_disk(mddev, &info);
3811 goto done_unlock;
3812 }
3813
3814 case HOT_REMOVE_DISK:
3815 err = hot_remove_disk(mddev, new_decode_dev(arg));
3816 goto done_unlock;
3817
3818 case HOT_ADD_DISK:
3819 err = hot_add_disk(mddev, new_decode_dev(arg));
3820 goto done_unlock;
3821
3822 case SET_DISK_FAULTY:
3823 err = set_disk_faulty(mddev, new_decode_dev(arg));
3824 goto done_unlock;
3825
3826 case RUN_ARRAY:
3827 err = do_md_run (mddev);
3828 goto done_unlock;
3829
NeilBrown32a76272005-06-21 17:17:14 -07003830 case SET_BITMAP_FILE:
3831 err = set_bitmap_file(mddev, (int)arg);
3832 goto done_unlock;
3833
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 default:
3835 if (_IOC_TYPE(cmd) == MD_MAJOR)
3836 printk(KERN_WARNING "md: %s(pid %d) used"
3837 " obsolete MD ioctl, upgrade your"
3838 " software to use new ictls.\n",
3839 current->comm, current->pid);
3840 err = -EINVAL;
3841 goto abort_unlock;
3842 }
3843
3844done_unlock:
3845abort_unlock:
3846 mddev_unlock(mddev);
3847
3848 return err;
3849done:
3850 if (err)
3851 MD_BUG();
3852abort:
3853 return err;
3854}
3855
3856static int md_open(struct inode *inode, struct file *file)
3857{
3858 /*
3859 * Succeed if we can lock the mddev, which confirms that
3860 * it isn't being stopped right now.
3861 */
3862 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3863 int err;
3864
3865 if ((err = mddev_lock(mddev)))
3866 goto out;
3867
3868 err = 0;
3869 mddev_get(mddev);
3870 mddev_unlock(mddev);
3871
3872 check_disk_change(inode->i_bdev);
3873 out:
3874 return err;
3875}
3876
3877static int md_release(struct inode *inode, struct file * file)
3878{
3879 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3880
3881 if (!mddev)
3882 BUG();
3883 mddev_put(mddev);
3884
3885 return 0;
3886}
3887
3888static int md_media_changed(struct gendisk *disk)
3889{
3890 mddev_t *mddev = disk->private_data;
3891
3892 return mddev->changed;
3893}
3894
3895static int md_revalidate(struct gendisk *disk)
3896{
3897 mddev_t *mddev = disk->private_data;
3898
3899 mddev->changed = 0;
3900 return 0;
3901}
3902static struct block_device_operations md_fops =
3903{
3904 .owner = THIS_MODULE,
3905 .open = md_open,
3906 .release = md_release,
3907 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003908 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 .media_changed = md_media_changed,
3910 .revalidate_disk= md_revalidate,
3911};
3912
Adrian Bunk75c96f82005-05-05 16:16:09 -07003913static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
3915 mdk_thread_t *thread = arg;
3916
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 /*
3918 * md_thread is a 'system-thread', it's priority should be very
3919 * high. We avoid resource deadlocks individually in each
3920 * raid personality. (RAID5 does preallocation) We also use RR and
3921 * the very same RT priority as kswapd, thus we will never get
3922 * into a priority inversion deadlock.
3923 *
3924 * we definitely have to have equal or higher priority than
3925 * bdflush, otherwise bdflush will deadlock if there are too
3926 * many dirty RAID5 blocks.
3927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
NeilBrown6985c432005-10-19 21:23:47 -07003929 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07003930 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
NeilBrown93588e22005-11-15 00:09:12 -08003932 /* We need to wait INTERRUPTIBLE so that
3933 * we don't add to the load-average.
3934 * That means we need to be sure no signals are
3935 * pending
3936 */
3937 if (signal_pending(current))
3938 flush_signals(current);
3939
3940 wait_event_interruptible_timeout
3941 (thread->wqueue,
3942 test_bit(THREAD_WAKEUP, &thread->flags)
3943 || kthread_should_stop(),
3944 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003945 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
3947 clear_bit(THREAD_WAKEUP, &thread->flags);
3948
NeilBrown787453c2005-11-08 21:39:43 -08003949 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 }
NeilBrowna6fb0932005-09-09 16:23:56 -07003951
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 return 0;
3953}
3954
3955void md_wakeup_thread(mdk_thread_t *thread)
3956{
3957 if (thread) {
3958 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3959 set_bit(THREAD_WAKEUP, &thread->flags);
3960 wake_up(&thread->wqueue);
3961 }
3962}
3963
3964mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3965 const char *name)
3966{
3967 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
NeilBrown9ffae0c2006-01-06 00:20:32 -08003969 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 if (!thread)
3971 return NULL;
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 init_waitqueue_head(&thread->wqueue);
3974
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 thread->run = run;
3976 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07003977 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07003978 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07003979 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 kfree(thread);
3981 return NULL;
3982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 return thread;
3984}
3985
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986void md_unregister_thread(mdk_thread_t *thread)
3987{
NeilBrownd28446f2005-04-16 15:26:41 -07003988 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07003989
3990 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 kfree(thread);
3992}
3993
3994void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3995{
3996 if (!mddev) {
3997 MD_BUG();
3998 return;
3999 }
4000
NeilBrownb2d444d2005-11-08 21:39:31 -08004001 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 return;
NeilBrown32a76272005-06-21 17:17:14 -07004003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4005 mdname(mddev),
4006 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4007 __builtin_return_address(0),__builtin_return_address(1),
4008 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004009*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 if (!mddev->pers->error_handler)
4011 return;
4012 mddev->pers->error_handler(mddev,rdev);
4013 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4014 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4015 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004016 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
4019/* seq_file implementation /proc/mdstat */
4020
4021static void status_unused(struct seq_file *seq)
4022{
4023 int i = 0;
4024 mdk_rdev_t *rdev;
4025 struct list_head *tmp;
4026
4027 seq_printf(seq, "unused devices: ");
4028
4029 ITERATE_RDEV_PENDING(rdev,tmp) {
4030 char b[BDEVNAME_SIZE];
4031 i++;
4032 seq_printf(seq, "%s ",
4033 bdevname(rdev->bdev,b));
4034 }
4035 if (!i)
4036 seq_printf(seq, "<none>");
4037
4038 seq_printf(seq, "\n");
4039}
4040
4041
4042static void status_resync(struct seq_file *seq, mddev_t * mddev)
4043{
4044 unsigned long max_blocks, resync, res, dt, db, rt;
4045
4046 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4047
4048 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4049 max_blocks = mddev->resync_max_sectors >> 1;
4050 else
4051 max_blocks = mddev->size;
4052
4053 /*
4054 * Should not happen.
4055 */
4056 if (!max_blocks) {
4057 MD_BUG();
4058 return;
4059 }
4060 res = (resync/1024)*1000/(max_blocks/1024 + 1);
4061 {
4062 int i, x = res/50, y = 20-x;
4063 seq_printf(seq, "[");
4064 for (i = 0; i < x; i++)
4065 seq_printf(seq, "=");
4066 seq_printf(seq, ">");
4067 for (i = 0; i < y; i++)
4068 seq_printf(seq, ".");
4069 seq_printf(seq, "] ");
4070 }
4071 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
4072 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4073 "resync" : "recovery"),
4074 res/10, res % 10, resync, max_blocks);
4075
4076 /*
4077 * We do not want to overflow, so the order of operands and
4078 * the * 100 / 100 trick are important. We do a +1 to be
4079 * safe against division by zero. We only estimate anyway.
4080 *
4081 * dt: time from mark until now
4082 * db: blocks written from mark until now
4083 * rt: remaining time
4084 */
4085 dt = ((jiffies - mddev->resync_mark) / HZ);
4086 if (!dt) dt++;
4087 db = resync - (mddev->resync_mark_cnt/2);
4088 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
4089
4090 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4091
4092 seq_printf(seq, " speed=%ldK/sec", db/dt);
4093}
4094
4095static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4096{
4097 struct list_head *tmp;
4098 loff_t l = *pos;
4099 mddev_t *mddev;
4100
4101 if (l >= 0x10000)
4102 return NULL;
4103 if (!l--)
4104 /* header */
4105 return (void*)1;
4106
4107 spin_lock(&all_mddevs_lock);
4108 list_for_each(tmp,&all_mddevs)
4109 if (!l--) {
4110 mddev = list_entry(tmp, mddev_t, all_mddevs);
4111 mddev_get(mddev);
4112 spin_unlock(&all_mddevs_lock);
4113 return mddev;
4114 }
4115 spin_unlock(&all_mddevs_lock);
4116 if (!l--)
4117 return (void*)2;/* tail */
4118 return NULL;
4119}
4120
4121static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4122{
4123 struct list_head *tmp;
4124 mddev_t *next_mddev, *mddev = v;
4125
4126 ++*pos;
4127 if (v == (void*)2)
4128 return NULL;
4129
4130 spin_lock(&all_mddevs_lock);
4131 if (v == (void*)1)
4132 tmp = all_mddevs.next;
4133 else
4134 tmp = mddev->all_mddevs.next;
4135 if (tmp != &all_mddevs)
4136 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4137 else {
4138 next_mddev = (void*)2;
4139 *pos = 0x10000;
4140 }
4141 spin_unlock(&all_mddevs_lock);
4142
4143 if (v != (void*)1)
4144 mddev_put(mddev);
4145 return next_mddev;
4146
4147}
4148
4149static void md_seq_stop(struct seq_file *seq, void *v)
4150{
4151 mddev_t *mddev = v;
4152
4153 if (mddev && v != (void*)1 && v != (void*)2)
4154 mddev_put(mddev);
4155}
4156
NeilBrownd7603b72006-01-06 00:20:30 -08004157struct mdstat_info {
4158 int event;
4159};
4160
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161static int md_seq_show(struct seq_file *seq, void *v)
4162{
4163 mddev_t *mddev = v;
4164 sector_t size;
4165 struct list_head *tmp2;
4166 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004167 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004168 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
4170 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004171 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 seq_printf(seq, "Personalities : ");
4173 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004174 list_for_each_entry(pers, &pers_list, list)
4175 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
4177 spin_unlock(&pers_lock);
4178 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004179 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 return 0;
4181 }
4182 if (v == (void*)2) {
4183 status_unused(seq);
4184 return 0;
4185 }
4186
4187 if (mddev_lock(mddev)!=0)
4188 return -EINTR;
4189 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4190 seq_printf(seq, "%s : %sactive", mdname(mddev),
4191 mddev->pers ? "" : "in");
4192 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004193 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004195 if (mddev->ro==2)
4196 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 seq_printf(seq, " %s", mddev->pers->name);
4198 }
4199
4200 size = 0;
4201 ITERATE_RDEV(mddev,rdev,tmp2) {
4202 char b[BDEVNAME_SIZE];
4203 seq_printf(seq, " %s[%d]",
4204 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004205 if (test_bit(WriteMostly, &rdev->flags))
4206 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004207 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 seq_printf(seq, "(F)");
4209 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004210 } else if (rdev->raid_disk < 0)
4211 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 size += rdev->size;
4213 }
4214
4215 if (!list_empty(&mddev->disks)) {
4216 if (mddev->pers)
4217 seq_printf(seq, "\n %llu blocks",
4218 (unsigned long long)mddev->array_size);
4219 else
4220 seq_printf(seq, "\n %llu blocks",
4221 (unsigned long long)size);
4222 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004223 if (mddev->persistent) {
4224 if (mddev->major_version != 0 ||
4225 mddev->minor_version != 90) {
4226 seq_printf(seq," super %d.%d",
4227 mddev->major_version,
4228 mddev->minor_version);
4229 }
4230 } else
4231 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
4233 if (mddev->pers) {
4234 mddev->pers->status (seq, mddev);
4235 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004236 if (mddev->pers->sync_request) {
4237 if (mddev->curr_resync > 2) {
4238 status_resync (seq, mddev);
4239 seq_printf(seq, "\n ");
4240 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4241 seq_printf(seq, "\tresync=DELAYED\n ");
4242 else if (mddev->recovery_cp < MaxSector)
4243 seq_printf(seq, "\tresync=PENDING\n ");
4244 }
NeilBrown32a76272005-06-21 17:17:14 -07004245 } else
4246 seq_printf(seq, "\n ");
4247
4248 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07004249 unsigned long chunk_kb;
4250 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07004251 spin_lock_irqsave(&bitmap->lock, flags);
4252 chunk_kb = bitmap->chunksize >> 10;
4253 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4254 "%lu%s chunk",
4255 bitmap->pages - bitmap->missing_pages,
4256 bitmap->pages,
4257 (bitmap->pages - bitmap->missing_pages)
4258 << (PAGE_SHIFT - 10),
4259 chunk_kb ? chunk_kb : bitmap->chunksize,
4260 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07004261 if (bitmap->file) {
4262 seq_printf(seq, ", file: ");
4263 seq_path(seq, bitmap->file->f_vfsmnt,
4264 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07004265 }
NeilBrown78d742d2005-06-21 17:17:15 -07004266
NeilBrown32a76272005-06-21 17:17:14 -07004267 seq_printf(seq, "\n");
4268 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 }
4270
4271 seq_printf(seq, "\n");
4272 }
4273 mddev_unlock(mddev);
4274
4275 return 0;
4276}
4277
4278static struct seq_operations md_seq_ops = {
4279 .start = md_seq_start,
4280 .next = md_seq_next,
4281 .stop = md_seq_stop,
4282 .show = md_seq_show,
4283};
4284
4285static int md_seq_open(struct inode *inode, struct file *file)
4286{
4287 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08004288 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4289 if (mi == NULL)
4290 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
4292 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08004293 if (error)
4294 kfree(mi);
4295 else {
4296 struct seq_file *p = file->private_data;
4297 p->private = mi;
4298 mi->event = atomic_read(&md_event_count);
4299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 return error;
4301}
4302
NeilBrownd7603b72006-01-06 00:20:30 -08004303static int md_seq_release(struct inode *inode, struct file *file)
4304{
4305 struct seq_file *m = file->private_data;
4306 struct mdstat_info *mi = m->private;
4307 m->private = NULL;
4308 kfree(mi);
4309 return seq_release(inode, file);
4310}
4311
4312static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4313{
4314 struct seq_file *m = filp->private_data;
4315 struct mdstat_info *mi = m->private;
4316 int mask;
4317
4318 poll_wait(filp, &md_event_waiters, wait);
4319
4320 /* always allow read */
4321 mask = POLLIN | POLLRDNORM;
4322
4323 if (mi->event != atomic_read(&md_event_count))
4324 mask |= POLLERR | POLLPRI;
4325 return mask;
4326}
4327
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328static struct file_operations md_seq_fops = {
4329 .open = md_seq_open,
4330 .read = seq_read,
4331 .llseek = seq_lseek,
NeilBrownd7603b72006-01-06 00:20:30 -08004332 .release = md_seq_release,
4333 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334};
4335
NeilBrown2604b702006-01-06 00:20:36 -08004336int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004339 list_add_tail(&p->list, &pers_list);
4340 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 spin_unlock(&pers_lock);
4342 return 0;
4343}
4344
NeilBrown2604b702006-01-06 00:20:36 -08004345int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
NeilBrown2604b702006-01-06 00:20:36 -08004347 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004349 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 spin_unlock(&pers_lock);
4351 return 0;
4352}
4353
4354static int is_mddev_idle(mddev_t *mddev)
4355{
4356 mdk_rdev_t * rdev;
4357 struct list_head *tmp;
4358 int idle;
4359 unsigned long curr_events;
4360
4361 idle = 1;
4362 ITERATE_RDEV(mddev,rdev,tmp) {
4363 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01004364 curr_events = disk_stat_read(disk, sectors[0]) +
4365 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08004367 /* The difference between curr_events and last_events
4368 * will be affected by any new non-sync IO (making
4369 * curr_events bigger) and any difference in the amount of
4370 * in-flight syncio (making current_events bigger or smaller)
4371 * The amount in-flight is currently limited to
4372 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4373 * which is at most 4096 sectors.
4374 * These numbers are fairly fragile and should be made
4375 * more robust, probably by enforcing the
4376 * 'window size' that md_do_sync sort-of uses.
4377 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 * Note: the following is an unsigned comparison.
4379 */
NeilBrownc0e48522005-11-18 01:11:01 -08004380 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 rdev->last_events = curr_events;
4382 idle = 0;
4383 }
4384 }
4385 return idle;
4386}
4387
4388void md_done_sync(mddev_t *mddev, int blocks, int ok)
4389{
4390 /* another "blocks" (512byte) blocks have been synced */
4391 atomic_sub(blocks, &mddev->recovery_active);
4392 wake_up(&mddev->recovery_wait);
4393 if (!ok) {
4394 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4395 md_wakeup_thread(mddev->thread);
4396 // stop recovery, signal do_sync ....
4397 }
4398}
4399
4400
NeilBrown06d91a52005-06-21 17:17:12 -07004401/* md_write_start(mddev, bi)
4402 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07004403 * in superblock) before writing, schedule a superblock update
4404 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07004405 */
NeilBrown3d310eb2005-06-21 17:17:26 -07004406void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407{
NeilBrown06d91a52005-06-21 17:17:12 -07004408 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07004409 return;
NeilBrown06d91a52005-06-21 17:17:12 -07004410
NeilBrownf91de922005-11-08 21:39:36 -08004411 BUG_ON(mddev->ro == 1);
4412 if (mddev->ro == 2) {
4413 /* need to switch to read/write */
4414 mddev->ro = 0;
4415 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4416 md_wakeup_thread(mddev->thread);
4417 }
NeilBrown06d91a52005-06-21 17:17:12 -07004418 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07004419 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08004420 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07004421 if (mddev->in_sync) {
4422 mddev->in_sync = 0;
4423 mddev->sb_dirty = 1;
4424 md_wakeup_thread(mddev->thread);
4425 }
NeilBrowna9701a32005-11-08 21:39:34 -08004426 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07004427 }
NeilBrown3d310eb2005-06-21 17:17:26 -07004428 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429}
4430
4431void md_write_end(mddev_t *mddev)
4432{
4433 if (atomic_dec_and_test(&mddev->writes_pending)) {
4434 if (mddev->safemode == 2)
4435 md_wakeup_thread(mddev->thread);
4436 else
4437 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
4438 }
4439}
4440
Adrian Bunk75c96f82005-05-05 16:16:09 -07004441static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
4443#define SYNC_MARKS 10
4444#define SYNC_MARK_STEP (3*HZ)
4445static void md_do_sync(mddev_t *mddev)
4446{
4447 mddev_t *mddev2;
4448 unsigned int currspeed = 0,
4449 window;
NeilBrown57afd892005-06-21 17:17:13 -07004450 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 unsigned long mark[SYNC_MARKS];
4452 sector_t mark_cnt[SYNC_MARKS];
4453 int last_mark,m;
4454 struct list_head *tmp;
4455 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07004456 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458 /* just incase thread restarts... */
4459 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
4460 return;
4461
4462 /* we overload curr_resync somewhat here.
4463 * 0 == not engaged in resync at all
4464 * 2 == checking that there is no conflict with another sync
4465 * 1 == like 2, but have yielded to allow conflicting resync to
4466 * commense
4467 * other == active in resync - this many blocks
4468 *
4469 * Before starting a resync we must have set curr_resync to
4470 * 2, and then checked that every "conflicting" array has curr_resync
4471 * less than ours. When we find one that is the same or higher
4472 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
4473 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
4474 * This will mean we have to start checking from the beginning again.
4475 *
4476 */
4477
4478 do {
4479 mddev->curr_resync = 2;
4480
4481 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08004482 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07004483 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 goto skip;
4485 }
4486 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 if (mddev2 == mddev)
4488 continue;
4489 if (mddev2->curr_resync &&
4490 match_mddev_units(mddev,mddev2)) {
4491 DEFINE_WAIT(wq);
4492 if (mddev < mddev2 && mddev->curr_resync == 2) {
4493 /* arbitrarily yield */
4494 mddev->curr_resync = 1;
4495 wake_up(&resync_wait);
4496 }
4497 if (mddev > mddev2 && mddev->curr_resync == 1)
4498 /* no need to wait here, we can wait the next
4499 * time 'round when curr_resync == 2
4500 */
4501 continue;
NeilBrown787453c2005-11-08 21:39:43 -08004502 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
4503 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07004504 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 printk(KERN_INFO "md: delaying resync of %s"
4506 " until %s has finished resync (they"
4507 " share one or more physical units)\n",
4508 mdname(mddev), mdname(mddev2));
4509 mddev_put(mddev2);
4510 schedule();
4511 finish_wait(&resync_wait, &wq);
4512 goto try_again;
4513 }
4514 finish_wait(&resync_wait, &wq);
4515 }
4516 }
4517 } while (mddev->curr_resync < 2);
4518
NeilBrown9d888832005-11-08 21:39:26 -08004519 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07004521 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 */
4523 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08004524 mddev->resync_mismatches = 0;
4525 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 /* recovery follows the physical size of devices */
4527 max_sectors = mddev->size << 1;
4528
4529 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4530 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
NeilBrown88202a02006-01-06 00:21:36 -08004531 " %d KB/sec/disc.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07004532 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 "(but not more than %d KB/sec) for reconstruction.\n",
NeilBrown88202a02006-01-06 00:21:36 -08004534 speed_max(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
4536 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07004537 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08004538 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4539 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 j = mddev->recovery_cp;
4541 else
4542 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07004543 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 for (m = 0; m < SYNC_MARKS; m++) {
4545 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004546 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 }
4548 last_mark = 0;
4549 mddev->resync_mark = mark[last_mark];
4550 mddev->resync_mark_cnt = mark_cnt[last_mark];
4551
4552 /*
4553 * Tune reconstruction:
4554 */
4555 window = 32*(PAGE_SIZE/512);
4556 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4557 window/2,(unsigned long long) max_sectors/2);
4558
4559 atomic_set(&mddev->recovery_active, 0);
4560 init_waitqueue_head(&mddev->recovery_wait);
4561 last_check = 0;
4562
4563 if (j>2) {
4564 printk(KERN_INFO
4565 "md: resuming recovery of %s from checkpoint.\n",
4566 mdname(mddev));
4567 mddev->curr_resync = j;
4568 }
4569
4570 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07004571 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
NeilBrown57afd892005-06-21 17:17:13 -07004573 skipped = 0;
4574 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08004575 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07004576 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4578 goto out;
4579 }
NeilBrown57afd892005-06-21 17:17:13 -07004580
4581 if (!skipped) { /* actual IO requested */
4582 io_sectors += sectors;
4583 atomic_add(sectors, &mddev->recovery_active);
4584 }
4585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 j += sectors;
4587 if (j>1) mddev->curr_resync = j;
NeilBrownd7603b72006-01-06 00:20:30 -08004588 if (last_check == 0)
4589 /* this is the earliers that rebuilt will be
4590 * visible in /proc/mdstat
4591 */
4592 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07004593
4594 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 continue;
4596
NeilBrown57afd892005-06-21 17:17:13 -07004597 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
4599 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4600 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4601 break;
4602
4603 repeat:
4604 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4605 /* step marks */
4606 int next = (last_mark+1) % SYNC_MARKS;
4607
4608 mddev->resync_mark = mark[next];
4609 mddev->resync_mark_cnt = mark_cnt[next];
4610 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004611 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 last_mark = next;
4613 }
4614
4615
NeilBrown787453c2005-11-08 21:39:43 -08004616 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 /*
4618 * got a signal, exit.
4619 */
4620 printk(KERN_INFO
4621 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4623 goto out;
4624 }
4625
4626 /*
4627 * this loop exits only if either when we are slower than
4628 * the 'hard' speed limit, or the system was IO-idle for
4629 * a jiffy.
4630 * the system might be non-idle CPU-wise, but we only care
4631 * about not overloading the IO subsystem. (things like an
4632 * e2fsck being done on the RAID array should execute fast)
4633 */
4634 mddev->queue->unplug_fn(mddev->queue);
4635 cond_resched();
4636
NeilBrown57afd892005-06-21 17:17:13 -07004637 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4638 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
NeilBrown88202a02006-01-06 00:21:36 -08004640 if (currspeed > speed_min(mddev)) {
4641 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08004643 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 goto repeat;
4645 }
4646 }
4647 }
4648 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4649 /*
4650 * this also signals 'finished resyncing' to md_stop
4651 */
4652 out:
4653 mddev->queue->unplug_fn(mddev->queue);
4654
4655 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4656
4657 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004658 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
4660 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4661 mddev->curr_resync > 2 &&
4662 mddev->curr_resync >= mddev->recovery_cp) {
4663 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4664 printk(KERN_INFO
4665 "md: checkpointing recovery of %s.\n",
4666 mdname(mddev));
4667 mddev->recovery_cp = mddev->curr_resync;
4668 } else
4669 mddev->recovery_cp = MaxSector;
4670 }
4671
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 skip:
4673 mddev->curr_resync = 0;
4674 wake_up(&resync_wait);
4675 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4676 md_wakeup_thread(mddev->thread);
4677}
4678
4679
4680/*
4681 * This routine is regularly called by all per-raid-array threads to
4682 * deal with generic issues like resync and super-block update.
4683 * Raid personalities that don't have a thread (linear/raid0) do not
4684 * need this as they never do any recovery or update the superblock.
4685 *
4686 * It does not do any resync itself, but rather "forks" off other threads
4687 * to do that as needed.
4688 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4689 * "->recovery" and create a thread at ->sync_thread.
4690 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4691 * and wakeups up this thread which will reap the thread and finish up.
4692 * This thread also removes any faulty devices (with nr_pending == 0).
4693 *
4694 * The overall approach is:
4695 * 1/ if the superblock needs updating, update it.
4696 * 2/ If a recovery thread is running, don't do anything else.
4697 * 3/ If recovery has finished, clean up, possibly marking spares active.
4698 * 4/ If there are any faulty devices, remove them.
4699 * 5/ If array is degraded, try to add spares devices
4700 * 6/ If array has spares or is not in-sync, start a resync thread.
4701 */
4702void md_check_recovery(mddev_t *mddev)
4703{
4704 mdk_rdev_t *rdev;
4705 struct list_head *rtmp;
4706
4707
NeilBrown5f404022005-06-21 17:17:16 -07004708 if (mddev->bitmap)
4709 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
4711 if (mddev->ro)
4712 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004713
4714 if (signal_pending(current)) {
4715 if (mddev->pers->sync_request) {
4716 printk(KERN_INFO "md: %s in immediate safe mode\n",
4717 mdname(mddev));
4718 mddev->safemode = 2;
4719 }
4720 flush_signals(current);
4721 }
4722
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 if ( ! (
4724 mddev->sb_dirty ||
4725 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004726 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4727 (mddev->safemode == 1) ||
4728 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4729 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 ))
4731 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004732
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 if (mddev_trylock(mddev)==0) {
4734 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004735
NeilBrowna9701a32005-11-08 21:39:34 -08004736 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004737 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4738 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4739 mddev->in_sync = 1;
4740 mddev->sb_dirty = 1;
4741 }
4742 if (mddev->safemode == 1)
4743 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08004744 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004745
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 if (mddev->sb_dirty)
4747 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004748
NeilBrown06d91a52005-06-21 17:17:12 -07004749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4751 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4752 /* resync/recovery still happening */
4753 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4754 goto unlock;
4755 }
4756 if (mddev->sync_thread) {
4757 /* resync has finished, collect result */
4758 md_unregister_thread(mddev->sync_thread);
4759 mddev->sync_thread = NULL;
4760 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4761 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4762 /* success...*/
4763 /* activate any spares */
4764 mddev->pers->spare_active(mddev);
4765 }
4766 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004767
4768 /* if array is no-longer degraded, then any saved_raid_disk
4769 * information must be scrapped
4770 */
4771 if (!mddev->degraded)
4772 ITERATE_RDEV(mddev,rdev,rtmp)
4773 rdev->saved_raid_disk = -1;
4774
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 mddev->recovery = 0;
4776 /* flag recovery needed just to double check */
4777 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08004778 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 goto unlock;
4780 }
NeilBrown24dd4692005-11-08 21:39:26 -08004781 /* Clear some bits that don't mean anything, but
4782 * might be left set
4783 */
4784 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4785 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4786 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4787 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
4789 /* no recovery is running.
4790 * remove any failed drives, then
4791 * add spares if possible.
4792 * Spare are also removed and re-added, to allow
4793 * the personality to fail the re-add.
4794 */
4795 ITERATE_RDEV(mddev,rdev,rtmp)
4796 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004797 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004799 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4800 char nm[20];
4801 sprintf(nm,"rd%d", rdev->raid_disk);
4802 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 }
4806
4807 if (mddev->degraded) {
4808 ITERATE_RDEV(mddev,rdev,rtmp)
4809 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08004810 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004811 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4812 char nm[20];
4813 sprintf(nm, "rd%d", rdev->raid_disk);
4814 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 spares++;
NeilBrownd7603b72006-01-06 00:20:30 -08004816 md_new_event(mddev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08004817 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 break;
4819 }
4820 }
4821
NeilBrown24dd4692005-11-08 21:39:26 -08004822 if (spares) {
4823 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4824 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4825 } else if (mddev->recovery_cp < MaxSector) {
4826 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4827 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4828 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08004830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 if (mddev->pers->sync_request) {
4832 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07004833 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4834 /* We are adding a device or devices to an array
4835 * which has the bitmap stored on all devices.
4836 * So make sure all bitmap pages get written
4837 */
4838 bitmap_write_all(mddev->bitmap);
4839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 mddev->sync_thread = md_register_thread(md_do_sync,
4841 mddev,
4842 "%s_resync");
4843 if (!mddev->sync_thread) {
4844 printk(KERN_ERR "%s: could not start resync"
4845 " thread...\n",
4846 mdname(mddev));
4847 /* leave the spares where they are, it shouldn't hurt */
4848 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08004849 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004851 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 }
4853 unlock:
4854 mddev_unlock(mddev);
4855 }
4856}
4857
Adrian Bunk75c96f82005-05-05 16:16:09 -07004858static int md_notify_reboot(struct notifier_block *this,
4859 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860{
4861 struct list_head *tmp;
4862 mddev_t *mddev;
4863
4864 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4865
4866 printk(KERN_INFO "md: stopping all md devices.\n");
4867
4868 ITERATE_MDDEV(mddev,tmp)
4869 if (mddev_trylock(mddev)==0)
4870 do_md_stop (mddev, 1);
4871 /*
4872 * certain more exotic SCSI devices are known to be
4873 * volatile wrt too early system reboots. While the
4874 * right place to handle this issue is the given
4875 * driver, we do want to have a safe RAID driver ...
4876 */
4877 mdelay(1000*1);
4878 }
4879 return NOTIFY_DONE;
4880}
4881
Adrian Bunk75c96f82005-05-05 16:16:09 -07004882static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 .notifier_call = md_notify_reboot,
4884 .next = NULL,
4885 .priority = INT_MAX, /* before any real devices */
4886};
4887
4888static void md_geninit(void)
4889{
4890 struct proc_dir_entry *p;
4891
4892 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4893
4894 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4895 if (p)
4896 p->proc_fops = &md_seq_fops;
4897}
4898
Adrian Bunk75c96f82005-05-05 16:16:09 -07004899static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900{
4901 int minor;
4902
4903 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4904 " MD_SB_DISKS=%d\n",
4905 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4906 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08004907 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07004908 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
4910 if (register_blkdev(MAJOR_NR, "md"))
4911 return -1;
4912 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4913 unregister_blkdev(MAJOR_NR, "md");
4914 return -1;
4915 }
4916 devfs_mk_dir("md");
4917 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4918 md_probe, NULL, NULL);
4919 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4920 md_probe, NULL, NULL);
4921
4922 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4923 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4924 S_IFBLK|S_IRUSR|S_IWUSR,
4925 "md/%d", minor);
4926
4927 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4928 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4929 S_IFBLK|S_IRUSR|S_IWUSR,
4930 "md/mdp%d", minor);
4931
4932
4933 register_reboot_notifier(&md_notifier);
4934 raid_table_header = register_sysctl_table(raid_root_table, 1);
4935
4936 md_geninit();
4937 return (0);
4938}
4939
4940
4941#ifndef MODULE
4942
4943/*
4944 * Searches all registered partitions for autorun RAID arrays
4945 * at boot time.
4946 */
4947static dev_t detected_devices[128];
4948static int dev_cnt;
4949
4950void md_autodetect_dev(dev_t dev)
4951{
4952 if (dev_cnt >= 0 && dev_cnt < 127)
4953 detected_devices[dev_cnt++] = dev;
4954}
4955
4956
4957static void autostart_arrays(int part)
4958{
4959 mdk_rdev_t *rdev;
4960 int i;
4961
4962 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4963
4964 for (i = 0; i < dev_cnt; i++) {
4965 dev_t dev = detected_devices[i];
4966
4967 rdev = md_import_device(dev,0, 0);
4968 if (IS_ERR(rdev))
4969 continue;
4970
NeilBrownb2d444d2005-11-08 21:39:31 -08004971 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 MD_BUG();
4973 continue;
4974 }
4975 list_add(&rdev->same_set, &pending_raid_disks);
4976 }
4977 dev_cnt = 0;
4978
4979 autorun_devices(part);
4980}
4981
4982#endif
4983
4984static __exit void md_exit(void)
4985{
4986 mddev_t *mddev;
4987 struct list_head *tmp;
4988 int i;
4989 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4990 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4991 for (i=0; i < MAX_MD_DEVS; i++)
4992 devfs_remove("md/%d", i);
4993 for (i=0; i < MAX_MD_DEVS; i++)
4994 devfs_remove("md/d%d", i);
4995
4996 devfs_remove("md");
4997
4998 unregister_blkdev(MAJOR_NR,"md");
4999 unregister_blkdev(mdp_major, "mdp");
5000 unregister_reboot_notifier(&md_notifier);
5001 unregister_sysctl_table(raid_table_header);
5002 remove_proc_entry("mdstat", NULL);
5003 ITERATE_MDDEV(mddev,tmp) {
5004 struct gendisk *disk = mddev->gendisk;
5005 if (!disk)
5006 continue;
5007 export_array(mddev);
5008 del_gendisk(disk);
5009 put_disk(disk);
5010 mddev->gendisk = NULL;
5011 mddev_put(mddev);
5012 }
5013}
5014
5015module_init(md_init)
5016module_exit(md_exit)
5017
NeilBrownf91de922005-11-08 21:39:36 -08005018static int get_ro(char *buffer, struct kernel_param *kp)
5019{
5020 return sprintf(buffer, "%d", start_readonly);
5021}
5022static int set_ro(const char *val, struct kernel_param *kp)
5023{
5024 char *e;
5025 int num = simple_strtoul(val, &e, 10);
5026 if (*val && (*e == '\0' || *e == '\n')) {
5027 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005028 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005029 }
5030 return -EINVAL;
5031}
5032
5033module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005034module_param(start_dirty_degraded, int, 0644);
5035
NeilBrownf91de922005-11-08 21:39:36 -08005036
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037EXPORT_SYMBOL(register_md_personality);
5038EXPORT_SYMBOL(unregister_md_personality);
5039EXPORT_SYMBOL(md_error);
5040EXPORT_SYMBOL(md_done_sync);
5041EXPORT_SYMBOL(md_write_start);
5042EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043EXPORT_SYMBOL(md_register_thread);
5044EXPORT_SYMBOL(md_unregister_thread);
5045EXPORT_SYMBOL(md_wakeup_thread);
5046EXPORT_SYMBOL(md_print_devices);
5047EXPORT_SYMBOL(md_check_recovery);
5048MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005049MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005050MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);