blob: 50ab4a936e30a3e405e314f648049c518446e018 [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
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700392 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
393 printk("md: super_written gets error=%d, uptodate=%d\n",
394 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
395 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800396 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700397 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700398
NeilBrowna9701a32005-11-08 21:39:34 -0800399 if (atomic_dec_and_test(&mddev->pending_writes))
400 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700401 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700402 return 0;
403}
404
NeilBrowna9701a32005-11-08 21:39:34 -0800405static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
406{
407 struct bio *bio2 = bio->bi_private;
408 mdk_rdev_t *rdev = bio2->bi_private;
409 mddev_t *mddev = rdev->mddev;
410 if (bio->bi_size)
411 return 1;
412
413 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
414 error == -EOPNOTSUPP) {
415 unsigned long flags;
416 /* barriers don't appear to be supported :-( */
417 set_bit(BarriersNotsupp, &rdev->flags);
418 mddev->barriers_work = 0;
419 spin_lock_irqsave(&mddev->write_lock, flags);
420 bio2->bi_next = mddev->biolist;
421 mddev->biolist = bio2;
422 spin_unlock_irqrestore(&mddev->write_lock, flags);
423 wake_up(&mddev->sb_wait);
424 bio_put(bio);
425 return 0;
426 }
427 bio_put(bio2);
428 bio->bi_private = rdev;
429 return super_written(bio, bytes_done, error);
430}
431
NeilBrown7bfa19f2005-06-21 17:17:28 -0700432void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
433 sector_t sector, int size, struct page *page)
434{
435 /* write first size bytes of page to sector of rdev
436 * Increment mddev->pending_writes before returning
437 * and decrement it on completion, waking up sb_wait
438 * if zero is reached.
439 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800440 *
441 * As we might need to resubmit the request if BIO_RW_BARRIER
442 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700443 */
444 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800445 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700446
447 bio->bi_bdev = rdev->bdev;
448 bio->bi_sector = sector;
449 bio_add_page(bio, page, size, 0);
450 bio->bi_private = rdev;
451 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800452 bio->bi_rw = rw;
453
NeilBrown7bfa19f2005-06-21 17:17:28 -0700454 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800455 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
456 struct bio *rbio;
457 rw |= (1<<BIO_RW_BARRIER);
458 rbio = bio_clone(bio, GFP_NOIO);
459 rbio->bi_private = bio;
460 rbio->bi_end_io = super_written_barrier;
461 submit_bio(rw, rbio);
462 } else
463 submit_bio(rw, bio);
464}
465
466void md_super_wait(mddev_t *mddev)
467{
468 /* wait for all superblock writes that were scheduled to complete.
469 * if any had to be retried (due to BARRIER problems), retry them
470 */
471 DEFINE_WAIT(wq);
472 for(;;) {
473 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
474 if (atomic_read(&mddev->pending_writes)==0)
475 break;
476 while (mddev->biolist) {
477 struct bio *bio;
478 spin_lock_irq(&mddev->write_lock);
479 bio = mddev->biolist;
480 mddev->biolist = bio->bi_next ;
481 bio->bi_next = NULL;
482 spin_unlock_irq(&mddev->write_lock);
483 submit_bio(bio->bi_rw, bio);
484 }
485 schedule();
486 }
487 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
491{
492 if (bio->bi_size)
493 return 1;
494
495 complete((struct completion*)bio->bi_private);
496 return 0;
497}
498
NeilBrowna654b9d82005-06-21 17:17:27 -0700499int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 struct page *page, int rw)
501{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700502 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct completion event;
504 int ret;
505
506 rw |= (1 << BIO_RW_SYNC);
507
508 bio->bi_bdev = bdev;
509 bio->bi_sector = sector;
510 bio_add_page(bio, page, size, 0);
511 init_completion(&event);
512 bio->bi_private = &event;
513 bio->bi_end_io = bi_complete;
514 submit_bio(rw, bio);
515 wait_for_completion(&event);
516
517 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
518 bio_put(bio);
519 return ret;
520}
NeilBrowna8745db2006-01-06 00:20:34 -0800521EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
NeilBrown0002b272005-09-09 16:23:53 -0700523static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 char b[BDEVNAME_SIZE];
526 if (!rdev->sb_page) {
527 MD_BUG();
528 return -EINVAL;
529 }
530 if (rdev->sb_loaded)
531 return 0;
532
533
NeilBrown0002b272005-09-09 16:23:53 -0700534 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto fail;
536 rdev->sb_loaded = 1;
537 return 0;
538
539fail:
540 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
541 bdevname(rdev->bdev,b));
542 return -EINVAL;
543}
544
545static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
546{
547 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
548 (sb1->set_uuid1 == sb2->set_uuid1) &&
549 (sb1->set_uuid2 == sb2->set_uuid2) &&
550 (sb1->set_uuid3 == sb2->set_uuid3))
551
552 return 1;
553
554 return 0;
555}
556
557
558static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
559{
560 int ret;
561 mdp_super_t *tmp1, *tmp2;
562
563 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
564 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
565
566 if (!tmp1 || !tmp2) {
567 ret = 0;
568 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
569 goto abort;
570 }
571
572 *tmp1 = *sb1;
573 *tmp2 = *sb2;
574
575 /*
576 * nr_disks is not constant
577 */
578 tmp1->nr_disks = 0;
579 tmp2->nr_disks = 0;
580
581 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
582 ret = 0;
583 else
584 ret = 1;
585
586abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700587 kfree(tmp1);
588 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return ret;
590}
591
592static unsigned int calc_sb_csum(mdp_super_t * sb)
593{
594 unsigned int disk_csum, csum;
595
596 disk_csum = sb->sb_csum;
597 sb->sb_csum = 0;
598 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
599 sb->sb_csum = disk_csum;
600 return csum;
601}
602
603
604/*
605 * Handle superblock details.
606 * We want to be able to handle multiple superblock formats
607 * so we have a common interface to them all, and an array of
608 * different handlers.
609 * We rely on user-space to write the initial superblock, and support
610 * reading and updating of superblocks.
611 * Interface methods are:
612 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
613 * loads and validates a superblock on dev.
614 * if refdev != NULL, compare superblocks on both devices
615 * Return:
616 * 0 - dev has a superblock that is compatible with refdev
617 * 1 - dev has a superblock that is compatible and newer than refdev
618 * so dev should be used as the refdev in future
619 * -EINVAL superblock incompatible or invalid
620 * -othererror e.g. -EIO
621 *
622 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
623 * Verify that dev is acceptable into mddev.
624 * The first time, mddev->raid_disks will be 0, and data from
625 * dev should be merged in. Subsequent calls check that dev
626 * is new enough. Return 0 or -EINVAL
627 *
628 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
629 * Update the superblock for rdev with data in mddev
630 * This does not write to disc.
631 *
632 */
633
634struct super_type {
635 char *name;
636 struct module *owner;
637 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
638 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
639 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
640};
641
642/*
643 * load_super for 0.90.0
644 */
645static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
646{
647 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
648 mdp_super_t *sb;
649 int ret;
650 sector_t sb_offset;
651
652 /*
653 * Calculate the position of the superblock,
654 * it's at the end of the disk.
655 *
656 * It also happens to be a multiple of 4Kb.
657 */
658 sb_offset = calc_dev_sboffset(rdev->bdev);
659 rdev->sb_offset = sb_offset;
660
NeilBrown0002b272005-09-09 16:23:53 -0700661 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (ret) return ret;
663
664 ret = -EINVAL;
665
666 bdevname(rdev->bdev, b);
667 sb = (mdp_super_t*)page_address(rdev->sb_page);
668
669 if (sb->md_magic != MD_SB_MAGIC) {
670 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
671 b);
672 goto abort;
673 }
674
675 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800676 sb->minor_version < 90 ||
677 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
679 sb->major_version, sb->minor_version,
680 b);
681 goto abort;
682 }
683
684 if (sb->raid_disks <= 0)
685 goto abort;
686
687 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
688 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
689 b);
690 goto abort;
691 }
692
693 rdev->preferred_minor = sb->md_minor;
694 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700695 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (sb->level == LEVEL_MULTIPATH)
698 rdev->desc_nr = -1;
699 else
700 rdev->desc_nr = sb->this_disk.number;
701
702 if (refdev == 0)
703 ret = 1;
704 else {
705 __u64 ev1, ev2;
706 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
707 if (!uuid_equal(refsb, sb)) {
708 printk(KERN_WARNING "md: %s has different UUID to %s\n",
709 b, bdevname(refdev->bdev,b2));
710 goto abort;
711 }
712 if (!sb_equal(refsb, sb)) {
713 printk(KERN_WARNING "md: %s has same UUID"
714 " but different superblock to %s\n",
715 b, bdevname(refdev->bdev, b2));
716 goto abort;
717 }
718 ev1 = md_event(sb);
719 ev2 = md_event(refsb);
720 if (ev1 > ev2)
721 ret = 1;
722 else
723 ret = 0;
724 }
725 rdev->size = calc_dev_size(rdev, sb->chunk_size);
726
NeilBrown2bf071b2006-01-06 00:20:55 -0800727 if (rdev->size < sb->size && sb->level > 1)
728 /* "this cannot possibly happen" ... */
729 ret = -EINVAL;
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 abort:
732 return ret;
733}
734
735/*
736 * validate_super for 0.90.0
737 */
738static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
739{
740 mdp_disk_t *desc;
741 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700742 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
NeilBrown41158c72005-06-21 17:17:25 -0700744 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800745 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (mddev->raid_disks == 0) {
747 mddev->major_version = 0;
748 mddev->minor_version = sb->minor_version;
749 mddev->patch_version = sb->patch_version;
750 mddev->persistent = ! sb->not_persistent;
751 mddev->chunk_size = sb->chunk_size;
752 mddev->ctime = sb->ctime;
753 mddev->utime = sb->utime;
754 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800755 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 mddev->layout = sb->layout;
757 mddev->raid_disks = sb->raid_disks;
758 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700759 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700760 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700761 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
NeilBrownf6705572006-03-27 01:18:11 -0800763 if (mddev->minor_version >= 91) {
764 mddev->reshape_position = sb->reshape_position;
765 mddev->delta_disks = sb->delta_disks;
766 mddev->new_level = sb->new_level;
767 mddev->new_layout = sb->new_layout;
768 mddev->new_chunk = sb->new_chunk;
769 } else {
770 mddev->reshape_position = MaxSector;
771 mddev->delta_disks = 0;
772 mddev->new_level = mddev->level;
773 mddev->new_layout = mddev->layout;
774 mddev->new_chunk = mddev->chunk_size;
775 }
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (sb->state & (1<<MD_SB_CLEAN))
778 mddev->recovery_cp = MaxSector;
779 else {
780 if (sb->events_hi == sb->cp_events_hi &&
781 sb->events_lo == sb->cp_events_lo) {
782 mddev->recovery_cp = sb->recovery_cp;
783 } else
784 mddev->recovery_cp = 0;
785 }
786
787 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
788 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
789 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
790 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
791
792 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700793
794 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
795 mddev->bitmap_file == NULL) {
NeilBrownc5a10f62006-03-27 01:18:03 -0800796 if (mddev->level != 1 && mddev->level != 4
797 && mddev->level != 5 && mddev->level != 6
NeilBrown6cce3b22006-01-06 00:20:16 -0800798 && mddev->level != 10) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700799 /* FIXME use a better test */
NeilBrown6cce3b22006-01-06 00:20:16 -0800800 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -0700801 return -EINVAL;
802 }
NeilBrown36fa3062005-09-09 16:23:45 -0700803 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700804 }
805
NeilBrown41158c72005-06-21 17:17:25 -0700806 } else if (mddev->pers == NULL) {
807 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ++ev1;
809 if (ev1 < mddev->events)
810 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700811 } else if (mddev->bitmap) {
812 /* if adding to array with a bitmap, then we can accept an
813 * older device ... but not too old.
814 */
NeilBrown41158c72005-06-21 17:17:25 -0700815 if (ev1 < mddev->bitmap->events_cleared)
816 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700817 } else {
818 if (ev1 < mddev->events)
819 /* just a hot-add of a new device, leave raid_disk at -1 */
820 return 0;
821 }
NeilBrown41158c72005-06-21 17:17:25 -0700822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 desc = sb->disks + rdev->desc_nr;
825
826 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800827 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700828 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
829 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800830 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 rdev->raid_disk = desc->raid_disk;
832 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700833 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
834 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700835 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800836 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return 0;
838}
839
840/*
841 * sync_super for 0.90.0
842 */
843static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
844{
845 mdp_super_t *sb;
846 struct list_head *tmp;
847 mdk_rdev_t *rdev2;
848 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /* make rdev->sb match mddev data..
852 *
853 * 1/ zero out disks
854 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
855 * 3/ any empty disks < next_spare become removed
856 *
857 * disks[0] gets initialised to REMOVED because
858 * we cannot be sure from other fields if it has
859 * been initialised or not.
860 */
861 int i;
862 int active=0, working=0,failed=0,spare=0,nr_disks=0;
863
NeilBrown61181562005-09-09 16:24:02 -0700864 rdev->sb_size = MD_SB_BYTES;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 sb = (mdp_super_t*)page_address(rdev->sb_page);
867
868 memset(sb, 0, sizeof(*sb));
869
870 sb->md_magic = MD_SB_MAGIC;
871 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 sb->patch_version = mddev->patch_version;
873 sb->gvalid_words = 0; /* ignored */
874 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
875 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
876 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
877 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
878
879 sb->ctime = mddev->ctime;
880 sb->level = mddev->level;
881 sb->size = mddev->size;
882 sb->raid_disks = mddev->raid_disks;
883 sb->md_minor = mddev->md_minor;
884 sb->not_persistent = !mddev->persistent;
885 sb->utime = mddev->utime;
886 sb->state = 0;
887 sb->events_hi = (mddev->events>>32);
888 sb->events_lo = (u32)mddev->events;
889
NeilBrownf6705572006-03-27 01:18:11 -0800890 if (mddev->reshape_position == MaxSector)
891 sb->minor_version = 90;
892 else {
893 sb->minor_version = 91;
894 sb->reshape_position = mddev->reshape_position;
895 sb->new_level = mddev->new_level;
896 sb->delta_disks = mddev->delta_disks;
897 sb->new_layout = mddev->new_layout;
898 sb->new_chunk = mddev->new_chunk;
899 }
900 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (mddev->in_sync)
902 {
903 sb->recovery_cp = mddev->recovery_cp;
904 sb->cp_events_hi = (mddev->events>>32);
905 sb->cp_events_lo = (u32)mddev->events;
906 if (mddev->recovery_cp == MaxSector)
907 sb->state = (1<< MD_SB_CLEAN);
908 } else
909 sb->recovery_cp = 0;
910
911 sb->layout = mddev->layout;
912 sb->chunk_size = mddev->chunk_size;
913
NeilBrowna654b9d82005-06-21 17:17:27 -0700914 if (mddev->bitmap && mddev->bitmap_file == NULL)
915 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 sb->disks[0].state = (1<<MD_DISK_REMOVED);
918 ITERATE_RDEV(mddev,rdev2,tmp) {
919 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800920 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800921 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
922 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800923 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800925 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800926 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 d = &sb->disks[rdev2->desc_nr];
928 nr_disks++;
929 d->number = rdev2->desc_nr;
930 d->major = MAJOR(rdev2->bdev->bd_dev);
931 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800932 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
933 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 d->raid_disk = rdev2->raid_disk;
935 else
936 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800937 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800939 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 d->state = (1<<MD_DISK_ACTIVE);
941 d->state |= (1<<MD_DISK_SYNC);
942 active++;
943 working++;
944 } else {
945 d->state = 0;
946 spare++;
947 working++;
948 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700949 if (test_bit(WriteMostly, &rdev2->flags))
950 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* now set the "removed" and "faulty" bits on any missing devices */
953 for (i=0 ; i < mddev->raid_disks ; i++) {
954 mdp_disk_t *d = &sb->disks[i];
955 if (d->state == 0 && d->number == 0) {
956 d->number = i;
957 d->raid_disk = i;
958 d->state = (1<<MD_DISK_REMOVED);
959 d->state |= (1<<MD_DISK_FAULTY);
960 failed++;
961 }
962 }
963 sb->nr_disks = nr_disks;
964 sb->active_disks = active;
965 sb->working_disks = working;
966 sb->failed_disks = failed;
967 sb->spare_disks = spare;
968
969 sb->this_disk = sb->disks[rdev->desc_nr];
970 sb->sb_csum = calc_sb_csum(sb);
971}
972
973/*
974 * version 1 superblock
975 */
976
NeilBrown1c05b4b2006-10-21 10:24:08 -0700977static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
NeilBrown1c05b4b2006-10-21 10:24:08 -0700979 __le32 disk_csum;
980 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 unsigned long long newcsum;
982 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -0700983 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 int i;
985
986 disk_csum = sb->sb_csum;
987 sb->sb_csum = 0;
988 newcsum = 0;
989 for (i=0; size>=4; size -= 4 )
990 newcsum += le32_to_cpu(*isuper++);
991
992 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -0700993 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
996 sb->sb_csum = disk_csum;
997 return cpu_to_le32(csum);
998}
999
1000static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1001{
1002 struct mdp_superblock_1 *sb;
1003 int ret;
1004 sector_t sb_offset;
1005 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001006 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 /*
1009 * Calculate the position of the superblock.
1010 * It is always aligned to a 4K boundary and
1011 * depeding on minor_version, it can be:
1012 * 0: At least 8K, but less than 12K, from end of device
1013 * 1: At start of device
1014 * 2: 4K from start of device.
1015 */
1016 switch(minor_version) {
1017 case 0:
1018 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1019 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001020 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /* convert from sectors to K */
1022 sb_offset /= 2;
1023 break;
1024 case 1:
1025 sb_offset = 0;
1026 break;
1027 case 2:
1028 sb_offset = 4;
1029 break;
1030 default:
1031 return -EINVAL;
1032 }
1033 rdev->sb_offset = sb_offset;
1034
NeilBrown0002b272005-09-09 16:23:53 -07001035 /* superblock is rarely larger than 1K, but it can be larger,
1036 * and it is safe to read 4k, so we do that
1037 */
1038 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (ret) return ret;
1040
1041
1042 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1043
1044 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1045 sb->major_version != cpu_to_le32(1) ||
1046 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1047 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001048 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -EINVAL;
1050
1051 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1052 printk("md: invalid superblock checksum on %s\n",
1053 bdevname(rdev->bdev,b));
1054 return -EINVAL;
1055 }
1056 if (le64_to_cpu(sb->data_size) < 10) {
1057 printk("md: data_size too small on %s\n",
1058 bdevname(rdev->bdev,b));
1059 return -EINVAL;
1060 }
1061 rdev->preferred_minor = 0xffff;
1062 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001063 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
NeilBrown0002b272005-09-09 16:23:53 -07001065 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001066 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001067 if (rdev->sb_size & bmask)
1068 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1069
NeilBrown31b65a02006-07-10 04:44:14 -07001070 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1071 rdev->desc_nr = -1;
1072 else
1073 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (refdev == 0)
NeilBrown8ed75462006-02-03 03:03:41 -08001076 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 else {
1078 __u64 ev1, ev2;
1079 struct mdp_superblock_1 *refsb =
1080 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1081
1082 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1083 sb->level != refsb->level ||
1084 sb->layout != refsb->layout ||
1085 sb->chunksize != refsb->chunksize) {
1086 printk(KERN_WARNING "md: %s has strangely different"
1087 " superblock to %s\n",
1088 bdevname(rdev->bdev,b),
1089 bdevname(refdev->bdev,b2));
1090 return -EINVAL;
1091 }
1092 ev1 = le64_to_cpu(sb->events);
1093 ev2 = le64_to_cpu(refsb->events);
1094
1095 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001096 ret = 1;
1097 else
1098 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 if (minor_version)
1101 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1102 else
1103 rdev->size = rdev->sb_offset;
1104 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1105 return -EINVAL;
1106 rdev->size = le64_to_cpu(sb->data_size)/2;
1107 if (le32_to_cpu(sb->chunksize))
1108 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001109
NeilBrown1c05b4b2006-10-21 10:24:08 -07001110 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001111 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001112 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1116{
1117 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001118 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
NeilBrown41158c72005-06-21 17:17:25 -07001120 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001121 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (mddev->raid_disks == 0) {
1123 mddev->major_version = 1;
1124 mddev->patch_version = 0;
1125 mddev->persistent = 1;
1126 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1127 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1128 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1129 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001130 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 mddev->layout = le32_to_cpu(sb->layout);
1132 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1133 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001134 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001135 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001136 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1139 memcpy(mddev->uuid, sb->set_uuid, 16);
1140
1141 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001142
NeilBrown71c08052005-09-09 16:23:51 -07001143 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -07001144 mddev->bitmap_file == NULL ) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001145 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1146 && mddev->level != 10) {
1147 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -07001148 return -EINVAL;
1149 }
1150 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1151 }
NeilBrownf6705572006-03-27 01:18:11 -08001152 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1153 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1154 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1155 mddev->new_level = le32_to_cpu(sb->new_level);
1156 mddev->new_layout = le32_to_cpu(sb->new_layout);
1157 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1158 } else {
1159 mddev->reshape_position = MaxSector;
1160 mddev->delta_disks = 0;
1161 mddev->new_level = mddev->level;
1162 mddev->new_layout = mddev->layout;
1163 mddev->new_chunk = mddev->chunk_size;
1164 }
1165
NeilBrown41158c72005-06-21 17:17:25 -07001166 } else if (mddev->pers == NULL) {
1167 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ++ev1;
1169 if (ev1 < mddev->events)
1170 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001171 } else if (mddev->bitmap) {
1172 /* If adding to array with a bitmap, then we can accept an
1173 * older device, but not too old.
1174 */
NeilBrown41158c72005-06-21 17:17:25 -07001175 if (ev1 < mddev->bitmap->events_cleared)
1176 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001177 } else {
1178 if (ev1 < mddev->events)
1179 /* just a hot-add of a new device, leave raid_disk at -1 */
1180 return 0;
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (mddev->level != LEVEL_MULTIPATH) {
1183 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1185 switch(role) {
1186 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 break;
1188 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001189 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 break;
1191 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001192 if ((le32_to_cpu(sb->feature_map) &
1193 MD_FEATURE_RECOVERY_OFFSET))
1194 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1195 else
1196 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 rdev->raid_disk = role;
1198 break;
1199 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001200 if (sb->devflags & WriteMostly1)
1201 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001202 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001203 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return 0;
1206}
1207
1208static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1209{
1210 struct mdp_superblock_1 *sb;
1211 struct list_head *tmp;
1212 mdk_rdev_t *rdev2;
1213 int max_dev, i;
1214 /* make rdev->sb match mddev and rdev data. */
1215
1216 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1217
1218 sb->feature_map = 0;
1219 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001220 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 memset(sb->pad1, 0, sizeof(sb->pad1));
1222 memset(sb->pad2, 0, sizeof(sb->pad2));
1223 memset(sb->pad3, 0, sizeof(sb->pad3));
1224
1225 sb->utime = cpu_to_le64((__u64)mddev->utime);
1226 sb->events = cpu_to_le64(mddev->events);
1227 if (mddev->in_sync)
1228 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1229 else
1230 sb->resync_offset = cpu_to_le64(0);
1231
NeilBrown1c05b4b2006-10-21 10:24:08 -07001232 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001233
NeilBrownf0ca3402006-02-02 14:28:04 -08001234 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001235 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001236
NeilBrowna654b9d82005-06-21 17:17:27 -07001237 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1238 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001239 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001240 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001241
1242 if (rdev->raid_disk >= 0 &&
1243 !test_bit(In_sync, &rdev->flags) &&
1244 rdev->recovery_offset > 0) {
1245 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1246 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1247 }
1248
NeilBrownf6705572006-03-27 01:18:11 -08001249 if (mddev->reshape_position != MaxSector) {
1250 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1251 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1252 sb->new_layout = cpu_to_le32(mddev->new_layout);
1253 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1254 sb->new_level = cpu_to_le32(mddev->new_level);
1255 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1256 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 max_dev = 0;
1259 ITERATE_RDEV(mddev,rdev2,tmp)
1260 if (rdev2->desc_nr+1 > max_dev)
1261 max_dev = rdev2->desc_nr+1;
1262
1263 sb->max_dev = cpu_to_le32(max_dev);
1264 for (i=0; i<max_dev;i++)
1265 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1266
1267 ITERATE_RDEV(mddev,rdev2,tmp) {
1268 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001269 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001271 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001273 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1274 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 else
1276 sb->dev_roles[i] = cpu_to_le16(0xffff);
1277 }
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 sb->sb_csum = calc_sb_1_csum(sb);
1280}
1281
1282
Adrian Bunk75c96f82005-05-05 16:16:09 -07001283static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 [0] = {
1285 .name = "0.90.0",
1286 .owner = THIS_MODULE,
1287 .load_super = super_90_load,
1288 .validate_super = super_90_validate,
1289 .sync_super = super_90_sync,
1290 },
1291 [1] = {
1292 .name = "md-1",
1293 .owner = THIS_MODULE,
1294 .load_super = super_1_load,
1295 .validate_super = super_1_validate,
1296 .sync_super = super_1_sync,
1297 },
1298};
1299
1300static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1301{
1302 struct list_head *tmp;
1303 mdk_rdev_t *rdev;
1304
1305 ITERATE_RDEV(mddev,rdev,tmp)
1306 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1307 return rdev;
1308
1309 return NULL;
1310}
1311
1312static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1313{
1314 struct list_head *tmp;
1315 mdk_rdev_t *rdev;
1316
1317 ITERATE_RDEV(mddev1,rdev,tmp)
1318 if (match_dev_unit(mddev2, rdev))
1319 return 1;
1320
1321 return 0;
1322}
1323
1324static LIST_HEAD(pending_raid_disks);
1325
1326static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1327{
1328 mdk_rdev_t *same_pdev;
1329 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001330 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001331 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if (rdev->mddev) {
1334 MD_BUG();
1335 return -EINVAL;
1336 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001337 /* make sure rdev->size exceeds mddev->size */
1338 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1339 if (mddev->pers)
1340 /* Cannot change size, so fail */
1341 return -ENOSPC;
1342 else
1343 mddev->size = rdev->size;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 same_pdev = match_dev_unit(mddev, rdev);
1346 if (same_pdev)
1347 printk(KERN_WARNING
1348 "%s: WARNING: %s appears to be on the same physical"
1349 " disk as %s. True\n protection against single-disk"
1350 " failure might be compromised.\n",
1351 mdname(mddev), bdevname(rdev->bdev,b),
1352 bdevname(same_pdev->bdev,b2));
1353
1354 /* Verify rdev->desc_nr is unique.
1355 * If it is -1, assign a free number, else
1356 * check number is not in use
1357 */
1358 if (rdev->desc_nr < 0) {
1359 int choice = 0;
1360 if (mddev->pers) choice = mddev->raid_disks;
1361 while (find_rdev_nr(mddev, choice))
1362 choice++;
1363 rdev->desc_nr = choice;
1364 } else {
1365 if (find_rdev_nr(mddev, rdev->desc_nr))
1366 return -EBUSY;
1367 }
NeilBrown19133a42005-11-08 21:39:35 -08001368 bdevname(rdev->bdev,b);
1369 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1370 return -ENOMEM;
Neil Brown1edf80d2006-01-12 01:05:23 -08001371 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1372 *s = '!';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 list_add(&rdev->same_set, &mddev->disks);
1375 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001376 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001377
NeilBrown9c791972005-11-08 21:39:28 -08001378 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001379 kobject_add(&rdev->kobj);
1380
NeilBrownf637b9f2005-11-08 21:39:37 -08001381 if (rdev->bdev->bd_part)
1382 ko = &rdev->bdev->bd_part->kobj;
1383 else
1384 ko = &rdev->bdev->bd_disk->kobj;
1385 sysfs_create_link(&rdev->kobj, ko, "block");
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001386 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return 0;
1388}
1389
1390static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1391{
1392 char b[BDEVNAME_SIZE];
1393 if (!rdev->mddev) {
1394 MD_BUG();
1395 return;
1396 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001397 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 list_del_init(&rdev->same_set);
1399 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1400 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001401 sysfs_remove_link(&rdev->kobj, "block");
1402 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405/*
1406 * prevent the device from being mounted, repartitioned or
1407 * otherwise reused by a RAID array (or any other kernel
1408 * subsystem), by bd_claiming the device.
1409 */
1410static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1411{
1412 int err = 0;
1413 struct block_device *bdev;
1414 char b[BDEVNAME_SIZE];
1415
Ingo Molnar663d4402006-07-03 00:25:33 -07001416 bdev = open_partition_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (IS_ERR(bdev)) {
1418 printk(KERN_ERR "md: could not open %s.\n",
1419 __bdevname(dev, b));
1420 return PTR_ERR(bdev);
1421 }
1422 err = bd_claim(bdev, rdev);
1423 if (err) {
1424 printk(KERN_ERR "md: could not bd_claim %s.\n",
1425 bdevname(bdev, b));
Ingo Molnar663d4402006-07-03 00:25:33 -07001426 blkdev_put_partition(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return err;
1428 }
1429 rdev->bdev = bdev;
1430 return err;
1431}
1432
1433static void unlock_rdev(mdk_rdev_t *rdev)
1434{
1435 struct block_device *bdev = rdev->bdev;
1436 rdev->bdev = NULL;
1437 if (!bdev)
1438 MD_BUG();
1439 bd_release(bdev);
Ingo Molnar663d4402006-07-03 00:25:33 -07001440 blkdev_put_partition(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
1443void md_autodetect_dev(dev_t dev);
1444
1445static void export_rdev(mdk_rdev_t * rdev)
1446{
1447 char b[BDEVNAME_SIZE];
1448 printk(KERN_INFO "md: export_rdev(%s)\n",
1449 bdevname(rdev->bdev,b));
1450 if (rdev->mddev)
1451 MD_BUG();
1452 free_disk_sb(rdev);
1453 list_del_init(&rdev->same_set);
1454#ifndef MODULE
1455 md_autodetect_dev(rdev->bdev->bd_dev);
1456#endif
1457 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001458 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461static void kick_rdev_from_array(mdk_rdev_t * rdev)
1462{
1463 unbind_rdev_from_array(rdev);
1464 export_rdev(rdev);
1465}
1466
1467static void export_array(mddev_t *mddev)
1468{
1469 struct list_head *tmp;
1470 mdk_rdev_t *rdev;
1471
1472 ITERATE_RDEV(mddev,rdev,tmp) {
1473 if (!rdev->mddev) {
1474 MD_BUG();
1475 continue;
1476 }
1477 kick_rdev_from_array(rdev);
1478 }
1479 if (!list_empty(&mddev->disks))
1480 MD_BUG();
1481 mddev->raid_disks = 0;
1482 mddev->major_version = 0;
1483}
1484
1485static void print_desc(mdp_disk_t *desc)
1486{
1487 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1488 desc->major,desc->minor,desc->raid_disk,desc->state);
1489}
1490
1491static void print_sb(mdp_super_t *sb)
1492{
1493 int i;
1494
1495 printk(KERN_INFO
1496 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1497 sb->major_version, sb->minor_version, sb->patch_version,
1498 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1499 sb->ctime);
1500 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1501 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1502 sb->md_minor, sb->layout, sb->chunk_size);
1503 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1504 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1505 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1506 sb->failed_disks, sb->spare_disks,
1507 sb->sb_csum, (unsigned long)sb->events_lo);
1508
1509 printk(KERN_INFO);
1510 for (i = 0; i < MD_SB_DISKS; i++) {
1511 mdp_disk_t *desc;
1512
1513 desc = sb->disks + i;
1514 if (desc->number || desc->major || desc->minor ||
1515 desc->raid_disk || (desc->state && (desc->state != 4))) {
1516 printk(" D %2d: ", i);
1517 print_desc(desc);
1518 }
1519 }
1520 printk(KERN_INFO "md: THIS: ");
1521 print_desc(&sb->this_disk);
1522
1523}
1524
1525static void print_rdev(mdk_rdev_t *rdev)
1526{
1527 char b[BDEVNAME_SIZE];
1528 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1529 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001530 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1531 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (rdev->sb_loaded) {
1533 printk(KERN_INFO "md: rdev superblock:\n");
1534 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1535 } else
1536 printk(KERN_INFO "md: no rdev superblock!\n");
1537}
1538
Adrian Bunk5e563412006-06-26 00:27:42 -07001539static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 struct list_head *tmp, *tmp2;
1542 mdk_rdev_t *rdev;
1543 mddev_t *mddev;
1544 char b[BDEVNAME_SIZE];
1545
1546 printk("\n");
1547 printk("md: **********************************\n");
1548 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1549 printk("md: **********************************\n");
1550 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
NeilBrown32a76272005-06-21 17:17:14 -07001552 if (mddev->bitmap)
1553 bitmap_print_sb(mddev->bitmap);
1554 else
1555 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 ITERATE_RDEV(mddev,rdev,tmp2)
1557 printk("<%s>", bdevname(rdev->bdev,b));
1558 printk("\n");
1559
1560 ITERATE_RDEV(mddev,rdev,tmp2)
1561 print_rdev(rdev);
1562 }
1563 printk("md: **********************************\n");
1564 printk("\n");
1565}
1566
1567
NeilBrown42543762006-06-26 00:27:57 -07001568static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
NeilBrown42543762006-06-26 00:27:57 -07001570 /* Update each superblock (in-memory image), but
1571 * if we are allowed to, skip spares which already
1572 * have the right event counter, or have one earlier
1573 * (which would mean they aren't being marked as dirty
1574 * with the rest of the array)
1575 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 mdk_rdev_t *rdev;
1577 struct list_head *tmp;
1578
1579 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrown42543762006-06-26 00:27:57 -07001580 if (rdev->sb_events == mddev->events ||
1581 (nospares &&
1582 rdev->raid_disk < 0 &&
1583 (rdev->sb_events&1)==0 &&
1584 rdev->sb_events+1 == mddev->events)) {
1585 /* Don't update this superblock */
1586 rdev->sb_loaded = 2;
1587 } else {
1588 super_types[mddev->major_version].
1589 sync_super(mddev, rdev);
1590 rdev->sb_loaded = 1;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593}
1594
NeilBrown850b2b42006-10-03 01:15:46 -07001595static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001597 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 struct list_head *tmp;
1599 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001600 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001601 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001604 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001605
NeilBrown850b2b42006-10-03 01:15:46 -07001606 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1607 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1608 force_change = 1;
1609 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1610 /* just a clean<-> dirty transition, possibly leave spares alone,
1611 * though if events isn't the right even/odd, we will have to do
1612 * spares after all
1613 */
1614 nospares = 1;
1615 if (force_change)
1616 nospares = 0;
1617 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001618 /* If the array is degraded, then skipping spares is both
1619 * dangerous and fairly pointless.
1620 * Dangerous because a device that was removed from the array
1621 * might have a event_count that still looks up-to-date,
1622 * so it can be re-added without a resync.
1623 * Pointless because if there are any spares to skip,
1624 * then a recovery will happen and soon that array won't
1625 * be degraded any more and the spare can go back to sleep then.
1626 */
NeilBrown850b2b42006-10-03 01:15:46 -07001627 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001628
NeilBrown06d91a52005-06-21 17:17:12 -07001629 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001631
1632 /* If this is just a dirty<->clean transition, and the array is clean
1633 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001634 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001635 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1636 && (mddev->events & 1))
1637 mddev->events--;
1638 else {
1639 /* otherwise we have to go forward and ... */
1640 mddev->events ++;
1641 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1642 /* .. if the array isn't clean, insist on an odd 'events' */
1643 if ((mddev->events&1)==0) {
1644 mddev->events++;
1645 nospares = 0;
1646 }
1647 } else {
1648 /* otherwise insist on an even 'events' (for clean states) */
1649 if ((mddev->events&1)) {
1650 mddev->events++;
1651 nospares = 0;
1652 }
1653 }
1654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if (!mddev->events) {
1657 /*
1658 * oops, this 64-bit counter should never wrap.
1659 * Either we are in around ~1 trillion A.C., assuming
1660 * 1 reboot per second, or we have a bug:
1661 */
1662 MD_BUG();
1663 mddev->events --;
1664 }
NeilBrown42543762006-06-26 00:27:57 -07001665 sync_sbs(mddev, nospares);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 /*
1668 * do not write anything to disk if using
1669 * nonpersistent superblocks
1670 */
NeilBrown06d91a52005-06-21 17:17:12 -07001671 if (!mddev->persistent) {
NeilBrown850b2b42006-10-03 01:15:46 -07001672 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001673 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001674 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001676 }
NeilBrowna9701a32005-11-08 21:39:34 -08001677 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 dprintk(KERN_INFO
1680 "md: updating %s RAID superblock on device (in sync %d)\n",
1681 mdname(mddev),mddev->in_sync);
1682
NeilBrown32a76272005-06-21 17:17:14 -07001683 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 ITERATE_RDEV(mddev,rdev,tmp) {
1685 char b[BDEVNAME_SIZE];
1686 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001687 if (rdev->sb_loaded != 1)
1688 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001689 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 dprintk("(skipping faulty ");
1691
1692 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001693 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001694 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001695 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001696 rdev->sb_page);
1697 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1698 bdevname(rdev->bdev,b),
1699 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001700 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 } else
1703 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001704 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 /* only need to write one superblock... */
1706 break;
1707 }
NeilBrowna9701a32005-11-08 21:39:34 -08001708 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001709 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001710
NeilBrowna9701a32005-11-08 21:39:34 -08001711 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001712 if (mddev->in_sync != sync_req ||
1713 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001714 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001715 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001716 goto repeat;
1717 }
NeilBrown850b2b42006-10-03 01:15:46 -07001718 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001719 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001720 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
NeilBrownbce74da2006-01-06 00:20:41 -08001724/* words written to sysfs files may, or my not, be \n terminated.
1725 * We want to accept with case. For this we use cmd_match.
1726 */
1727static int cmd_match(const char *cmd, const char *str)
1728{
1729 /* See if cmd, written into a sysfs file, matches
1730 * str. They must either be the same, or cmd can
1731 * have a trailing newline
1732 */
1733 while (*cmd && *str && *cmd == *str) {
1734 cmd++;
1735 str++;
1736 }
1737 if (*cmd == '\n')
1738 cmd++;
1739 if (*str || *cmd)
1740 return 0;
1741 return 1;
1742}
1743
NeilBrown86e6ffd2005-11-08 21:39:24 -08001744struct rdev_sysfs_entry {
1745 struct attribute attr;
1746 ssize_t (*show)(mdk_rdev_t *, char *);
1747 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1748};
1749
1750static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001751state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001752{
1753 char *sep = "";
1754 int len=0;
1755
NeilBrownb2d444d2005-11-08 21:39:31 -08001756 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001757 len+= sprintf(page+len, "%sfaulty",sep);
1758 sep = ",";
1759 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001760 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001761 len += sprintf(page+len, "%sin_sync",sep);
1762 sep = ",";
1763 }
NeilBrownf6556752006-06-26 00:28:01 -07001764 if (test_bit(WriteMostly, &rdev->flags)) {
1765 len += sprintf(page+len, "%swrite_mostly",sep);
1766 sep = ",";
1767 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001768 if (!test_bit(Faulty, &rdev->flags) &&
1769 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001770 len += sprintf(page+len, "%sspare", sep);
1771 sep = ",";
1772 }
1773 return len+sprintf(page+len, "\n");
1774}
1775
NeilBrown45dc2de2006-06-26 00:27:58 -07001776static ssize_t
1777state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1778{
1779 /* can write
1780 * faulty - simulates and error
1781 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001782 * writemostly - sets write_mostly
1783 * -writemostly - clears write_mostly
NeilBrown45dc2de2006-06-26 00:27:58 -07001784 */
1785 int err = -EINVAL;
1786 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1787 md_error(rdev->mddev, rdev);
1788 err = 0;
1789 } else if (cmd_match(buf, "remove")) {
1790 if (rdev->raid_disk >= 0)
1791 err = -EBUSY;
1792 else {
1793 mddev_t *mddev = rdev->mddev;
1794 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07001795 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001796 md_new_event(mddev);
1797 err = 0;
1798 }
NeilBrownf6556752006-06-26 00:28:01 -07001799 } else if (cmd_match(buf, "writemostly")) {
1800 set_bit(WriteMostly, &rdev->flags);
1801 err = 0;
1802 } else if (cmd_match(buf, "-writemostly")) {
1803 clear_bit(WriteMostly, &rdev->flags);
1804 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001805 }
1806 return err ? err : len;
1807}
NeilBrown80ca3a42006-07-10 04:44:18 -07001808static struct rdev_sysfs_entry rdev_state =
1809__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001810
1811static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001812super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001813{
1814 if (rdev->sb_loaded && rdev->sb_size) {
1815 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1816 return rdev->sb_size;
1817 } else
1818 return 0;
1819}
NeilBrown96de1e62005-11-08 21:39:39 -08001820static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1821
NeilBrown4dbcdc72006-01-06 00:20:52 -08001822static ssize_t
1823errors_show(mdk_rdev_t *rdev, char *page)
1824{
1825 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1826}
1827
1828static ssize_t
1829errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1830{
1831 char *e;
1832 unsigned long n = simple_strtoul(buf, &e, 10);
1833 if (*buf && (*e == 0 || *e == '\n')) {
1834 atomic_set(&rdev->corrected_errors, n);
1835 return len;
1836 }
1837 return -EINVAL;
1838}
1839static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001840__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001841
NeilBrown014236d2006-01-06 00:20:55 -08001842static ssize_t
1843slot_show(mdk_rdev_t *rdev, char *page)
1844{
1845 if (rdev->raid_disk < 0)
1846 return sprintf(page, "none\n");
1847 else
1848 return sprintf(page, "%d\n", rdev->raid_disk);
1849}
1850
1851static ssize_t
1852slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1853{
1854 char *e;
1855 int slot = simple_strtoul(buf, &e, 10);
1856 if (strncmp(buf, "none", 4)==0)
1857 slot = -1;
1858 else if (e==buf || (*e && *e!= '\n'))
1859 return -EINVAL;
1860 if (rdev->mddev->pers)
1861 /* Cannot set slot in active array (yet) */
1862 return -EBUSY;
1863 if (slot >= rdev->mddev->raid_disks)
1864 return -ENOSPC;
1865 rdev->raid_disk = slot;
1866 /* assume it is working */
1867 rdev->flags = 0;
1868 set_bit(In_sync, &rdev->flags);
1869 return len;
1870}
1871
1872
1873static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001874__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001875
NeilBrown93c8cad2006-01-06 00:20:56 -08001876static ssize_t
1877offset_show(mdk_rdev_t *rdev, char *page)
1878{
Andrew Morton6961ece2006-01-06 00:20:59 -08001879 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001880}
1881
1882static ssize_t
1883offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1884{
1885 char *e;
1886 unsigned long long offset = 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->data_offset = offset;
1892 return len;
1893}
1894
1895static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001896__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001897
NeilBrown83303b62006-01-06 00:21:06 -08001898static ssize_t
1899rdev_size_show(mdk_rdev_t *rdev, char *page)
1900{
1901 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1902}
1903
1904static ssize_t
1905rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1906{
1907 char *e;
1908 unsigned long long size = simple_strtoull(buf, &e, 10);
1909 if (e==buf || (*e && *e != '\n'))
1910 return -EINVAL;
1911 if (rdev->mddev->pers)
1912 return -EBUSY;
1913 rdev->size = size;
1914 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1915 rdev->mddev->size = size;
1916 return len;
1917}
1918
1919static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07001920__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08001921
NeilBrown86e6ffd2005-11-08 21:39:24 -08001922static struct attribute *rdev_default_attrs[] = {
1923 &rdev_state.attr,
1924 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001925 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001926 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001927 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001928 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001929 NULL,
1930};
1931static ssize_t
1932rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1933{
1934 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1935 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1936
1937 if (!entry->show)
1938 return -EIO;
1939 return entry->show(rdev, page);
1940}
1941
1942static ssize_t
1943rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1944 const char *page, size_t length)
1945{
1946 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1947 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1948
1949 if (!entry->store)
1950 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07001951 if (!capable(CAP_SYS_ADMIN))
1952 return -EACCES;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001953 return entry->store(rdev, page, length);
1954}
1955
1956static void rdev_free(struct kobject *ko)
1957{
1958 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1959 kfree(rdev);
1960}
1961static struct sysfs_ops rdev_sysfs_ops = {
1962 .show = rdev_attr_show,
1963 .store = rdev_attr_store,
1964};
1965static struct kobj_type rdev_ktype = {
1966 .release = rdev_free,
1967 .sysfs_ops = &rdev_sysfs_ops,
1968 .default_attrs = rdev_default_attrs,
1969};
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971/*
1972 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1973 *
1974 * mark the device faulty if:
1975 *
1976 * - the device is nonexistent (zero size)
1977 * - the device has no valid superblock
1978 *
1979 * a faulty rdev _never_ has rdev->sb set.
1980 */
1981static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1982{
1983 char b[BDEVNAME_SIZE];
1984 int err;
1985 mdk_rdev_t *rdev;
1986 sector_t size;
1987
NeilBrown9ffae0c2006-01-06 00:20:32 -08001988 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (!rdev) {
1990 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1991 return ERR_PTR(-ENOMEM);
1992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 if ((err = alloc_disk_sb(rdev)))
1995 goto abort_free;
1996
1997 err = lock_rdev(rdev, newdev);
1998 if (err)
1999 goto abort_free;
2000
NeilBrown86e6ffd2005-11-08 21:39:24 -08002001 rdev->kobj.parent = NULL;
2002 rdev->kobj.ktype = &rdev_ktype;
2003 kobject_init(&rdev->kobj);
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002006 rdev->saved_raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002007 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002009 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002011 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002012 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2015 if (!size) {
2016 printk(KERN_WARNING
2017 "md: %s has zero or unknown size, marking faulty!\n",
2018 bdevname(rdev->bdev,b));
2019 err = -EINVAL;
2020 goto abort_free;
2021 }
2022
2023 if (super_format >= 0) {
2024 err = super_types[super_format].
2025 load_super(rdev, NULL, super_minor);
2026 if (err == -EINVAL) {
2027 printk(KERN_WARNING
2028 "md: %s has invalid sb, not importing!\n",
2029 bdevname(rdev->bdev,b));
2030 goto abort_free;
2031 }
2032 if (err < 0) {
2033 printk(KERN_WARNING
2034 "md: could not read %s's sb, not importing!\n",
2035 bdevname(rdev->bdev,b));
2036 goto abort_free;
2037 }
2038 }
2039 INIT_LIST_HEAD(&rdev->same_set);
2040
2041 return rdev;
2042
2043abort_free:
2044 if (rdev->sb_page) {
2045 if (rdev->bdev)
2046 unlock_rdev(rdev);
2047 free_disk_sb(rdev);
2048 }
2049 kfree(rdev);
2050 return ERR_PTR(err);
2051}
2052
2053/*
2054 * Check a full RAID array for plausibility
2055 */
2056
2057
NeilBrowna757e642005-04-16 15:26:42 -07002058static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
2060 int i;
2061 struct list_head *tmp;
2062 mdk_rdev_t *rdev, *freshest;
2063 char b[BDEVNAME_SIZE];
2064
2065 freshest = NULL;
2066 ITERATE_RDEV(mddev,rdev,tmp)
2067 switch (super_types[mddev->major_version].
2068 load_super(rdev, freshest, mddev->minor_version)) {
2069 case 1:
2070 freshest = rdev;
2071 break;
2072 case 0:
2073 break;
2074 default:
2075 printk( KERN_ERR \
2076 "md: fatal superblock inconsistency in %s"
2077 " -- removing from array\n",
2078 bdevname(rdev->bdev,b));
2079 kick_rdev_from_array(rdev);
2080 }
2081
2082
2083 super_types[mddev->major_version].
2084 validate_super(mddev, freshest);
2085
2086 i = 0;
2087 ITERATE_RDEV(mddev,rdev,tmp) {
2088 if (rdev != freshest)
2089 if (super_types[mddev->major_version].
2090 validate_super(mddev, rdev)) {
2091 printk(KERN_WARNING "md: kicking non-fresh %s"
2092 " from array!\n",
2093 bdevname(rdev->bdev,b));
2094 kick_rdev_from_array(rdev);
2095 continue;
2096 }
2097 if (mddev->level == LEVEL_MULTIPATH) {
2098 rdev->desc_nr = i++;
2099 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002100 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
2102 }
2103
2104
2105
2106 if (mddev->recovery_cp != MaxSector &&
2107 mddev->level >= 1)
2108 printk(KERN_ERR "md: %s: raid array is not clean"
2109 " -- starting background reconstruction\n",
2110 mdname(mddev));
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
NeilBrowneae17012005-11-08 21:39:23 -08002114static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002115safe_delay_show(mddev_t *mddev, char *page)
2116{
2117 int msec = (mddev->safemode_delay*1000)/HZ;
2118 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2119}
2120static ssize_t
2121safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2122{
2123 int scale=1;
2124 int dot=0;
2125 int i;
2126 unsigned long msec;
2127 char buf[30];
2128 char *e;
2129 /* remove a period, and count digits after it */
2130 if (len >= sizeof(buf))
2131 return -EINVAL;
2132 strlcpy(buf, cbuf, len);
2133 buf[len] = 0;
2134 for (i=0; i<len; i++) {
2135 if (dot) {
2136 if (isdigit(buf[i])) {
2137 buf[i-1] = buf[i];
2138 scale *= 10;
2139 }
2140 buf[i] = 0;
2141 } else if (buf[i] == '.') {
2142 dot=1;
2143 buf[i] = 0;
2144 }
2145 }
2146 msec = simple_strtoul(buf, &e, 10);
2147 if (e == buf || (*e && *e != '\n'))
2148 return -EINVAL;
2149 msec = (msec * 1000) / scale;
2150 if (msec == 0)
2151 mddev->safemode_delay = 0;
2152 else {
2153 mddev->safemode_delay = (msec*HZ)/1000;
2154 if (mddev->safemode_delay == 0)
2155 mddev->safemode_delay = 1;
2156 }
2157 return len;
2158}
2159static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002160__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002161
2162static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002163level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002164{
NeilBrown2604b702006-01-06 00:20:36 -08002165 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002166 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002167 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002168 else if (mddev->clevel[0])
2169 return sprintf(page, "%s\n", mddev->clevel);
2170 else if (mddev->level != LEVEL_NONE)
2171 return sprintf(page, "%d\n", mddev->level);
2172 else
2173 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002174}
2175
NeilBrownd9d166c2006-01-06 00:20:51 -08002176static ssize_t
2177level_store(mddev_t *mddev, const char *buf, size_t len)
2178{
2179 int rv = len;
2180 if (mddev->pers)
2181 return -EBUSY;
2182 if (len == 0)
2183 return 0;
2184 if (len >= sizeof(mddev->clevel))
2185 return -ENOSPC;
2186 strncpy(mddev->clevel, buf, len);
2187 if (mddev->clevel[len-1] == '\n')
2188 len--;
2189 mddev->clevel[len] = 0;
2190 mddev->level = LEVEL_NONE;
2191 return rv;
2192}
2193
2194static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002195__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002196
NeilBrownd4dbd022006-06-26 00:27:59 -07002197
2198static ssize_t
2199layout_show(mddev_t *mddev, char *page)
2200{
2201 /* just a number, not meaningful for all levels */
2202 return sprintf(page, "%d\n", mddev->layout);
2203}
2204
2205static ssize_t
2206layout_store(mddev_t *mddev, const char *buf, size_t len)
2207{
2208 char *e;
2209 unsigned long n = simple_strtoul(buf, &e, 10);
2210 if (mddev->pers)
2211 return -EBUSY;
2212
2213 if (!*buf || (*e && *e != '\n'))
2214 return -EINVAL;
2215
2216 mddev->layout = n;
2217 return len;
2218}
2219static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002220__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002221
2222
NeilBrowneae17012005-11-08 21:39:23 -08002223static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002224raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002225{
NeilBrownbb636542005-11-08 21:39:45 -08002226 if (mddev->raid_disks == 0)
2227 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002228 return sprintf(page, "%d\n", mddev->raid_disks);
2229}
2230
NeilBrownda943b92006-01-06 00:20:54 -08002231static int update_raid_disks(mddev_t *mddev, int raid_disks);
2232
2233static ssize_t
2234raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2235{
2236 /* can only set raid_disks if array is not yet active */
2237 char *e;
2238 int rv = 0;
2239 unsigned long n = simple_strtoul(buf, &e, 10);
2240
2241 if (!*buf || (*e && *e != '\n'))
2242 return -EINVAL;
2243
2244 if (mddev->pers)
2245 rv = update_raid_disks(mddev, n);
2246 else
2247 mddev->raid_disks = n;
2248 return rv ? rv : len;
2249}
2250static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002251__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002252
NeilBrown24dd4692005-11-08 21:39:26 -08002253static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002254chunk_size_show(mddev_t *mddev, char *page)
2255{
2256 return sprintf(page, "%d\n", mddev->chunk_size);
2257}
2258
2259static ssize_t
2260chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2261{
2262 /* can only set chunk_size if array is not yet active */
2263 char *e;
2264 unsigned long n = simple_strtoul(buf, &e, 10);
2265
2266 if (mddev->pers)
2267 return -EBUSY;
2268 if (!*buf || (*e && *e != '\n'))
2269 return -EINVAL;
2270
2271 mddev->chunk_size = n;
2272 return len;
2273}
2274static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002275__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002276
NeilBrowna94213b2006-06-26 00:28:00 -07002277static ssize_t
2278resync_start_show(mddev_t *mddev, char *page)
2279{
2280 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2281}
2282
2283static ssize_t
2284resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2285{
2286 /* can only set chunk_size if array is not yet active */
2287 char *e;
2288 unsigned long long n = simple_strtoull(buf, &e, 10);
2289
2290 if (mddev->pers)
2291 return -EBUSY;
2292 if (!*buf || (*e && *e != '\n'))
2293 return -EINVAL;
2294
2295 mddev->recovery_cp = n;
2296 return len;
2297}
2298static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002299__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002300
NeilBrown9e653b62006-06-26 00:27:58 -07002301/*
2302 * The array state can be:
2303 *
2304 * clear
2305 * No devices, no size, no level
2306 * Equivalent to STOP_ARRAY ioctl
2307 * inactive
2308 * May have some settings, but array is not active
2309 * all IO results in error
2310 * When written, doesn't tear down array, but just stops it
2311 * suspended (not supported yet)
2312 * All IO requests will block. The array can be reconfigured.
2313 * Writing this, if accepted, will block until array is quiessent
2314 * readonly
2315 * no resync can happen. no superblocks get written.
2316 * write requests fail
2317 * read-auto
2318 * like readonly, but behaves like 'clean' on a write request.
2319 *
2320 * clean - no pending writes, but otherwise active.
2321 * When written to inactive array, starts without resync
2322 * If a write request arrives then
2323 * if metadata is known, mark 'dirty' and switch to 'active'.
2324 * if not known, block and switch to write-pending
2325 * If written to an active array that has pending writes, then fails.
2326 * active
2327 * fully active: IO and resync can be happening.
2328 * When written to inactive array, starts with resync
2329 *
2330 * write-pending
2331 * clean, but writes are blocked waiting for 'active' to be written.
2332 *
2333 * active-idle
2334 * like active, but no writes have been seen for a while (100msec).
2335 *
2336 */
2337enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2338 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002339static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002340 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2341 "write-pending", "active-idle", NULL };
2342
2343static int match_word(const char *word, char **list)
2344{
2345 int n;
2346 for (n=0; list[n]; n++)
2347 if (cmd_match(word, list[n]))
2348 break;
2349 return n;
2350}
2351
2352static ssize_t
2353array_state_show(mddev_t *mddev, char *page)
2354{
2355 enum array_state st = inactive;
2356
2357 if (mddev->pers)
2358 switch(mddev->ro) {
2359 case 1:
2360 st = readonly;
2361 break;
2362 case 2:
2363 st = read_auto;
2364 break;
2365 case 0:
2366 if (mddev->in_sync)
2367 st = clean;
2368 else if (mddev->safemode)
2369 st = active_idle;
2370 else
2371 st = active;
2372 }
2373 else {
2374 if (list_empty(&mddev->disks) &&
2375 mddev->raid_disks == 0 &&
2376 mddev->size == 0)
2377 st = clear;
2378 else
2379 st = inactive;
2380 }
2381 return sprintf(page, "%s\n", array_states[st]);
2382}
2383
2384static int do_md_stop(mddev_t * mddev, int ro);
2385static int do_md_run(mddev_t * mddev);
2386static int restart_array(mddev_t *mddev);
2387
2388static ssize_t
2389array_state_store(mddev_t *mddev, const char *buf, size_t len)
2390{
2391 int err = -EINVAL;
2392 enum array_state st = match_word(buf, array_states);
2393 switch(st) {
2394 case bad_word:
2395 break;
2396 case clear:
2397 /* stopping an active array */
2398 if (mddev->pers) {
2399 if (atomic_read(&mddev->active) > 1)
2400 return -EBUSY;
2401 err = do_md_stop(mddev, 0);
2402 }
2403 break;
2404 case inactive:
2405 /* stopping an active array */
2406 if (mddev->pers) {
2407 if (atomic_read(&mddev->active) > 1)
2408 return -EBUSY;
2409 err = do_md_stop(mddev, 2);
2410 }
2411 break;
2412 case suspended:
2413 break; /* not supported yet */
2414 case readonly:
2415 if (mddev->pers)
2416 err = do_md_stop(mddev, 1);
2417 else {
2418 mddev->ro = 1;
2419 err = do_md_run(mddev);
2420 }
2421 break;
2422 case read_auto:
2423 /* stopping an active array */
2424 if (mddev->pers) {
2425 err = do_md_stop(mddev, 1);
2426 if (err == 0)
2427 mddev->ro = 2; /* FIXME mark devices writable */
2428 } else {
2429 mddev->ro = 2;
2430 err = do_md_run(mddev);
2431 }
2432 break;
2433 case clean:
2434 if (mddev->pers) {
2435 restart_array(mddev);
2436 spin_lock_irq(&mddev->write_lock);
2437 if (atomic_read(&mddev->writes_pending) == 0) {
2438 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07002439 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002440 }
2441 spin_unlock_irq(&mddev->write_lock);
2442 } else {
2443 mddev->ro = 0;
2444 mddev->recovery_cp = MaxSector;
2445 err = do_md_run(mddev);
2446 }
2447 break;
2448 case active:
2449 if (mddev->pers) {
2450 restart_array(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002451 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002452 wake_up(&mddev->sb_wait);
2453 err = 0;
2454 } else {
2455 mddev->ro = 0;
2456 err = do_md_run(mddev);
2457 }
2458 break;
2459 case write_pending:
2460 case active_idle:
2461 /* these cannot be set */
2462 break;
2463 }
2464 if (err)
2465 return err;
2466 else
2467 return len;
2468}
NeilBrown80ca3a42006-07-10 04:44:18 -07002469static struct md_sysfs_entry md_array_state =
2470__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002471
NeilBrown6d7ff732006-01-06 00:21:16 -08002472static ssize_t
2473null_show(mddev_t *mddev, char *page)
2474{
2475 return -EINVAL;
2476}
2477
2478static ssize_t
2479new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2480{
2481 /* buf must be %d:%d\n? giving major and minor numbers */
2482 /* The new device is added to the array.
2483 * If the array has a persistent superblock, we read the
2484 * superblock to initialise info and check validity.
2485 * Otherwise, only checking done is that in bind_rdev_to_array,
2486 * which mainly checks size.
2487 */
2488 char *e;
2489 int major = simple_strtoul(buf, &e, 10);
2490 int minor;
2491 dev_t dev;
2492 mdk_rdev_t *rdev;
2493 int err;
2494
2495 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2496 return -EINVAL;
2497 minor = simple_strtoul(e+1, &e, 10);
2498 if (*e && *e != '\n')
2499 return -EINVAL;
2500 dev = MKDEV(major, minor);
2501 if (major != MAJOR(dev) ||
2502 minor != MINOR(dev))
2503 return -EOVERFLOW;
2504
2505
2506 if (mddev->persistent) {
2507 rdev = md_import_device(dev, mddev->major_version,
2508 mddev->minor_version);
2509 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2510 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2511 mdk_rdev_t, same_set);
2512 err = super_types[mddev->major_version]
2513 .load_super(rdev, rdev0, mddev->minor_version);
2514 if (err < 0)
2515 goto out;
2516 }
2517 } else
2518 rdev = md_import_device(dev, -1, -1);
2519
2520 if (IS_ERR(rdev))
2521 return PTR_ERR(rdev);
2522 err = bind_rdev_to_array(rdev, mddev);
2523 out:
2524 if (err)
2525 export_rdev(rdev);
2526 return err ? err : len;
2527}
2528
2529static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002530__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002531
2532static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002533bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2534{
2535 char *end;
2536 unsigned long chunk, end_chunk;
2537
2538 if (!mddev->bitmap)
2539 goto out;
2540 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2541 while (*buf) {
2542 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2543 if (buf == end) break;
2544 if (*end == '-') { /* range */
2545 buf = end + 1;
2546 end_chunk = simple_strtoul(buf, &end, 0);
2547 if (buf == end) break;
2548 }
2549 if (*end && !isspace(*end)) break;
2550 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2551 buf = end;
2552 while (isspace(*buf)) buf++;
2553 }
2554 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2555out:
2556 return len;
2557}
2558
2559static struct md_sysfs_entry md_bitmap =
2560__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2561
2562static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002563size_show(mddev_t *mddev, char *page)
2564{
2565 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2566}
2567
2568static int update_size(mddev_t *mddev, unsigned long size);
2569
2570static ssize_t
2571size_store(mddev_t *mddev, const char *buf, size_t len)
2572{
2573 /* If array is inactive, we can reduce the component size, but
2574 * not increase it (except from 0).
2575 * If array is active, we can try an on-line resize
2576 */
2577 char *e;
2578 int err = 0;
2579 unsigned long long size = simple_strtoull(buf, &e, 10);
2580 if (!*buf || *buf == '\n' ||
2581 (*e && *e != '\n'))
2582 return -EINVAL;
2583
2584 if (mddev->pers) {
2585 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002586 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002587 } else {
2588 if (mddev->size == 0 ||
2589 mddev->size > size)
2590 mddev->size = size;
2591 else
2592 err = -ENOSPC;
2593 }
2594 return err ? err : len;
2595}
2596
2597static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002598__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002599
NeilBrown8bb93aa2006-01-06 00:20:50 -08002600
2601/* Metdata version.
2602 * This is either 'none' for arrays with externally managed metadata,
2603 * or N.M for internally known formats
2604 */
2605static ssize_t
2606metadata_show(mddev_t *mddev, char *page)
2607{
2608 if (mddev->persistent)
2609 return sprintf(page, "%d.%d\n",
2610 mddev->major_version, mddev->minor_version);
2611 else
2612 return sprintf(page, "none\n");
2613}
2614
2615static ssize_t
2616metadata_store(mddev_t *mddev, const char *buf, size_t len)
2617{
2618 int major, minor;
2619 char *e;
2620 if (!list_empty(&mddev->disks))
2621 return -EBUSY;
2622
2623 if (cmd_match(buf, "none")) {
2624 mddev->persistent = 0;
2625 mddev->major_version = 0;
2626 mddev->minor_version = 90;
2627 return len;
2628 }
2629 major = simple_strtoul(buf, &e, 10);
2630 if (e==buf || *e != '.')
2631 return -EINVAL;
2632 buf = e+1;
2633 minor = simple_strtoul(buf, &e, 10);
2634 if (e==buf || *e != '\n')
2635 return -EINVAL;
2636 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2637 super_types[major].name == NULL)
2638 return -ENOENT;
2639 mddev->major_version = major;
2640 mddev->minor_version = minor;
2641 mddev->persistent = 1;
2642 return len;
2643}
2644
2645static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002646__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002647
NeilBrowna35b0d62006-01-06 00:20:49 -08002648static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002649action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002650{
NeilBrown7eec3142005-11-08 21:39:44 -08002651 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002652 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2653 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002654 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2655 type = "reshape";
2656 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002657 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2658 type = "resync";
2659 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2660 type = "check";
2661 else
2662 type = "repair";
2663 } else
2664 type = "recover";
2665 }
2666 return sprintf(page, "%s\n", type);
2667}
2668
2669static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002670action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002671{
NeilBrown7eec3142005-11-08 21:39:44 -08002672 if (!mddev->pers || !mddev->pers->sync_request)
2673 return -EINVAL;
2674
NeilBrownbce74da2006-01-06 00:20:41 -08002675 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002676 if (mddev->sync_thread) {
2677 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2678 md_unregister_thread(mddev->sync_thread);
2679 mddev->sync_thread = NULL;
2680 mddev->recovery = 0;
2681 }
NeilBrown03c902e2006-01-06 00:20:46 -08002682 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2683 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002684 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002685 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002686 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002687 else if (cmd_match(page, "reshape")) {
2688 int err;
2689 if (mddev->pers->start_reshape == NULL)
2690 return -EINVAL;
2691 err = mddev->pers->start_reshape(mddev);
2692 if (err)
2693 return err;
2694 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002695 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002696 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002697 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002698 return -EINVAL;
2699 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2700 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002701 }
NeilBrown03c902e2006-01-06 00:20:46 -08002702 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002703 md_wakeup_thread(mddev->thread);
2704 return len;
2705}
2706
NeilBrown9d888832005-11-08 21:39:26 -08002707static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002708mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002709{
2710 return sprintf(page, "%llu\n",
2711 (unsigned long long) mddev->resync_mismatches);
2712}
2713
NeilBrown80ca3a42006-07-10 04:44:18 -07002714static struct md_sysfs_entry md_scan_mode =
2715__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002716
NeilBrown96de1e62005-11-08 21:39:39 -08002717
NeilBrown80ca3a42006-07-10 04:44:18 -07002718static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002719
NeilBrown88202a02006-01-06 00:21:36 -08002720static ssize_t
2721sync_min_show(mddev_t *mddev, char *page)
2722{
2723 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2724 mddev->sync_speed_min ? "local": "system");
2725}
2726
2727static ssize_t
2728sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2729{
2730 int min;
2731 char *e;
2732 if (strncmp(buf, "system", 6)==0) {
2733 mddev->sync_speed_min = 0;
2734 return len;
2735 }
2736 min = simple_strtoul(buf, &e, 10);
2737 if (buf == e || (*e && *e != '\n') || min <= 0)
2738 return -EINVAL;
2739 mddev->sync_speed_min = min;
2740 return len;
2741}
2742
2743static struct md_sysfs_entry md_sync_min =
2744__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2745
2746static ssize_t
2747sync_max_show(mddev_t *mddev, char *page)
2748{
2749 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2750 mddev->sync_speed_max ? "local": "system");
2751}
2752
2753static ssize_t
2754sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2755{
2756 int max;
2757 char *e;
2758 if (strncmp(buf, "system", 6)==0) {
2759 mddev->sync_speed_max = 0;
2760 return len;
2761 }
2762 max = simple_strtoul(buf, &e, 10);
2763 if (buf == e || (*e && *e != '\n') || max <= 0)
2764 return -EINVAL;
2765 mddev->sync_speed_max = max;
2766 return len;
2767}
2768
2769static struct md_sysfs_entry md_sync_max =
2770__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2771
2772
2773static ssize_t
2774sync_speed_show(mddev_t *mddev, char *page)
2775{
2776 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07002777 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08002778 dt = ((jiffies - mddev->resync_mark) / HZ);
2779 if (!dt) dt++;
2780 db = resync - (mddev->resync_mark_cnt);
2781 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2782}
2783
NeilBrown80ca3a42006-07-10 04:44:18 -07002784static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08002785
2786static ssize_t
2787sync_completed_show(mddev_t *mddev, char *page)
2788{
2789 unsigned long max_blocks, resync;
2790
2791 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2792 max_blocks = mddev->resync_max_sectors;
2793 else
2794 max_blocks = mddev->size << 1;
2795
2796 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2797 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2798}
2799
NeilBrown80ca3a42006-07-10 04:44:18 -07002800static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08002801
NeilBrowne464eaf2006-03-27 01:18:14 -08002802static ssize_t
2803suspend_lo_show(mddev_t *mddev, char *page)
2804{
2805 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2806}
2807
2808static ssize_t
2809suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2810{
2811 char *e;
2812 unsigned long long new = simple_strtoull(buf, &e, 10);
2813
2814 if (mddev->pers->quiesce == NULL)
2815 return -EINVAL;
2816 if (buf == e || (*e && *e != '\n'))
2817 return -EINVAL;
2818 if (new >= mddev->suspend_hi ||
2819 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2820 mddev->suspend_lo = new;
2821 mddev->pers->quiesce(mddev, 2);
2822 return len;
2823 } else
2824 return -EINVAL;
2825}
2826static struct md_sysfs_entry md_suspend_lo =
2827__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2828
2829
2830static ssize_t
2831suspend_hi_show(mddev_t *mddev, char *page)
2832{
2833 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2834}
2835
2836static ssize_t
2837suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2838{
2839 char *e;
2840 unsigned long long new = simple_strtoull(buf, &e, 10);
2841
2842 if (mddev->pers->quiesce == NULL)
2843 return -EINVAL;
2844 if (buf == e || (*e && *e != '\n'))
2845 return -EINVAL;
2846 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2847 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2848 mddev->suspend_hi = new;
2849 mddev->pers->quiesce(mddev, 1);
2850 mddev->pers->quiesce(mddev, 0);
2851 return len;
2852 } else
2853 return -EINVAL;
2854}
2855static struct md_sysfs_entry md_suspend_hi =
2856__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2857
2858
NeilBrowneae17012005-11-08 21:39:23 -08002859static struct attribute *md_default_attrs[] = {
2860 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07002861 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002862 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002863 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002864 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07002865 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002866 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08002867 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07002868 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07002869 &md_array_state.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002870 NULL,
2871};
2872
2873static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002874 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002875 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002876 &md_sync_min.attr,
2877 &md_sync_max.attr,
2878 &md_sync_speed.attr,
2879 &md_sync_completed.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08002880 &md_suspend_lo.attr,
2881 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07002882 &md_bitmap.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002883 NULL,
2884};
NeilBrown411036f2005-11-08 21:39:40 -08002885static struct attribute_group md_redundancy_group = {
2886 .name = NULL,
2887 .attrs = md_redundancy_attrs,
2888};
2889
NeilBrowneae17012005-11-08 21:39:23 -08002890
2891static ssize_t
2892md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2893{
2894 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2895 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002896 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002897
2898 if (!entry->show)
2899 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002900 rv = mddev_lock(mddev);
2901 if (!rv) {
2902 rv = entry->show(mddev, page);
2903 mddev_unlock(mddev);
2904 }
NeilBrown96de1e62005-11-08 21:39:39 -08002905 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002906}
2907
2908static ssize_t
2909md_attr_store(struct kobject *kobj, struct attribute *attr,
2910 const char *page, size_t length)
2911{
2912 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2913 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002914 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002915
2916 if (!entry->store)
2917 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002918 if (!capable(CAP_SYS_ADMIN))
2919 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002920 rv = mddev_lock(mddev);
2921 if (!rv) {
2922 rv = entry->store(mddev, page, length);
2923 mddev_unlock(mddev);
2924 }
NeilBrown96de1e62005-11-08 21:39:39 -08002925 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002926}
2927
2928static void md_free(struct kobject *ko)
2929{
2930 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2931 kfree(mddev);
2932}
2933
2934static struct sysfs_ops md_sysfs_ops = {
2935 .show = md_attr_show,
2936 .store = md_attr_store,
2937};
2938static struct kobj_type md_ktype = {
2939 .release = md_free,
2940 .sysfs_ops = &md_sysfs_ops,
2941 .default_attrs = md_default_attrs,
2942};
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944int mdp_major = 0;
2945
2946static struct kobject *md_probe(dev_t dev, int *part, void *data)
2947{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002948 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 mddev_t *mddev = mddev_find(dev);
2950 struct gendisk *disk;
2951 int partitioned = (MAJOR(dev) != MD_MAJOR);
2952 int shift = partitioned ? MdpMinorShift : 0;
2953 int unit = MINOR(dev) >> shift;
2954
2955 if (!mddev)
2956 return NULL;
2957
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002958 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002960 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 mddev_put(mddev);
2962 return NULL;
2963 }
2964 disk = alloc_disk(1 << shift);
2965 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002966 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 mddev_put(mddev);
2968 return NULL;
2969 }
2970 disk->major = MAJOR(dev);
2971 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07002972 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07002974 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 disk->fops = &md_fops;
2977 disk->private_data = mddev;
2978 disk->queue = mddev->queue;
2979 add_disk(disk);
2980 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002981 mutex_unlock(&disks_mutex);
NeilBrown9c791972005-11-08 21:39:28 -08002982 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08002983 mddev->kobj.k_name = NULL;
2984 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2985 mddev->kobj.ktype = &md_ktype;
2986 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return NULL;
2988}
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990static void md_safemode_timeout(unsigned long data)
2991{
2992 mddev_t *mddev = (mddev_t *) data;
2993
2994 mddev->safemode = 1;
2995 md_wakeup_thread(mddev->thread);
2996}
2997
NeilBrown6ff8d8e2006-01-06 00:20:15 -08002998static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
3000static int do_md_run(mddev_t * mddev)
3001{
NeilBrown2604b702006-01-06 00:20:36 -08003002 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 int chunk_size;
3004 struct list_head *tmp;
3005 mdk_rdev_t *rdev;
3006 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003007 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 char b[BDEVNAME_SIZE];
3009
NeilBrowna757e642005-04-16 15:26:42 -07003010 if (list_empty(&mddev->disks))
3011 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 if (mddev->pers)
3015 return -EBUSY;
3016
3017 /*
3018 * Analyze all RAID superblock(s)
3019 */
NeilBrowna757e642005-04-16 15:26:42 -07003020 if (!mddev->raid_disks)
3021 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
NeilBrown2604b702006-01-06 00:20:36 -08003025 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 if (chunk_size > MAX_CHUNK_SIZE) {
3027 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3028 chunk_size, MAX_CHUNK_SIZE);
3029 return -EINVAL;
3030 }
3031 /*
3032 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3033 */
3034 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003035 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return -EINVAL;
3037 }
3038 if (chunk_size < PAGE_SIZE) {
3039 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3040 chunk_size, PAGE_SIZE);
3041 return -EINVAL;
3042 }
3043
3044 /* devices must have minimum size of one chunk */
3045 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003046 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 continue;
3048 if (rdev->size < chunk_size / 1024) {
3049 printk(KERN_WARNING
3050 "md: Dev %s smaller than chunk_size:"
3051 " %lluk < %dk\n",
3052 bdevname(rdev->bdev,b),
3053 (unsigned long long)rdev->size,
3054 chunk_size / 1024);
3055 return -EINVAL;
3056 }
3057 }
3058 }
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003061 if (mddev->level != LEVEL_NONE)
3062 request_module("md-level-%d", mddev->level);
3063 else if (mddev->clevel[0])
3064 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065#endif
3066
3067 /*
3068 * Drop all container device buffers, from now on
3069 * the only valid external interface is through the md
3070 * device.
3071 * Also find largest hardsector size
3072 */
3073 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003074 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 continue;
3076 sync_blockdev(rdev->bdev);
3077 invalidate_bdev(rdev->bdev, 0);
3078 }
3079
3080 md_probe(mddev->unit, NULL, NULL);
3081 disk = mddev->gendisk;
3082 if (!disk)
3083 return -ENOMEM;
3084
3085 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003086 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003087 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003089 if (mddev->level != LEVEL_NONE)
3090 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3091 mddev->level);
3092 else
3093 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3094 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return -EINVAL;
3096 }
NeilBrown2604b702006-01-06 00:20:36 -08003097 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003099 mddev->level = pers->level;
3100 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
NeilBrownf6705572006-03-27 01:18:11 -08003102 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003103 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003104 /* This personality cannot handle reshaping... */
3105 mddev->pers = NULL;
3106 module_put(pers->owner);
3107 return -EINVAL;
3108 }
3109
NeilBrown657390d2005-08-26 18:34:16 -07003110 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003112 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08003113 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
NeilBrownf91de922005-11-08 21:39:36 -08003115 if (start_readonly)
3116 mddev->ro = 2; /* read-only, but switch on first write */
3117
NeilBrownb15c2e52006-01-06 00:20:16 -08003118 err = mddev->pers->run(mddev);
3119 if (!err && mddev->pers->sync_request) {
3120 err = bitmap_create(mddev);
3121 if (err) {
3122 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3123 mdname(mddev), err);
3124 mddev->pers->stop(mddev);
3125 }
3126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (err) {
3128 printk(KERN_ERR "md: pers->run() failed ...\n");
3129 module_put(mddev->pers->owner);
3130 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003131 bitmap_destroy(mddev);
3132 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 }
NeilBrown411036f2005-11-08 21:39:40 -08003134 if (mddev->pers->sync_request)
3135 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08003136 else if (mddev->ro == 2) /* auto-readonly not meaningful */
3137 mddev->ro = 0;
3138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 atomic_set(&mddev->writes_pending,0);
3140 mddev->safemode = 0;
3141 mddev->safemode_timer.function = md_safemode_timeout;
3142 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003143 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003145
3146 ITERATE_RDEV(mddev,rdev,tmp)
3147 if (rdev->raid_disk >= 0) {
3148 char nm[20];
3149 sprintf(nm, "rd%d", rdev->raid_disk);
3150 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3154
NeilBrown850b2b42006-10-03 01:15:46 -07003155 if (mddev->flags)
3156 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
3158 set_capacity(disk, mddev->array_size<<1);
3159
3160 /* If we call blk_queue_make_request here, it will
3161 * re-initialise max_sectors etc which may have been
3162 * refined inside -> run. So just set the bits we need to set.
3163 * Most initialisation happended when we called
3164 * blk_queue_make_request(..., md_fail_request)
3165 * earlier.
3166 */
3167 mddev->queue->queuedata = mddev;
3168 mddev->queue->make_request_fn = mddev->pers->make_request;
3169
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003170 /* If there is a partially-recovered drive we need to
3171 * start recovery here. If we leave it to md_check_recovery,
3172 * it will remove the drives and not do the right thing
3173 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003174 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003175 struct list_head *rtmp;
3176 int spares = 0;
3177 ITERATE_RDEV(mddev,rdev,rtmp)
3178 if (rdev->raid_disk >= 0 &&
3179 !test_bit(In_sync, &rdev->flags) &&
3180 !test_bit(Faulty, &rdev->flags))
3181 /* complete an interrupted recovery */
3182 spares++;
3183 if (spares && mddev->pers->sync_request) {
3184 mddev->recovery = 0;
3185 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3186 mddev->sync_thread = md_register_thread(md_do_sync,
3187 mddev,
3188 "%s_resync");
3189 if (!mddev->sync_thread) {
3190 printk(KERN_ERR "%s: could not start resync"
3191 " thread...\n",
3192 mdname(mddev));
3193 /* leave the spares where they are, it shouldn't hurt */
3194 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003195 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003196 }
3197 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003198 md_wakeup_thread(mddev->thread);
3199 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003202 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 return 0;
3204}
3205
3206static int restart_array(mddev_t *mddev)
3207{
3208 struct gendisk *disk = mddev->gendisk;
3209 int err;
3210
3211 /*
3212 * Complain if it has no devices
3213 */
3214 err = -ENXIO;
3215 if (list_empty(&mddev->disks))
3216 goto out;
3217
3218 if (mddev->pers) {
3219 err = -EBUSY;
3220 if (!mddev->ro)
3221 goto out;
3222
3223 mddev->safemode = 0;
3224 mddev->ro = 0;
3225 set_disk_ro(disk, 0);
3226
3227 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3228 mdname(mddev));
3229 /*
3230 * Kick recovery or resync if necessary
3231 */
3232 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3233 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003234 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003236 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239out:
3240 return err;
3241}
3242
NeilBrownacc55e22006-06-26 00:27:47 -07003243/* similar to deny_write_access, but accounts for our holding a reference
3244 * to the file ourselves */
3245static int deny_bitmap_write_access(struct file * file)
3246{
3247 struct inode *inode = file->f_mapping->host;
3248
3249 spin_lock(&inode->i_lock);
3250 if (atomic_read(&inode->i_writecount) > 1) {
3251 spin_unlock(&inode->i_lock);
3252 return -ETXTBSY;
3253 }
3254 atomic_set(&inode->i_writecount, -1);
3255 spin_unlock(&inode->i_lock);
3256
3257 return 0;
3258}
3259
3260static void restore_bitmap_write_access(struct file *file)
3261{
3262 struct inode *inode = file->f_mapping->host;
3263
3264 spin_lock(&inode->i_lock);
3265 atomic_set(&inode->i_writecount, 1);
3266 spin_unlock(&inode->i_lock);
3267}
3268
NeilBrown9e653b62006-06-26 00:27:58 -07003269/* mode:
3270 * 0 - completely stop and dis-assemble array
3271 * 1 - switch to readonly
3272 * 2 - stop but do not disassemble array
3273 */
3274static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
3276 int err = 0;
3277 struct gendisk *disk = mddev->gendisk;
3278
3279 if (mddev->pers) {
3280 if (atomic_read(&mddev->active)>2) {
3281 printk("md: %s still in use.\n",mdname(mddev));
3282 return -EBUSY;
3283 }
3284
3285 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003286 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3288 md_unregister_thread(mddev->sync_thread);
3289 mddev->sync_thread = NULL;
3290 }
3291
3292 del_timer_sync(&mddev->safemode_timer);
3293
3294 invalidate_partition(disk, 0);
3295
NeilBrown9e653b62006-06-26 00:27:58 -07003296 switch(mode) {
3297 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003299 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 goto out;
3301 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003302 break;
3303 case 0: /* disassemble */
3304 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003305 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003306 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 if (mddev->ro)
3308 set_disk_ro(disk, 0);
3309 blk_queue_make_request(mddev->queue, md_fail_request);
3310 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08003311 if (mddev->pers->sync_request)
3312 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 module_put(mddev->pers->owner);
3315 mddev->pers = NULL;
3316 if (mddev->ro)
3317 mddev->ro = 0;
3318 }
NeilBrown850b2b42006-10-03 01:15:46 -07003319 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 /* mark array as shutdown cleanly */
3321 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003322 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 }
NeilBrown9e653b62006-06-26 00:27:58 -07003324 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003326 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 }
NeilBrown32a76272005-06-21 17:17:14 -07003328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 /*
3330 * Free resources if final stop
3331 */
NeilBrown9e653b62006-06-26 00:27:58 -07003332 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003333 mdk_rdev_t *rdev;
3334 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 struct gendisk *disk;
3336 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3337
NeilBrown978f9462006-02-02 14:28:05 -08003338 bitmap_destroy(mddev);
3339 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003340 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003341 fput(mddev->bitmap_file);
3342 mddev->bitmap_file = NULL;
3343 }
3344 mddev->bitmap_offset = 0;
3345
NeilBrown86e6ffd2005-11-08 21:39:24 -08003346 ITERATE_RDEV(mddev,rdev,tmp)
3347 if (rdev->raid_disk >= 0) {
3348 char nm[20];
3349 sprintf(nm, "rd%d", rdev->raid_disk);
3350 sysfs_remove_link(&mddev->kobj, nm);
3351 }
3352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 export_array(mddev);
3354
3355 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003356 mddev->size = 0;
3357 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003358 mddev->recovery_cp = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 disk = mddev->gendisk;
3361 if (disk)
3362 set_capacity(disk, 0);
3363 mddev->changed = 1;
NeilBrowna8a55c32006-06-26 00:27:39 -07003364 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3366 mdname(mddev));
3367 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003368 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369out:
3370 return err;
3371}
3372
3373static void autorun_array(mddev_t *mddev)
3374{
3375 mdk_rdev_t *rdev;
3376 struct list_head *tmp;
3377 int err;
3378
NeilBrowna757e642005-04-16 15:26:42 -07003379 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
3382 printk(KERN_INFO "md: running: ");
3383
3384 ITERATE_RDEV(mddev,rdev,tmp) {
3385 char b[BDEVNAME_SIZE];
3386 printk("<%s>", bdevname(rdev->bdev,b));
3387 }
3388 printk("\n");
3389
3390 err = do_md_run (mddev);
3391 if (err) {
3392 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3393 do_md_stop (mddev, 0);
3394 }
3395}
3396
3397/*
3398 * lets try to run arrays based on all disks that have arrived
3399 * until now. (those are in pending_raid_disks)
3400 *
3401 * the method: pick the first pending disk, collect all disks with
3402 * the same UUID, remove all from the pending list and put them into
3403 * the 'same_array' list. Then order this list based on superblock
3404 * update time (freshest comes first), kick out 'old' disks and
3405 * compare superblocks. If everything's fine then run it.
3406 *
3407 * If "unit" is allocated, then bump its reference count
3408 */
3409static void autorun_devices(int part)
3410{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 struct list_head *tmp;
3412 mdk_rdev_t *rdev0, *rdev;
3413 mddev_t *mddev;
3414 char b[BDEVNAME_SIZE];
3415
3416 printk(KERN_INFO "md: autorun ...\n");
3417 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003418 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003420 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 rdev0 = list_entry(pending_raid_disks.next,
3422 mdk_rdev_t, same_set);
3423
3424 printk(KERN_INFO "md: considering %s ...\n",
3425 bdevname(rdev0->bdev,b));
3426 INIT_LIST_HEAD(&candidates);
3427 ITERATE_RDEV_PENDING(rdev,tmp)
3428 if (super_90_load(rdev, rdev0, 0) >= 0) {
3429 printk(KERN_INFO "md: adding %s ...\n",
3430 bdevname(rdev->bdev,b));
3431 list_move(&rdev->same_set, &candidates);
3432 }
3433 /*
3434 * now we have a set of devices, with all of them having
3435 * mostly sane superblocks. It's time to allocate the
3436 * mddev.
3437 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003438 if (part) {
3439 dev = MKDEV(mdp_major,
3440 rdev0->preferred_minor << MdpMinorShift);
3441 unit = MINOR(dev) >> MdpMinorShift;
3442 } else {
3443 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3444 unit = MINOR(dev);
3445 }
3446 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3448 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3449 break;
3450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
3452 md_probe(dev, NULL, NULL);
3453 mddev = mddev_find(dev);
3454 if (!mddev) {
3455 printk(KERN_ERR
3456 "md: cannot allocate memory for md drive.\n");
3457 break;
3458 }
3459 if (mddev_lock(mddev))
3460 printk(KERN_WARNING "md: %s locked, cannot run\n",
3461 mdname(mddev));
3462 else if (mddev->raid_disks || mddev->major_version
3463 || !list_empty(&mddev->disks)) {
3464 printk(KERN_WARNING
3465 "md: %s already running, cannot run %s\n",
3466 mdname(mddev), bdevname(rdev0->bdev,b));
3467 mddev_unlock(mddev);
3468 } else {
3469 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3470 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3471 list_del_init(&rdev->same_set);
3472 if (bind_rdev_to_array(rdev, mddev))
3473 export_rdev(rdev);
3474 }
3475 autorun_array(mddev);
3476 mddev_unlock(mddev);
3477 }
3478 /* on success, candidates will be empty, on error
3479 * it won't...
3480 */
3481 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3482 export_rdev(rdev);
3483 mddev_put(mddev);
3484 }
3485 printk(KERN_INFO "md: ... autorun DONE.\n");
3486}
3487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488static int get_version(void __user * arg)
3489{
3490 mdu_version_t ver;
3491
3492 ver.major = MD_MAJOR_VERSION;
3493 ver.minor = MD_MINOR_VERSION;
3494 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3495
3496 if (copy_to_user(arg, &ver, sizeof(ver)))
3497 return -EFAULT;
3498
3499 return 0;
3500}
3501
3502static int get_array_info(mddev_t * mddev, void __user * arg)
3503{
3504 mdu_array_info_t info;
3505 int nr,working,active,failed,spare;
3506 mdk_rdev_t *rdev;
3507 struct list_head *tmp;
3508
3509 nr=working=active=failed=spare=0;
3510 ITERATE_RDEV(mddev,rdev,tmp) {
3511 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003512 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 failed++;
3514 else {
3515 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003516 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 active++;
3518 else
3519 spare++;
3520 }
3521 }
3522
3523 info.major_version = mddev->major_version;
3524 info.minor_version = mddev->minor_version;
3525 info.patch_version = MD_PATCHLEVEL_VERSION;
3526 info.ctime = mddev->ctime;
3527 info.level = mddev->level;
3528 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003529 if (info.size != mddev->size) /* overflow */
3530 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 info.nr_disks = nr;
3532 info.raid_disks = mddev->raid_disks;
3533 info.md_minor = mddev->md_minor;
3534 info.not_persistent= !mddev->persistent;
3535
3536 info.utime = mddev->utime;
3537 info.state = 0;
3538 if (mddev->in_sync)
3539 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003540 if (mddev->bitmap && mddev->bitmap_offset)
3541 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 info.active_disks = active;
3543 info.working_disks = working;
3544 info.failed_disks = failed;
3545 info.spare_disks = spare;
3546
3547 info.layout = mddev->layout;
3548 info.chunk_size = mddev->chunk_size;
3549
3550 if (copy_to_user(arg, &info, sizeof(info)))
3551 return -EFAULT;
3552
3553 return 0;
3554}
3555
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003556static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003557{
3558 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3559 char *ptr, *buf = NULL;
3560 int err = -ENOMEM;
3561
3562 file = kmalloc(sizeof(*file), GFP_KERNEL);
3563 if (!file)
3564 goto out;
3565
3566 /* bitmap disabled, zero the first byte and copy out */
3567 if (!mddev->bitmap || !mddev->bitmap->file) {
3568 file->pathname[0] = '\0';
3569 goto copy_out;
3570 }
3571
3572 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3573 if (!buf)
3574 goto out;
3575
3576 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3577 if (!ptr)
3578 goto out;
3579
3580 strcpy(file->pathname, ptr);
3581
3582copy_out:
3583 err = 0;
3584 if (copy_to_user(arg, file, sizeof(*file)))
3585 err = -EFAULT;
3586out:
3587 kfree(buf);
3588 kfree(file);
3589 return err;
3590}
3591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592static int get_disk_info(mddev_t * mddev, void __user * arg)
3593{
3594 mdu_disk_info_t info;
3595 unsigned int nr;
3596 mdk_rdev_t *rdev;
3597
3598 if (copy_from_user(&info, arg, sizeof(info)))
3599 return -EFAULT;
3600
3601 nr = info.number;
3602
3603 rdev = find_rdev_nr(mddev, nr);
3604 if (rdev) {
3605 info.major = MAJOR(rdev->bdev->bd_dev);
3606 info.minor = MINOR(rdev->bdev->bd_dev);
3607 info.raid_disk = rdev->raid_disk;
3608 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003609 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003611 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 info.state |= (1<<MD_DISK_ACTIVE);
3613 info.state |= (1<<MD_DISK_SYNC);
3614 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003615 if (test_bit(WriteMostly, &rdev->flags))
3616 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 } else {
3618 info.major = info.minor = 0;
3619 info.raid_disk = -1;
3620 info.state = (1<<MD_DISK_REMOVED);
3621 }
3622
3623 if (copy_to_user(arg, &info, sizeof(info)))
3624 return -EFAULT;
3625
3626 return 0;
3627}
3628
3629static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3630{
3631 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3632 mdk_rdev_t *rdev;
3633 dev_t dev = MKDEV(info->major,info->minor);
3634
3635 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3636 return -EOVERFLOW;
3637
3638 if (!mddev->raid_disks) {
3639 int err;
3640 /* expecting a device which has a superblock */
3641 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3642 if (IS_ERR(rdev)) {
3643 printk(KERN_WARNING
3644 "md: md_import_device returned %ld\n",
3645 PTR_ERR(rdev));
3646 return PTR_ERR(rdev);
3647 }
3648 if (!list_empty(&mddev->disks)) {
3649 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3650 mdk_rdev_t, same_set);
3651 int err = super_types[mddev->major_version]
3652 .load_super(rdev, rdev0, mddev->minor_version);
3653 if (err < 0) {
3654 printk(KERN_WARNING
3655 "md: %s has different UUID to %s\n",
3656 bdevname(rdev->bdev,b),
3657 bdevname(rdev0->bdev,b2));
3658 export_rdev(rdev);
3659 return -EINVAL;
3660 }
3661 }
3662 err = bind_rdev_to_array(rdev, mddev);
3663 if (err)
3664 export_rdev(rdev);
3665 return err;
3666 }
3667
3668 /*
3669 * add_new_disk can be used once the array is assembled
3670 * to add "hot spares". They must already have a superblock
3671 * written
3672 */
3673 if (mddev->pers) {
3674 int err;
3675 if (!mddev->pers->hot_add_disk) {
3676 printk(KERN_WARNING
3677 "%s: personality does not support diskops!\n",
3678 mdname(mddev));
3679 return -EINVAL;
3680 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003681 if (mddev->persistent)
3682 rdev = md_import_device(dev, mddev->major_version,
3683 mddev->minor_version);
3684 else
3685 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 if (IS_ERR(rdev)) {
3687 printk(KERN_WARNING
3688 "md: md_import_device returned %ld\n",
3689 PTR_ERR(rdev));
3690 return PTR_ERR(rdev);
3691 }
NeilBrown41158c72005-06-21 17:17:25 -07003692 /* set save_raid_disk if appropriate */
3693 if (!mddev->persistent) {
3694 if (info->state & (1<<MD_DISK_SYNC) &&
3695 info->raid_disk < mddev->raid_disks)
3696 rdev->raid_disk = info->raid_disk;
3697 else
3698 rdev->raid_disk = -1;
3699 } else
3700 super_types[mddev->major_version].
3701 validate_super(mddev, rdev);
3702 rdev->saved_raid_disk = rdev->raid_disk;
3703
NeilBrownb2d444d2005-11-08 21:39:31 -08003704 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003705 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3706 set_bit(WriteMostly, &rdev->flags);
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 rdev->raid_disk = -1;
3709 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07003710 if (!err && !mddev->pers->hot_remove_disk) {
3711 /* If there is hot_add_disk but no hot_remove_disk
3712 * then added disks for geometry changes,
3713 * and should be added immediately.
3714 */
3715 super_types[mddev->major_version].
3716 validate_super(mddev, rdev);
3717 err = mddev->pers->hot_add_disk(mddev, rdev);
3718 if (err)
3719 unbind_rdev_from_array(rdev);
3720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 if (err)
3722 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003723
3724 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003725 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 return err;
3727 }
3728
3729 /* otherwise, add_new_disk is only allowed
3730 * for major_version==0 superblocks
3731 */
3732 if (mddev->major_version != 0) {
3733 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3734 mdname(mddev));
3735 return -EINVAL;
3736 }
3737
3738 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3739 int err;
3740 rdev = md_import_device (dev, -1, 0);
3741 if (IS_ERR(rdev)) {
3742 printk(KERN_WARNING
3743 "md: error, md_import_device() returned %ld\n",
3744 PTR_ERR(rdev));
3745 return PTR_ERR(rdev);
3746 }
3747 rdev->desc_nr = info->number;
3748 if (info->raid_disk < mddev->raid_disks)
3749 rdev->raid_disk = info->raid_disk;
3750 else
3751 rdev->raid_disk = -1;
3752
NeilBrownb2d444d2005-11-08 21:39:31 -08003753 rdev->flags = 0;
3754
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003756 if (info->state & (1<<MD_DISK_SYNC))
3757 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003759 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3760 set_bit(WriteMostly, &rdev->flags);
3761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 if (!mddev->persistent) {
3763 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3764 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3765 } else
3766 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3767 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3768
NeilBrown2bf071b2006-01-06 00:20:55 -08003769 err = bind_rdev_to_array(rdev, mddev);
3770 if (err) {
3771 export_rdev(rdev);
3772 return err;
3773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 }
3775
3776 return 0;
3777}
3778
3779static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3780{
3781 char b[BDEVNAME_SIZE];
3782 mdk_rdev_t *rdev;
3783
3784 if (!mddev->pers)
3785 return -ENODEV;
3786
3787 rdev = find_rdev(mddev, dev);
3788 if (!rdev)
3789 return -ENXIO;
3790
3791 if (rdev->raid_disk >= 0)
3792 goto busy;
3793
3794 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07003795 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08003796 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
3798 return 0;
3799busy:
3800 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3801 bdevname(rdev->bdev,b), mdname(mddev));
3802 return -EBUSY;
3803}
3804
3805static int hot_add_disk(mddev_t * mddev, dev_t dev)
3806{
3807 char b[BDEVNAME_SIZE];
3808 int err;
3809 unsigned int size;
3810 mdk_rdev_t *rdev;
3811
3812 if (!mddev->pers)
3813 return -ENODEV;
3814
3815 if (mddev->major_version != 0) {
3816 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3817 " version-0 superblocks.\n",
3818 mdname(mddev));
3819 return -EINVAL;
3820 }
3821 if (!mddev->pers->hot_add_disk) {
3822 printk(KERN_WARNING
3823 "%s: personality does not support diskops!\n",
3824 mdname(mddev));
3825 return -EINVAL;
3826 }
3827
3828 rdev = md_import_device (dev, -1, 0);
3829 if (IS_ERR(rdev)) {
3830 printk(KERN_WARNING
3831 "md: error, md_import_device() returned %ld\n",
3832 PTR_ERR(rdev));
3833 return -EINVAL;
3834 }
3835
3836 if (mddev->persistent)
3837 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3838 else
3839 rdev->sb_offset =
3840 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3841
3842 size = calc_dev_size(rdev, mddev->chunk_size);
3843 rdev->size = size;
3844
NeilBrownb2d444d2005-11-08 21:39:31 -08003845 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 printk(KERN_WARNING
3847 "md: can not hot-add faulty %s disk to %s!\n",
3848 bdevname(rdev->bdev,b), mdname(mddev));
3849 err = -EINVAL;
3850 goto abort_export;
3851 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003852 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07003854 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08003855 err = bind_rdev_to_array(rdev, mddev);
3856 if (err)
3857 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
3859 /*
3860 * The rest should better be atomic, we can have disk failures
3861 * noticed in interrupt contexts ...
3862 */
3863
3864 if (rdev->desc_nr == mddev->max_disks) {
3865 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3866 mdname(mddev));
3867 err = -EBUSY;
3868 goto abort_unbind_export;
3869 }
3870
3871 rdev->raid_disk = -1;
3872
NeilBrown850b2b42006-10-03 01:15:46 -07003873 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
3875 /*
3876 * Kick recovery, maybe this spare has to be added to the
3877 * array immediately.
3878 */
3879 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3880 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08003881 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 return 0;
3883
3884abort_unbind_export:
3885 unbind_rdev_from_array(rdev);
3886
3887abort_export:
3888 export_rdev(rdev);
3889 return err;
3890}
3891
NeilBrown32a76272005-06-21 17:17:14 -07003892static int set_bitmap_file(mddev_t *mddev, int fd)
3893{
3894 int err;
3895
NeilBrown36fa3062005-09-09 16:23:45 -07003896 if (mddev->pers) {
3897 if (!mddev->pers->quiesce)
3898 return -EBUSY;
3899 if (mddev->recovery || mddev->sync_thread)
3900 return -EBUSY;
3901 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07003902 }
3903
NeilBrown36fa3062005-09-09 16:23:45 -07003904
3905 if (fd >= 0) {
3906 if (mddev->bitmap)
3907 return -EEXIST; /* cannot add when bitmap is present */
3908 mddev->bitmap_file = fget(fd);
3909
3910 if (mddev->bitmap_file == NULL) {
3911 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3912 mdname(mddev));
3913 return -EBADF;
3914 }
3915
3916 err = deny_bitmap_write_access(mddev->bitmap_file);
3917 if (err) {
3918 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3919 mdname(mddev));
3920 fput(mddev->bitmap_file);
3921 mddev->bitmap_file = NULL;
3922 return err;
3923 }
NeilBrowna654b9d82005-06-21 17:17:27 -07003924 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07003925 } else if (mddev->bitmap == NULL)
3926 return -ENOENT; /* cannot remove what isn't there */
3927 err = 0;
3928 if (mddev->pers) {
3929 mddev->pers->quiesce(mddev, 1);
3930 if (fd >= 0)
3931 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07003932 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07003933 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07003934 fd = -1; /* make sure to put the file */
3935 }
NeilBrown36fa3062005-09-09 16:23:45 -07003936 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07003937 }
3938 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07003939 if (mddev->bitmap_file) {
3940 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07003941 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07003942 }
NeilBrown36fa3062005-09-09 16:23:45 -07003943 mddev->bitmap_file = NULL;
3944 }
3945
NeilBrown32a76272005-06-21 17:17:14 -07003946 return err;
3947}
3948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949/*
3950 * set_array_info is used two different ways
3951 * The original usage is when creating a new array.
3952 * In this usage, raid_disks is > 0 and it together with
3953 * level, size, not_persistent,layout,chunksize determine the
3954 * shape of the array.
3955 * This will always create an array with a type-0.90.0 superblock.
3956 * The newer usage is when assembling an array.
3957 * In this case raid_disks will be 0, and the major_version field is
3958 * use to determine which style super-blocks are to be found on the devices.
3959 * The minor and patch _version numbers are also kept incase the
3960 * super_block handler wishes to interpret them.
3961 */
3962static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3963{
3964
3965 if (info->raid_disks == 0) {
3966 /* just setting version number for superblock loading */
3967 if (info->major_version < 0 ||
3968 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3969 super_types[info->major_version].name == NULL) {
3970 /* maybe try to auto-load a module? */
3971 printk(KERN_INFO
3972 "md: superblock version %d not known\n",
3973 info->major_version);
3974 return -EINVAL;
3975 }
3976 mddev->major_version = info->major_version;
3977 mddev->minor_version = info->minor_version;
3978 mddev->patch_version = info->patch_version;
3979 return 0;
3980 }
3981 mddev->major_version = MD_MAJOR_VERSION;
3982 mddev->minor_version = MD_MINOR_VERSION;
3983 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3984 mddev->ctime = get_seconds();
3985
3986 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08003987 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 mddev->size = info->size;
3989 mddev->raid_disks = info->raid_disks;
3990 /* don't set md_minor, it is determined by which /dev/md* was
3991 * openned
3992 */
3993 if (info->state & (1<<MD_SB_CLEAN))
3994 mddev->recovery_cp = MaxSector;
3995 else
3996 mddev->recovery_cp = 0;
3997 mddev->persistent = ! info->not_persistent;
3998
3999 mddev->layout = info->layout;
4000 mddev->chunk_size = info->chunk_size;
4001
4002 mddev->max_disks = MD_SB_DISKS;
4003
NeilBrown850b2b42006-10-03 01:15:46 -07004004 mddev->flags = 0;
4005 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
NeilBrownb2a27032005-11-28 13:44:12 -08004007 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4008 mddev->bitmap_offset = 0;
4009
NeilBrownf6705572006-03-27 01:18:11 -08004010 mddev->reshape_position = MaxSector;
4011
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 /*
4013 * Generate a 128 bit UUID
4014 */
4015 get_random_bytes(mddev->uuid, 16);
4016
NeilBrownf6705572006-03-27 01:18:11 -08004017 mddev->new_level = mddev->level;
4018 mddev->new_chunk = mddev->chunk_size;
4019 mddev->new_layout = mddev->layout;
4020 mddev->delta_disks = 0;
4021
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 return 0;
4023}
4024
NeilBrowna35b0d62006-01-06 00:20:49 -08004025static int update_size(mddev_t *mddev, unsigned long size)
4026{
4027 mdk_rdev_t * rdev;
4028 int rv;
4029 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004030 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004031
4032 if (mddev->pers->resize == NULL)
4033 return -EINVAL;
4034 /* The "size" is the amount of each device that is used.
4035 * This can only make sense for arrays with redundancy.
4036 * linear and raid0 always use whatever space is available
4037 * We can only consider changing the size if no resync
4038 * or reconstruction is happening, and if the new size
4039 * is acceptable. It must fit before the sb_offset or,
4040 * if that is <data_offset, it must fit before the
4041 * size of each device.
4042 * If size is zero, we find the largest size that fits.
4043 */
4044 if (mddev->sync_thread)
4045 return -EBUSY;
4046 ITERATE_RDEV(mddev,rdev,tmp) {
4047 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004048 avail = rdev->size * 2;
4049
NeilBrowna35b0d62006-01-06 00:20:49 -08004050 if (fit && (size == 0 || size > avail/2))
4051 size = avail/2;
4052 if (avail < ((sector_t)size << 1))
4053 return -ENOSPC;
4054 }
4055 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4056 if (!rv) {
4057 struct block_device *bdev;
4058
4059 bdev = bdget_disk(mddev->gendisk, 0);
4060 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004061 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004062 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004063 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004064 bdput(bdev);
4065 }
4066 }
4067 return rv;
4068}
4069
NeilBrownda943b92006-01-06 00:20:54 -08004070static int update_raid_disks(mddev_t *mddev, int raid_disks)
4071{
4072 int rv;
4073 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004074 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08004075 return -EINVAL;
4076 if (raid_disks <= 0 ||
4077 raid_disks >= mddev->max_disks)
4078 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004079 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08004080 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004081 mddev->delta_disks = raid_disks - mddev->raid_disks;
4082
4083 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b92006-01-06 00:20:54 -08004084 return rv;
4085}
4086
4087
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088/*
4089 * update_array_info is used to change the configuration of an
4090 * on-line array.
4091 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4092 * fields in the info are checked against the array.
4093 * Any differences that cannot be handled will cause an error.
4094 * Normally, only one change can be managed at a time.
4095 */
4096static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4097{
4098 int rv = 0;
4099 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004100 int state = 0;
4101
4102 /* calculate expected state,ignoring low bits */
4103 if (mddev->bitmap && mddev->bitmap_offset)
4104 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
4106 if (mddev->major_version != info->major_version ||
4107 mddev->minor_version != info->minor_version ||
4108/* mddev->patch_version != info->patch_version || */
4109 mddev->ctime != info->ctime ||
4110 mddev->level != info->level ||
4111/* mddev->layout != info->layout || */
4112 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004113 mddev->chunk_size != info->chunk_size ||
4114 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4115 ((state^info->state) & 0xfffffe00)
4116 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 return -EINVAL;
4118 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004119 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 if (mddev->raid_disks != info->raid_disks) cnt++;
4121 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004122 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 if (cnt == 0) return 0;
4124 if (cnt > 1) return -EINVAL;
4125
4126 if (mddev->layout != info->layout) {
4127 /* Change layout
4128 * we don't need to do anything at the md level, the
4129 * personality will take care of it all.
4130 */
4131 if (mddev->pers->reconfig == NULL)
4132 return -EINVAL;
4133 else
4134 return mddev->pers->reconfig(mddev, info->layout, -1);
4135 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004136 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004137 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
NeilBrownda943b92006-01-06 00:20:54 -08004139 if (mddev->raid_disks != info->raid_disks)
4140 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
NeilBrown36fa3062005-09-09 16:23:45 -07004142 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4143 if (mddev->pers->quiesce == NULL)
4144 return -EINVAL;
4145 if (mddev->recovery || mddev->sync_thread)
4146 return -EBUSY;
4147 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4148 /* add the bitmap */
4149 if (mddev->bitmap)
4150 return -EEXIST;
4151 if (mddev->default_bitmap_offset == 0)
4152 return -EINVAL;
4153 mddev->bitmap_offset = mddev->default_bitmap_offset;
4154 mddev->pers->quiesce(mddev, 1);
4155 rv = bitmap_create(mddev);
4156 if (rv)
4157 bitmap_destroy(mddev);
4158 mddev->pers->quiesce(mddev, 0);
4159 } else {
4160 /* remove the bitmap */
4161 if (!mddev->bitmap)
4162 return -ENOENT;
4163 if (mddev->bitmap->file)
4164 return -EINVAL;
4165 mddev->pers->quiesce(mddev, 1);
4166 bitmap_destroy(mddev);
4167 mddev->pers->quiesce(mddev, 0);
4168 mddev->bitmap_offset = 0;
4169 }
4170 }
NeilBrown850b2b42006-10-03 01:15:46 -07004171 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 return rv;
4173}
4174
4175static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4176{
4177 mdk_rdev_t *rdev;
4178
4179 if (mddev->pers == NULL)
4180 return -ENODEV;
4181
4182 rdev = find_rdev(mddev, dev);
4183 if (!rdev)
4184 return -ENODEV;
4185
4186 md_error(mddev, rdev);
4187 return 0;
4188}
4189
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004190static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4191{
4192 mddev_t *mddev = bdev->bd_disk->private_data;
4193
4194 geo->heads = 2;
4195 geo->sectors = 4;
4196 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4197 return 0;
4198}
4199
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200static int md_ioctl(struct inode *inode, struct file *file,
4201 unsigned int cmd, unsigned long arg)
4202{
4203 int err = 0;
4204 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 mddev_t *mddev = NULL;
4206
4207 if (!capable(CAP_SYS_ADMIN))
4208 return -EACCES;
4209
4210 /*
4211 * Commands dealing with the RAID driver but not any
4212 * particular array:
4213 */
4214 switch (cmd)
4215 {
4216 case RAID_VERSION:
4217 err = get_version(argp);
4218 goto done;
4219
4220 case PRINT_RAID_DEBUG:
4221 err = 0;
4222 md_print_devices();
4223 goto done;
4224
4225#ifndef MODULE
4226 case RAID_AUTORUN:
4227 err = 0;
4228 autostart_arrays(arg);
4229 goto done;
4230#endif
4231 default:;
4232 }
4233
4234 /*
4235 * Commands creating/starting a new array:
4236 */
4237
4238 mddev = inode->i_bdev->bd_disk->private_data;
4239
4240 if (!mddev) {
4241 BUG();
4242 goto abort;
4243 }
4244
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 err = mddev_lock(mddev);
4246 if (err) {
4247 printk(KERN_INFO
4248 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4249 err, cmd);
4250 goto abort;
4251 }
4252
4253 switch (cmd)
4254 {
4255 case SET_ARRAY_INFO:
4256 {
4257 mdu_array_info_t info;
4258 if (!arg)
4259 memset(&info, 0, sizeof(info));
4260 else if (copy_from_user(&info, argp, sizeof(info))) {
4261 err = -EFAULT;
4262 goto abort_unlock;
4263 }
4264 if (mddev->pers) {
4265 err = update_array_info(mddev, &info);
4266 if (err) {
4267 printk(KERN_WARNING "md: couldn't update"
4268 " array info. %d\n", err);
4269 goto abort_unlock;
4270 }
4271 goto done_unlock;
4272 }
4273 if (!list_empty(&mddev->disks)) {
4274 printk(KERN_WARNING
4275 "md: array %s already has disks!\n",
4276 mdname(mddev));
4277 err = -EBUSY;
4278 goto abort_unlock;
4279 }
4280 if (mddev->raid_disks) {
4281 printk(KERN_WARNING
4282 "md: array %s already initialised!\n",
4283 mdname(mddev));
4284 err = -EBUSY;
4285 goto abort_unlock;
4286 }
4287 err = set_array_info(mddev, &info);
4288 if (err) {
4289 printk(KERN_WARNING "md: couldn't set"
4290 " array info. %d\n", err);
4291 goto abort_unlock;
4292 }
4293 }
4294 goto done_unlock;
4295
4296 default:;
4297 }
4298
4299 /*
4300 * Commands querying/configuring an existing array:
4301 */
NeilBrown32a76272005-06-21 17:17:14 -07004302 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4303 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
4304 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4305 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 err = -ENODEV;
4307 goto abort_unlock;
4308 }
4309
4310 /*
4311 * Commands even a read-only array can execute:
4312 */
4313 switch (cmd)
4314 {
4315 case GET_ARRAY_INFO:
4316 err = get_array_info(mddev, argp);
4317 goto done_unlock;
4318
NeilBrown32a76272005-06-21 17:17:14 -07004319 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004320 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004321 goto done_unlock;
4322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 case GET_DISK_INFO:
4324 err = get_disk_info(mddev, argp);
4325 goto done_unlock;
4326
4327 case RESTART_ARRAY_RW:
4328 err = restart_array(mddev);
4329 goto done_unlock;
4330
4331 case STOP_ARRAY:
4332 err = do_md_stop (mddev, 0);
4333 goto done_unlock;
4334
4335 case STOP_ARRAY_RO:
4336 err = do_md_stop (mddev, 1);
4337 goto done_unlock;
4338
4339 /*
4340 * We have a problem here : there is no easy way to give a CHS
4341 * virtual geometry. We currently pretend that we have a 2 heads
4342 * 4 sectors (with a BIG number of cylinders...). This drives
4343 * dosfs just mad... ;-)
4344 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 }
4346
4347 /*
4348 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004349 * superblock, so we do not allow them on read-only arrays.
4350 * However non-MD ioctls (e.g. get-size) will still come through
4351 * here and hit the 'default' below, so only disallow
4352 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 */
NeilBrownf91de922005-11-08 21:39:36 -08004354 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4355 mddev->ro && mddev->pers) {
4356 if (mddev->ro == 2) {
4357 mddev->ro = 0;
4358 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4359 md_wakeup_thread(mddev->thread);
4360
4361 } else {
4362 err = -EROFS;
4363 goto abort_unlock;
4364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 }
4366
4367 switch (cmd)
4368 {
4369 case ADD_NEW_DISK:
4370 {
4371 mdu_disk_info_t info;
4372 if (copy_from_user(&info, argp, sizeof(info)))
4373 err = -EFAULT;
4374 else
4375 err = add_new_disk(mddev, &info);
4376 goto done_unlock;
4377 }
4378
4379 case HOT_REMOVE_DISK:
4380 err = hot_remove_disk(mddev, new_decode_dev(arg));
4381 goto done_unlock;
4382
4383 case HOT_ADD_DISK:
4384 err = hot_add_disk(mddev, new_decode_dev(arg));
4385 goto done_unlock;
4386
4387 case SET_DISK_FAULTY:
4388 err = set_disk_faulty(mddev, new_decode_dev(arg));
4389 goto done_unlock;
4390
4391 case RUN_ARRAY:
4392 err = do_md_run (mddev);
4393 goto done_unlock;
4394
NeilBrown32a76272005-06-21 17:17:14 -07004395 case SET_BITMAP_FILE:
4396 err = set_bitmap_file(mddev, (int)arg);
4397 goto done_unlock;
4398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 err = -EINVAL;
4401 goto abort_unlock;
4402 }
4403
4404done_unlock:
4405abort_unlock:
4406 mddev_unlock(mddev);
4407
4408 return err;
4409done:
4410 if (err)
4411 MD_BUG();
4412abort:
4413 return err;
4414}
4415
4416static int md_open(struct inode *inode, struct file *file)
4417{
4418 /*
4419 * Succeed if we can lock the mddev, which confirms that
4420 * it isn't being stopped right now.
4421 */
4422 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4423 int err;
4424
4425 if ((err = mddev_lock(mddev)))
4426 goto out;
4427
4428 err = 0;
4429 mddev_get(mddev);
4430 mddev_unlock(mddev);
4431
4432 check_disk_change(inode->i_bdev);
4433 out:
4434 return err;
4435}
4436
4437static int md_release(struct inode *inode, struct file * file)
4438{
4439 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4440
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004441 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 mddev_put(mddev);
4443
4444 return 0;
4445}
4446
4447static int md_media_changed(struct gendisk *disk)
4448{
4449 mddev_t *mddev = disk->private_data;
4450
4451 return mddev->changed;
4452}
4453
4454static int md_revalidate(struct gendisk *disk)
4455{
4456 mddev_t *mddev = disk->private_data;
4457
4458 mddev->changed = 0;
4459 return 0;
4460}
4461static struct block_device_operations md_fops =
4462{
4463 .owner = THIS_MODULE,
4464 .open = md_open,
4465 .release = md_release,
4466 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004467 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 .media_changed = md_media_changed,
4469 .revalidate_disk= md_revalidate,
4470};
4471
Adrian Bunk75c96f82005-05-05 16:16:09 -07004472static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
4474 mdk_thread_t *thread = arg;
4475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 /*
4477 * md_thread is a 'system-thread', it's priority should be very
4478 * high. We avoid resource deadlocks individually in each
4479 * raid personality. (RAID5 does preallocation) We also use RR and
4480 * the very same RT priority as kswapd, thus we will never get
4481 * into a priority inversion deadlock.
4482 *
4483 * we definitely have to have equal or higher priority than
4484 * bdflush, otherwise bdflush will deadlock if there are too
4485 * many dirty RAID5 blocks.
4486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
NeilBrown6985c432005-10-19 21:23:47 -07004488 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004489 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
NeilBrown93588e22005-11-15 00:09:12 -08004491 /* We need to wait INTERRUPTIBLE so that
4492 * we don't add to the load-average.
4493 * That means we need to be sure no signals are
4494 * pending
4495 */
4496 if (signal_pending(current))
4497 flush_signals(current);
4498
4499 wait_event_interruptible_timeout
4500 (thread->wqueue,
4501 test_bit(THREAD_WAKEUP, &thread->flags)
4502 || kthread_should_stop(),
4503 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07004504 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
4506 clear_bit(THREAD_WAKEUP, &thread->flags);
4507
NeilBrown787453c2005-11-08 21:39:43 -08004508 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return 0;
4512}
4513
4514void md_wakeup_thread(mdk_thread_t *thread)
4515{
4516 if (thread) {
4517 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4518 set_bit(THREAD_WAKEUP, &thread->flags);
4519 wake_up(&thread->wqueue);
4520 }
4521}
4522
4523mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4524 const char *name)
4525{
4526 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
NeilBrown9ffae0c2006-01-06 00:20:32 -08004528 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 if (!thread)
4530 return NULL;
4531
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 init_waitqueue_head(&thread->wqueue);
4533
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 thread->run = run;
4535 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004536 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004537 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004538 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 kfree(thread);
4540 return NULL;
4541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 return thread;
4543}
4544
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545void md_unregister_thread(mdk_thread_t *thread)
4546{
NeilBrownd28446f2005-04-16 15:26:41 -07004547 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07004548
4549 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 kfree(thread);
4551}
4552
4553void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4554{
4555 if (!mddev) {
4556 MD_BUG();
4557 return;
4558 }
4559
NeilBrownb2d444d2005-11-08 21:39:31 -08004560 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 return;
NeilBrown32a76272005-06-21 17:17:14 -07004562/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4564 mdname(mddev),
4565 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4566 __builtin_return_address(0),__builtin_return_address(1),
4567 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004568*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004569 if (!mddev->pers)
4570 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 if (!mddev->pers->error_handler)
4572 return;
4573 mddev->pers->error_handler(mddev,rdev);
4574 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4575 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4576 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004577 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578}
4579
4580/* seq_file implementation /proc/mdstat */
4581
4582static void status_unused(struct seq_file *seq)
4583{
4584 int i = 0;
4585 mdk_rdev_t *rdev;
4586 struct list_head *tmp;
4587
4588 seq_printf(seq, "unused devices: ");
4589
4590 ITERATE_RDEV_PENDING(rdev,tmp) {
4591 char b[BDEVNAME_SIZE];
4592 i++;
4593 seq_printf(seq, "%s ",
4594 bdevname(rdev->bdev,b));
4595 }
4596 if (!i)
4597 seq_printf(seq, "<none>");
4598
4599 seq_printf(seq, "\n");
4600}
4601
4602
4603static void status_resync(struct seq_file *seq, mddev_t * mddev)
4604{
NeilBrown4588b422006-03-27 01:18:04 -08004605 sector_t max_blocks, resync, res;
4606 unsigned long dt, db, rt;
4607 int scale;
4608 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
4610 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4611
4612 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4613 max_blocks = mddev->resync_max_sectors >> 1;
4614 else
4615 max_blocks = mddev->size;
4616
4617 /*
4618 * Should not happen.
4619 */
4620 if (!max_blocks) {
4621 MD_BUG();
4622 return;
4623 }
NeilBrown4588b422006-03-27 01:18:04 -08004624 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4625 * in a sector_t, and (max_blocks>>scale) will fit in a
4626 * u32, as those are the requirements for sector_div.
4627 * Thus 'scale' must be at least 10
4628 */
4629 scale = 10;
4630 if (sizeof(sector_t) > sizeof(unsigned long)) {
4631 while ( max_blocks/2 > (1ULL<<(scale+32)))
4632 scale++;
4633 }
4634 res = (resync>>scale)*1000;
4635 sector_div(res, (u32)((max_blocks>>scale)+1));
4636
4637 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 {
NeilBrown4588b422006-03-27 01:18:04 -08004639 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 seq_printf(seq, "[");
4641 for (i = 0; i < x; i++)
4642 seq_printf(seq, "=");
4643 seq_printf(seq, ">");
4644 for (i = 0; i < y; i++)
4645 seq_printf(seq, ".");
4646 seq_printf(seq, "] ");
4647 }
NeilBrown4588b422006-03-27 01:18:04 -08004648 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004649 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4650 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07004651 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
4652 "check" :
4653 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4654 "resync" : "recovery"))),
4655 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08004656 (unsigned long long) resync,
4657 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
4659 /*
4660 * We do not want to overflow, so the order of operands and
4661 * the * 100 / 100 trick are important. We do a +1 to be
4662 * safe against division by zero. We only estimate anyway.
4663 *
4664 * dt: time from mark until now
4665 * db: blocks written from mark until now
4666 * rt: remaining time
4667 */
4668 dt = ((jiffies - mddev->resync_mark) / HZ);
4669 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07004670 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
4671 - mddev->resync_mark_cnt;
4672 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
4674 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4675
NeilBrownff4e8d92006-07-10 04:44:16 -07004676 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677}
4678
4679static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4680{
4681 struct list_head *tmp;
4682 loff_t l = *pos;
4683 mddev_t *mddev;
4684
4685 if (l >= 0x10000)
4686 return NULL;
4687 if (!l--)
4688 /* header */
4689 return (void*)1;
4690
4691 spin_lock(&all_mddevs_lock);
4692 list_for_each(tmp,&all_mddevs)
4693 if (!l--) {
4694 mddev = list_entry(tmp, mddev_t, all_mddevs);
4695 mddev_get(mddev);
4696 spin_unlock(&all_mddevs_lock);
4697 return mddev;
4698 }
4699 spin_unlock(&all_mddevs_lock);
4700 if (!l--)
4701 return (void*)2;/* tail */
4702 return NULL;
4703}
4704
4705static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4706{
4707 struct list_head *tmp;
4708 mddev_t *next_mddev, *mddev = v;
4709
4710 ++*pos;
4711 if (v == (void*)2)
4712 return NULL;
4713
4714 spin_lock(&all_mddevs_lock);
4715 if (v == (void*)1)
4716 tmp = all_mddevs.next;
4717 else
4718 tmp = mddev->all_mddevs.next;
4719 if (tmp != &all_mddevs)
4720 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4721 else {
4722 next_mddev = (void*)2;
4723 *pos = 0x10000;
4724 }
4725 spin_unlock(&all_mddevs_lock);
4726
4727 if (v != (void*)1)
4728 mddev_put(mddev);
4729 return next_mddev;
4730
4731}
4732
4733static void md_seq_stop(struct seq_file *seq, void *v)
4734{
4735 mddev_t *mddev = v;
4736
4737 if (mddev && v != (void*)1 && v != (void*)2)
4738 mddev_put(mddev);
4739}
4740
NeilBrownd7603b72006-01-06 00:20:30 -08004741struct mdstat_info {
4742 int event;
4743};
4744
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745static int md_seq_show(struct seq_file *seq, void *v)
4746{
4747 mddev_t *mddev = v;
4748 sector_t size;
4749 struct list_head *tmp2;
4750 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004751 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004752 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
4754 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004755 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 seq_printf(seq, "Personalities : ");
4757 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004758 list_for_each_entry(pers, &pers_list, list)
4759 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
4761 spin_unlock(&pers_lock);
4762 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004763 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 return 0;
4765 }
4766 if (v == (void*)2) {
4767 status_unused(seq);
4768 return 0;
4769 }
4770
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004771 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4775 seq_printf(seq, "%s : %sactive", mdname(mddev),
4776 mddev->pers ? "" : "in");
4777 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004778 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004780 if (mddev->ro==2)
4781 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 seq_printf(seq, " %s", mddev->pers->name);
4783 }
4784
4785 size = 0;
4786 ITERATE_RDEV(mddev,rdev,tmp2) {
4787 char b[BDEVNAME_SIZE];
4788 seq_printf(seq, " %s[%d]",
4789 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004790 if (test_bit(WriteMostly, &rdev->flags))
4791 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004792 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 seq_printf(seq, "(F)");
4794 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004795 } else if (rdev->raid_disk < 0)
4796 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 size += rdev->size;
4798 }
4799
4800 if (!list_empty(&mddev->disks)) {
4801 if (mddev->pers)
4802 seq_printf(seq, "\n %llu blocks",
4803 (unsigned long long)mddev->array_size);
4804 else
4805 seq_printf(seq, "\n %llu blocks",
4806 (unsigned long long)size);
4807 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004808 if (mddev->persistent) {
4809 if (mddev->major_version != 0 ||
4810 mddev->minor_version != 90) {
4811 seq_printf(seq," super %d.%d",
4812 mddev->major_version,
4813 mddev->minor_version);
4814 }
4815 } else
4816 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
4818 if (mddev->pers) {
4819 mddev->pers->status (seq, mddev);
4820 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004821 if (mddev->pers->sync_request) {
4822 if (mddev->curr_resync > 2) {
4823 status_resync (seq, mddev);
4824 seq_printf(seq, "\n ");
4825 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4826 seq_printf(seq, "\tresync=DELAYED\n ");
4827 else if (mddev->recovery_cp < MaxSector)
4828 seq_printf(seq, "\tresync=PENDING\n ");
4829 }
NeilBrown32a76272005-06-21 17:17:14 -07004830 } else
4831 seq_printf(seq, "\n ");
4832
4833 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07004834 unsigned long chunk_kb;
4835 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07004836 spin_lock_irqsave(&bitmap->lock, flags);
4837 chunk_kb = bitmap->chunksize >> 10;
4838 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4839 "%lu%s chunk",
4840 bitmap->pages - bitmap->missing_pages,
4841 bitmap->pages,
4842 (bitmap->pages - bitmap->missing_pages)
4843 << (PAGE_SHIFT - 10),
4844 chunk_kb ? chunk_kb : bitmap->chunksize,
4845 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07004846 if (bitmap->file) {
4847 seq_printf(seq, ", file: ");
4848 seq_path(seq, bitmap->file->f_vfsmnt,
4849 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07004850 }
NeilBrown78d742d2005-06-21 17:17:15 -07004851
NeilBrown32a76272005-06-21 17:17:14 -07004852 seq_printf(seq, "\n");
4853 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 }
4855
4856 seq_printf(seq, "\n");
4857 }
4858 mddev_unlock(mddev);
4859
4860 return 0;
4861}
4862
4863static struct seq_operations md_seq_ops = {
4864 .start = md_seq_start,
4865 .next = md_seq_next,
4866 .stop = md_seq_stop,
4867 .show = md_seq_show,
4868};
4869
4870static int md_seq_open(struct inode *inode, struct file *file)
4871{
4872 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08004873 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4874 if (mi == NULL)
4875 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
4877 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08004878 if (error)
4879 kfree(mi);
4880 else {
4881 struct seq_file *p = file->private_data;
4882 p->private = mi;
4883 mi->event = atomic_read(&md_event_count);
4884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 return error;
4886}
4887
NeilBrownd7603b72006-01-06 00:20:30 -08004888static int md_seq_release(struct inode *inode, struct file *file)
4889{
4890 struct seq_file *m = file->private_data;
4891 struct mdstat_info *mi = m->private;
4892 m->private = NULL;
4893 kfree(mi);
4894 return seq_release(inode, file);
4895}
4896
4897static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4898{
4899 struct seq_file *m = filp->private_data;
4900 struct mdstat_info *mi = m->private;
4901 int mask;
4902
4903 poll_wait(filp, &md_event_waiters, wait);
4904
4905 /* always allow read */
4906 mask = POLLIN | POLLRDNORM;
4907
4908 if (mi->event != atomic_read(&md_event_count))
4909 mask |= POLLERR | POLLPRI;
4910 return mask;
4911}
4912
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913static struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07004914 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 .open = md_seq_open,
4916 .read = seq_read,
4917 .llseek = seq_lseek,
NeilBrownd7603b72006-01-06 00:20:30 -08004918 .release = md_seq_release,
4919 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920};
4921
NeilBrown2604b702006-01-06 00:20:36 -08004922int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004925 list_add_tail(&p->list, &pers_list);
4926 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 spin_unlock(&pers_lock);
4928 return 0;
4929}
4930
NeilBrown2604b702006-01-06 00:20:36 -08004931int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
NeilBrown2604b702006-01-06 00:20:36 -08004933 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004935 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 spin_unlock(&pers_lock);
4937 return 0;
4938}
4939
4940static int is_mddev_idle(mddev_t *mddev)
4941{
4942 mdk_rdev_t * rdev;
4943 struct list_head *tmp;
4944 int idle;
4945 unsigned long curr_events;
4946
4947 idle = 1;
4948 ITERATE_RDEV(mddev,rdev,tmp) {
4949 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01004950 curr_events = disk_stat_read(disk, sectors[0]) +
4951 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08004953 /* The difference between curr_events and last_events
4954 * will be affected by any new non-sync IO (making
4955 * curr_events bigger) and any difference in the amount of
4956 * in-flight syncio (making current_events bigger or smaller)
4957 * The amount in-flight is currently limited to
4958 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4959 * which is at most 4096 sectors.
4960 * These numbers are fairly fragile and should be made
4961 * more robust, probably by enforcing the
4962 * 'window size' that md_do_sync sort-of uses.
4963 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 * Note: the following is an unsigned comparison.
4965 */
NeilBrownc0e48522005-11-18 01:11:01 -08004966 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 rdev->last_events = curr_events;
4968 idle = 0;
4969 }
4970 }
4971 return idle;
4972}
4973
4974void md_done_sync(mddev_t *mddev, int blocks, int ok)
4975{
4976 /* another "blocks" (512byte) blocks have been synced */
4977 atomic_sub(blocks, &mddev->recovery_active);
4978 wake_up(&mddev->recovery_wait);
4979 if (!ok) {
4980 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4981 md_wakeup_thread(mddev->thread);
4982 // stop recovery, signal do_sync ....
4983 }
4984}
4985
4986
NeilBrown06d91a52005-06-21 17:17:12 -07004987/* md_write_start(mddev, bi)
4988 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07004989 * in superblock) before writing, schedule a superblock update
4990 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07004991 */
NeilBrown3d310eb2005-06-21 17:17:26 -07004992void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993{
NeilBrown06d91a52005-06-21 17:17:12 -07004994 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07004995 return;
NeilBrown06d91a52005-06-21 17:17:12 -07004996
NeilBrownf91de922005-11-08 21:39:36 -08004997 BUG_ON(mddev->ro == 1);
4998 if (mddev->ro == 2) {
4999 /* need to switch to read/write */
5000 mddev->ro = 0;
5001 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5002 md_wakeup_thread(mddev->thread);
5003 }
NeilBrown06d91a52005-06-21 17:17:12 -07005004 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005005 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005006 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005007 if (mddev->in_sync) {
5008 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005009 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005010 md_wakeup_thread(mddev->thread);
5011 }
NeilBrowna9701a32005-11-08 21:39:34 -08005012 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005013 }
NeilBrown850b2b42006-10-03 01:15:46 -07005014 wait_event(mddev->sb_wait, mddev->flags==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015}
5016
5017void md_write_end(mddev_t *mddev)
5018{
5019 if (atomic_dec_and_test(&mddev->writes_pending)) {
5020 if (mddev->safemode == 2)
5021 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005022 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5024 }
5025}
5026
Adrian Bunk75c96f82005-05-05 16:16:09 -07005027static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
5029#define SYNC_MARKS 10
5030#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005031void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032{
5033 mddev_t *mddev2;
5034 unsigned int currspeed = 0,
5035 window;
NeilBrown57afd892005-06-21 17:17:13 -07005036 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 unsigned long mark[SYNC_MARKS];
5038 sector_t mark_cnt[SYNC_MARKS];
5039 int last_mark,m;
5040 struct list_head *tmp;
5041 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005042 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005043 struct list_head *rtmp;
5044 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005045 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
5047 /* just incase thread restarts... */
5048 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5049 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005050 if (mddev->ro) /* never try to sync a read-only array */
5051 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
NeilBrown61df9d92006-10-03 01:15:57 -07005053 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5054 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5055 desc = "data-check";
5056 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5057 desc = "requested-resync";
5058 else
5059 desc = "resync";
5060 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5061 desc = "reshape";
5062 else
5063 desc = "recovery";
5064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 /* we overload curr_resync somewhat here.
5066 * 0 == not engaged in resync at all
5067 * 2 == checking that there is no conflict with another sync
5068 * 1 == like 2, but have yielded to allow conflicting resync to
5069 * commense
5070 * other == active in resync - this many blocks
5071 *
5072 * Before starting a resync we must have set curr_resync to
5073 * 2, and then checked that every "conflicting" array has curr_resync
5074 * less than ours. When we find one that is the same or higher
5075 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5076 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5077 * This will mean we have to start checking from the beginning again.
5078 *
5079 */
5080
5081 do {
5082 mddev->curr_resync = 2;
5083
5084 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005085 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005086 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 goto skip;
5088 }
5089 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 if (mddev2 == mddev)
5091 continue;
5092 if (mddev2->curr_resync &&
5093 match_mddev_units(mddev,mddev2)) {
5094 DEFINE_WAIT(wq);
5095 if (mddev < mddev2 && mddev->curr_resync == 2) {
5096 /* arbitrarily yield */
5097 mddev->curr_resync = 1;
5098 wake_up(&resync_wait);
5099 }
5100 if (mddev > mddev2 && mddev->curr_resync == 1)
5101 /* no need to wait here, we can wait the next
5102 * time 'round when curr_resync == 2
5103 */
5104 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005105 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5106 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005107 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005108 printk(KERN_INFO "md: delaying %s of %s"
5109 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005111 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 mddev_put(mddev2);
5113 schedule();
5114 finish_wait(&resync_wait, &wq);
5115 goto try_again;
5116 }
5117 finish_wait(&resync_wait, &wq);
5118 }
5119 }
5120 } while (mddev->curr_resync < 2);
5121
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005122 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005123 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005125 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 */
5127 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005128 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005129 /* we don't use the checkpoint if there's a bitmap */
5130 if (!mddev->bitmap &&
5131 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5132 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005133 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5134 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005135 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 /* recovery follows the physical size of devices */
5137 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005138 j = MaxSector;
5139 ITERATE_RDEV(mddev,rdev,rtmp)
5140 if (rdev->raid_disk >= 0 &&
5141 !test_bit(Faulty, &rdev->flags) &&
5142 !test_bit(In_sync, &rdev->flags) &&
5143 rdev->recovery_offset < j)
5144 j = rdev->recovery_offset;
5145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
NeilBrown61df9d92006-10-03 01:15:57 -07005147 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5148 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5149 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005150 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005151 "(but not more than %d KB/sec) for %s.\n",
5152 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153
5154 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005155
NeilBrown57afd892005-06-21 17:17:13 -07005156 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 for (m = 0; m < SYNC_MARKS; m++) {
5158 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005159 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 }
5161 last_mark = 0;
5162 mddev->resync_mark = mark[last_mark];
5163 mddev->resync_mark_cnt = mark_cnt[last_mark];
5164
5165 /*
5166 * Tune reconstruction:
5167 */
5168 window = 32*(PAGE_SIZE/512);
5169 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5170 window/2,(unsigned long long) max_sectors/2);
5171
5172 atomic_set(&mddev->recovery_active, 0);
5173 init_waitqueue_head(&mddev->recovery_wait);
5174 last_check = 0;
5175
5176 if (j>2) {
5177 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005178 "md: resuming %s of %s from checkpoint.\n",
5179 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 mddev->curr_resync = j;
5181 }
5182
5183 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005184 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
NeilBrown57afd892005-06-21 17:17:13 -07005186 skipped = 0;
5187 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08005188 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005189 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5191 goto out;
5192 }
NeilBrown57afd892005-06-21 17:17:13 -07005193
5194 if (!skipped) { /* actual IO requested */
5195 io_sectors += sectors;
5196 atomic_add(sectors, &mddev->recovery_active);
5197 }
5198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 j += sectors;
5200 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005201 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005202 if (last_check == 0)
5203 /* this is the earliers that rebuilt will be
5204 * visible in /proc/mdstat
5205 */
5206 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005207
5208 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 continue;
5210
NeilBrown57afd892005-06-21 17:17:13 -07005211 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212
5213 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5214 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5215 break;
5216
5217 repeat:
5218 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5219 /* step marks */
5220 int next = (last_mark+1) % SYNC_MARKS;
5221
5222 mddev->resync_mark = mark[next];
5223 mddev->resync_mark_cnt = mark_cnt[next];
5224 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005225 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 last_mark = next;
5227 }
5228
5229
NeilBrown787453c2005-11-08 21:39:43 -08005230 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 /*
5232 * got a signal, exit.
5233 */
5234 printk(KERN_INFO
5235 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5237 goto out;
5238 }
5239
5240 /*
5241 * this loop exits only if either when we are slower than
5242 * the 'hard' speed limit, or the system was IO-idle for
5243 * a jiffy.
5244 * the system might be non-idle CPU-wise, but we only care
5245 * about not overloading the IO subsystem. (things like an
5246 * e2fsck being done on the RAID array should execute fast)
5247 */
5248 mddev->queue->unplug_fn(mddev->queue);
5249 cond_resched();
5250
NeilBrown57afd892005-06-21 17:17:13 -07005251 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5252 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
NeilBrown88202a02006-01-06 00:21:36 -08005254 if (currspeed > speed_min(mddev)) {
5255 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005257 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 goto repeat;
5259 }
5260 }
5261 }
NeilBrown61df9d92006-10-03 01:15:57 -07005262 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 /*
5264 * this also signals 'finished resyncing' to md_stop
5265 */
5266 out:
5267 mddev->queue->unplug_fn(mddev->queue);
5268
5269 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5270
5271 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005272 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
5274 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005275 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
5276 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005277 mddev->curr_resync > 2) {
5278 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5279 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5280 if (mddev->curr_resync >= mddev->recovery_cp) {
5281 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005282 "md: checkpointing %s of %s.\n",
5283 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005284 mddev->recovery_cp = mddev->curr_resync;
5285 }
5286 } else
5287 mddev->recovery_cp = MaxSector;
5288 } else {
5289 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5290 mddev->curr_resync = MaxSector;
5291 ITERATE_RDEV(mddev,rdev,rtmp)
5292 if (rdev->raid_disk >= 0 &&
5293 !test_bit(Faulty, &rdev->flags) &&
5294 !test_bit(In_sync, &rdev->flags) &&
5295 rdev->recovery_offset < mddev->curr_resync)
5296 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 }
5299
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 skip:
5301 mddev->curr_resync = 0;
5302 wake_up(&resync_wait);
5303 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5304 md_wakeup_thread(mddev->thread);
5305}
NeilBrown29269552006-03-27 01:18:10 -08005306EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
5308
5309/*
5310 * This routine is regularly called by all per-raid-array threads to
5311 * deal with generic issues like resync and super-block update.
5312 * Raid personalities that don't have a thread (linear/raid0) do not
5313 * need this as they never do any recovery or update the superblock.
5314 *
5315 * It does not do any resync itself, but rather "forks" off other threads
5316 * to do that as needed.
5317 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5318 * "->recovery" and create a thread at ->sync_thread.
5319 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5320 * and wakeups up this thread which will reap the thread and finish up.
5321 * This thread also removes any faulty devices (with nr_pending == 0).
5322 *
5323 * The overall approach is:
5324 * 1/ if the superblock needs updating, update it.
5325 * 2/ If a recovery thread is running, don't do anything else.
5326 * 3/ If recovery has finished, clean up, possibly marking spares active.
5327 * 4/ If there are any faulty devices, remove them.
5328 * 5/ If array is degraded, try to add spares devices
5329 * 6/ If array has spares or is not in-sync, start a resync thread.
5330 */
5331void md_check_recovery(mddev_t *mddev)
5332{
5333 mdk_rdev_t *rdev;
5334 struct list_head *rtmp;
5335
5336
NeilBrown5f404022005-06-21 17:17:16 -07005337 if (mddev->bitmap)
5338 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
5340 if (mddev->ro)
5341 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005342
5343 if (signal_pending(current)) {
5344 if (mddev->pers->sync_request) {
5345 printk(KERN_INFO "md: %s in immediate safe mode\n",
5346 mdname(mddev));
5347 mddev->safemode = 2;
5348 }
5349 flush_signals(current);
5350 }
5351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 if ( ! (
NeilBrown850b2b42006-10-03 01:15:46 -07005353 mddev->flags ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005355 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5356 (mddev->safemode == 1) ||
5357 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5358 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 ))
5360 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005361
NeilBrowndf5b89b2006-03-27 01:18:20 -08005362 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07005364
NeilBrowna9701a32005-11-08 21:39:34 -08005365 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005366 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5367 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5368 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07005369 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownfca4d842005-06-21 17:17:11 -07005370 }
5371 if (mddev->safemode == 1)
5372 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005373 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005374
NeilBrown850b2b42006-10-03 01:15:46 -07005375 if (mddev->flags)
5376 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005377
NeilBrown06d91a52005-06-21 17:17:12 -07005378
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5380 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5381 /* resync/recovery still happening */
5382 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5383 goto unlock;
5384 }
5385 if (mddev->sync_thread) {
5386 /* resync has finished, collect result */
5387 md_unregister_thread(mddev->sync_thread);
5388 mddev->sync_thread = NULL;
5389 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5390 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5391 /* success...*/
5392 /* activate any spares */
5393 mddev->pers->spare_active(mddev);
5394 }
NeilBrown850b2b42006-10-03 01:15:46 -07005395 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005396
5397 /* if array is no-longer degraded, then any saved_raid_disk
5398 * information must be scrapped
5399 */
5400 if (!mddev->degraded)
5401 ITERATE_RDEV(mddev,rdev,rtmp)
5402 rdev->saved_raid_disk = -1;
5403
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 mddev->recovery = 0;
5405 /* flag recovery needed just to double check */
5406 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005407 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 goto unlock;
5409 }
NeilBrown24dd4692005-11-08 21:39:26 -08005410 /* Clear some bits that don't mean anything, but
5411 * might be left set
5412 */
5413 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5414 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5415 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5416 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005418 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5419 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 /* no recovery is running.
5421 * remove any failed drives, then
5422 * add spares if possible.
5423 * Spare are also removed and re-added, to allow
5424 * the personality to fail the re-add.
5425 */
5426 ITERATE_RDEV(mddev,rdev,rtmp)
5427 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005428 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08005430 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
5431 char nm[20];
5432 sprintf(nm,"rd%d", rdev->raid_disk);
5433 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08005435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 }
5437
5438 if (mddev->degraded) {
5439 ITERATE_RDEV(mddev,rdev,rtmp)
5440 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08005441 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005442 rdev->recovery_offset = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08005443 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5444 char nm[20];
5445 sprintf(nm, "rd%d", rdev->raid_disk);
5446 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 spares++;
NeilBrownd7603b72006-01-06 00:20:30 -08005448 md_new_event(mddev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08005449 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 break;
5451 }
5452 }
5453
NeilBrown24dd4692005-11-08 21:39:26 -08005454 if (spares) {
5455 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5456 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5457 } else if (mddev->recovery_cp < MaxSector) {
5458 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5459 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5460 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005462
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 if (mddev->pers->sync_request) {
5464 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005465 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5466 /* We are adding a device or devices to an array
5467 * which has the bitmap stored on all devices.
5468 * So make sure all bitmap pages get written
5469 */
5470 bitmap_write_all(mddev->bitmap);
5471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 mddev->sync_thread = md_register_thread(md_do_sync,
5473 mddev,
5474 "%s_resync");
5475 if (!mddev->sync_thread) {
5476 printk(KERN_ERR "%s: could not start resync"
5477 " thread...\n",
5478 mdname(mddev));
5479 /* leave the spares where they are, it shouldn't hurt */
5480 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005481 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005483 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 }
5485 unlock:
5486 mddev_unlock(mddev);
5487 }
5488}
5489
Adrian Bunk75c96f82005-05-05 16:16:09 -07005490static int md_notify_reboot(struct notifier_block *this,
5491 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492{
5493 struct list_head *tmp;
5494 mddev_t *mddev;
5495
5496 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5497
5498 printk(KERN_INFO "md: stopping all md devices.\n");
5499
5500 ITERATE_MDDEV(mddev,tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005501 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005503 mddev_unlock(mddev);
5504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 /*
5506 * certain more exotic SCSI devices are known to be
5507 * volatile wrt too early system reboots. While the
5508 * right place to handle this issue is the given
5509 * driver, we do want to have a safe RAID driver ...
5510 */
5511 mdelay(1000*1);
5512 }
5513 return NOTIFY_DONE;
5514}
5515
Adrian Bunk75c96f82005-05-05 16:16:09 -07005516static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 .notifier_call = md_notify_reboot,
5518 .next = NULL,
5519 .priority = INT_MAX, /* before any real devices */
5520};
5521
5522static void md_geninit(void)
5523{
5524 struct proc_dir_entry *p;
5525
5526 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5527
5528 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5529 if (p)
5530 p->proc_fops = &md_seq_fops;
5531}
5532
Adrian Bunk75c96f82005-05-05 16:16:09 -07005533static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 if (register_blkdev(MAJOR_NR, "md"))
5536 return -1;
5537 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5538 unregister_blkdev(MAJOR_NR, "md");
5539 return -1;
5540 }
NeilBrowne8703fe2006-10-03 01:15:59 -07005541 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
5542 md_probe, NULL, NULL);
5543 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 md_probe, NULL, NULL);
5545
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 register_reboot_notifier(&md_notifier);
5547 raid_table_header = register_sysctl_table(raid_root_table, 1);
5548
5549 md_geninit();
5550 return (0);
5551}
5552
5553
5554#ifndef MODULE
5555
5556/*
5557 * Searches all registered partitions for autorun RAID arrays
5558 * at boot time.
5559 */
5560static dev_t detected_devices[128];
5561static int dev_cnt;
5562
5563void md_autodetect_dev(dev_t dev)
5564{
5565 if (dev_cnt >= 0 && dev_cnt < 127)
5566 detected_devices[dev_cnt++] = dev;
5567}
5568
5569
5570static void autostart_arrays(int part)
5571{
5572 mdk_rdev_t *rdev;
5573 int i;
5574
5575 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5576
5577 for (i = 0; i < dev_cnt; i++) {
5578 dev_t dev = detected_devices[i];
5579
5580 rdev = md_import_device(dev,0, 0);
5581 if (IS_ERR(rdev))
5582 continue;
5583
NeilBrownb2d444d2005-11-08 21:39:31 -08005584 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 MD_BUG();
5586 continue;
5587 }
5588 list_add(&rdev->same_set, &pending_raid_disks);
5589 }
5590 dev_cnt = 0;
5591
5592 autorun_devices(part);
5593}
5594
5595#endif
5596
5597static __exit void md_exit(void)
5598{
5599 mddev_t *mddev;
5600 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07005601
NeilBrowne8703fe2006-10-03 01:15:59 -07005602 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
5603 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
5605 unregister_blkdev(MAJOR_NR,"md");
5606 unregister_blkdev(mdp_major, "mdp");
5607 unregister_reboot_notifier(&md_notifier);
5608 unregister_sysctl_table(raid_table_header);
5609 remove_proc_entry("mdstat", NULL);
5610 ITERATE_MDDEV(mddev,tmp) {
5611 struct gendisk *disk = mddev->gendisk;
5612 if (!disk)
5613 continue;
5614 export_array(mddev);
5615 del_gendisk(disk);
5616 put_disk(disk);
5617 mddev->gendisk = NULL;
5618 mddev_put(mddev);
5619 }
5620}
5621
5622module_init(md_init)
5623module_exit(md_exit)
5624
NeilBrownf91de922005-11-08 21:39:36 -08005625static int get_ro(char *buffer, struct kernel_param *kp)
5626{
5627 return sprintf(buffer, "%d", start_readonly);
5628}
5629static int set_ro(const char *val, struct kernel_param *kp)
5630{
5631 char *e;
5632 int num = simple_strtoul(val, &e, 10);
5633 if (*val && (*e == '\0' || *e == '\n')) {
5634 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005635 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005636 }
5637 return -EINVAL;
5638}
5639
NeilBrown80ca3a42006-07-10 04:44:18 -07005640module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
5641module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005642
NeilBrownf91de922005-11-08 21:39:36 -08005643
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644EXPORT_SYMBOL(register_md_personality);
5645EXPORT_SYMBOL(unregister_md_personality);
5646EXPORT_SYMBOL(md_error);
5647EXPORT_SYMBOL(md_done_sync);
5648EXPORT_SYMBOL(md_write_start);
5649EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650EXPORT_SYMBOL(md_register_thread);
5651EXPORT_SYMBOL(md_unregister_thread);
5652EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653EXPORT_SYMBOL(md_check_recovery);
5654MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005655MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005656MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);