blob: b6d16022a53e15e25006814459b9159c10611bad [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>
NeilBrowna6fb0932005-09-09 16:23:56 -070036#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/linkage.h>
38#include <linux/raid/md.h>
NeilBrown32a76272005-06-21 17:17:14 -070039#include <linux/raid/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/buffer_head.h> /* for invalidate_bdev */
42#include <linux/suspend.h>
NeilBrownd7603b72006-01-06 00:20:30 -080043#include <linux/poll.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080044#include <linux/mutex.h>
NeilBrown16f17b32006-06-26 00:27:37 -070045#include <linux/ctype.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
Adrian Bunk5e563412006-06-26 00:27:42 -070074static void md_print_devices(void);
75
76#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
79 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
80 * is 1000 KB/sec, so the extra system load does not show up that much.
81 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070082 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * subsystem is idle. There is also an 'absolute maximum' reconstruction
84 * speed limit - in case reconstruction slows down your system despite
85 * idle IO detection.
86 *
87 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080088 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
90
91static int sysctl_speed_limit_min = 1000;
92static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080093static inline int speed_min(mddev_t *mddev)
94{
95 return mddev->sync_speed_min ?
96 mddev->sync_speed_min : sysctl_speed_limit_min;
97}
98
99static inline int speed_max(mddev_t *mddev)
100{
101 return mddev->sync_speed_max ?
102 mddev->sync_speed_max : sysctl_speed_limit_max;
103}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105static struct ctl_table_header *raid_table_header;
106
107static ctl_table raid_table[] = {
108 {
109 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
110 .procname = "speed_limit_min",
111 .data = &sysctl_speed_limit_min,
112 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700113 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .proc_handler = &proc_dointvec,
115 },
116 {
117 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
118 .procname = "speed_limit_max",
119 .data = &sysctl_speed_limit_max,
120 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700121 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .proc_handler = &proc_dointvec,
123 },
124 { .ctl_name = 0 }
125};
126
127static ctl_table raid_dir_table[] = {
128 {
129 .ctl_name = DEV_RAID,
130 .procname = "raid",
131 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700132 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .child = raid_table,
134 },
135 { .ctl_name = 0 }
136};
137
138static ctl_table raid_root_table[] = {
139 {
140 .ctl_name = CTL_DEV,
141 .procname = "dev",
142 .maxlen = 0,
143 .mode = 0555,
144 .child = raid_dir_table,
145 },
146 { .ctl_name = 0 }
147};
148
149static struct block_device_operations md_fops;
150
NeilBrownf91de922005-11-08 21:39:36 -0800151static int start_readonly;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
NeilBrownd7603b72006-01-06 00:20:30 -0800154 * We have a system wide 'event count' that is incremented
155 * on any 'interesting' event, and readers of /proc/mdstat
156 * can use 'poll' or 'select' to find out when the event
157 * count increases.
158 *
159 * Events are:
160 * start array, stop array, error, add device, remove device,
161 * start build, activate spare
162 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800163static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800164static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800165void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800166{
167 atomic_inc(&md_event_count);
168 wake_up(&md_event_waiters);
NeilBrown4508a7a2006-03-20 17:53:53 +1100169 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -0800170}
NeilBrown29269552006-03-27 01:18:10 -0800171EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800172
NeilBrownc331eb02006-05-30 21:27:13 -0700173/* Alternate version that can be called from interrupts
174 * when calling sysfs_notify isn't needed.
175 */
Adrian Bunk05381952006-06-26 00:28:01 -0700176static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700177{
178 atomic_inc(&md_event_count);
179 wake_up(&md_event_waiters);
180}
181
NeilBrownd7603b72006-01-06 00:20:30 -0800182/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * Enables to iterate over all existing md arrays
184 * all_mddevs_lock protects this list.
185 */
186static LIST_HEAD(all_mddevs);
187static DEFINE_SPINLOCK(all_mddevs_lock);
188
189
190/*
191 * iterates through all used mddevs in the system.
192 * We take care to grab the all_mddevs_lock whenever navigating
193 * the list, and to always hold a refcount when unlocked.
194 * Any code which breaks out of this loop while own
195 * a reference to the current mddev and must mddev_put it.
196 */
197#define ITERATE_MDDEV(mddev,tmp) \
198 \
199 for (({ spin_lock(&all_mddevs_lock); \
200 tmp = all_mddevs.next; \
201 mddev = NULL;}); \
202 ({ if (tmp != &all_mddevs) \
203 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
204 spin_unlock(&all_mddevs_lock); \
205 if (mddev) mddev_put(mddev); \
206 mddev = list_entry(tmp, mddev_t, all_mddevs); \
207 tmp != &all_mddevs;}); \
208 ({ spin_lock(&all_mddevs_lock); \
209 tmp = tmp->next;}) \
210 )
211
212
213static int md_fail_request (request_queue_t *q, struct bio *bio)
214{
215 bio_io_error(bio, bio->bi_size);
216 return 0;
217}
218
219static inline mddev_t *mddev_get(mddev_t *mddev)
220{
221 atomic_inc(&mddev->active);
222 return mddev;
223}
224
225static void mddev_put(mddev_t *mddev)
226{
227 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
228 return;
229 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
230 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800231 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500232 blk_cleanup_queue(mddev->queue);
NeilBrowneae17012005-11-08 21:39:23 -0800233 kobject_unregister(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800234 } else
235 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238static mddev_t * mddev_find(dev_t unit)
239{
240 mddev_t *mddev, *new = NULL;
241
242 retry:
243 spin_lock(&all_mddevs_lock);
244 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
245 if (mddev->unit == unit) {
246 mddev_get(mddev);
247 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700248 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return mddev;
250 }
251
252 if (new) {
253 list_add(&new->all_mddevs, &all_mddevs);
254 spin_unlock(&all_mddevs_lock);
255 return new;
256 }
257 spin_unlock(&all_mddevs_lock);
258
NeilBrown9ffae0c2006-01-06 00:20:32 -0800259 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!new)
261 return NULL;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 new->unit = unit;
264 if (MAJOR(unit) == MD_MAJOR)
265 new->md_minor = MINOR(unit);
266 else
267 new->md_minor = MINOR(unit) >> MdpMinorShift;
268
NeilBrowndf5b89b2006-03-27 01:18:20 -0800269 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 INIT_LIST_HEAD(&new->disks);
271 INIT_LIST_HEAD(&new->all_mddevs);
272 init_timer(&new->safemode_timer);
273 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700274 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700275 init_waitqueue_head(&new->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 new->queue = blk_alloc_queue(GFP_KERNEL);
278 if (!new->queue) {
279 kfree(new);
280 return NULL;
281 }
NeilBrown89e5c8b2006-03-27 01:18:02 -0800282 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 blk_queue_make_request(new->queue, md_fail_request);
285
286 goto retry;
287}
288
289static inline int mddev_lock(mddev_t * mddev)
290{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800291 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static inline int mddev_trylock(mddev_t * mddev)
295{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800296 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299static inline void mddev_unlock(mddev_t * mddev)
300{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800301 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
NeilBrown005eca52005-08-22 13:11:08 -0700303 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
NeilBrown2989ddb2006-01-06 00:20:43 -0800306static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 mdk_rdev_t * rdev;
309 struct list_head *tmp;
310
311 ITERATE_RDEV(mddev,rdev,tmp) {
312 if (rdev->desc_nr == nr)
313 return rdev;
314 }
315 return NULL;
316}
317
318static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
319{
320 struct list_head *tmp;
321 mdk_rdev_t *rdev;
322
323 ITERATE_RDEV(mddev,rdev,tmp) {
324 if (rdev->bdev->bd_dev == dev)
325 return rdev;
326 }
327 return NULL;
328}
329
NeilBrownd9d166c2006-01-06 00:20:51 -0800330static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800331{
332 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800333 list_for_each_entry(pers, &pers_list, list) {
334 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800335 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800336 if (strcmp(pers->name, clevel)==0)
337 return pers;
338 }
NeilBrown2604b702006-01-06 00:20:36 -0800339 return NULL;
340}
341
Jesper Juhl77933d72005-07-27 11:46:09 -0700342static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
345 return MD_NEW_SIZE_BLOCKS(size);
346}
347
348static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
349{
350 sector_t size;
351
352 size = rdev->sb_offset;
353
354 if (chunk_size)
355 size &= ~((sector_t)chunk_size/1024 - 1);
356 return size;
357}
358
359static int alloc_disk_sb(mdk_rdev_t * rdev)
360{
361 if (rdev->sb_page)
362 MD_BUG();
363
364 rdev->sb_page = alloc_page(GFP_KERNEL);
365 if (!rdev->sb_page) {
366 printk(KERN_ALERT "md: out of memory.\n");
367 return -EINVAL;
368 }
369
370 return 0;
371}
372
373static void free_disk_sb(mdk_rdev_t * rdev)
374{
375 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800376 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 rdev->sb_loaded = 0;
378 rdev->sb_page = NULL;
379 rdev->sb_offset = 0;
380 rdev->size = 0;
381 }
382}
383
384
NeilBrown7bfa19f2005-06-21 17:17:28 -0700385static int super_written(struct bio *bio, unsigned int bytes_done, int error)
386{
387 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800388 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700389 if (bio->bi_size)
390 return 1;
391
392 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
NeilBrowna9701a32005-11-08 21:39:34 -0800393 md_error(mddev, rdev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700394
NeilBrowna9701a32005-11-08 21:39:34 -0800395 if (atomic_dec_and_test(&mddev->pending_writes))
396 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700397 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700398 return 0;
399}
400
NeilBrowna9701a32005-11-08 21:39:34 -0800401static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
402{
403 struct bio *bio2 = bio->bi_private;
404 mdk_rdev_t *rdev = bio2->bi_private;
405 mddev_t *mddev = rdev->mddev;
406 if (bio->bi_size)
407 return 1;
408
409 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
410 error == -EOPNOTSUPP) {
411 unsigned long flags;
412 /* barriers don't appear to be supported :-( */
413 set_bit(BarriersNotsupp, &rdev->flags);
414 mddev->barriers_work = 0;
415 spin_lock_irqsave(&mddev->write_lock, flags);
416 bio2->bi_next = mddev->biolist;
417 mddev->biolist = bio2;
418 spin_unlock_irqrestore(&mddev->write_lock, flags);
419 wake_up(&mddev->sb_wait);
420 bio_put(bio);
421 return 0;
422 }
423 bio_put(bio2);
424 bio->bi_private = rdev;
425 return super_written(bio, bytes_done, error);
426}
427
NeilBrown7bfa19f2005-06-21 17:17:28 -0700428void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
429 sector_t sector, int size, struct page *page)
430{
431 /* write first size bytes of page to sector of rdev
432 * Increment mddev->pending_writes before returning
433 * and decrement it on completion, waking up sb_wait
434 * if zero is reached.
435 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800436 *
437 * As we might need to resubmit the request if BIO_RW_BARRIER
438 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700439 */
440 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800441 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700442
443 bio->bi_bdev = rdev->bdev;
444 bio->bi_sector = sector;
445 bio_add_page(bio, page, size, 0);
446 bio->bi_private = rdev;
447 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800448 bio->bi_rw = rw;
449
NeilBrown7bfa19f2005-06-21 17:17:28 -0700450 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800451 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
452 struct bio *rbio;
453 rw |= (1<<BIO_RW_BARRIER);
454 rbio = bio_clone(bio, GFP_NOIO);
455 rbio->bi_private = bio;
456 rbio->bi_end_io = super_written_barrier;
457 submit_bio(rw, rbio);
458 } else
459 submit_bio(rw, bio);
460}
461
462void md_super_wait(mddev_t *mddev)
463{
464 /* wait for all superblock writes that were scheduled to complete.
465 * if any had to be retried (due to BARRIER problems), retry them
466 */
467 DEFINE_WAIT(wq);
468 for(;;) {
469 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
470 if (atomic_read(&mddev->pending_writes)==0)
471 break;
472 while (mddev->biolist) {
473 struct bio *bio;
474 spin_lock_irq(&mddev->write_lock);
475 bio = mddev->biolist;
476 mddev->biolist = bio->bi_next ;
477 bio->bi_next = NULL;
478 spin_unlock_irq(&mddev->write_lock);
479 submit_bio(bio->bi_rw, bio);
480 }
481 schedule();
482 }
483 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
487{
488 if (bio->bi_size)
489 return 1;
490
491 complete((struct completion*)bio->bi_private);
492 return 0;
493}
494
NeilBrowna654b9d82005-06-21 17:17:27 -0700495int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct page *page, int rw)
497{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700498 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct completion event;
500 int ret;
501
502 rw |= (1 << BIO_RW_SYNC);
503
504 bio->bi_bdev = bdev;
505 bio->bi_sector = sector;
506 bio_add_page(bio, page, size, 0);
507 init_completion(&event);
508 bio->bi_private = &event;
509 bio->bi_end_io = bi_complete;
510 submit_bio(rw, bio);
511 wait_for_completion(&event);
512
513 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
514 bio_put(bio);
515 return ret;
516}
NeilBrowna8745db2006-01-06 00:20:34 -0800517EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
NeilBrown0002b272005-09-09 16:23:53 -0700519static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 char b[BDEVNAME_SIZE];
522 if (!rdev->sb_page) {
523 MD_BUG();
524 return -EINVAL;
525 }
526 if (rdev->sb_loaded)
527 return 0;
528
529
NeilBrown0002b272005-09-09 16:23:53 -0700530 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto fail;
532 rdev->sb_loaded = 1;
533 return 0;
534
535fail:
536 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
537 bdevname(rdev->bdev,b));
538 return -EINVAL;
539}
540
541static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
542{
543 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
544 (sb1->set_uuid1 == sb2->set_uuid1) &&
545 (sb1->set_uuid2 == sb2->set_uuid2) &&
546 (sb1->set_uuid3 == sb2->set_uuid3))
547
548 return 1;
549
550 return 0;
551}
552
553
554static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
555{
556 int ret;
557 mdp_super_t *tmp1, *tmp2;
558
559 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
560 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
561
562 if (!tmp1 || !tmp2) {
563 ret = 0;
564 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
565 goto abort;
566 }
567
568 *tmp1 = *sb1;
569 *tmp2 = *sb2;
570
571 /*
572 * nr_disks is not constant
573 */
574 tmp1->nr_disks = 0;
575 tmp2->nr_disks = 0;
576
577 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
578 ret = 0;
579 else
580 ret = 1;
581
582abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700583 kfree(tmp1);
584 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return ret;
586}
587
588static unsigned int calc_sb_csum(mdp_super_t * sb)
589{
590 unsigned int disk_csum, csum;
591
592 disk_csum = sb->sb_csum;
593 sb->sb_csum = 0;
594 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
595 sb->sb_csum = disk_csum;
596 return csum;
597}
598
599
600/*
601 * Handle superblock details.
602 * We want to be able to handle multiple superblock formats
603 * so we have a common interface to them all, and an array of
604 * different handlers.
605 * We rely on user-space to write the initial superblock, and support
606 * reading and updating of superblocks.
607 * Interface methods are:
608 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
609 * loads and validates a superblock on dev.
610 * if refdev != NULL, compare superblocks on both devices
611 * Return:
612 * 0 - dev has a superblock that is compatible with refdev
613 * 1 - dev has a superblock that is compatible and newer than refdev
614 * so dev should be used as the refdev in future
615 * -EINVAL superblock incompatible or invalid
616 * -othererror e.g. -EIO
617 *
618 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
619 * Verify that dev is acceptable into mddev.
620 * The first time, mddev->raid_disks will be 0, and data from
621 * dev should be merged in. Subsequent calls check that dev
622 * is new enough. Return 0 or -EINVAL
623 *
624 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
625 * Update the superblock for rdev with data in mddev
626 * This does not write to disc.
627 *
628 */
629
630struct super_type {
631 char *name;
632 struct module *owner;
633 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
634 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
635 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
636};
637
638/*
639 * load_super for 0.90.0
640 */
641static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
642{
643 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
644 mdp_super_t *sb;
645 int ret;
646 sector_t sb_offset;
647
648 /*
649 * Calculate the position of the superblock,
650 * it's at the end of the disk.
651 *
652 * It also happens to be a multiple of 4Kb.
653 */
654 sb_offset = calc_dev_sboffset(rdev->bdev);
655 rdev->sb_offset = sb_offset;
656
NeilBrown0002b272005-09-09 16:23:53 -0700657 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (ret) return ret;
659
660 ret = -EINVAL;
661
662 bdevname(rdev->bdev, b);
663 sb = (mdp_super_t*)page_address(rdev->sb_page);
664
665 if (sb->md_magic != MD_SB_MAGIC) {
666 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
667 b);
668 goto abort;
669 }
670
671 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800672 sb->minor_version < 90 ||
673 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
675 sb->major_version, sb->minor_version,
676 b);
677 goto abort;
678 }
679
680 if (sb->raid_disks <= 0)
681 goto abort;
682
683 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
684 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
685 b);
686 goto abort;
687 }
688
689 rdev->preferred_minor = sb->md_minor;
690 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700691 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (sb->level == LEVEL_MULTIPATH)
694 rdev->desc_nr = -1;
695 else
696 rdev->desc_nr = sb->this_disk.number;
697
698 if (refdev == 0)
699 ret = 1;
700 else {
701 __u64 ev1, ev2;
702 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
703 if (!uuid_equal(refsb, sb)) {
704 printk(KERN_WARNING "md: %s has different UUID to %s\n",
705 b, bdevname(refdev->bdev,b2));
706 goto abort;
707 }
708 if (!sb_equal(refsb, sb)) {
709 printk(KERN_WARNING "md: %s has same UUID"
710 " but different superblock to %s\n",
711 b, bdevname(refdev->bdev, b2));
712 goto abort;
713 }
714 ev1 = md_event(sb);
715 ev2 = md_event(refsb);
716 if (ev1 > ev2)
717 ret = 1;
718 else
719 ret = 0;
720 }
721 rdev->size = calc_dev_size(rdev, sb->chunk_size);
722
NeilBrown2bf071b2006-01-06 00:20:55 -0800723 if (rdev->size < sb->size && sb->level > 1)
724 /* "this cannot possibly happen" ... */
725 ret = -EINVAL;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 abort:
728 return ret;
729}
730
731/*
732 * validate_super for 0.90.0
733 */
734static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
735{
736 mdp_disk_t *desc;
737 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700738 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
NeilBrown41158c72005-06-21 17:17:25 -0700740 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800741 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (mddev->raid_disks == 0) {
743 mddev->major_version = 0;
744 mddev->minor_version = sb->minor_version;
745 mddev->patch_version = sb->patch_version;
746 mddev->persistent = ! sb->not_persistent;
747 mddev->chunk_size = sb->chunk_size;
748 mddev->ctime = sb->ctime;
749 mddev->utime = sb->utime;
750 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800751 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 mddev->layout = sb->layout;
753 mddev->raid_disks = sb->raid_disks;
754 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700755 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700756 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700757 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
NeilBrownf6705572006-03-27 01:18:11 -0800759 if (mddev->minor_version >= 91) {
760 mddev->reshape_position = sb->reshape_position;
761 mddev->delta_disks = sb->delta_disks;
762 mddev->new_level = sb->new_level;
763 mddev->new_layout = sb->new_layout;
764 mddev->new_chunk = sb->new_chunk;
765 } else {
766 mddev->reshape_position = MaxSector;
767 mddev->delta_disks = 0;
768 mddev->new_level = mddev->level;
769 mddev->new_layout = mddev->layout;
770 mddev->new_chunk = mddev->chunk_size;
771 }
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (sb->state & (1<<MD_SB_CLEAN))
774 mddev->recovery_cp = MaxSector;
775 else {
776 if (sb->events_hi == sb->cp_events_hi &&
777 sb->events_lo == sb->cp_events_lo) {
778 mddev->recovery_cp = sb->recovery_cp;
779 } else
780 mddev->recovery_cp = 0;
781 }
782
783 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
784 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
785 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
786 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
787
788 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700789
790 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
791 mddev->bitmap_file == NULL) {
NeilBrownc5a10f62006-03-27 01:18:03 -0800792 if (mddev->level != 1 && mddev->level != 4
793 && mddev->level != 5 && mddev->level != 6
NeilBrown6cce3b22006-01-06 00:20:16 -0800794 && mddev->level != 10) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700795 /* FIXME use a better test */
NeilBrown6cce3b22006-01-06 00:20:16 -0800796 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -0700797 return -EINVAL;
798 }
NeilBrown36fa3062005-09-09 16:23:45 -0700799 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700800 }
801
NeilBrown41158c72005-06-21 17:17:25 -0700802 } else if (mddev->pers == NULL) {
803 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 ++ev1;
805 if (ev1 < mddev->events)
806 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700807 } else if (mddev->bitmap) {
808 /* if adding to array with a bitmap, then we can accept an
809 * older device ... but not too old.
810 */
NeilBrown41158c72005-06-21 17:17:25 -0700811 if (ev1 < mddev->bitmap->events_cleared)
812 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700813 } else {
814 if (ev1 < mddev->events)
815 /* just a hot-add of a new device, leave raid_disk at -1 */
816 return 0;
817 }
NeilBrown41158c72005-06-21 17:17:25 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 desc = sb->disks + rdev->desc_nr;
821
822 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800823 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700824 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
825 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800826 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 rdev->raid_disk = desc->raid_disk;
828 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700829 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
830 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700831 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800832 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return 0;
834}
835
836/*
837 * sync_super for 0.90.0
838 */
839static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
840{
841 mdp_super_t *sb;
842 struct list_head *tmp;
843 mdk_rdev_t *rdev2;
844 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 /* make rdev->sb match mddev data..
848 *
849 * 1/ zero out disks
850 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
851 * 3/ any empty disks < next_spare become removed
852 *
853 * disks[0] gets initialised to REMOVED because
854 * we cannot be sure from other fields if it has
855 * been initialised or not.
856 */
857 int i;
858 int active=0, working=0,failed=0,spare=0,nr_disks=0;
859
NeilBrown61181562005-09-09 16:24:02 -0700860 rdev->sb_size = MD_SB_BYTES;
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 sb = (mdp_super_t*)page_address(rdev->sb_page);
863
864 memset(sb, 0, sizeof(*sb));
865
866 sb->md_magic = MD_SB_MAGIC;
867 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 sb->patch_version = mddev->patch_version;
869 sb->gvalid_words = 0; /* ignored */
870 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
871 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
872 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
873 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
874
875 sb->ctime = mddev->ctime;
876 sb->level = mddev->level;
877 sb->size = mddev->size;
878 sb->raid_disks = mddev->raid_disks;
879 sb->md_minor = mddev->md_minor;
880 sb->not_persistent = !mddev->persistent;
881 sb->utime = mddev->utime;
882 sb->state = 0;
883 sb->events_hi = (mddev->events>>32);
884 sb->events_lo = (u32)mddev->events;
885
NeilBrownf6705572006-03-27 01:18:11 -0800886 if (mddev->reshape_position == MaxSector)
887 sb->minor_version = 90;
888 else {
889 sb->minor_version = 91;
890 sb->reshape_position = mddev->reshape_position;
891 sb->new_level = mddev->new_level;
892 sb->delta_disks = mddev->delta_disks;
893 sb->new_layout = mddev->new_layout;
894 sb->new_chunk = mddev->new_chunk;
895 }
896 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (mddev->in_sync)
898 {
899 sb->recovery_cp = mddev->recovery_cp;
900 sb->cp_events_hi = (mddev->events>>32);
901 sb->cp_events_lo = (u32)mddev->events;
902 if (mddev->recovery_cp == MaxSector)
903 sb->state = (1<< MD_SB_CLEAN);
904 } else
905 sb->recovery_cp = 0;
906
907 sb->layout = mddev->layout;
908 sb->chunk_size = mddev->chunk_size;
909
NeilBrowna654b9d82005-06-21 17:17:27 -0700910 if (mddev->bitmap && mddev->bitmap_file == NULL)
911 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 sb->disks[0].state = (1<<MD_DISK_REMOVED);
914 ITERATE_RDEV(mddev,rdev2,tmp) {
915 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800916 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800917 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
918 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800919 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800921 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800922 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 d = &sb->disks[rdev2->desc_nr];
924 nr_disks++;
925 d->number = rdev2->desc_nr;
926 d->major = MAJOR(rdev2->bdev->bd_dev);
927 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800928 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
929 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 d->raid_disk = rdev2->raid_disk;
931 else
932 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800933 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800935 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 d->state = (1<<MD_DISK_ACTIVE);
937 d->state |= (1<<MD_DISK_SYNC);
938 active++;
939 working++;
940 } else {
941 d->state = 0;
942 spare++;
943 working++;
944 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700945 if (test_bit(WriteMostly, &rdev2->flags))
946 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /* now set the "removed" and "faulty" bits on any missing devices */
949 for (i=0 ; i < mddev->raid_disks ; i++) {
950 mdp_disk_t *d = &sb->disks[i];
951 if (d->state == 0 && d->number == 0) {
952 d->number = i;
953 d->raid_disk = i;
954 d->state = (1<<MD_DISK_REMOVED);
955 d->state |= (1<<MD_DISK_FAULTY);
956 failed++;
957 }
958 }
959 sb->nr_disks = nr_disks;
960 sb->active_disks = active;
961 sb->working_disks = working;
962 sb->failed_disks = failed;
963 sb->spare_disks = spare;
964
965 sb->this_disk = sb->disks[rdev->desc_nr];
966 sb->sb_csum = calc_sb_csum(sb);
967}
968
969/*
970 * version 1 superblock
971 */
972
973static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
974{
975 unsigned int disk_csum, csum;
976 unsigned long long newcsum;
977 int size = 256 + le32_to_cpu(sb->max_dev)*2;
978 unsigned int *isuper = (unsigned int*)sb;
979 int i;
980
981 disk_csum = sb->sb_csum;
982 sb->sb_csum = 0;
983 newcsum = 0;
984 for (i=0; size>=4; size -= 4 )
985 newcsum += le32_to_cpu(*isuper++);
986
987 if (size == 2)
988 newcsum += le16_to_cpu(*(unsigned short*) isuper);
989
990 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
991 sb->sb_csum = disk_csum;
992 return cpu_to_le32(csum);
993}
994
995static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
996{
997 struct mdp_superblock_1 *sb;
998 int ret;
999 sector_t sb_offset;
1000 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001001 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /*
1004 * Calculate the position of the superblock.
1005 * It is always aligned to a 4K boundary and
1006 * depeding on minor_version, it can be:
1007 * 0: At least 8K, but less than 12K, from end of device
1008 * 1: At start of device
1009 * 2: 4K from start of device.
1010 */
1011 switch(minor_version) {
1012 case 0:
1013 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1014 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001015 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /* convert from sectors to K */
1017 sb_offset /= 2;
1018 break;
1019 case 1:
1020 sb_offset = 0;
1021 break;
1022 case 2:
1023 sb_offset = 4;
1024 break;
1025 default:
1026 return -EINVAL;
1027 }
1028 rdev->sb_offset = sb_offset;
1029
NeilBrown0002b272005-09-09 16:23:53 -07001030 /* superblock is rarely larger than 1K, but it can be larger,
1031 * and it is safe to read 4k, so we do that
1032 */
1033 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (ret) return ret;
1035
1036
1037 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1038
1039 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1040 sb->major_version != cpu_to_le32(1) ||
1041 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1042 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001043 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return -EINVAL;
1045
1046 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1047 printk("md: invalid superblock checksum on %s\n",
1048 bdevname(rdev->bdev,b));
1049 return -EINVAL;
1050 }
1051 if (le64_to_cpu(sb->data_size) < 10) {
1052 printk("md: data_size too small on %s\n",
1053 bdevname(rdev->bdev,b));
1054 return -EINVAL;
1055 }
1056 rdev->preferred_minor = 0xffff;
1057 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001058 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
NeilBrown0002b272005-09-09 16:23:53 -07001060 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001061 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001062 if (rdev->sb_size & bmask)
1063 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1064
NeilBrown31b65a02006-07-10 04:44:14 -07001065 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1066 rdev->desc_nr = -1;
1067 else
1068 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (refdev == 0)
NeilBrown8ed75462006-02-03 03:03:41 -08001071 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 else {
1073 __u64 ev1, ev2;
1074 struct mdp_superblock_1 *refsb =
1075 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1076
1077 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1078 sb->level != refsb->level ||
1079 sb->layout != refsb->layout ||
1080 sb->chunksize != refsb->chunksize) {
1081 printk(KERN_WARNING "md: %s has strangely different"
1082 " superblock to %s\n",
1083 bdevname(rdev->bdev,b),
1084 bdevname(refdev->bdev,b2));
1085 return -EINVAL;
1086 }
1087 ev1 = le64_to_cpu(sb->events);
1088 ev2 = le64_to_cpu(refsb->events);
1089
1090 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001091 ret = 1;
1092 else
1093 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095 if (minor_version)
1096 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1097 else
1098 rdev->size = rdev->sb_offset;
1099 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1100 return -EINVAL;
1101 rdev->size = le64_to_cpu(sb->data_size)/2;
1102 if (le32_to_cpu(sb->chunksize))
1103 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001104
1105 if (le32_to_cpu(sb->size) > rdev->size*2)
1106 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001107 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
1110static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1111{
1112 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001113 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
NeilBrown41158c72005-06-21 17:17:25 -07001115 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001116 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (mddev->raid_disks == 0) {
1118 mddev->major_version = 1;
1119 mddev->patch_version = 0;
1120 mddev->persistent = 1;
1121 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1122 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1123 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1124 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001125 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 mddev->layout = le32_to_cpu(sb->layout);
1127 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1128 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001129 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001130 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001131 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1134 memcpy(mddev->uuid, sb->set_uuid, 16);
1135
1136 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001137
NeilBrown71c08052005-09-09 16:23:51 -07001138 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -07001139 mddev->bitmap_file == NULL ) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001140 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1141 && mddev->level != 10) {
1142 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -07001143 return -EINVAL;
1144 }
1145 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1146 }
NeilBrownf6705572006-03-27 01:18:11 -08001147 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1148 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1149 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1150 mddev->new_level = le32_to_cpu(sb->new_level);
1151 mddev->new_layout = le32_to_cpu(sb->new_layout);
1152 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1153 } else {
1154 mddev->reshape_position = MaxSector;
1155 mddev->delta_disks = 0;
1156 mddev->new_level = mddev->level;
1157 mddev->new_layout = mddev->layout;
1158 mddev->new_chunk = mddev->chunk_size;
1159 }
1160
NeilBrown41158c72005-06-21 17:17:25 -07001161 } else if (mddev->pers == NULL) {
1162 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 ++ev1;
1164 if (ev1 < mddev->events)
1165 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001166 } else if (mddev->bitmap) {
1167 /* If adding to array with a bitmap, then we can accept an
1168 * older device, but not too old.
1169 */
NeilBrown41158c72005-06-21 17:17:25 -07001170 if (ev1 < mddev->bitmap->events_cleared)
1171 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001172 } else {
1173 if (ev1 < mddev->events)
1174 /* just a hot-add of a new device, leave raid_disk at -1 */
1175 return 0;
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (mddev->level != LEVEL_MULTIPATH) {
1178 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1180 switch(role) {
1181 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 break;
1183 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001184 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 break;
1186 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001187 if ((le32_to_cpu(sb->feature_map) &
1188 MD_FEATURE_RECOVERY_OFFSET))
1189 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1190 else
1191 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 rdev->raid_disk = role;
1193 break;
1194 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001195 if (sb->devflags & WriteMostly1)
1196 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001197 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001198 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
1201}
1202
1203static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1204{
1205 struct mdp_superblock_1 *sb;
1206 struct list_head *tmp;
1207 mdk_rdev_t *rdev2;
1208 int max_dev, i;
1209 /* make rdev->sb match mddev and rdev data. */
1210
1211 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1212
1213 sb->feature_map = 0;
1214 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001215 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 memset(sb->pad1, 0, sizeof(sb->pad1));
1217 memset(sb->pad2, 0, sizeof(sb->pad2));
1218 memset(sb->pad3, 0, sizeof(sb->pad3));
1219
1220 sb->utime = cpu_to_le64((__u64)mddev->utime);
1221 sb->events = cpu_to_le64(mddev->events);
1222 if (mddev->in_sync)
1223 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1224 else
1225 sb->resync_offset = cpu_to_le64(0);
1226
NeilBrown4dbcdc72006-01-06 00:20:52 -08001227 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1228
NeilBrownf0ca3402006-02-02 14:28:04 -08001229 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001230 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001231
NeilBrowna654b9d82005-06-21 17:17:27 -07001232 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1233 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001234 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001235 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001236
1237 if (rdev->raid_disk >= 0 &&
1238 !test_bit(In_sync, &rdev->flags) &&
1239 rdev->recovery_offset > 0) {
1240 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1241 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1242 }
1243
NeilBrownf6705572006-03-27 01:18:11 -08001244 if (mddev->reshape_position != MaxSector) {
1245 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1246 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1247 sb->new_layout = cpu_to_le32(mddev->new_layout);
1248 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1249 sb->new_level = cpu_to_le32(mddev->new_level);
1250 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1251 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 max_dev = 0;
1254 ITERATE_RDEV(mddev,rdev2,tmp)
1255 if (rdev2->desc_nr+1 > max_dev)
1256 max_dev = rdev2->desc_nr+1;
1257
1258 sb->max_dev = cpu_to_le32(max_dev);
1259 for (i=0; i<max_dev;i++)
1260 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1261
1262 ITERATE_RDEV(mddev,rdev2,tmp) {
1263 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001264 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001266 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001268 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1269 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 else
1271 sb->dev_roles[i] = cpu_to_le16(0xffff);
1272 }
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 sb->sb_csum = calc_sb_1_csum(sb);
1275}
1276
1277
Adrian Bunk75c96f82005-05-05 16:16:09 -07001278static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 [0] = {
1280 .name = "0.90.0",
1281 .owner = THIS_MODULE,
1282 .load_super = super_90_load,
1283 .validate_super = super_90_validate,
1284 .sync_super = super_90_sync,
1285 },
1286 [1] = {
1287 .name = "md-1",
1288 .owner = THIS_MODULE,
1289 .load_super = super_1_load,
1290 .validate_super = super_1_validate,
1291 .sync_super = super_1_sync,
1292 },
1293};
1294
1295static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1296{
1297 struct list_head *tmp;
1298 mdk_rdev_t *rdev;
1299
1300 ITERATE_RDEV(mddev,rdev,tmp)
1301 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1302 return rdev;
1303
1304 return NULL;
1305}
1306
1307static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1308{
1309 struct list_head *tmp;
1310 mdk_rdev_t *rdev;
1311
1312 ITERATE_RDEV(mddev1,rdev,tmp)
1313 if (match_dev_unit(mddev2, rdev))
1314 return 1;
1315
1316 return 0;
1317}
1318
1319static LIST_HEAD(pending_raid_disks);
1320
1321static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1322{
1323 mdk_rdev_t *same_pdev;
1324 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001325 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001326 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 if (rdev->mddev) {
1329 MD_BUG();
1330 return -EINVAL;
1331 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001332 /* make sure rdev->size exceeds mddev->size */
1333 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1334 if (mddev->pers)
1335 /* Cannot change size, so fail */
1336 return -ENOSPC;
1337 else
1338 mddev->size = rdev->size;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 same_pdev = match_dev_unit(mddev, rdev);
1341 if (same_pdev)
1342 printk(KERN_WARNING
1343 "%s: WARNING: %s appears to be on the same physical"
1344 " disk as %s. True\n protection against single-disk"
1345 " failure might be compromised.\n",
1346 mdname(mddev), bdevname(rdev->bdev,b),
1347 bdevname(same_pdev->bdev,b2));
1348
1349 /* Verify rdev->desc_nr is unique.
1350 * If it is -1, assign a free number, else
1351 * check number is not in use
1352 */
1353 if (rdev->desc_nr < 0) {
1354 int choice = 0;
1355 if (mddev->pers) choice = mddev->raid_disks;
1356 while (find_rdev_nr(mddev, choice))
1357 choice++;
1358 rdev->desc_nr = choice;
1359 } else {
1360 if (find_rdev_nr(mddev, rdev->desc_nr))
1361 return -EBUSY;
1362 }
NeilBrown19133a42005-11-08 21:39:35 -08001363 bdevname(rdev->bdev,b);
1364 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1365 return -ENOMEM;
Neil Brown1edf80d2006-01-12 01:05:23 -08001366 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1367 *s = '!';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 list_add(&rdev->same_set, &mddev->disks);
1370 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001371 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001372
NeilBrown9c791972005-11-08 21:39:28 -08001373 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001374 kobject_add(&rdev->kobj);
1375
NeilBrownf637b9f2005-11-08 21:39:37 -08001376 if (rdev->bdev->bd_part)
1377 ko = &rdev->bdev->bd_part->kobj;
1378 else
1379 ko = &rdev->bdev->bd_disk->kobj;
1380 sysfs_create_link(&rdev->kobj, ko, "block");
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001381 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return 0;
1383}
1384
1385static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1386{
1387 char b[BDEVNAME_SIZE];
1388 if (!rdev->mddev) {
1389 MD_BUG();
1390 return;
1391 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001392 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 list_del_init(&rdev->same_set);
1394 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1395 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001396 sysfs_remove_link(&rdev->kobj, "block");
1397 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400/*
1401 * prevent the device from being mounted, repartitioned or
1402 * otherwise reused by a RAID array (or any other kernel
1403 * subsystem), by bd_claiming the device.
1404 */
1405static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1406{
1407 int err = 0;
1408 struct block_device *bdev;
1409 char b[BDEVNAME_SIZE];
1410
Ingo Molnar663d4402006-07-03 00:25:33 -07001411 bdev = open_partition_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (IS_ERR(bdev)) {
1413 printk(KERN_ERR "md: could not open %s.\n",
1414 __bdevname(dev, b));
1415 return PTR_ERR(bdev);
1416 }
1417 err = bd_claim(bdev, rdev);
1418 if (err) {
1419 printk(KERN_ERR "md: could not bd_claim %s.\n",
1420 bdevname(bdev, b));
Ingo Molnar663d4402006-07-03 00:25:33 -07001421 blkdev_put_partition(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return err;
1423 }
1424 rdev->bdev = bdev;
1425 return err;
1426}
1427
1428static void unlock_rdev(mdk_rdev_t *rdev)
1429{
1430 struct block_device *bdev = rdev->bdev;
1431 rdev->bdev = NULL;
1432 if (!bdev)
1433 MD_BUG();
1434 bd_release(bdev);
Ingo Molnar663d4402006-07-03 00:25:33 -07001435 blkdev_put_partition(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
1438void md_autodetect_dev(dev_t dev);
1439
1440static void export_rdev(mdk_rdev_t * rdev)
1441{
1442 char b[BDEVNAME_SIZE];
1443 printk(KERN_INFO "md: export_rdev(%s)\n",
1444 bdevname(rdev->bdev,b));
1445 if (rdev->mddev)
1446 MD_BUG();
1447 free_disk_sb(rdev);
1448 list_del_init(&rdev->same_set);
1449#ifndef MODULE
1450 md_autodetect_dev(rdev->bdev->bd_dev);
1451#endif
1452 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001453 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
1456static void kick_rdev_from_array(mdk_rdev_t * rdev)
1457{
1458 unbind_rdev_from_array(rdev);
1459 export_rdev(rdev);
1460}
1461
1462static void export_array(mddev_t *mddev)
1463{
1464 struct list_head *tmp;
1465 mdk_rdev_t *rdev;
1466
1467 ITERATE_RDEV(mddev,rdev,tmp) {
1468 if (!rdev->mddev) {
1469 MD_BUG();
1470 continue;
1471 }
1472 kick_rdev_from_array(rdev);
1473 }
1474 if (!list_empty(&mddev->disks))
1475 MD_BUG();
1476 mddev->raid_disks = 0;
1477 mddev->major_version = 0;
1478}
1479
1480static void print_desc(mdp_disk_t *desc)
1481{
1482 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1483 desc->major,desc->minor,desc->raid_disk,desc->state);
1484}
1485
1486static void print_sb(mdp_super_t *sb)
1487{
1488 int i;
1489
1490 printk(KERN_INFO
1491 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1492 sb->major_version, sb->minor_version, sb->patch_version,
1493 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1494 sb->ctime);
1495 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1496 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1497 sb->md_minor, sb->layout, sb->chunk_size);
1498 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1499 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1500 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1501 sb->failed_disks, sb->spare_disks,
1502 sb->sb_csum, (unsigned long)sb->events_lo);
1503
1504 printk(KERN_INFO);
1505 for (i = 0; i < MD_SB_DISKS; i++) {
1506 mdp_disk_t *desc;
1507
1508 desc = sb->disks + i;
1509 if (desc->number || desc->major || desc->minor ||
1510 desc->raid_disk || (desc->state && (desc->state != 4))) {
1511 printk(" D %2d: ", i);
1512 print_desc(desc);
1513 }
1514 }
1515 printk(KERN_INFO "md: THIS: ");
1516 print_desc(&sb->this_disk);
1517
1518}
1519
1520static void print_rdev(mdk_rdev_t *rdev)
1521{
1522 char b[BDEVNAME_SIZE];
1523 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1524 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001525 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1526 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (rdev->sb_loaded) {
1528 printk(KERN_INFO "md: rdev superblock:\n");
1529 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1530 } else
1531 printk(KERN_INFO "md: no rdev superblock!\n");
1532}
1533
Adrian Bunk5e563412006-06-26 00:27:42 -07001534static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 struct list_head *tmp, *tmp2;
1537 mdk_rdev_t *rdev;
1538 mddev_t *mddev;
1539 char b[BDEVNAME_SIZE];
1540
1541 printk("\n");
1542 printk("md: **********************************\n");
1543 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1544 printk("md: **********************************\n");
1545 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
NeilBrown32a76272005-06-21 17:17:14 -07001547 if (mddev->bitmap)
1548 bitmap_print_sb(mddev->bitmap);
1549 else
1550 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 ITERATE_RDEV(mddev,rdev,tmp2)
1552 printk("<%s>", bdevname(rdev->bdev,b));
1553 printk("\n");
1554
1555 ITERATE_RDEV(mddev,rdev,tmp2)
1556 print_rdev(rdev);
1557 }
1558 printk("md: **********************************\n");
1559 printk("\n");
1560}
1561
1562
NeilBrown42543762006-06-26 00:27:57 -07001563static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
NeilBrown42543762006-06-26 00:27:57 -07001565 /* Update each superblock (in-memory image), but
1566 * if we are allowed to, skip spares which already
1567 * have the right event counter, or have one earlier
1568 * (which would mean they aren't being marked as dirty
1569 * with the rest of the array)
1570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 mdk_rdev_t *rdev;
1572 struct list_head *tmp;
1573
1574 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrown42543762006-06-26 00:27:57 -07001575 if (rdev->sb_events == mddev->events ||
1576 (nospares &&
1577 rdev->raid_disk < 0 &&
1578 (rdev->sb_events&1)==0 &&
1579 rdev->sb_events+1 == mddev->events)) {
1580 /* Don't update this superblock */
1581 rdev->sb_loaded = 2;
1582 } else {
1583 super_types[mddev->major_version].
1584 sync_super(mddev, rdev);
1585 rdev->sb_loaded = 1;
1586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588}
1589
NeilBrownf6705572006-03-27 01:18:11 -08001590void md_update_sb(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001592 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 struct list_head *tmp;
1594 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001595 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001596 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001599 spin_lock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001600 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001602 if (mddev->sb_dirty == 3)
1603 /* just a clean<-> dirty transition, possibly leave spares alone,
1604 * though if events isn't the right even/odd, we will have to do
1605 * spares after all
1606 */
1607 nospares = 1;
1608
1609 /* If this is just a dirty<->clean transition, and the array is clean
1610 * and 'events' is odd, we can roll back to the previous clean state */
1611 if (mddev->sb_dirty == 3
1612 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1613 && (mddev->events & 1))
1614 mddev->events--;
1615 else {
1616 /* otherwise we have to go forward and ... */
1617 mddev->events ++;
1618 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1619 /* .. if the array isn't clean, insist on an odd 'events' */
1620 if ((mddev->events&1)==0) {
1621 mddev->events++;
1622 nospares = 0;
1623 }
1624 } else {
1625 /* otherwise insist on an even 'events' (for clean states) */
1626 if ((mddev->events&1)) {
1627 mddev->events++;
1628 nospares = 0;
1629 }
1630 }
1631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 if (!mddev->events) {
1634 /*
1635 * oops, this 64-bit counter should never wrap.
1636 * Either we are in around ~1 trillion A.C., assuming
1637 * 1 reboot per second, or we have a bug:
1638 */
1639 MD_BUG();
1640 mddev->events --;
1641 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001642 mddev->sb_dirty = 2;
NeilBrown42543762006-06-26 00:27:57 -07001643 sync_sbs(mddev, nospares);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 /*
1646 * do not write anything to disk if using
1647 * nonpersistent superblocks
1648 */
NeilBrown06d91a52005-06-21 17:17:12 -07001649 if (!mddev->persistent) {
1650 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001651 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001652 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001654 }
NeilBrowna9701a32005-11-08 21:39:34 -08001655 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 dprintk(KERN_INFO
1658 "md: updating %s RAID superblock on device (in sync %d)\n",
1659 mdname(mddev),mddev->in_sync);
1660
NeilBrown32a76272005-06-21 17:17:14 -07001661 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 ITERATE_RDEV(mddev,rdev,tmp) {
1663 char b[BDEVNAME_SIZE];
1664 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001665 if (rdev->sb_loaded != 1)
1666 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001667 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 dprintk("(skipping faulty ");
1669
1670 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001671 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001672 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001673 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001674 rdev->sb_page);
1675 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1676 bdevname(rdev->bdev,b),
1677 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001678 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 } else
1681 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001682 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /* only need to write one superblock... */
1684 break;
1685 }
NeilBrowna9701a32005-11-08 21:39:34 -08001686 md_super_wait(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001687 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1688
NeilBrowna9701a32005-11-08 21:39:34 -08001689 spin_lock_irq(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001690 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001691 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001692 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001693 goto repeat;
1694 }
1695 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001696 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001697 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
NeilBrownf6705572006-03-27 01:18:11 -08001700EXPORT_SYMBOL_GPL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
NeilBrownbce74da2006-01-06 00:20:41 -08001702/* words written to sysfs files may, or my not, be \n terminated.
1703 * We want to accept with case. For this we use cmd_match.
1704 */
1705static int cmd_match(const char *cmd, const char *str)
1706{
1707 /* See if cmd, written into a sysfs file, matches
1708 * str. They must either be the same, or cmd can
1709 * have a trailing newline
1710 */
1711 while (*cmd && *str && *cmd == *str) {
1712 cmd++;
1713 str++;
1714 }
1715 if (*cmd == '\n')
1716 cmd++;
1717 if (*str || *cmd)
1718 return 0;
1719 return 1;
1720}
1721
NeilBrown86e6ffd2005-11-08 21:39:24 -08001722struct rdev_sysfs_entry {
1723 struct attribute attr;
1724 ssize_t (*show)(mdk_rdev_t *, char *);
1725 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1726};
1727
1728static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001729state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001730{
1731 char *sep = "";
1732 int len=0;
1733
NeilBrownb2d444d2005-11-08 21:39:31 -08001734 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001735 len+= sprintf(page+len, "%sfaulty",sep);
1736 sep = ",";
1737 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001738 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001739 len += sprintf(page+len, "%sin_sync",sep);
1740 sep = ",";
1741 }
NeilBrownf6556752006-06-26 00:28:01 -07001742 if (test_bit(WriteMostly, &rdev->flags)) {
1743 len += sprintf(page+len, "%swrite_mostly",sep);
1744 sep = ",";
1745 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001746 if (!test_bit(Faulty, &rdev->flags) &&
1747 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001748 len += sprintf(page+len, "%sspare", sep);
1749 sep = ",";
1750 }
1751 return len+sprintf(page+len, "\n");
1752}
1753
NeilBrown45dc2de2006-06-26 00:27:58 -07001754static ssize_t
1755state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1756{
1757 /* can write
1758 * faulty - simulates and error
1759 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001760 * writemostly - sets write_mostly
1761 * -writemostly - clears write_mostly
NeilBrown45dc2de2006-06-26 00:27:58 -07001762 */
1763 int err = -EINVAL;
1764 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1765 md_error(rdev->mddev, rdev);
1766 err = 0;
1767 } else if (cmd_match(buf, "remove")) {
1768 if (rdev->raid_disk >= 0)
1769 err = -EBUSY;
1770 else {
1771 mddev_t *mddev = rdev->mddev;
1772 kick_rdev_from_array(rdev);
1773 md_update_sb(mddev);
1774 md_new_event(mddev);
1775 err = 0;
1776 }
NeilBrownf6556752006-06-26 00:28:01 -07001777 } else if (cmd_match(buf, "writemostly")) {
1778 set_bit(WriteMostly, &rdev->flags);
1779 err = 0;
1780 } else if (cmd_match(buf, "-writemostly")) {
1781 clear_bit(WriteMostly, &rdev->flags);
1782 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001783 }
1784 return err ? err : len;
1785}
NeilBrown80ca3a42006-07-10 04:44:18 -07001786static struct rdev_sysfs_entry rdev_state =
1787__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001788
1789static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001790super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001791{
1792 if (rdev->sb_loaded && rdev->sb_size) {
1793 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1794 return rdev->sb_size;
1795 } else
1796 return 0;
1797}
NeilBrown96de1e62005-11-08 21:39:39 -08001798static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1799
NeilBrown4dbcdc72006-01-06 00:20:52 -08001800static ssize_t
1801errors_show(mdk_rdev_t *rdev, char *page)
1802{
1803 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1804}
1805
1806static ssize_t
1807errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1808{
1809 char *e;
1810 unsigned long n = simple_strtoul(buf, &e, 10);
1811 if (*buf && (*e == 0 || *e == '\n')) {
1812 atomic_set(&rdev->corrected_errors, n);
1813 return len;
1814 }
1815 return -EINVAL;
1816}
1817static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001818__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001819
NeilBrown014236d2006-01-06 00:20:55 -08001820static ssize_t
1821slot_show(mdk_rdev_t *rdev, char *page)
1822{
1823 if (rdev->raid_disk < 0)
1824 return sprintf(page, "none\n");
1825 else
1826 return sprintf(page, "%d\n", rdev->raid_disk);
1827}
1828
1829static ssize_t
1830slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1831{
1832 char *e;
1833 int slot = simple_strtoul(buf, &e, 10);
1834 if (strncmp(buf, "none", 4)==0)
1835 slot = -1;
1836 else if (e==buf || (*e && *e!= '\n'))
1837 return -EINVAL;
1838 if (rdev->mddev->pers)
1839 /* Cannot set slot in active array (yet) */
1840 return -EBUSY;
1841 if (slot >= rdev->mddev->raid_disks)
1842 return -ENOSPC;
1843 rdev->raid_disk = slot;
1844 /* assume it is working */
1845 rdev->flags = 0;
1846 set_bit(In_sync, &rdev->flags);
1847 return len;
1848}
1849
1850
1851static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001852__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001853
NeilBrown93c8cad2006-01-06 00:20:56 -08001854static ssize_t
1855offset_show(mdk_rdev_t *rdev, char *page)
1856{
Andrew Morton6961ece2006-01-06 00:20:59 -08001857 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001858}
1859
1860static ssize_t
1861offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1862{
1863 char *e;
1864 unsigned long long offset = simple_strtoull(buf, &e, 10);
1865 if (e==buf || (*e && *e != '\n'))
1866 return -EINVAL;
1867 if (rdev->mddev->pers)
1868 return -EBUSY;
1869 rdev->data_offset = offset;
1870 return len;
1871}
1872
1873static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001874__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001875
NeilBrown83303b62006-01-06 00:21:06 -08001876static ssize_t
1877rdev_size_show(mdk_rdev_t *rdev, char *page)
1878{
1879 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1880}
1881
1882static ssize_t
1883rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1884{
1885 char *e;
1886 unsigned long long size = simple_strtoull(buf, &e, 10);
1887 if (e==buf || (*e && *e != '\n'))
1888 return -EINVAL;
1889 if (rdev->mddev->pers)
1890 return -EBUSY;
1891 rdev->size = size;
1892 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1893 rdev->mddev->size = size;
1894 return len;
1895}
1896
1897static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07001898__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08001899
NeilBrown86e6ffd2005-11-08 21:39:24 -08001900static struct attribute *rdev_default_attrs[] = {
1901 &rdev_state.attr,
1902 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001903 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001904 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001905 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001906 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001907 NULL,
1908};
1909static ssize_t
1910rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1911{
1912 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1913 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1914
1915 if (!entry->show)
1916 return -EIO;
1917 return entry->show(rdev, page);
1918}
1919
1920static ssize_t
1921rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1922 const char *page, size_t length)
1923{
1924 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1925 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1926
1927 if (!entry->store)
1928 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07001929 if (!capable(CAP_SYS_ADMIN))
1930 return -EACCES;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001931 return entry->store(rdev, page, length);
1932}
1933
1934static void rdev_free(struct kobject *ko)
1935{
1936 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1937 kfree(rdev);
1938}
1939static struct sysfs_ops rdev_sysfs_ops = {
1940 .show = rdev_attr_show,
1941 .store = rdev_attr_store,
1942};
1943static struct kobj_type rdev_ktype = {
1944 .release = rdev_free,
1945 .sysfs_ops = &rdev_sysfs_ops,
1946 .default_attrs = rdev_default_attrs,
1947};
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949/*
1950 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1951 *
1952 * mark the device faulty if:
1953 *
1954 * - the device is nonexistent (zero size)
1955 * - the device has no valid superblock
1956 *
1957 * a faulty rdev _never_ has rdev->sb set.
1958 */
1959static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1960{
1961 char b[BDEVNAME_SIZE];
1962 int err;
1963 mdk_rdev_t *rdev;
1964 sector_t size;
1965
NeilBrown9ffae0c2006-01-06 00:20:32 -08001966 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (!rdev) {
1968 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1969 return ERR_PTR(-ENOMEM);
1970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 if ((err = alloc_disk_sb(rdev)))
1973 goto abort_free;
1974
1975 err = lock_rdev(rdev, newdev);
1976 if (err)
1977 goto abort_free;
1978
NeilBrown86e6ffd2005-11-08 21:39:24 -08001979 rdev->kobj.parent = NULL;
1980 rdev->kobj.ktype = &rdev_ktype;
1981 kobject_init(&rdev->kobj);
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 rdev->desc_nr = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001984 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07001986 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001988 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001989 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1992 if (!size) {
1993 printk(KERN_WARNING
1994 "md: %s has zero or unknown size, marking faulty!\n",
1995 bdevname(rdev->bdev,b));
1996 err = -EINVAL;
1997 goto abort_free;
1998 }
1999
2000 if (super_format >= 0) {
2001 err = super_types[super_format].
2002 load_super(rdev, NULL, super_minor);
2003 if (err == -EINVAL) {
2004 printk(KERN_WARNING
2005 "md: %s has invalid sb, not importing!\n",
2006 bdevname(rdev->bdev,b));
2007 goto abort_free;
2008 }
2009 if (err < 0) {
2010 printk(KERN_WARNING
2011 "md: could not read %s's sb, not importing!\n",
2012 bdevname(rdev->bdev,b));
2013 goto abort_free;
2014 }
2015 }
2016 INIT_LIST_HEAD(&rdev->same_set);
2017
2018 return rdev;
2019
2020abort_free:
2021 if (rdev->sb_page) {
2022 if (rdev->bdev)
2023 unlock_rdev(rdev);
2024 free_disk_sb(rdev);
2025 }
2026 kfree(rdev);
2027 return ERR_PTR(err);
2028}
2029
2030/*
2031 * Check a full RAID array for plausibility
2032 */
2033
2034
NeilBrowna757e642005-04-16 15:26:42 -07002035static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
2037 int i;
2038 struct list_head *tmp;
2039 mdk_rdev_t *rdev, *freshest;
2040 char b[BDEVNAME_SIZE];
2041
2042 freshest = NULL;
2043 ITERATE_RDEV(mddev,rdev,tmp)
2044 switch (super_types[mddev->major_version].
2045 load_super(rdev, freshest, mddev->minor_version)) {
2046 case 1:
2047 freshest = rdev;
2048 break;
2049 case 0:
2050 break;
2051 default:
2052 printk( KERN_ERR \
2053 "md: fatal superblock inconsistency in %s"
2054 " -- removing from array\n",
2055 bdevname(rdev->bdev,b));
2056 kick_rdev_from_array(rdev);
2057 }
2058
2059
2060 super_types[mddev->major_version].
2061 validate_super(mddev, freshest);
2062
2063 i = 0;
2064 ITERATE_RDEV(mddev,rdev,tmp) {
2065 if (rdev != freshest)
2066 if (super_types[mddev->major_version].
2067 validate_super(mddev, rdev)) {
2068 printk(KERN_WARNING "md: kicking non-fresh %s"
2069 " from array!\n",
2070 bdevname(rdev->bdev,b));
2071 kick_rdev_from_array(rdev);
2072 continue;
2073 }
2074 if (mddev->level == LEVEL_MULTIPATH) {
2075 rdev->desc_nr = i++;
2076 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002077 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
2079 }
2080
2081
2082
2083 if (mddev->recovery_cp != MaxSector &&
2084 mddev->level >= 1)
2085 printk(KERN_ERR "md: %s: raid array is not clean"
2086 " -- starting background reconstruction\n",
2087 mdname(mddev));
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
NeilBrowneae17012005-11-08 21:39:23 -08002091static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002092safe_delay_show(mddev_t *mddev, char *page)
2093{
2094 int msec = (mddev->safemode_delay*1000)/HZ;
2095 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2096}
2097static ssize_t
2098safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2099{
2100 int scale=1;
2101 int dot=0;
2102 int i;
2103 unsigned long msec;
2104 char buf[30];
2105 char *e;
2106 /* remove a period, and count digits after it */
2107 if (len >= sizeof(buf))
2108 return -EINVAL;
2109 strlcpy(buf, cbuf, len);
2110 buf[len] = 0;
2111 for (i=0; i<len; i++) {
2112 if (dot) {
2113 if (isdigit(buf[i])) {
2114 buf[i-1] = buf[i];
2115 scale *= 10;
2116 }
2117 buf[i] = 0;
2118 } else if (buf[i] == '.') {
2119 dot=1;
2120 buf[i] = 0;
2121 }
2122 }
2123 msec = simple_strtoul(buf, &e, 10);
2124 if (e == buf || (*e && *e != '\n'))
2125 return -EINVAL;
2126 msec = (msec * 1000) / scale;
2127 if (msec == 0)
2128 mddev->safemode_delay = 0;
2129 else {
2130 mddev->safemode_delay = (msec*HZ)/1000;
2131 if (mddev->safemode_delay == 0)
2132 mddev->safemode_delay = 1;
2133 }
2134 return len;
2135}
2136static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002137__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002138
2139static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002140level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002141{
NeilBrown2604b702006-01-06 00:20:36 -08002142 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002143 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002144 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002145 else if (mddev->clevel[0])
2146 return sprintf(page, "%s\n", mddev->clevel);
2147 else if (mddev->level != LEVEL_NONE)
2148 return sprintf(page, "%d\n", mddev->level);
2149 else
2150 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002151}
2152
NeilBrownd9d166c2006-01-06 00:20:51 -08002153static ssize_t
2154level_store(mddev_t *mddev, const char *buf, size_t len)
2155{
2156 int rv = len;
2157 if (mddev->pers)
2158 return -EBUSY;
2159 if (len == 0)
2160 return 0;
2161 if (len >= sizeof(mddev->clevel))
2162 return -ENOSPC;
2163 strncpy(mddev->clevel, buf, len);
2164 if (mddev->clevel[len-1] == '\n')
2165 len--;
2166 mddev->clevel[len] = 0;
2167 mddev->level = LEVEL_NONE;
2168 return rv;
2169}
2170
2171static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002172__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002173
NeilBrownd4dbd022006-06-26 00:27:59 -07002174
2175static ssize_t
2176layout_show(mddev_t *mddev, char *page)
2177{
2178 /* just a number, not meaningful for all levels */
2179 return sprintf(page, "%d\n", mddev->layout);
2180}
2181
2182static ssize_t
2183layout_store(mddev_t *mddev, const char *buf, size_t len)
2184{
2185 char *e;
2186 unsigned long n = simple_strtoul(buf, &e, 10);
2187 if (mddev->pers)
2188 return -EBUSY;
2189
2190 if (!*buf || (*e && *e != '\n'))
2191 return -EINVAL;
2192
2193 mddev->layout = n;
2194 return len;
2195}
2196static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002197__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002198
2199
NeilBrowneae17012005-11-08 21:39:23 -08002200static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002201raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002202{
NeilBrownbb636542005-11-08 21:39:45 -08002203 if (mddev->raid_disks == 0)
2204 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002205 return sprintf(page, "%d\n", mddev->raid_disks);
2206}
2207
NeilBrownda943b992006-01-06 00:20:54 -08002208static int update_raid_disks(mddev_t *mddev, int raid_disks);
2209
2210static ssize_t
2211raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2212{
2213 /* can only set raid_disks if array is not yet active */
2214 char *e;
2215 int rv = 0;
2216 unsigned long n = simple_strtoul(buf, &e, 10);
2217
2218 if (!*buf || (*e && *e != '\n'))
2219 return -EINVAL;
2220
2221 if (mddev->pers)
2222 rv = update_raid_disks(mddev, n);
2223 else
2224 mddev->raid_disks = n;
2225 return rv ? rv : len;
2226}
2227static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002228__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002229
NeilBrown24dd4692005-11-08 21:39:26 -08002230static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002231chunk_size_show(mddev_t *mddev, char *page)
2232{
2233 return sprintf(page, "%d\n", mddev->chunk_size);
2234}
2235
2236static ssize_t
2237chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2238{
2239 /* can only set chunk_size if array is not yet active */
2240 char *e;
2241 unsigned long n = simple_strtoul(buf, &e, 10);
2242
2243 if (mddev->pers)
2244 return -EBUSY;
2245 if (!*buf || (*e && *e != '\n'))
2246 return -EINVAL;
2247
2248 mddev->chunk_size = n;
2249 return len;
2250}
2251static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002252__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002253
NeilBrowna94213b2006-06-26 00:28:00 -07002254static ssize_t
2255resync_start_show(mddev_t *mddev, char *page)
2256{
2257 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2258}
2259
2260static ssize_t
2261resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2262{
2263 /* can only set chunk_size if array is not yet active */
2264 char *e;
2265 unsigned long long n = simple_strtoull(buf, &e, 10);
2266
2267 if (mddev->pers)
2268 return -EBUSY;
2269 if (!*buf || (*e && *e != '\n'))
2270 return -EINVAL;
2271
2272 mddev->recovery_cp = n;
2273 return len;
2274}
2275static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002276__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002277
NeilBrown9e653b62006-06-26 00:27:58 -07002278/*
2279 * The array state can be:
2280 *
2281 * clear
2282 * No devices, no size, no level
2283 * Equivalent to STOP_ARRAY ioctl
2284 * inactive
2285 * May have some settings, but array is not active
2286 * all IO results in error
2287 * When written, doesn't tear down array, but just stops it
2288 * suspended (not supported yet)
2289 * All IO requests will block. The array can be reconfigured.
2290 * Writing this, if accepted, will block until array is quiessent
2291 * readonly
2292 * no resync can happen. no superblocks get written.
2293 * write requests fail
2294 * read-auto
2295 * like readonly, but behaves like 'clean' on a write request.
2296 *
2297 * clean - no pending writes, but otherwise active.
2298 * When written to inactive array, starts without resync
2299 * If a write request arrives then
2300 * if metadata is known, mark 'dirty' and switch to 'active'.
2301 * if not known, block and switch to write-pending
2302 * If written to an active array that has pending writes, then fails.
2303 * active
2304 * fully active: IO and resync can be happening.
2305 * When written to inactive array, starts with resync
2306 *
2307 * write-pending
2308 * clean, but writes are blocked waiting for 'active' to be written.
2309 *
2310 * active-idle
2311 * like active, but no writes have been seen for a while (100msec).
2312 *
2313 */
2314enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2315 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002316static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002317 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2318 "write-pending", "active-idle", NULL };
2319
2320static int match_word(const char *word, char **list)
2321{
2322 int n;
2323 for (n=0; list[n]; n++)
2324 if (cmd_match(word, list[n]))
2325 break;
2326 return n;
2327}
2328
2329static ssize_t
2330array_state_show(mddev_t *mddev, char *page)
2331{
2332 enum array_state st = inactive;
2333
2334 if (mddev->pers)
2335 switch(mddev->ro) {
2336 case 1:
2337 st = readonly;
2338 break;
2339 case 2:
2340 st = read_auto;
2341 break;
2342 case 0:
2343 if (mddev->in_sync)
2344 st = clean;
2345 else if (mddev->safemode)
2346 st = active_idle;
2347 else
2348 st = active;
2349 }
2350 else {
2351 if (list_empty(&mddev->disks) &&
2352 mddev->raid_disks == 0 &&
2353 mddev->size == 0)
2354 st = clear;
2355 else
2356 st = inactive;
2357 }
2358 return sprintf(page, "%s\n", array_states[st]);
2359}
2360
2361static int do_md_stop(mddev_t * mddev, int ro);
2362static int do_md_run(mddev_t * mddev);
2363static int restart_array(mddev_t *mddev);
2364
2365static ssize_t
2366array_state_store(mddev_t *mddev, const char *buf, size_t len)
2367{
2368 int err = -EINVAL;
2369 enum array_state st = match_word(buf, array_states);
2370 switch(st) {
2371 case bad_word:
2372 break;
2373 case clear:
2374 /* stopping an active array */
2375 if (mddev->pers) {
2376 if (atomic_read(&mddev->active) > 1)
2377 return -EBUSY;
2378 err = do_md_stop(mddev, 0);
2379 }
2380 break;
2381 case inactive:
2382 /* stopping an active array */
2383 if (mddev->pers) {
2384 if (atomic_read(&mddev->active) > 1)
2385 return -EBUSY;
2386 err = do_md_stop(mddev, 2);
2387 }
2388 break;
2389 case suspended:
2390 break; /* not supported yet */
2391 case readonly:
2392 if (mddev->pers)
2393 err = do_md_stop(mddev, 1);
2394 else {
2395 mddev->ro = 1;
2396 err = do_md_run(mddev);
2397 }
2398 break;
2399 case read_auto:
2400 /* stopping an active array */
2401 if (mddev->pers) {
2402 err = do_md_stop(mddev, 1);
2403 if (err == 0)
2404 mddev->ro = 2; /* FIXME mark devices writable */
2405 } else {
2406 mddev->ro = 2;
2407 err = do_md_run(mddev);
2408 }
2409 break;
2410 case clean:
2411 if (mddev->pers) {
2412 restart_array(mddev);
2413 spin_lock_irq(&mddev->write_lock);
2414 if (atomic_read(&mddev->writes_pending) == 0) {
2415 mddev->in_sync = 1;
2416 mddev->sb_dirty = 1;
2417 }
2418 spin_unlock_irq(&mddev->write_lock);
2419 } else {
2420 mddev->ro = 0;
2421 mddev->recovery_cp = MaxSector;
2422 err = do_md_run(mddev);
2423 }
2424 break;
2425 case active:
2426 if (mddev->pers) {
2427 restart_array(mddev);
2428 mddev->sb_dirty = 0;
2429 wake_up(&mddev->sb_wait);
2430 err = 0;
2431 } else {
2432 mddev->ro = 0;
2433 err = do_md_run(mddev);
2434 }
2435 break;
2436 case write_pending:
2437 case active_idle:
2438 /* these cannot be set */
2439 break;
2440 }
2441 if (err)
2442 return err;
2443 else
2444 return len;
2445}
NeilBrown80ca3a42006-07-10 04:44:18 -07002446static struct md_sysfs_entry md_array_state =
2447__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002448
NeilBrown6d7ff7382006-01-06 00:21:16 -08002449static ssize_t
2450null_show(mddev_t *mddev, char *page)
2451{
2452 return -EINVAL;
2453}
2454
2455static ssize_t
2456new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2457{
2458 /* buf must be %d:%d\n? giving major and minor numbers */
2459 /* The new device is added to the array.
2460 * If the array has a persistent superblock, we read the
2461 * superblock to initialise info and check validity.
2462 * Otherwise, only checking done is that in bind_rdev_to_array,
2463 * which mainly checks size.
2464 */
2465 char *e;
2466 int major = simple_strtoul(buf, &e, 10);
2467 int minor;
2468 dev_t dev;
2469 mdk_rdev_t *rdev;
2470 int err;
2471
2472 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2473 return -EINVAL;
2474 minor = simple_strtoul(e+1, &e, 10);
2475 if (*e && *e != '\n')
2476 return -EINVAL;
2477 dev = MKDEV(major, minor);
2478 if (major != MAJOR(dev) ||
2479 minor != MINOR(dev))
2480 return -EOVERFLOW;
2481
2482
2483 if (mddev->persistent) {
2484 rdev = md_import_device(dev, mddev->major_version,
2485 mddev->minor_version);
2486 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2487 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2488 mdk_rdev_t, same_set);
2489 err = super_types[mddev->major_version]
2490 .load_super(rdev, rdev0, mddev->minor_version);
2491 if (err < 0)
2492 goto out;
2493 }
2494 } else
2495 rdev = md_import_device(dev, -1, -1);
2496
2497 if (IS_ERR(rdev))
2498 return PTR_ERR(rdev);
2499 err = bind_rdev_to_array(rdev, mddev);
2500 out:
2501 if (err)
2502 export_rdev(rdev);
2503 return err ? err : len;
2504}
2505
2506static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002507__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002508
2509static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002510size_show(mddev_t *mddev, char *page)
2511{
2512 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2513}
2514
2515static int update_size(mddev_t *mddev, unsigned long size);
2516
2517static ssize_t
2518size_store(mddev_t *mddev, const char *buf, size_t len)
2519{
2520 /* If array is inactive, we can reduce the component size, but
2521 * not increase it (except from 0).
2522 * If array is active, we can try an on-line resize
2523 */
2524 char *e;
2525 int err = 0;
2526 unsigned long long size = simple_strtoull(buf, &e, 10);
2527 if (!*buf || *buf == '\n' ||
2528 (*e && *e != '\n'))
2529 return -EINVAL;
2530
2531 if (mddev->pers) {
2532 err = update_size(mddev, size);
2533 md_update_sb(mddev);
2534 } else {
2535 if (mddev->size == 0 ||
2536 mddev->size > size)
2537 mddev->size = size;
2538 else
2539 err = -ENOSPC;
2540 }
2541 return err ? err : len;
2542}
2543
2544static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002545__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002546
NeilBrown8bb93aa2006-01-06 00:20:50 -08002547
2548/* Metdata version.
2549 * This is either 'none' for arrays with externally managed metadata,
2550 * or N.M for internally known formats
2551 */
2552static ssize_t
2553metadata_show(mddev_t *mddev, char *page)
2554{
2555 if (mddev->persistent)
2556 return sprintf(page, "%d.%d\n",
2557 mddev->major_version, mddev->minor_version);
2558 else
2559 return sprintf(page, "none\n");
2560}
2561
2562static ssize_t
2563metadata_store(mddev_t *mddev, const char *buf, size_t len)
2564{
2565 int major, minor;
2566 char *e;
2567 if (!list_empty(&mddev->disks))
2568 return -EBUSY;
2569
2570 if (cmd_match(buf, "none")) {
2571 mddev->persistent = 0;
2572 mddev->major_version = 0;
2573 mddev->minor_version = 90;
2574 return len;
2575 }
2576 major = simple_strtoul(buf, &e, 10);
2577 if (e==buf || *e != '.')
2578 return -EINVAL;
2579 buf = e+1;
2580 minor = simple_strtoul(buf, &e, 10);
2581 if (e==buf || *e != '\n')
2582 return -EINVAL;
2583 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2584 super_types[major].name == NULL)
2585 return -ENOENT;
2586 mddev->major_version = major;
2587 mddev->minor_version = minor;
2588 mddev->persistent = 1;
2589 return len;
2590}
2591
2592static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002593__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002594
NeilBrowna35b0d62006-01-06 00:20:49 -08002595static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002596action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002597{
NeilBrown7eec3142005-11-08 21:39:44 -08002598 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002599 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2600 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002601 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2602 type = "reshape";
2603 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002604 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2605 type = "resync";
2606 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2607 type = "check";
2608 else
2609 type = "repair";
2610 } else
2611 type = "recover";
2612 }
2613 return sprintf(page, "%s\n", type);
2614}
2615
2616static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002617action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002618{
NeilBrown7eec3142005-11-08 21:39:44 -08002619 if (!mddev->pers || !mddev->pers->sync_request)
2620 return -EINVAL;
2621
NeilBrownbce74da2006-01-06 00:20:41 -08002622 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002623 if (mddev->sync_thread) {
2624 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2625 md_unregister_thread(mddev->sync_thread);
2626 mddev->sync_thread = NULL;
2627 mddev->recovery = 0;
2628 }
NeilBrown03c902e2006-01-06 00:20:46 -08002629 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2630 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002631 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002632 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002633 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002634 else if (cmd_match(page, "reshape")) {
2635 int err;
2636 if (mddev->pers->start_reshape == NULL)
2637 return -EINVAL;
2638 err = mddev->pers->start_reshape(mddev);
2639 if (err)
2640 return err;
2641 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002642 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002643 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002644 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002645 return -EINVAL;
2646 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2647 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002648 }
NeilBrown03c902e2006-01-06 00:20:46 -08002649 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002650 md_wakeup_thread(mddev->thread);
2651 return len;
2652}
2653
NeilBrown9d888832005-11-08 21:39:26 -08002654static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002655mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002656{
2657 return sprintf(page, "%llu\n",
2658 (unsigned long long) mddev->resync_mismatches);
2659}
2660
NeilBrown80ca3a42006-07-10 04:44:18 -07002661static struct md_sysfs_entry md_scan_mode =
2662__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002663
NeilBrown96de1e62005-11-08 21:39:39 -08002664
NeilBrown80ca3a42006-07-10 04:44:18 -07002665static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002666
NeilBrown88202a02006-01-06 00:21:36 -08002667static ssize_t
2668sync_min_show(mddev_t *mddev, char *page)
2669{
2670 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2671 mddev->sync_speed_min ? "local": "system");
2672}
2673
2674static ssize_t
2675sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2676{
2677 int min;
2678 char *e;
2679 if (strncmp(buf, "system", 6)==0) {
2680 mddev->sync_speed_min = 0;
2681 return len;
2682 }
2683 min = simple_strtoul(buf, &e, 10);
2684 if (buf == e || (*e && *e != '\n') || min <= 0)
2685 return -EINVAL;
2686 mddev->sync_speed_min = min;
2687 return len;
2688}
2689
2690static struct md_sysfs_entry md_sync_min =
2691__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2692
2693static ssize_t
2694sync_max_show(mddev_t *mddev, char *page)
2695{
2696 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2697 mddev->sync_speed_max ? "local": "system");
2698}
2699
2700static ssize_t
2701sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2702{
2703 int max;
2704 char *e;
2705 if (strncmp(buf, "system", 6)==0) {
2706 mddev->sync_speed_max = 0;
2707 return len;
2708 }
2709 max = simple_strtoul(buf, &e, 10);
2710 if (buf == e || (*e && *e != '\n') || max <= 0)
2711 return -EINVAL;
2712 mddev->sync_speed_max = max;
2713 return len;
2714}
2715
2716static struct md_sysfs_entry md_sync_max =
2717__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2718
2719
2720static ssize_t
2721sync_speed_show(mddev_t *mddev, char *page)
2722{
2723 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07002724 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08002725 dt = ((jiffies - mddev->resync_mark) / HZ);
2726 if (!dt) dt++;
2727 db = resync - (mddev->resync_mark_cnt);
2728 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2729}
2730
NeilBrown80ca3a42006-07-10 04:44:18 -07002731static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08002732
2733static ssize_t
2734sync_completed_show(mddev_t *mddev, char *page)
2735{
2736 unsigned long max_blocks, resync;
2737
2738 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2739 max_blocks = mddev->resync_max_sectors;
2740 else
2741 max_blocks = mddev->size << 1;
2742
2743 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2744 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2745}
2746
NeilBrown80ca3a42006-07-10 04:44:18 -07002747static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08002748
NeilBrowne464eaf2006-03-27 01:18:14 -08002749static ssize_t
2750suspend_lo_show(mddev_t *mddev, char *page)
2751{
2752 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2753}
2754
2755static ssize_t
2756suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2757{
2758 char *e;
2759 unsigned long long new = simple_strtoull(buf, &e, 10);
2760
2761 if (mddev->pers->quiesce == NULL)
2762 return -EINVAL;
2763 if (buf == e || (*e && *e != '\n'))
2764 return -EINVAL;
2765 if (new >= mddev->suspend_hi ||
2766 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2767 mddev->suspend_lo = new;
2768 mddev->pers->quiesce(mddev, 2);
2769 return len;
2770 } else
2771 return -EINVAL;
2772}
2773static struct md_sysfs_entry md_suspend_lo =
2774__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2775
2776
2777static ssize_t
2778suspend_hi_show(mddev_t *mddev, char *page)
2779{
2780 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2781}
2782
2783static ssize_t
2784suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2785{
2786 char *e;
2787 unsigned long long new = simple_strtoull(buf, &e, 10);
2788
2789 if (mddev->pers->quiesce == NULL)
2790 return -EINVAL;
2791 if (buf == e || (*e && *e != '\n'))
2792 return -EINVAL;
2793 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2794 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2795 mddev->suspend_hi = new;
2796 mddev->pers->quiesce(mddev, 1);
2797 mddev->pers->quiesce(mddev, 0);
2798 return len;
2799 } else
2800 return -EINVAL;
2801}
2802static struct md_sysfs_entry md_suspend_hi =
2803__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2804
2805
NeilBrowneae17012005-11-08 21:39:23 -08002806static struct attribute *md_default_attrs[] = {
2807 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07002808 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002809 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002810 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002811 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07002812 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002813 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08002814 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07002815 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07002816 &md_array_state.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002817 NULL,
2818};
2819
2820static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002821 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002822 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002823 &md_sync_min.attr,
2824 &md_sync_max.attr,
2825 &md_sync_speed.attr,
2826 &md_sync_completed.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08002827 &md_suspend_lo.attr,
2828 &md_suspend_hi.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002829 NULL,
2830};
NeilBrown411036f2005-11-08 21:39:40 -08002831static struct attribute_group md_redundancy_group = {
2832 .name = NULL,
2833 .attrs = md_redundancy_attrs,
2834};
2835
NeilBrowneae17012005-11-08 21:39:23 -08002836
2837static ssize_t
2838md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2839{
2840 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2841 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002842 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002843
2844 if (!entry->show)
2845 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002846 rv = mddev_lock(mddev);
2847 if (!rv) {
2848 rv = entry->show(mddev, page);
2849 mddev_unlock(mddev);
2850 }
NeilBrown96de1e62005-11-08 21:39:39 -08002851 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002852}
2853
2854static ssize_t
2855md_attr_store(struct kobject *kobj, struct attribute *attr,
2856 const char *page, size_t length)
2857{
2858 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2859 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002860 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002861
2862 if (!entry->store)
2863 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002864 if (!capable(CAP_SYS_ADMIN))
2865 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002866 rv = mddev_lock(mddev);
2867 if (!rv) {
2868 rv = entry->store(mddev, page, length);
2869 mddev_unlock(mddev);
2870 }
NeilBrown96de1e62005-11-08 21:39:39 -08002871 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002872}
2873
2874static void md_free(struct kobject *ko)
2875{
2876 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2877 kfree(mddev);
2878}
2879
2880static struct sysfs_ops md_sysfs_ops = {
2881 .show = md_attr_show,
2882 .store = md_attr_store,
2883};
2884static struct kobj_type md_ktype = {
2885 .release = md_free,
2886 .sysfs_ops = &md_sysfs_ops,
2887 .default_attrs = md_default_attrs,
2888};
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890int mdp_major = 0;
2891
2892static struct kobject *md_probe(dev_t dev, int *part, void *data)
2893{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002894 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 mddev_t *mddev = mddev_find(dev);
2896 struct gendisk *disk;
2897 int partitioned = (MAJOR(dev) != MD_MAJOR);
2898 int shift = partitioned ? MdpMinorShift : 0;
2899 int unit = MINOR(dev) >> shift;
2900
2901 if (!mddev)
2902 return NULL;
2903
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002904 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002906 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 mddev_put(mddev);
2908 return NULL;
2909 }
2910 disk = alloc_disk(1 << shift);
2911 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002912 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 mddev_put(mddev);
2914 return NULL;
2915 }
2916 disk->major = MAJOR(dev);
2917 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07002918 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07002920 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 disk->fops = &md_fops;
2923 disk->private_data = mddev;
2924 disk->queue = mddev->queue;
2925 add_disk(disk);
2926 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002927 mutex_unlock(&disks_mutex);
NeilBrown9c791972005-11-08 21:39:28 -08002928 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08002929 mddev->kobj.k_name = NULL;
2930 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2931 mddev->kobj.ktype = &md_ktype;
2932 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return NULL;
2934}
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936static void md_safemode_timeout(unsigned long data)
2937{
2938 mddev_t *mddev = (mddev_t *) data;
2939
2940 mddev->safemode = 1;
2941 md_wakeup_thread(mddev->thread);
2942}
2943
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002944static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946static int do_md_run(mddev_t * mddev)
2947{
NeilBrown2604b702006-01-06 00:20:36 -08002948 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 int chunk_size;
2950 struct list_head *tmp;
2951 mdk_rdev_t *rdev;
2952 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08002953 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 char b[BDEVNAME_SIZE];
2955
NeilBrowna757e642005-04-16 15:26:42 -07002956 if (list_empty(&mddev->disks))
2957 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960 if (mddev->pers)
2961 return -EBUSY;
2962
2963 /*
2964 * Analyze all RAID superblock(s)
2965 */
NeilBrowna757e642005-04-16 15:26:42 -07002966 if (!mddev->raid_disks)
2967 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
2969 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
NeilBrown2604b702006-01-06 00:20:36 -08002971 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 if (chunk_size > MAX_CHUNK_SIZE) {
2973 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2974 chunk_size, MAX_CHUNK_SIZE);
2975 return -EINVAL;
2976 }
2977 /*
2978 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2979 */
2980 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07002981 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return -EINVAL;
2983 }
2984 if (chunk_size < PAGE_SIZE) {
2985 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2986 chunk_size, PAGE_SIZE);
2987 return -EINVAL;
2988 }
2989
2990 /* devices must have minimum size of one chunk */
2991 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002992 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 continue;
2994 if (rdev->size < chunk_size / 1024) {
2995 printk(KERN_WARNING
2996 "md: Dev %s smaller than chunk_size:"
2997 " %lluk < %dk\n",
2998 bdevname(rdev->bdev,b),
2999 (unsigned long long)rdev->size,
3000 chunk_size / 1024);
3001 return -EINVAL;
3002 }
3003 }
3004 }
3005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003007 if (mddev->level != LEVEL_NONE)
3008 request_module("md-level-%d", mddev->level);
3009 else if (mddev->clevel[0])
3010 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011#endif
3012
3013 /*
3014 * Drop all container device buffers, from now on
3015 * the only valid external interface is through the md
3016 * device.
3017 * Also find largest hardsector size
3018 */
3019 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003020 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 continue;
3022 sync_blockdev(rdev->bdev);
3023 invalidate_bdev(rdev->bdev, 0);
3024 }
3025
3026 md_probe(mddev->unit, NULL, NULL);
3027 disk = mddev->gendisk;
3028 if (!disk)
3029 return -ENOMEM;
3030
3031 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003032 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003033 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003035 if (mddev->level != LEVEL_NONE)
3036 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3037 mddev->level);
3038 else
3039 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3040 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return -EINVAL;
3042 }
NeilBrown2604b702006-01-06 00:20:36 -08003043 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003045 mddev->level = pers->level;
3046 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
NeilBrownf6705572006-03-27 01:18:11 -08003048 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003049 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003050 /* This personality cannot handle reshaping... */
3051 mddev->pers = NULL;
3052 module_put(pers->owner);
3053 return -EINVAL;
3054 }
3055
NeilBrown657390d2005-08-26 18:34:16 -07003056 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003058 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003059 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
NeilBrownf91de922005-11-08 21:39:36 -08003061 if (start_readonly)
3062 mddev->ro = 2; /* read-only, but switch on first write */
3063
NeilBrownb15c2e52006-01-06 00:20:16 -08003064 err = mddev->pers->run(mddev);
3065 if (!err && mddev->pers->sync_request) {
3066 err = bitmap_create(mddev);
3067 if (err) {
3068 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3069 mdname(mddev), err);
3070 mddev->pers->stop(mddev);
3071 }
3072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (err) {
3074 printk(KERN_ERR "md: pers->run() failed ...\n");
3075 module_put(mddev->pers->owner);
3076 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003077 bitmap_destroy(mddev);
3078 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 }
NeilBrown411036f2005-11-08 21:39:40 -08003080 if (mddev->pers->sync_request)
3081 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08003082 else if (mddev->ro == 2) /* auto-readonly not meaningful */
3083 mddev->ro = 0;
3084
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 atomic_set(&mddev->writes_pending,0);
3086 mddev->safemode = 0;
3087 mddev->safemode_timer.function = md_safemode_timeout;
3088 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003089 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003091
3092 ITERATE_RDEV(mddev,rdev,tmp)
3093 if (rdev->raid_disk >= 0) {
3094 char nm[20];
3095 sprintf(nm, "rd%d", rdev->raid_disk);
3096 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3100
3101 if (mddev->sb_dirty)
3102 md_update_sb(mddev);
3103
3104 set_capacity(disk, mddev->array_size<<1);
3105
3106 /* If we call blk_queue_make_request here, it will
3107 * re-initialise max_sectors etc which may have been
3108 * refined inside -> run. So just set the bits we need to set.
3109 * Most initialisation happended when we called
3110 * blk_queue_make_request(..., md_fail_request)
3111 * earlier.
3112 */
3113 mddev->queue->queuedata = mddev;
3114 mddev->queue->make_request_fn = mddev->pers->make_request;
3115
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003116 /* If there is a partially-recovered drive we need to
3117 * start recovery here. If we leave it to md_check_recovery,
3118 * it will remove the drives and not do the right thing
3119 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003120 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003121 struct list_head *rtmp;
3122 int spares = 0;
3123 ITERATE_RDEV(mddev,rdev,rtmp)
3124 if (rdev->raid_disk >= 0 &&
3125 !test_bit(In_sync, &rdev->flags) &&
3126 !test_bit(Faulty, &rdev->flags))
3127 /* complete an interrupted recovery */
3128 spares++;
3129 if (spares && mddev->pers->sync_request) {
3130 mddev->recovery = 0;
3131 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3132 mddev->sync_thread = md_register_thread(md_do_sync,
3133 mddev,
3134 "%s_resync");
3135 if (!mddev->sync_thread) {
3136 printk(KERN_ERR "%s: could not start resync"
3137 " thread...\n",
3138 mdname(mddev));
3139 /* leave the spares where they are, it shouldn't hurt */
3140 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003141 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003142 }
3143 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003144 md_wakeup_thread(mddev->thread);
3145 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003148 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return 0;
3150}
3151
3152static int restart_array(mddev_t *mddev)
3153{
3154 struct gendisk *disk = mddev->gendisk;
3155 int err;
3156
3157 /*
3158 * Complain if it has no devices
3159 */
3160 err = -ENXIO;
3161 if (list_empty(&mddev->disks))
3162 goto out;
3163
3164 if (mddev->pers) {
3165 err = -EBUSY;
3166 if (!mddev->ro)
3167 goto out;
3168
3169 mddev->safemode = 0;
3170 mddev->ro = 0;
3171 set_disk_ro(disk, 0);
3172
3173 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3174 mdname(mddev));
3175 /*
3176 * Kick recovery or resync if necessary
3177 */
3178 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3179 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003180 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003182 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185out:
3186 return err;
3187}
3188
NeilBrownacc55e22006-06-26 00:27:47 -07003189/* similar to deny_write_access, but accounts for our holding a reference
3190 * to the file ourselves */
3191static int deny_bitmap_write_access(struct file * file)
3192{
3193 struct inode *inode = file->f_mapping->host;
3194
3195 spin_lock(&inode->i_lock);
3196 if (atomic_read(&inode->i_writecount) > 1) {
3197 spin_unlock(&inode->i_lock);
3198 return -ETXTBSY;
3199 }
3200 atomic_set(&inode->i_writecount, -1);
3201 spin_unlock(&inode->i_lock);
3202
3203 return 0;
3204}
3205
3206static void restore_bitmap_write_access(struct file *file)
3207{
3208 struct inode *inode = file->f_mapping->host;
3209
3210 spin_lock(&inode->i_lock);
3211 atomic_set(&inode->i_writecount, 1);
3212 spin_unlock(&inode->i_lock);
3213}
3214
NeilBrown9e653b62006-06-26 00:27:58 -07003215/* mode:
3216 * 0 - completely stop and dis-assemble array
3217 * 1 - switch to readonly
3218 * 2 - stop but do not disassemble array
3219 */
3220static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221{
3222 int err = 0;
3223 struct gendisk *disk = mddev->gendisk;
3224
3225 if (mddev->pers) {
3226 if (atomic_read(&mddev->active)>2) {
3227 printk("md: %s still in use.\n",mdname(mddev));
3228 return -EBUSY;
3229 }
3230
3231 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003232 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3234 md_unregister_thread(mddev->sync_thread);
3235 mddev->sync_thread = NULL;
3236 }
3237
3238 del_timer_sync(&mddev->safemode_timer);
3239
3240 invalidate_partition(disk, 0);
3241
NeilBrown9e653b62006-06-26 00:27:58 -07003242 switch(mode) {
3243 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003245 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 goto out;
3247 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003248 break;
3249 case 0: /* disassemble */
3250 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003251 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003252 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 if (mddev->ro)
3254 set_disk_ro(disk, 0);
3255 blk_queue_make_request(mddev->queue, md_fail_request);
3256 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08003257 if (mddev->pers->sync_request)
3258 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 module_put(mddev->pers->owner);
3261 mddev->pers = NULL;
3262 if (mddev->ro)
3263 mddev->ro = 0;
3264 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003265 if (!mddev->in_sync || mddev->sb_dirty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 /* mark array as shutdown cleanly */
3267 mddev->in_sync = 1;
3268 md_update_sb(mddev);
3269 }
NeilBrown9e653b62006-06-26 00:27:58 -07003270 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003272 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 }
NeilBrown32a76272005-06-21 17:17:14 -07003274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 /*
3276 * Free resources if final stop
3277 */
NeilBrown9e653b62006-06-26 00:27:58 -07003278 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003279 mdk_rdev_t *rdev;
3280 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 struct gendisk *disk;
3282 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3283
NeilBrown978f9462006-02-02 14:28:05 -08003284 bitmap_destroy(mddev);
3285 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003286 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003287 fput(mddev->bitmap_file);
3288 mddev->bitmap_file = NULL;
3289 }
3290 mddev->bitmap_offset = 0;
3291
NeilBrown86e6ffd2005-11-08 21:39:24 -08003292 ITERATE_RDEV(mddev,rdev,tmp)
3293 if (rdev->raid_disk >= 0) {
3294 char nm[20];
3295 sprintf(nm, "rd%d", rdev->raid_disk);
3296 sysfs_remove_link(&mddev->kobj, nm);
3297 }
3298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 export_array(mddev);
3300
3301 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003302 mddev->size = 0;
3303 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003304 mddev->recovery_cp = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 disk = mddev->gendisk;
3307 if (disk)
3308 set_capacity(disk, 0);
3309 mddev->changed = 1;
NeilBrowna8a55c32006-06-26 00:27:39 -07003310 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3312 mdname(mddev));
3313 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003314 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315out:
3316 return err;
3317}
3318
3319static void autorun_array(mddev_t *mddev)
3320{
3321 mdk_rdev_t *rdev;
3322 struct list_head *tmp;
3323 int err;
3324
NeilBrowna757e642005-04-16 15:26:42 -07003325 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
3328 printk(KERN_INFO "md: running: ");
3329
3330 ITERATE_RDEV(mddev,rdev,tmp) {
3331 char b[BDEVNAME_SIZE];
3332 printk("<%s>", bdevname(rdev->bdev,b));
3333 }
3334 printk("\n");
3335
3336 err = do_md_run (mddev);
3337 if (err) {
3338 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3339 do_md_stop (mddev, 0);
3340 }
3341}
3342
3343/*
3344 * lets try to run arrays based on all disks that have arrived
3345 * until now. (those are in pending_raid_disks)
3346 *
3347 * the method: pick the first pending disk, collect all disks with
3348 * the same UUID, remove all from the pending list and put them into
3349 * the 'same_array' list. Then order this list based on superblock
3350 * update time (freshest comes first), kick out 'old' disks and
3351 * compare superblocks. If everything's fine then run it.
3352 *
3353 * If "unit" is allocated, then bump its reference count
3354 */
3355static void autorun_devices(int part)
3356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 struct list_head *tmp;
3358 mdk_rdev_t *rdev0, *rdev;
3359 mddev_t *mddev;
3360 char b[BDEVNAME_SIZE];
3361
3362 printk(KERN_INFO "md: autorun ...\n");
3363 while (!list_empty(&pending_raid_disks)) {
3364 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003365 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 rdev0 = list_entry(pending_raid_disks.next,
3367 mdk_rdev_t, same_set);
3368
3369 printk(KERN_INFO "md: considering %s ...\n",
3370 bdevname(rdev0->bdev,b));
3371 INIT_LIST_HEAD(&candidates);
3372 ITERATE_RDEV_PENDING(rdev,tmp)
3373 if (super_90_load(rdev, rdev0, 0) >= 0) {
3374 printk(KERN_INFO "md: adding %s ...\n",
3375 bdevname(rdev->bdev,b));
3376 list_move(&rdev->same_set, &candidates);
3377 }
3378 /*
3379 * now we have a set of devices, with all of them having
3380 * mostly sane superblocks. It's time to allocate the
3381 * mddev.
3382 */
3383 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
3384 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3385 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3386 break;
3387 }
3388 if (part)
3389 dev = MKDEV(mdp_major,
3390 rdev0->preferred_minor << MdpMinorShift);
3391 else
3392 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3393
3394 md_probe(dev, NULL, NULL);
3395 mddev = mddev_find(dev);
3396 if (!mddev) {
3397 printk(KERN_ERR
3398 "md: cannot allocate memory for md drive.\n");
3399 break;
3400 }
3401 if (mddev_lock(mddev))
3402 printk(KERN_WARNING "md: %s locked, cannot run\n",
3403 mdname(mddev));
3404 else if (mddev->raid_disks || mddev->major_version
3405 || !list_empty(&mddev->disks)) {
3406 printk(KERN_WARNING
3407 "md: %s already running, cannot run %s\n",
3408 mdname(mddev), bdevname(rdev0->bdev,b));
3409 mddev_unlock(mddev);
3410 } else {
3411 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3412 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3413 list_del_init(&rdev->same_set);
3414 if (bind_rdev_to_array(rdev, mddev))
3415 export_rdev(rdev);
3416 }
3417 autorun_array(mddev);
3418 mddev_unlock(mddev);
3419 }
3420 /* on success, candidates will be empty, on error
3421 * it won't...
3422 */
3423 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3424 export_rdev(rdev);
3425 mddev_put(mddev);
3426 }
3427 printk(KERN_INFO "md: ... autorun DONE.\n");
3428}
3429
3430/*
3431 * import RAID devices based on one partition
3432 * if possible, the array gets run as well.
3433 */
3434
3435static int autostart_array(dev_t startdev)
3436{
3437 char b[BDEVNAME_SIZE];
3438 int err = -EINVAL, i;
3439 mdp_super_t *sb = NULL;
3440 mdk_rdev_t *start_rdev = NULL, *rdev;
3441
3442 start_rdev = md_import_device(startdev, 0, 0);
3443 if (IS_ERR(start_rdev))
3444 return err;
3445
3446
3447 /* NOTE: this can only work for 0.90.0 superblocks */
3448 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
3449 if (sb->major_version != 0 ||
3450 sb->minor_version != 90 ) {
3451 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
3452 export_rdev(start_rdev);
3453 return err;
3454 }
3455
NeilBrownb2d444d2005-11-08 21:39:31 -08003456 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 printk(KERN_WARNING
3458 "md: can not autostart based on faulty %s!\n",
3459 bdevname(start_rdev->bdev,b));
3460 export_rdev(start_rdev);
3461 return err;
3462 }
3463 list_add(&start_rdev->same_set, &pending_raid_disks);
3464
3465 for (i = 0; i < MD_SB_DISKS; i++) {
3466 mdp_disk_t *desc = sb->disks + i;
3467 dev_t dev = MKDEV(desc->major, desc->minor);
3468
3469 if (!dev)
3470 continue;
3471 if (dev == startdev)
3472 continue;
3473 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
3474 continue;
3475 rdev = md_import_device(dev, 0, 0);
3476 if (IS_ERR(rdev))
3477 continue;
3478
3479 list_add(&rdev->same_set, &pending_raid_disks);
3480 }
3481
3482 /*
3483 * possibly return codes
3484 */
3485 autorun_devices(0);
3486 return 0;
3487
3488}
3489
3490
3491static int get_version(void __user * arg)
3492{
3493 mdu_version_t ver;
3494
3495 ver.major = MD_MAJOR_VERSION;
3496 ver.minor = MD_MINOR_VERSION;
3497 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3498
3499 if (copy_to_user(arg, &ver, sizeof(ver)))
3500 return -EFAULT;
3501
3502 return 0;
3503}
3504
3505static int get_array_info(mddev_t * mddev, void __user * arg)
3506{
3507 mdu_array_info_t info;
3508 int nr,working,active,failed,spare;
3509 mdk_rdev_t *rdev;
3510 struct list_head *tmp;
3511
3512 nr=working=active=failed=spare=0;
3513 ITERATE_RDEV(mddev,rdev,tmp) {
3514 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003515 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 failed++;
3517 else {
3518 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003519 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 active++;
3521 else
3522 spare++;
3523 }
3524 }
3525
3526 info.major_version = mddev->major_version;
3527 info.minor_version = mddev->minor_version;
3528 info.patch_version = MD_PATCHLEVEL_VERSION;
3529 info.ctime = mddev->ctime;
3530 info.level = mddev->level;
3531 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003532 if (info.size != mddev->size) /* overflow */
3533 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 info.nr_disks = nr;
3535 info.raid_disks = mddev->raid_disks;
3536 info.md_minor = mddev->md_minor;
3537 info.not_persistent= !mddev->persistent;
3538
3539 info.utime = mddev->utime;
3540 info.state = 0;
3541 if (mddev->in_sync)
3542 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003543 if (mddev->bitmap && mddev->bitmap_offset)
3544 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 info.active_disks = active;
3546 info.working_disks = working;
3547 info.failed_disks = failed;
3548 info.spare_disks = spare;
3549
3550 info.layout = mddev->layout;
3551 info.chunk_size = mddev->chunk_size;
3552
3553 if (copy_to_user(arg, &info, sizeof(info)))
3554 return -EFAULT;
3555
3556 return 0;
3557}
3558
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003559static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003560{
3561 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3562 char *ptr, *buf = NULL;
3563 int err = -ENOMEM;
3564
3565 file = kmalloc(sizeof(*file), GFP_KERNEL);
3566 if (!file)
3567 goto out;
3568
3569 /* bitmap disabled, zero the first byte and copy out */
3570 if (!mddev->bitmap || !mddev->bitmap->file) {
3571 file->pathname[0] = '\0';
3572 goto copy_out;
3573 }
3574
3575 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3576 if (!buf)
3577 goto out;
3578
3579 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3580 if (!ptr)
3581 goto out;
3582
3583 strcpy(file->pathname, ptr);
3584
3585copy_out:
3586 err = 0;
3587 if (copy_to_user(arg, file, sizeof(*file)))
3588 err = -EFAULT;
3589out:
3590 kfree(buf);
3591 kfree(file);
3592 return err;
3593}
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595static int get_disk_info(mddev_t * mddev, void __user * arg)
3596{
3597 mdu_disk_info_t info;
3598 unsigned int nr;
3599 mdk_rdev_t *rdev;
3600
3601 if (copy_from_user(&info, arg, sizeof(info)))
3602 return -EFAULT;
3603
3604 nr = info.number;
3605
3606 rdev = find_rdev_nr(mddev, nr);
3607 if (rdev) {
3608 info.major = MAJOR(rdev->bdev->bd_dev);
3609 info.minor = MINOR(rdev->bdev->bd_dev);
3610 info.raid_disk = rdev->raid_disk;
3611 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003612 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003614 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 info.state |= (1<<MD_DISK_ACTIVE);
3616 info.state |= (1<<MD_DISK_SYNC);
3617 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003618 if (test_bit(WriteMostly, &rdev->flags))
3619 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 } else {
3621 info.major = info.minor = 0;
3622 info.raid_disk = -1;
3623 info.state = (1<<MD_DISK_REMOVED);
3624 }
3625
3626 if (copy_to_user(arg, &info, sizeof(info)))
3627 return -EFAULT;
3628
3629 return 0;
3630}
3631
3632static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3633{
3634 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3635 mdk_rdev_t *rdev;
3636 dev_t dev = MKDEV(info->major,info->minor);
3637
3638 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3639 return -EOVERFLOW;
3640
3641 if (!mddev->raid_disks) {
3642 int err;
3643 /* expecting a device which has a superblock */
3644 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3645 if (IS_ERR(rdev)) {
3646 printk(KERN_WARNING
3647 "md: md_import_device returned %ld\n",
3648 PTR_ERR(rdev));
3649 return PTR_ERR(rdev);
3650 }
3651 if (!list_empty(&mddev->disks)) {
3652 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3653 mdk_rdev_t, same_set);
3654 int err = super_types[mddev->major_version]
3655 .load_super(rdev, rdev0, mddev->minor_version);
3656 if (err < 0) {
3657 printk(KERN_WARNING
3658 "md: %s has different UUID to %s\n",
3659 bdevname(rdev->bdev,b),
3660 bdevname(rdev0->bdev,b2));
3661 export_rdev(rdev);
3662 return -EINVAL;
3663 }
3664 }
3665 err = bind_rdev_to_array(rdev, mddev);
3666 if (err)
3667 export_rdev(rdev);
3668 return err;
3669 }
3670
3671 /*
3672 * add_new_disk can be used once the array is assembled
3673 * to add "hot spares". They must already have a superblock
3674 * written
3675 */
3676 if (mddev->pers) {
3677 int err;
3678 if (!mddev->pers->hot_add_disk) {
3679 printk(KERN_WARNING
3680 "%s: personality does not support diskops!\n",
3681 mdname(mddev));
3682 return -EINVAL;
3683 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003684 if (mddev->persistent)
3685 rdev = md_import_device(dev, mddev->major_version,
3686 mddev->minor_version);
3687 else
3688 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 if (IS_ERR(rdev)) {
3690 printk(KERN_WARNING
3691 "md: md_import_device returned %ld\n",
3692 PTR_ERR(rdev));
3693 return PTR_ERR(rdev);
3694 }
NeilBrown41158c72005-06-21 17:17:25 -07003695 /* set save_raid_disk if appropriate */
3696 if (!mddev->persistent) {
3697 if (info->state & (1<<MD_DISK_SYNC) &&
3698 info->raid_disk < mddev->raid_disks)
3699 rdev->raid_disk = info->raid_disk;
3700 else
3701 rdev->raid_disk = -1;
3702 } else
3703 super_types[mddev->major_version].
3704 validate_super(mddev, rdev);
3705 rdev->saved_raid_disk = rdev->raid_disk;
3706
NeilBrownb2d444d2005-11-08 21:39:31 -08003707 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003708 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3709 set_bit(WriteMostly, &rdev->flags);
3710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 rdev->raid_disk = -1;
3712 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07003713 if (!err && !mddev->pers->hot_remove_disk) {
3714 /* If there is hot_add_disk but no hot_remove_disk
3715 * then added disks for geometry changes,
3716 * and should be added immediately.
3717 */
3718 super_types[mddev->major_version].
3719 validate_super(mddev, rdev);
3720 err = mddev->pers->hot_add_disk(mddev, rdev);
3721 if (err)
3722 unbind_rdev_from_array(rdev);
3723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 if (err)
3725 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003726
3727 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003728 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 return err;
3730 }
3731
3732 /* otherwise, add_new_disk is only allowed
3733 * for major_version==0 superblocks
3734 */
3735 if (mddev->major_version != 0) {
3736 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3737 mdname(mddev));
3738 return -EINVAL;
3739 }
3740
3741 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3742 int err;
3743 rdev = md_import_device (dev, -1, 0);
3744 if (IS_ERR(rdev)) {
3745 printk(KERN_WARNING
3746 "md: error, md_import_device() returned %ld\n",
3747 PTR_ERR(rdev));
3748 return PTR_ERR(rdev);
3749 }
3750 rdev->desc_nr = info->number;
3751 if (info->raid_disk < mddev->raid_disks)
3752 rdev->raid_disk = info->raid_disk;
3753 else
3754 rdev->raid_disk = -1;
3755
NeilBrownb2d444d2005-11-08 21:39:31 -08003756 rdev->flags = 0;
3757
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003759 if (info->state & (1<<MD_DISK_SYNC))
3760 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003762 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3763 set_bit(WriteMostly, &rdev->flags);
3764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 if (!mddev->persistent) {
3766 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3767 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3768 } else
3769 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3770 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3771
NeilBrown2bf071b2006-01-06 00:20:55 -08003772 err = bind_rdev_to_array(rdev, mddev);
3773 if (err) {
3774 export_rdev(rdev);
3775 return err;
3776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 }
3778
3779 return 0;
3780}
3781
3782static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3783{
3784 char b[BDEVNAME_SIZE];
3785 mdk_rdev_t *rdev;
3786
3787 if (!mddev->pers)
3788 return -ENODEV;
3789
3790 rdev = find_rdev(mddev, dev);
3791 if (!rdev)
3792 return -ENXIO;
3793
3794 if (rdev->raid_disk >= 0)
3795 goto busy;
3796
3797 kick_rdev_from_array(rdev);
3798 md_update_sb(mddev);
NeilBrownd7603b72006-01-06 00:20:30 -08003799 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
3801 return 0;
3802busy:
3803 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3804 bdevname(rdev->bdev,b), mdname(mddev));
3805 return -EBUSY;
3806}
3807
3808static int hot_add_disk(mddev_t * mddev, dev_t dev)
3809{
3810 char b[BDEVNAME_SIZE];
3811 int err;
3812 unsigned int size;
3813 mdk_rdev_t *rdev;
3814
3815 if (!mddev->pers)
3816 return -ENODEV;
3817
3818 if (mddev->major_version != 0) {
3819 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3820 " version-0 superblocks.\n",
3821 mdname(mddev));
3822 return -EINVAL;
3823 }
3824 if (!mddev->pers->hot_add_disk) {
3825 printk(KERN_WARNING
3826 "%s: personality does not support diskops!\n",
3827 mdname(mddev));
3828 return -EINVAL;
3829 }
3830
3831 rdev = md_import_device (dev, -1, 0);
3832 if (IS_ERR(rdev)) {
3833 printk(KERN_WARNING
3834 "md: error, md_import_device() returned %ld\n",
3835 PTR_ERR(rdev));
3836 return -EINVAL;
3837 }
3838
3839 if (mddev->persistent)
3840 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3841 else
3842 rdev->sb_offset =
3843 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3844
3845 size = calc_dev_size(rdev, mddev->chunk_size);
3846 rdev->size = size;
3847
NeilBrownb2d444d2005-11-08 21:39:31 -08003848 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 printk(KERN_WARNING
3850 "md: can not hot-add faulty %s disk to %s!\n",
3851 bdevname(rdev->bdev,b), mdname(mddev));
3852 err = -EINVAL;
3853 goto abort_export;
3854 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003855 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 rdev->desc_nr = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08003857 err = bind_rdev_to_array(rdev, mddev);
3858 if (err)
3859 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
3861 /*
3862 * The rest should better be atomic, we can have disk failures
3863 * noticed in interrupt contexts ...
3864 */
3865
3866 if (rdev->desc_nr == mddev->max_disks) {
3867 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3868 mdname(mddev));
3869 err = -EBUSY;
3870 goto abort_unbind_export;
3871 }
3872
3873 rdev->raid_disk = -1;
3874
3875 md_update_sb(mddev);
3876
3877 /*
3878 * Kick recovery, maybe this spare has to be added to the
3879 * array immediately.
3880 */
3881 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3882 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08003883 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 return 0;
3885
3886abort_unbind_export:
3887 unbind_rdev_from_array(rdev);
3888
3889abort_export:
3890 export_rdev(rdev);
3891 return err;
3892}
3893
NeilBrown32a76272005-06-21 17:17:14 -07003894static int set_bitmap_file(mddev_t *mddev, int fd)
3895{
3896 int err;
3897
NeilBrown36fa3062005-09-09 16:23:45 -07003898 if (mddev->pers) {
3899 if (!mddev->pers->quiesce)
3900 return -EBUSY;
3901 if (mddev->recovery || mddev->sync_thread)
3902 return -EBUSY;
3903 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07003904 }
3905
NeilBrown36fa3062005-09-09 16:23:45 -07003906
3907 if (fd >= 0) {
3908 if (mddev->bitmap)
3909 return -EEXIST; /* cannot add when bitmap is present */
3910 mddev->bitmap_file = fget(fd);
3911
3912 if (mddev->bitmap_file == NULL) {
3913 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3914 mdname(mddev));
3915 return -EBADF;
3916 }
3917
3918 err = deny_bitmap_write_access(mddev->bitmap_file);
3919 if (err) {
3920 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3921 mdname(mddev));
3922 fput(mddev->bitmap_file);
3923 mddev->bitmap_file = NULL;
3924 return err;
3925 }
NeilBrowna654b9d82005-06-21 17:17:27 -07003926 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07003927 } else if (mddev->bitmap == NULL)
3928 return -ENOENT; /* cannot remove what isn't there */
3929 err = 0;
3930 if (mddev->pers) {
3931 mddev->pers->quiesce(mddev, 1);
3932 if (fd >= 0)
3933 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07003934 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07003935 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07003936 fd = -1; /* make sure to put the file */
3937 }
NeilBrown36fa3062005-09-09 16:23:45 -07003938 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07003939 }
3940 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07003941 if (mddev->bitmap_file) {
3942 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07003943 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07003944 }
NeilBrown36fa3062005-09-09 16:23:45 -07003945 mddev->bitmap_file = NULL;
3946 }
3947
NeilBrown32a76272005-06-21 17:17:14 -07003948 return err;
3949}
3950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951/*
3952 * set_array_info is used two different ways
3953 * The original usage is when creating a new array.
3954 * In this usage, raid_disks is > 0 and it together with
3955 * level, size, not_persistent,layout,chunksize determine the
3956 * shape of the array.
3957 * This will always create an array with a type-0.90.0 superblock.
3958 * The newer usage is when assembling an array.
3959 * In this case raid_disks will be 0, and the major_version field is
3960 * use to determine which style super-blocks are to be found on the devices.
3961 * The minor and patch _version numbers are also kept incase the
3962 * super_block handler wishes to interpret them.
3963 */
3964static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3965{
3966
3967 if (info->raid_disks == 0) {
3968 /* just setting version number for superblock loading */
3969 if (info->major_version < 0 ||
3970 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3971 super_types[info->major_version].name == NULL) {
3972 /* maybe try to auto-load a module? */
3973 printk(KERN_INFO
3974 "md: superblock version %d not known\n",
3975 info->major_version);
3976 return -EINVAL;
3977 }
3978 mddev->major_version = info->major_version;
3979 mddev->minor_version = info->minor_version;
3980 mddev->patch_version = info->patch_version;
3981 return 0;
3982 }
3983 mddev->major_version = MD_MAJOR_VERSION;
3984 mddev->minor_version = MD_MINOR_VERSION;
3985 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3986 mddev->ctime = get_seconds();
3987
3988 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08003989 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 mddev->size = info->size;
3991 mddev->raid_disks = info->raid_disks;
3992 /* don't set md_minor, it is determined by which /dev/md* was
3993 * openned
3994 */
3995 if (info->state & (1<<MD_SB_CLEAN))
3996 mddev->recovery_cp = MaxSector;
3997 else
3998 mddev->recovery_cp = 0;
3999 mddev->persistent = ! info->not_persistent;
4000
4001 mddev->layout = info->layout;
4002 mddev->chunk_size = info->chunk_size;
4003
4004 mddev->max_disks = MD_SB_DISKS;
4005
4006 mddev->sb_dirty = 1;
4007
NeilBrownb2a27032005-11-28 13:44:12 -08004008 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4009 mddev->bitmap_offset = 0;
4010
NeilBrownf6705572006-03-27 01:18:11 -08004011 mddev->reshape_position = MaxSector;
4012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 /*
4014 * Generate a 128 bit UUID
4015 */
4016 get_random_bytes(mddev->uuid, 16);
4017
NeilBrownf6705572006-03-27 01:18:11 -08004018 mddev->new_level = mddev->level;
4019 mddev->new_chunk = mddev->chunk_size;
4020 mddev->new_layout = mddev->layout;
4021 mddev->delta_disks = 0;
4022
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 return 0;
4024}
4025
NeilBrowna35b0d62006-01-06 00:20:49 -08004026static int update_size(mddev_t *mddev, unsigned long size)
4027{
4028 mdk_rdev_t * rdev;
4029 int rv;
4030 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004031 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004032
4033 if (mddev->pers->resize == NULL)
4034 return -EINVAL;
4035 /* The "size" is the amount of each device that is used.
4036 * This can only make sense for arrays with redundancy.
4037 * linear and raid0 always use whatever space is available
4038 * We can only consider changing the size if no resync
4039 * or reconstruction is happening, and if the new size
4040 * is acceptable. It must fit before the sb_offset or,
4041 * if that is <data_offset, it must fit before the
4042 * size of each device.
4043 * If size is zero, we find the largest size that fits.
4044 */
4045 if (mddev->sync_thread)
4046 return -EBUSY;
4047 ITERATE_RDEV(mddev,rdev,tmp) {
4048 sector_t avail;
NeilBrowna35b0d62006-01-06 00:20:49 -08004049 if (rdev->sb_offset > rdev->data_offset)
4050 avail = (rdev->sb_offset*2) - rdev->data_offset;
4051 else
4052 avail = get_capacity(rdev->bdev->bd_disk)
4053 - rdev->data_offset;
4054 if (fit && (size == 0 || size > avail/2))
4055 size = avail/2;
4056 if (avail < ((sector_t)size << 1))
4057 return -ENOSPC;
4058 }
4059 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4060 if (!rv) {
4061 struct block_device *bdev;
4062
4063 bdev = bdget_disk(mddev->gendisk, 0);
4064 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004065 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004066 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004067 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004068 bdput(bdev);
4069 }
4070 }
4071 return rv;
4072}
4073
NeilBrownda943b992006-01-06 00:20:54 -08004074static int update_raid_disks(mddev_t *mddev, int raid_disks)
4075{
4076 int rv;
4077 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004078 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004079 return -EINVAL;
4080 if (raid_disks <= 0 ||
4081 raid_disks >= mddev->max_disks)
4082 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004083 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004084 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004085 mddev->delta_disks = raid_disks - mddev->raid_disks;
4086
4087 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004088 return rv;
4089}
4090
4091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092/*
4093 * update_array_info is used to change the configuration of an
4094 * on-line array.
4095 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4096 * fields in the info are checked against the array.
4097 * Any differences that cannot be handled will cause an error.
4098 * Normally, only one change can be managed at a time.
4099 */
4100static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4101{
4102 int rv = 0;
4103 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004104 int state = 0;
4105
4106 /* calculate expected state,ignoring low bits */
4107 if (mddev->bitmap && mddev->bitmap_offset)
4108 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
4110 if (mddev->major_version != info->major_version ||
4111 mddev->minor_version != info->minor_version ||
4112/* mddev->patch_version != info->patch_version || */
4113 mddev->ctime != info->ctime ||
4114 mddev->level != info->level ||
4115/* mddev->layout != info->layout || */
4116 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004117 mddev->chunk_size != info->chunk_size ||
4118 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4119 ((state^info->state) & 0xfffffe00)
4120 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 return -EINVAL;
4122 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004123 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 if (mddev->raid_disks != info->raid_disks) cnt++;
4125 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004126 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 if (cnt == 0) return 0;
4128 if (cnt > 1) return -EINVAL;
4129
4130 if (mddev->layout != info->layout) {
4131 /* Change layout
4132 * we don't need to do anything at the md level, the
4133 * personality will take care of it all.
4134 */
4135 if (mddev->pers->reconfig == NULL)
4136 return -EINVAL;
4137 else
4138 return mddev->pers->reconfig(mddev, info->layout, -1);
4139 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004140 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004141 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
NeilBrownda943b992006-01-06 00:20:54 -08004143 if (mddev->raid_disks != info->raid_disks)
4144 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
NeilBrown36fa3062005-09-09 16:23:45 -07004146 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4147 if (mddev->pers->quiesce == NULL)
4148 return -EINVAL;
4149 if (mddev->recovery || mddev->sync_thread)
4150 return -EBUSY;
4151 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4152 /* add the bitmap */
4153 if (mddev->bitmap)
4154 return -EEXIST;
4155 if (mddev->default_bitmap_offset == 0)
4156 return -EINVAL;
4157 mddev->bitmap_offset = mddev->default_bitmap_offset;
4158 mddev->pers->quiesce(mddev, 1);
4159 rv = bitmap_create(mddev);
4160 if (rv)
4161 bitmap_destroy(mddev);
4162 mddev->pers->quiesce(mddev, 0);
4163 } else {
4164 /* remove the bitmap */
4165 if (!mddev->bitmap)
4166 return -ENOENT;
4167 if (mddev->bitmap->file)
4168 return -EINVAL;
4169 mddev->pers->quiesce(mddev, 1);
4170 bitmap_destroy(mddev);
4171 mddev->pers->quiesce(mddev, 0);
4172 mddev->bitmap_offset = 0;
4173 }
4174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 md_update_sb(mddev);
4176 return rv;
4177}
4178
4179static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4180{
4181 mdk_rdev_t *rdev;
4182
4183 if (mddev->pers == NULL)
4184 return -ENODEV;
4185
4186 rdev = find_rdev(mddev, dev);
4187 if (!rdev)
4188 return -ENODEV;
4189
4190 md_error(mddev, rdev);
4191 return 0;
4192}
4193
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004194static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4195{
4196 mddev_t *mddev = bdev->bd_disk->private_data;
4197
4198 geo->heads = 2;
4199 geo->sectors = 4;
4200 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4201 return 0;
4202}
4203
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204static int md_ioctl(struct inode *inode, struct file *file,
4205 unsigned int cmd, unsigned long arg)
4206{
4207 int err = 0;
4208 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 mddev_t *mddev = NULL;
4210
4211 if (!capable(CAP_SYS_ADMIN))
4212 return -EACCES;
4213
4214 /*
4215 * Commands dealing with the RAID driver but not any
4216 * particular array:
4217 */
4218 switch (cmd)
4219 {
4220 case RAID_VERSION:
4221 err = get_version(argp);
4222 goto done;
4223
4224 case PRINT_RAID_DEBUG:
4225 err = 0;
4226 md_print_devices();
4227 goto done;
4228
4229#ifndef MODULE
4230 case RAID_AUTORUN:
4231 err = 0;
4232 autostart_arrays(arg);
4233 goto done;
4234#endif
4235 default:;
4236 }
4237
4238 /*
4239 * Commands creating/starting a new array:
4240 */
4241
4242 mddev = inode->i_bdev->bd_disk->private_data;
4243
4244 if (!mddev) {
4245 BUG();
4246 goto abort;
4247 }
4248
4249
4250 if (cmd == START_ARRAY) {
4251 /* START_ARRAY doesn't need to lock the array as autostart_array
4252 * does the locking, and it could even be a different array
4253 */
4254 static int cnt = 3;
4255 if (cnt > 0 ) {
4256 printk(KERN_WARNING
4257 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08004258 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 current->comm, current->pid);
4260 cnt--;
4261 }
4262 err = autostart_array(new_decode_dev(arg));
4263 if (err) {
4264 printk(KERN_WARNING "md: autostart failed!\n");
4265 goto abort;
4266 }
4267 goto done;
4268 }
4269
4270 err = mddev_lock(mddev);
4271 if (err) {
4272 printk(KERN_INFO
4273 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4274 err, cmd);
4275 goto abort;
4276 }
4277
4278 switch (cmd)
4279 {
4280 case SET_ARRAY_INFO:
4281 {
4282 mdu_array_info_t info;
4283 if (!arg)
4284 memset(&info, 0, sizeof(info));
4285 else if (copy_from_user(&info, argp, sizeof(info))) {
4286 err = -EFAULT;
4287 goto abort_unlock;
4288 }
4289 if (mddev->pers) {
4290 err = update_array_info(mddev, &info);
4291 if (err) {
4292 printk(KERN_WARNING "md: couldn't update"
4293 " array info. %d\n", err);
4294 goto abort_unlock;
4295 }
4296 goto done_unlock;
4297 }
4298 if (!list_empty(&mddev->disks)) {
4299 printk(KERN_WARNING
4300 "md: array %s already has disks!\n",
4301 mdname(mddev));
4302 err = -EBUSY;
4303 goto abort_unlock;
4304 }
4305 if (mddev->raid_disks) {
4306 printk(KERN_WARNING
4307 "md: array %s already initialised!\n",
4308 mdname(mddev));
4309 err = -EBUSY;
4310 goto abort_unlock;
4311 }
4312 err = set_array_info(mddev, &info);
4313 if (err) {
4314 printk(KERN_WARNING "md: couldn't set"
4315 " array info. %d\n", err);
4316 goto abort_unlock;
4317 }
4318 }
4319 goto done_unlock;
4320
4321 default:;
4322 }
4323
4324 /*
4325 * Commands querying/configuring an existing array:
4326 */
NeilBrown32a76272005-06-21 17:17:14 -07004327 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4328 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
4329 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4330 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 err = -ENODEV;
4332 goto abort_unlock;
4333 }
4334
4335 /*
4336 * Commands even a read-only array can execute:
4337 */
4338 switch (cmd)
4339 {
4340 case GET_ARRAY_INFO:
4341 err = get_array_info(mddev, argp);
4342 goto done_unlock;
4343
NeilBrown32a76272005-06-21 17:17:14 -07004344 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004345 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004346 goto done_unlock;
4347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 case GET_DISK_INFO:
4349 err = get_disk_info(mddev, argp);
4350 goto done_unlock;
4351
4352 case RESTART_ARRAY_RW:
4353 err = restart_array(mddev);
4354 goto done_unlock;
4355
4356 case STOP_ARRAY:
4357 err = do_md_stop (mddev, 0);
4358 goto done_unlock;
4359
4360 case STOP_ARRAY_RO:
4361 err = do_md_stop (mddev, 1);
4362 goto done_unlock;
4363
4364 /*
4365 * We have a problem here : there is no easy way to give a CHS
4366 * virtual geometry. We currently pretend that we have a 2 heads
4367 * 4 sectors (with a BIG number of cylinders...). This drives
4368 * dosfs just mad... ;-)
4369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 }
4371
4372 /*
4373 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004374 * superblock, so we do not allow them on read-only arrays.
4375 * However non-MD ioctls (e.g. get-size) will still come through
4376 * here and hit the 'default' below, so only disallow
4377 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 */
NeilBrownf91de922005-11-08 21:39:36 -08004379 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4380 mddev->ro && mddev->pers) {
4381 if (mddev->ro == 2) {
4382 mddev->ro = 0;
4383 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4384 md_wakeup_thread(mddev->thread);
4385
4386 } else {
4387 err = -EROFS;
4388 goto abort_unlock;
4389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 }
4391
4392 switch (cmd)
4393 {
4394 case ADD_NEW_DISK:
4395 {
4396 mdu_disk_info_t info;
4397 if (copy_from_user(&info, argp, sizeof(info)))
4398 err = -EFAULT;
4399 else
4400 err = add_new_disk(mddev, &info);
4401 goto done_unlock;
4402 }
4403
4404 case HOT_REMOVE_DISK:
4405 err = hot_remove_disk(mddev, new_decode_dev(arg));
4406 goto done_unlock;
4407
4408 case HOT_ADD_DISK:
4409 err = hot_add_disk(mddev, new_decode_dev(arg));
4410 goto done_unlock;
4411
4412 case SET_DISK_FAULTY:
4413 err = set_disk_faulty(mddev, new_decode_dev(arg));
4414 goto done_unlock;
4415
4416 case RUN_ARRAY:
4417 err = do_md_run (mddev);
4418 goto done_unlock;
4419
NeilBrown32a76272005-06-21 17:17:14 -07004420 case SET_BITMAP_FILE:
4421 err = set_bitmap_file(mddev, (int)arg);
4422 goto done_unlock;
4423
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 err = -EINVAL;
4426 goto abort_unlock;
4427 }
4428
4429done_unlock:
4430abort_unlock:
4431 mddev_unlock(mddev);
4432
4433 return err;
4434done:
4435 if (err)
4436 MD_BUG();
4437abort:
4438 return err;
4439}
4440
4441static int md_open(struct inode *inode, struct file *file)
4442{
4443 /*
4444 * Succeed if we can lock the mddev, which confirms that
4445 * it isn't being stopped right now.
4446 */
4447 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4448 int err;
4449
4450 if ((err = mddev_lock(mddev)))
4451 goto out;
4452
4453 err = 0;
4454 mddev_get(mddev);
4455 mddev_unlock(mddev);
4456
4457 check_disk_change(inode->i_bdev);
4458 out:
4459 return err;
4460}
4461
4462static int md_release(struct inode *inode, struct file * file)
4463{
4464 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4465
4466 if (!mddev)
4467 BUG();
4468 mddev_put(mddev);
4469
4470 return 0;
4471}
4472
4473static int md_media_changed(struct gendisk *disk)
4474{
4475 mddev_t *mddev = disk->private_data;
4476
4477 return mddev->changed;
4478}
4479
4480static int md_revalidate(struct gendisk *disk)
4481{
4482 mddev_t *mddev = disk->private_data;
4483
4484 mddev->changed = 0;
4485 return 0;
4486}
4487static struct block_device_operations md_fops =
4488{
4489 .owner = THIS_MODULE,
4490 .open = md_open,
4491 .release = md_release,
4492 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004493 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 .media_changed = md_media_changed,
4495 .revalidate_disk= md_revalidate,
4496};
4497
Adrian Bunk75c96f82005-05-05 16:16:09 -07004498static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499{
4500 mdk_thread_t *thread = arg;
4501
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 /*
4503 * md_thread is a 'system-thread', it's priority should be very
4504 * high. We avoid resource deadlocks individually in each
4505 * raid personality. (RAID5 does preallocation) We also use RR and
4506 * the very same RT priority as kswapd, thus we will never get
4507 * into a priority inversion deadlock.
4508 *
4509 * we definitely have to have equal or higher priority than
4510 * bdflush, otherwise bdflush will deadlock if there are too
4511 * many dirty RAID5 blocks.
4512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
NeilBrown6985c432005-10-19 21:23:47 -07004514 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004515 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
NeilBrown93588e22005-11-15 00:09:12 -08004517 /* We need to wait INTERRUPTIBLE so that
4518 * we don't add to the load-average.
4519 * That means we need to be sure no signals are
4520 * pending
4521 */
4522 if (signal_pending(current))
4523 flush_signals(current);
4524
4525 wait_event_interruptible_timeout
4526 (thread->wqueue,
4527 test_bit(THREAD_WAKEUP, &thread->flags)
4528 || kthread_should_stop(),
4529 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07004530 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
4532 clear_bit(THREAD_WAKEUP, &thread->flags);
4533
NeilBrown787453c2005-11-08 21:39:43 -08004534 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004536
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 return 0;
4538}
4539
4540void md_wakeup_thread(mdk_thread_t *thread)
4541{
4542 if (thread) {
4543 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4544 set_bit(THREAD_WAKEUP, &thread->flags);
4545 wake_up(&thread->wqueue);
4546 }
4547}
4548
4549mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4550 const char *name)
4551{
4552 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
NeilBrown9ffae0c2006-01-06 00:20:32 -08004554 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 if (!thread)
4556 return NULL;
4557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 init_waitqueue_head(&thread->wqueue);
4559
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 thread->run = run;
4561 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004562 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004563 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004564 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 kfree(thread);
4566 return NULL;
4567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 return thread;
4569}
4570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571void md_unregister_thread(mdk_thread_t *thread)
4572{
NeilBrownd28446f2005-04-16 15:26:41 -07004573 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07004574
4575 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 kfree(thread);
4577}
4578
4579void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4580{
4581 if (!mddev) {
4582 MD_BUG();
4583 return;
4584 }
4585
NeilBrownb2d444d2005-11-08 21:39:31 -08004586 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 return;
NeilBrown32a76272005-06-21 17:17:14 -07004588/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4590 mdname(mddev),
4591 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4592 __builtin_return_address(0),__builtin_return_address(1),
4593 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004594*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004595 if (!mddev->pers)
4596 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 if (!mddev->pers->error_handler)
4598 return;
4599 mddev->pers->error_handler(mddev,rdev);
4600 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4601 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4602 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004603 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604}
4605
4606/* seq_file implementation /proc/mdstat */
4607
4608static void status_unused(struct seq_file *seq)
4609{
4610 int i = 0;
4611 mdk_rdev_t *rdev;
4612 struct list_head *tmp;
4613
4614 seq_printf(seq, "unused devices: ");
4615
4616 ITERATE_RDEV_PENDING(rdev,tmp) {
4617 char b[BDEVNAME_SIZE];
4618 i++;
4619 seq_printf(seq, "%s ",
4620 bdevname(rdev->bdev,b));
4621 }
4622 if (!i)
4623 seq_printf(seq, "<none>");
4624
4625 seq_printf(seq, "\n");
4626}
4627
4628
4629static void status_resync(struct seq_file *seq, mddev_t * mddev)
4630{
NeilBrown4588b422006-03-27 01:18:04 -08004631 sector_t max_blocks, resync, res;
4632 unsigned long dt, db, rt;
4633 int scale;
4634 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
4636 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4637
4638 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4639 max_blocks = mddev->resync_max_sectors >> 1;
4640 else
4641 max_blocks = mddev->size;
4642
4643 /*
4644 * Should not happen.
4645 */
4646 if (!max_blocks) {
4647 MD_BUG();
4648 return;
4649 }
NeilBrown4588b422006-03-27 01:18:04 -08004650 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4651 * in a sector_t, and (max_blocks>>scale) will fit in a
4652 * u32, as those are the requirements for sector_div.
4653 * Thus 'scale' must be at least 10
4654 */
4655 scale = 10;
4656 if (sizeof(sector_t) > sizeof(unsigned long)) {
4657 while ( max_blocks/2 > (1ULL<<(scale+32)))
4658 scale++;
4659 }
4660 res = (resync>>scale)*1000;
4661 sector_div(res, (u32)((max_blocks>>scale)+1));
4662
4663 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 {
NeilBrown4588b422006-03-27 01:18:04 -08004665 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 seq_printf(seq, "[");
4667 for (i = 0; i < x; i++)
4668 seq_printf(seq, "=");
4669 seq_printf(seq, ">");
4670 for (i = 0; i < y; i++)
4671 seq_printf(seq, ".");
4672 seq_printf(seq, "] ");
4673 }
NeilBrown4588b422006-03-27 01:18:04 -08004674 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004675 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4676 "reshape" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
NeilBrownccfcc3c2006-03-27 01:18:09 -08004678 "resync" : "recovery")),
NeilBrown4588b422006-03-27 01:18:04 -08004679 per_milli/10, per_milli % 10,
4680 (unsigned long long) resync,
4681 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
4683 /*
4684 * We do not want to overflow, so the order of operands and
4685 * the * 100 / 100 trick are important. We do a +1 to be
4686 * safe against division by zero. We only estimate anyway.
4687 *
4688 * dt: time from mark until now
4689 * db: blocks written from mark until now
4690 * rt: remaining time
4691 */
4692 dt = ((jiffies - mddev->resync_mark) / HZ);
4693 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07004694 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
4695 - mddev->resync_mark_cnt;
4696 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
4698 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4699
NeilBrownff4e8d92006-07-10 04:44:16 -07004700 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701}
4702
4703static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4704{
4705 struct list_head *tmp;
4706 loff_t l = *pos;
4707 mddev_t *mddev;
4708
4709 if (l >= 0x10000)
4710 return NULL;
4711 if (!l--)
4712 /* header */
4713 return (void*)1;
4714
4715 spin_lock(&all_mddevs_lock);
4716 list_for_each(tmp,&all_mddevs)
4717 if (!l--) {
4718 mddev = list_entry(tmp, mddev_t, all_mddevs);
4719 mddev_get(mddev);
4720 spin_unlock(&all_mddevs_lock);
4721 return mddev;
4722 }
4723 spin_unlock(&all_mddevs_lock);
4724 if (!l--)
4725 return (void*)2;/* tail */
4726 return NULL;
4727}
4728
4729static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4730{
4731 struct list_head *tmp;
4732 mddev_t *next_mddev, *mddev = v;
4733
4734 ++*pos;
4735 if (v == (void*)2)
4736 return NULL;
4737
4738 spin_lock(&all_mddevs_lock);
4739 if (v == (void*)1)
4740 tmp = all_mddevs.next;
4741 else
4742 tmp = mddev->all_mddevs.next;
4743 if (tmp != &all_mddevs)
4744 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4745 else {
4746 next_mddev = (void*)2;
4747 *pos = 0x10000;
4748 }
4749 spin_unlock(&all_mddevs_lock);
4750
4751 if (v != (void*)1)
4752 mddev_put(mddev);
4753 return next_mddev;
4754
4755}
4756
4757static void md_seq_stop(struct seq_file *seq, void *v)
4758{
4759 mddev_t *mddev = v;
4760
4761 if (mddev && v != (void*)1 && v != (void*)2)
4762 mddev_put(mddev);
4763}
4764
NeilBrownd7603b72006-01-06 00:20:30 -08004765struct mdstat_info {
4766 int event;
4767};
4768
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769static int md_seq_show(struct seq_file *seq, void *v)
4770{
4771 mddev_t *mddev = v;
4772 sector_t size;
4773 struct list_head *tmp2;
4774 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004775 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004776 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
4778 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004779 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 seq_printf(seq, "Personalities : ");
4781 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004782 list_for_each_entry(pers, &pers_list, list)
4783 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
4785 spin_unlock(&pers_lock);
4786 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004787 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 return 0;
4789 }
4790 if (v == (void*)2) {
4791 status_unused(seq);
4792 return 0;
4793 }
4794
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004795 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004797
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4799 seq_printf(seq, "%s : %sactive", mdname(mddev),
4800 mddev->pers ? "" : "in");
4801 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004802 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004804 if (mddev->ro==2)
4805 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 seq_printf(seq, " %s", mddev->pers->name);
4807 }
4808
4809 size = 0;
4810 ITERATE_RDEV(mddev,rdev,tmp2) {
4811 char b[BDEVNAME_SIZE];
4812 seq_printf(seq, " %s[%d]",
4813 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004814 if (test_bit(WriteMostly, &rdev->flags))
4815 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004816 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 seq_printf(seq, "(F)");
4818 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004819 } else if (rdev->raid_disk < 0)
4820 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 size += rdev->size;
4822 }
4823
4824 if (!list_empty(&mddev->disks)) {
4825 if (mddev->pers)
4826 seq_printf(seq, "\n %llu blocks",
4827 (unsigned long long)mddev->array_size);
4828 else
4829 seq_printf(seq, "\n %llu blocks",
4830 (unsigned long long)size);
4831 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004832 if (mddev->persistent) {
4833 if (mddev->major_version != 0 ||
4834 mddev->minor_version != 90) {
4835 seq_printf(seq," super %d.%d",
4836 mddev->major_version,
4837 mddev->minor_version);
4838 }
4839 } else
4840 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
4842 if (mddev->pers) {
4843 mddev->pers->status (seq, mddev);
4844 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004845 if (mddev->pers->sync_request) {
4846 if (mddev->curr_resync > 2) {
4847 status_resync (seq, mddev);
4848 seq_printf(seq, "\n ");
4849 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4850 seq_printf(seq, "\tresync=DELAYED\n ");
4851 else if (mddev->recovery_cp < MaxSector)
4852 seq_printf(seq, "\tresync=PENDING\n ");
4853 }
NeilBrown32a76272005-06-21 17:17:14 -07004854 } else
4855 seq_printf(seq, "\n ");
4856
4857 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07004858 unsigned long chunk_kb;
4859 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07004860 spin_lock_irqsave(&bitmap->lock, flags);
4861 chunk_kb = bitmap->chunksize >> 10;
4862 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4863 "%lu%s chunk",
4864 bitmap->pages - bitmap->missing_pages,
4865 bitmap->pages,
4866 (bitmap->pages - bitmap->missing_pages)
4867 << (PAGE_SHIFT - 10),
4868 chunk_kb ? chunk_kb : bitmap->chunksize,
4869 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07004870 if (bitmap->file) {
4871 seq_printf(seq, ", file: ");
4872 seq_path(seq, bitmap->file->f_vfsmnt,
4873 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07004874 }
NeilBrown78d742d2005-06-21 17:17:15 -07004875
NeilBrown32a76272005-06-21 17:17:14 -07004876 seq_printf(seq, "\n");
4877 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 }
4879
4880 seq_printf(seq, "\n");
4881 }
4882 mddev_unlock(mddev);
4883
4884 return 0;
4885}
4886
4887static struct seq_operations md_seq_ops = {
4888 .start = md_seq_start,
4889 .next = md_seq_next,
4890 .stop = md_seq_stop,
4891 .show = md_seq_show,
4892};
4893
4894static int md_seq_open(struct inode *inode, struct file *file)
4895{
4896 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08004897 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4898 if (mi == NULL)
4899 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
4901 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08004902 if (error)
4903 kfree(mi);
4904 else {
4905 struct seq_file *p = file->private_data;
4906 p->private = mi;
4907 mi->event = atomic_read(&md_event_count);
4908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 return error;
4910}
4911
NeilBrownd7603b72006-01-06 00:20:30 -08004912static int md_seq_release(struct inode *inode, struct file *file)
4913{
4914 struct seq_file *m = file->private_data;
4915 struct mdstat_info *mi = m->private;
4916 m->private = NULL;
4917 kfree(mi);
4918 return seq_release(inode, file);
4919}
4920
4921static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4922{
4923 struct seq_file *m = filp->private_data;
4924 struct mdstat_info *mi = m->private;
4925 int mask;
4926
4927 poll_wait(filp, &md_event_waiters, wait);
4928
4929 /* always allow read */
4930 mask = POLLIN | POLLRDNORM;
4931
4932 if (mi->event != atomic_read(&md_event_count))
4933 mask |= POLLERR | POLLPRI;
4934 return mask;
4935}
4936
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937static struct file_operations md_seq_fops = {
4938 .open = md_seq_open,
4939 .read = seq_read,
4940 .llseek = seq_lseek,
NeilBrownd7603b72006-01-06 00:20:30 -08004941 .release = md_seq_release,
4942 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943};
4944
NeilBrown2604b702006-01-06 00:20:36 -08004945int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004948 list_add_tail(&p->list, &pers_list);
4949 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 spin_unlock(&pers_lock);
4951 return 0;
4952}
4953
NeilBrown2604b702006-01-06 00:20:36 -08004954int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
NeilBrown2604b702006-01-06 00:20:36 -08004956 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004958 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 spin_unlock(&pers_lock);
4960 return 0;
4961}
4962
4963static int is_mddev_idle(mddev_t *mddev)
4964{
4965 mdk_rdev_t * rdev;
4966 struct list_head *tmp;
4967 int idle;
4968 unsigned long curr_events;
4969
4970 idle = 1;
4971 ITERATE_RDEV(mddev,rdev,tmp) {
4972 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01004973 curr_events = disk_stat_read(disk, sectors[0]) +
4974 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08004976 /* The difference between curr_events and last_events
4977 * will be affected by any new non-sync IO (making
4978 * curr_events bigger) and any difference in the amount of
4979 * in-flight syncio (making current_events bigger or smaller)
4980 * The amount in-flight is currently limited to
4981 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4982 * which is at most 4096 sectors.
4983 * These numbers are fairly fragile and should be made
4984 * more robust, probably by enforcing the
4985 * 'window size' that md_do_sync sort-of uses.
4986 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 * Note: the following is an unsigned comparison.
4988 */
NeilBrownc0e48522005-11-18 01:11:01 -08004989 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 rdev->last_events = curr_events;
4991 idle = 0;
4992 }
4993 }
4994 return idle;
4995}
4996
4997void md_done_sync(mddev_t *mddev, int blocks, int ok)
4998{
4999 /* another "blocks" (512byte) blocks have been synced */
5000 atomic_sub(blocks, &mddev->recovery_active);
5001 wake_up(&mddev->recovery_wait);
5002 if (!ok) {
5003 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5004 md_wakeup_thread(mddev->thread);
5005 // stop recovery, signal do_sync ....
5006 }
5007}
5008
5009
NeilBrown06d91a52005-06-21 17:17:12 -07005010/* md_write_start(mddev, bi)
5011 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005012 * in superblock) before writing, schedule a superblock update
5013 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005014 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005015void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016{
NeilBrown06d91a52005-06-21 17:17:12 -07005017 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005018 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005019
NeilBrownf91de922005-11-08 21:39:36 -08005020 BUG_ON(mddev->ro == 1);
5021 if (mddev->ro == 2) {
5022 /* need to switch to read/write */
5023 mddev->ro = 0;
5024 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5025 md_wakeup_thread(mddev->thread);
5026 }
NeilBrown06d91a52005-06-21 17:17:12 -07005027 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005028 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005029 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005030 if (mddev->in_sync) {
5031 mddev->in_sync = 0;
NeilBrown42543762006-06-26 00:27:57 -07005032 mddev->sb_dirty = 3;
NeilBrown3d310eb2005-06-21 17:17:26 -07005033 md_wakeup_thread(mddev->thread);
5034 }
NeilBrowna9701a32005-11-08 21:39:34 -08005035 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005036 }
NeilBrown3d310eb2005-06-21 17:17:26 -07005037 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038}
5039
5040void md_write_end(mddev_t *mddev)
5041{
5042 if (atomic_dec_and_test(&mddev->writes_pending)) {
5043 if (mddev->safemode == 2)
5044 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005045 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5047 }
5048}
5049
Adrian Bunk75c96f82005-05-05 16:16:09 -07005050static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
5052#define SYNC_MARKS 10
5053#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005054void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055{
5056 mddev_t *mddev2;
5057 unsigned int currspeed = 0,
5058 window;
NeilBrown57afd892005-06-21 17:17:13 -07005059 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 unsigned long mark[SYNC_MARKS];
5061 sector_t mark_cnt[SYNC_MARKS];
5062 int last_mark,m;
5063 struct list_head *tmp;
5064 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005065 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005066 struct list_head *rtmp;
5067 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
5069 /* just incase thread restarts... */
5070 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5071 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005072 if (mddev->ro) /* never try to sync a read-only array */
5073 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
5075 /* we overload curr_resync somewhat here.
5076 * 0 == not engaged in resync at all
5077 * 2 == checking that there is no conflict with another sync
5078 * 1 == like 2, but have yielded to allow conflicting resync to
5079 * commense
5080 * other == active in resync - this many blocks
5081 *
5082 * Before starting a resync we must have set curr_resync to
5083 * 2, and then checked that every "conflicting" array has curr_resync
5084 * less than ours. When we find one that is the same or higher
5085 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5086 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5087 * This will mean we have to start checking from the beginning again.
5088 *
5089 */
5090
5091 do {
5092 mddev->curr_resync = 2;
5093
5094 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005095 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005096 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 goto skip;
5098 }
5099 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 if (mddev2 == mddev)
5101 continue;
5102 if (mddev2->curr_resync &&
5103 match_mddev_units(mddev,mddev2)) {
5104 DEFINE_WAIT(wq);
5105 if (mddev < mddev2 && mddev->curr_resync == 2) {
5106 /* arbitrarily yield */
5107 mddev->curr_resync = 1;
5108 wake_up(&resync_wait);
5109 }
5110 if (mddev > mddev2 && mddev->curr_resync == 1)
5111 /* no need to wait here, we can wait the next
5112 * time 'round when curr_resync == 2
5113 */
5114 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005115 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5116 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005117 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 printk(KERN_INFO "md: delaying resync of %s"
5119 " until %s has finished resync (they"
5120 " share one or more physical units)\n",
5121 mdname(mddev), mdname(mddev2));
5122 mddev_put(mddev2);
5123 schedule();
5124 finish_wait(&resync_wait, &wq);
5125 goto try_again;
5126 }
5127 finish_wait(&resync_wait, &wq);
5128 }
5129 }
5130 } while (mddev->curr_resync < 2);
5131
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005132 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005133 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005135 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 */
5137 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005138 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005139 /* we don't use the checkpoint if there's a bitmap */
5140 if (!mddev->bitmap &&
5141 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5142 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005143 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5144 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005145 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 /* recovery follows the physical size of devices */
5147 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005148 j = MaxSector;
5149 ITERATE_RDEV(mddev,rdev,rtmp)
5150 if (rdev->raid_disk >= 0 &&
5151 !test_bit(Faulty, &rdev->flags) &&
5152 !test_bit(In_sync, &rdev->flags) &&
5153 rdev->recovery_offset < j)
5154 j = rdev->recovery_offset;
5155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156
5157 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
5158 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
NeilBrown88202a02006-01-06 00:21:36 -08005159 " %d KB/sec/disc.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005160 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 "(but not more than %d KB/sec) for reconstruction.\n",
NeilBrown88202a02006-01-06 00:21:36 -08005162 speed_max(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163
5164 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005165
NeilBrown57afd892005-06-21 17:17:13 -07005166 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 for (m = 0; m < SYNC_MARKS; m++) {
5168 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005169 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 }
5171 last_mark = 0;
5172 mddev->resync_mark = mark[last_mark];
5173 mddev->resync_mark_cnt = mark_cnt[last_mark];
5174
5175 /*
5176 * Tune reconstruction:
5177 */
5178 window = 32*(PAGE_SIZE/512);
5179 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5180 window/2,(unsigned long long) max_sectors/2);
5181
5182 atomic_set(&mddev->recovery_active, 0);
5183 init_waitqueue_head(&mddev->recovery_wait);
5184 last_check = 0;
5185
5186 if (j>2) {
5187 printk(KERN_INFO
5188 "md: resuming recovery of %s from checkpoint.\n",
5189 mdname(mddev));
5190 mddev->curr_resync = j;
5191 }
5192
5193 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005194 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
NeilBrown57afd892005-06-21 17:17:13 -07005196 skipped = 0;
5197 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08005198 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005199 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5201 goto out;
5202 }
NeilBrown57afd892005-06-21 17:17:13 -07005203
5204 if (!skipped) { /* actual IO requested */
5205 io_sectors += sectors;
5206 atomic_add(sectors, &mddev->recovery_active);
5207 }
5208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 j += sectors;
5210 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005211 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005212 if (last_check == 0)
5213 /* this is the earliers that rebuilt will be
5214 * visible in /proc/mdstat
5215 */
5216 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005217
5218 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 continue;
5220
NeilBrown57afd892005-06-21 17:17:13 -07005221 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222
5223 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5224 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5225 break;
5226
5227 repeat:
5228 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5229 /* step marks */
5230 int next = (last_mark+1) % SYNC_MARKS;
5231
5232 mddev->resync_mark = mark[next];
5233 mddev->resync_mark_cnt = mark_cnt[next];
5234 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005235 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 last_mark = next;
5237 }
5238
5239
NeilBrown787453c2005-11-08 21:39:43 -08005240 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 /*
5242 * got a signal, exit.
5243 */
5244 printk(KERN_INFO
5245 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5247 goto out;
5248 }
5249
5250 /*
5251 * this loop exits only if either when we are slower than
5252 * the 'hard' speed limit, or the system was IO-idle for
5253 * a jiffy.
5254 * the system might be non-idle CPU-wise, but we only care
5255 * about not overloading the IO subsystem. (things like an
5256 * e2fsck being done on the RAID array should execute fast)
5257 */
5258 mddev->queue->unplug_fn(mddev->queue);
5259 cond_resched();
5260
NeilBrown57afd892005-06-21 17:17:13 -07005261 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5262 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
NeilBrown88202a02006-01-06 00:21:36 -08005264 if (currspeed > speed_min(mddev)) {
5265 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005267 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 goto repeat;
5269 }
5270 }
5271 }
5272 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
5273 /*
5274 * this also signals 'finished resyncing' to md_stop
5275 */
5276 out:
5277 mddev->queue->unplug_fn(mddev->queue);
5278
5279 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5280
5281 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005282 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
5284 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005285 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
5286 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005287 mddev->curr_resync > 2) {
5288 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5289 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5290 if (mddev->curr_resync >= mddev->recovery_cp) {
5291 printk(KERN_INFO
5292 "md: checkpointing recovery of %s.\n",
5293 mdname(mddev));
5294 mddev->recovery_cp = mddev->curr_resync;
5295 }
5296 } else
5297 mddev->recovery_cp = MaxSector;
5298 } else {
5299 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5300 mddev->curr_resync = MaxSector;
5301 ITERATE_RDEV(mddev,rdev,rtmp)
5302 if (rdev->raid_disk >= 0 &&
5303 !test_bit(Faulty, &rdev->flags) &&
5304 !test_bit(In_sync, &rdev->flags) &&
5305 rdev->recovery_offset < mddev->curr_resync)
5306 rdev->recovery_offset = mddev->curr_resync;
5307 mddev->sb_dirty = 1;
5308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 }
5310
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 skip:
5312 mddev->curr_resync = 0;
5313 wake_up(&resync_wait);
5314 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5315 md_wakeup_thread(mddev->thread);
5316}
NeilBrown29269552006-03-27 01:18:10 -08005317EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318
5319
5320/*
5321 * This routine is regularly called by all per-raid-array threads to
5322 * deal with generic issues like resync and super-block update.
5323 * Raid personalities that don't have a thread (linear/raid0) do not
5324 * need this as they never do any recovery or update the superblock.
5325 *
5326 * It does not do any resync itself, but rather "forks" off other threads
5327 * to do that as needed.
5328 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5329 * "->recovery" and create a thread at ->sync_thread.
5330 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5331 * and wakeups up this thread which will reap the thread and finish up.
5332 * This thread also removes any faulty devices (with nr_pending == 0).
5333 *
5334 * The overall approach is:
5335 * 1/ if the superblock needs updating, update it.
5336 * 2/ If a recovery thread is running, don't do anything else.
5337 * 3/ If recovery has finished, clean up, possibly marking spares active.
5338 * 4/ If there are any faulty devices, remove them.
5339 * 5/ If array is degraded, try to add spares devices
5340 * 6/ If array has spares or is not in-sync, start a resync thread.
5341 */
5342void md_check_recovery(mddev_t *mddev)
5343{
5344 mdk_rdev_t *rdev;
5345 struct list_head *rtmp;
5346
5347
NeilBrown5f404022005-06-21 17:17:16 -07005348 if (mddev->bitmap)
5349 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
5351 if (mddev->ro)
5352 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005353
5354 if (signal_pending(current)) {
5355 if (mddev->pers->sync_request) {
5356 printk(KERN_INFO "md: %s in immediate safe mode\n",
5357 mdname(mddev));
5358 mddev->safemode = 2;
5359 }
5360 flush_signals(current);
5361 }
5362
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 if ( ! (
5364 mddev->sb_dirty ||
5365 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005366 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5367 (mddev->safemode == 1) ||
5368 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5369 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 ))
5371 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005372
NeilBrowndf5b89b2006-03-27 01:18:20 -08005373 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07005375
NeilBrowna9701a32005-11-08 21:39:34 -08005376 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005377 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5378 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5379 mddev->in_sync = 1;
NeilBrown42543762006-06-26 00:27:57 -07005380 mddev->sb_dirty = 3;
NeilBrownfca4d842005-06-21 17:17:11 -07005381 }
5382 if (mddev->safemode == 1)
5383 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005384 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005385
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 if (mddev->sb_dirty)
5387 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07005388
NeilBrown06d91a52005-06-21 17:17:12 -07005389
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5391 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5392 /* resync/recovery still happening */
5393 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5394 goto unlock;
5395 }
5396 if (mddev->sync_thread) {
5397 /* resync has finished, collect result */
5398 md_unregister_thread(mddev->sync_thread);
5399 mddev->sync_thread = NULL;
5400 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5401 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5402 /* success...*/
5403 /* activate any spares */
5404 mddev->pers->spare_active(mddev);
5405 }
5406 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07005407
5408 /* if array is no-longer degraded, then any saved_raid_disk
5409 * information must be scrapped
5410 */
5411 if (!mddev->degraded)
5412 ITERATE_RDEV(mddev,rdev,rtmp)
5413 rdev->saved_raid_disk = -1;
5414
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 mddev->recovery = 0;
5416 /* flag recovery needed just to double check */
5417 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005418 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 goto unlock;
5420 }
NeilBrown24dd4692005-11-08 21:39:26 -08005421 /* Clear some bits that don't mean anything, but
5422 * might be left set
5423 */
5424 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5425 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5426 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5427 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005429 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5430 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 /* no recovery is running.
5432 * remove any failed drives, then
5433 * add spares if possible.
5434 * Spare are also removed and re-added, to allow
5435 * the personality to fail the re-add.
5436 */
5437 ITERATE_RDEV(mddev,rdev,rtmp)
5438 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005439 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08005441 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
5442 char nm[20];
5443 sprintf(nm,"rd%d", rdev->raid_disk);
5444 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08005446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 }
5448
5449 if (mddev->degraded) {
5450 ITERATE_RDEV(mddev,rdev,rtmp)
5451 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08005452 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005453 rdev->recovery_offset = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08005454 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5455 char nm[20];
5456 sprintf(nm, "rd%d", rdev->raid_disk);
5457 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 spares++;
NeilBrownd7603b72006-01-06 00:20:30 -08005459 md_new_event(mddev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08005460 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 break;
5462 }
5463 }
5464
NeilBrown24dd4692005-11-08 21:39:26 -08005465 if (spares) {
5466 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5467 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5468 } else if (mddev->recovery_cp < MaxSector) {
5469 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5470 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5471 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005473
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 if (mddev->pers->sync_request) {
5475 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005476 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5477 /* We are adding a device or devices to an array
5478 * which has the bitmap stored on all devices.
5479 * So make sure all bitmap pages get written
5480 */
5481 bitmap_write_all(mddev->bitmap);
5482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 mddev->sync_thread = md_register_thread(md_do_sync,
5484 mddev,
5485 "%s_resync");
5486 if (!mddev->sync_thread) {
5487 printk(KERN_ERR "%s: could not start resync"
5488 " thread...\n",
5489 mdname(mddev));
5490 /* leave the spares where they are, it shouldn't hurt */
5491 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005492 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005494 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 }
5496 unlock:
5497 mddev_unlock(mddev);
5498 }
5499}
5500
Adrian Bunk75c96f82005-05-05 16:16:09 -07005501static int md_notify_reboot(struct notifier_block *this,
5502 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503{
5504 struct list_head *tmp;
5505 mddev_t *mddev;
5506
5507 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5508
5509 printk(KERN_INFO "md: stopping all md devices.\n");
5510
5511 ITERATE_MDDEV(mddev,tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005512 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005514 mddev_unlock(mddev);
5515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 /*
5517 * certain more exotic SCSI devices are known to be
5518 * volatile wrt too early system reboots. While the
5519 * right place to handle this issue is the given
5520 * driver, we do want to have a safe RAID driver ...
5521 */
5522 mdelay(1000*1);
5523 }
5524 return NOTIFY_DONE;
5525}
5526
Adrian Bunk75c96f82005-05-05 16:16:09 -07005527static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 .notifier_call = md_notify_reboot,
5529 .next = NULL,
5530 .priority = INT_MAX, /* before any real devices */
5531};
5532
5533static void md_geninit(void)
5534{
5535 struct proc_dir_entry *p;
5536
5537 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5538
5539 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5540 if (p)
5541 p->proc_fops = &md_seq_fops;
5542}
5543
Adrian Bunk75c96f82005-05-05 16:16:09 -07005544static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5547 " MD_SB_DISKS=%d\n",
5548 MD_MAJOR_VERSION, MD_MINOR_VERSION,
5549 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08005550 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07005551 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
5553 if (register_blkdev(MAJOR_NR, "md"))
5554 return -1;
5555 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5556 unregister_blkdev(MAJOR_NR, "md");
5557 return -1;
5558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
5560 md_probe, NULL, NULL);
5561 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
5562 md_probe, NULL, NULL);
5563
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 register_reboot_notifier(&md_notifier);
5565 raid_table_header = register_sysctl_table(raid_root_table, 1);
5566
5567 md_geninit();
5568 return (0);
5569}
5570
5571
5572#ifndef MODULE
5573
5574/*
5575 * Searches all registered partitions for autorun RAID arrays
5576 * at boot time.
5577 */
5578static dev_t detected_devices[128];
5579static int dev_cnt;
5580
5581void md_autodetect_dev(dev_t dev)
5582{
5583 if (dev_cnt >= 0 && dev_cnt < 127)
5584 detected_devices[dev_cnt++] = dev;
5585}
5586
5587
5588static void autostart_arrays(int part)
5589{
5590 mdk_rdev_t *rdev;
5591 int i;
5592
5593 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5594
5595 for (i = 0; i < dev_cnt; i++) {
5596 dev_t dev = detected_devices[i];
5597
5598 rdev = md_import_device(dev,0, 0);
5599 if (IS_ERR(rdev))
5600 continue;
5601
NeilBrownb2d444d2005-11-08 21:39:31 -08005602 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 MD_BUG();
5604 continue;
5605 }
5606 list_add(&rdev->same_set, &pending_raid_disks);
5607 }
5608 dev_cnt = 0;
5609
5610 autorun_devices(part);
5611}
5612
5613#endif
5614
5615static __exit void md_exit(void)
5616{
5617 mddev_t *mddev;
5618 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07005619
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
5621 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
5623 unregister_blkdev(MAJOR_NR,"md");
5624 unregister_blkdev(mdp_major, "mdp");
5625 unregister_reboot_notifier(&md_notifier);
5626 unregister_sysctl_table(raid_table_header);
5627 remove_proc_entry("mdstat", NULL);
5628 ITERATE_MDDEV(mddev,tmp) {
5629 struct gendisk *disk = mddev->gendisk;
5630 if (!disk)
5631 continue;
5632 export_array(mddev);
5633 del_gendisk(disk);
5634 put_disk(disk);
5635 mddev->gendisk = NULL;
5636 mddev_put(mddev);
5637 }
5638}
5639
5640module_init(md_init)
5641module_exit(md_exit)
5642
NeilBrownf91de922005-11-08 21:39:36 -08005643static int get_ro(char *buffer, struct kernel_param *kp)
5644{
5645 return sprintf(buffer, "%d", start_readonly);
5646}
5647static int set_ro(const char *val, struct kernel_param *kp)
5648{
5649 char *e;
5650 int num = simple_strtoul(val, &e, 10);
5651 if (*val && (*e == '\0' || *e == '\n')) {
5652 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005653 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005654 }
5655 return -EINVAL;
5656}
5657
NeilBrown80ca3a42006-07-10 04:44:18 -07005658module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
5659module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005660
NeilBrownf91de922005-11-08 21:39:36 -08005661
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662EXPORT_SYMBOL(register_md_personality);
5663EXPORT_SYMBOL(unregister_md_personality);
5664EXPORT_SYMBOL(md_error);
5665EXPORT_SYMBOL(md_done_sync);
5666EXPORT_SYMBOL(md_write_start);
5667EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668EXPORT_SYMBOL(md_register_thread);
5669EXPORT_SYMBOL(md_unregister_thread);
5670EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671EXPORT_SYMBOL(md_check_recovery);
5672MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005673MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005674MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);