blob: 05febfd9f0712d0875b52dcb4f2c07cd15ff7c8f [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 */
NeilBrownd7603b72006-01-06 00:20:30 -080042#include <linux/poll.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080043#include <linux/mutex.h>
NeilBrown16f17b32006-06-26 00:27:37 -070044#include <linux/ctype.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.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
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001416 bdev = open_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));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001426 blkdev_put(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);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001440 blkdev_put(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)
NeilBrown1031be72007-01-26 00:57:02 -08001636 && (mddev->events & 1)
1637 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001638 mddev->events--;
1639 else {
1640 /* otherwise we have to go forward and ... */
1641 mddev->events ++;
1642 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1643 /* .. if the array isn't clean, insist on an odd 'events' */
1644 if ((mddev->events&1)==0) {
1645 mddev->events++;
1646 nospares = 0;
1647 }
1648 } else {
1649 /* otherwise insist on an even 'events' (for clean states) */
1650 if ((mddev->events&1)) {
1651 mddev->events++;
1652 nospares = 0;
1653 }
1654 }
1655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 if (!mddev->events) {
1658 /*
1659 * oops, this 64-bit counter should never wrap.
1660 * Either we are in around ~1 trillion A.C., assuming
1661 * 1 reboot per second, or we have a bug:
1662 */
1663 MD_BUG();
1664 mddev->events --;
1665 }
NeilBrown42543762006-06-26 00:27:57 -07001666 sync_sbs(mddev, nospares);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 /*
1669 * do not write anything to disk if using
1670 * nonpersistent superblocks
1671 */
NeilBrown06d91a52005-06-21 17:17:12 -07001672 if (!mddev->persistent) {
NeilBrown850b2b42006-10-03 01:15:46 -07001673 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001674 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001675 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001677 }
NeilBrowna9701a32005-11-08 21:39:34 -08001678 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 dprintk(KERN_INFO
1681 "md: updating %s RAID superblock on device (in sync %d)\n",
1682 mdname(mddev),mddev->in_sync);
1683
NeilBrown32a76272005-06-21 17:17:14 -07001684 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 ITERATE_RDEV(mddev,rdev,tmp) {
1686 char b[BDEVNAME_SIZE];
1687 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001688 if (rdev->sb_loaded != 1)
1689 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001690 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 dprintk("(skipping faulty ");
1692
1693 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001694 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001695 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001696 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001697 rdev->sb_page);
1698 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1699 bdevname(rdev->bdev,b),
1700 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001701 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 } else
1704 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001705 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /* only need to write one superblock... */
1707 break;
1708 }
NeilBrowna9701a32005-11-08 21:39:34 -08001709 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001710 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001711
NeilBrowna9701a32005-11-08 21:39:34 -08001712 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001713 if (mddev->in_sync != sync_req ||
1714 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001715 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001716 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001717 goto repeat;
1718 }
NeilBrown850b2b42006-10-03 01:15:46 -07001719 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001720 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001721 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
NeilBrownbce74da2006-01-06 00:20:41 -08001725/* words written to sysfs files may, or my not, be \n terminated.
1726 * We want to accept with case. For this we use cmd_match.
1727 */
1728static int cmd_match(const char *cmd, const char *str)
1729{
1730 /* See if cmd, written into a sysfs file, matches
1731 * str. They must either be the same, or cmd can
1732 * have a trailing newline
1733 */
1734 while (*cmd && *str && *cmd == *str) {
1735 cmd++;
1736 str++;
1737 }
1738 if (*cmd == '\n')
1739 cmd++;
1740 if (*str || *cmd)
1741 return 0;
1742 return 1;
1743}
1744
NeilBrown86e6ffd2005-11-08 21:39:24 -08001745struct rdev_sysfs_entry {
1746 struct attribute attr;
1747 ssize_t (*show)(mdk_rdev_t *, char *);
1748 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1749};
1750
1751static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001752state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001753{
1754 char *sep = "";
1755 int len=0;
1756
NeilBrownb2d444d2005-11-08 21:39:31 -08001757 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001758 len+= sprintf(page+len, "%sfaulty",sep);
1759 sep = ",";
1760 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001761 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001762 len += sprintf(page+len, "%sin_sync",sep);
1763 sep = ",";
1764 }
NeilBrownf6556752006-06-26 00:28:01 -07001765 if (test_bit(WriteMostly, &rdev->flags)) {
1766 len += sprintf(page+len, "%swrite_mostly",sep);
1767 sep = ",";
1768 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001769 if (!test_bit(Faulty, &rdev->flags) &&
1770 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001771 len += sprintf(page+len, "%sspare", sep);
1772 sep = ",";
1773 }
1774 return len+sprintf(page+len, "\n");
1775}
1776
NeilBrown45dc2de2006-06-26 00:27:58 -07001777static ssize_t
1778state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1779{
1780 /* can write
1781 * faulty - simulates and error
1782 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001783 * writemostly - sets write_mostly
1784 * -writemostly - clears write_mostly
NeilBrown45dc2de2006-06-26 00:27:58 -07001785 */
1786 int err = -EINVAL;
1787 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1788 md_error(rdev->mddev, rdev);
1789 err = 0;
1790 } else if (cmd_match(buf, "remove")) {
1791 if (rdev->raid_disk >= 0)
1792 err = -EBUSY;
1793 else {
1794 mddev_t *mddev = rdev->mddev;
1795 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001796 if (mddev->pers)
1797 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001798 md_new_event(mddev);
1799 err = 0;
1800 }
NeilBrownf6556752006-06-26 00:28:01 -07001801 } else if (cmd_match(buf, "writemostly")) {
1802 set_bit(WriteMostly, &rdev->flags);
1803 err = 0;
1804 } else if (cmd_match(buf, "-writemostly")) {
1805 clear_bit(WriteMostly, &rdev->flags);
1806 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001807 }
1808 return err ? err : len;
1809}
NeilBrown80ca3a42006-07-10 04:44:18 -07001810static struct rdev_sysfs_entry rdev_state =
1811__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001812
1813static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001814super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001815{
1816 if (rdev->sb_loaded && rdev->sb_size) {
1817 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1818 return rdev->sb_size;
1819 } else
1820 return 0;
1821}
NeilBrown96de1e62005-11-08 21:39:39 -08001822static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1823
NeilBrown4dbcdc72006-01-06 00:20:52 -08001824static ssize_t
1825errors_show(mdk_rdev_t *rdev, char *page)
1826{
1827 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1828}
1829
1830static ssize_t
1831errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1832{
1833 char *e;
1834 unsigned long n = simple_strtoul(buf, &e, 10);
1835 if (*buf && (*e == 0 || *e == '\n')) {
1836 atomic_set(&rdev->corrected_errors, n);
1837 return len;
1838 }
1839 return -EINVAL;
1840}
1841static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001842__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001843
NeilBrown014236d2006-01-06 00:20:55 -08001844static ssize_t
1845slot_show(mdk_rdev_t *rdev, char *page)
1846{
1847 if (rdev->raid_disk < 0)
1848 return sprintf(page, "none\n");
1849 else
1850 return sprintf(page, "%d\n", rdev->raid_disk);
1851}
1852
1853static ssize_t
1854slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1855{
1856 char *e;
1857 int slot = simple_strtoul(buf, &e, 10);
1858 if (strncmp(buf, "none", 4)==0)
1859 slot = -1;
1860 else if (e==buf || (*e && *e!= '\n'))
1861 return -EINVAL;
1862 if (rdev->mddev->pers)
1863 /* Cannot set slot in active array (yet) */
1864 return -EBUSY;
1865 if (slot >= rdev->mddev->raid_disks)
1866 return -ENOSPC;
1867 rdev->raid_disk = slot;
1868 /* assume it is working */
1869 rdev->flags = 0;
1870 set_bit(In_sync, &rdev->flags);
1871 return len;
1872}
1873
1874
1875static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001876__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001877
NeilBrown93c8cad2006-01-06 00:20:56 -08001878static ssize_t
1879offset_show(mdk_rdev_t *rdev, char *page)
1880{
Andrew Morton6961ece2006-01-06 00:20:59 -08001881 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001882}
1883
1884static ssize_t
1885offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1886{
1887 char *e;
1888 unsigned long long offset = simple_strtoull(buf, &e, 10);
1889 if (e==buf || (*e && *e != '\n'))
1890 return -EINVAL;
1891 if (rdev->mddev->pers)
1892 return -EBUSY;
1893 rdev->data_offset = offset;
1894 return len;
1895}
1896
1897static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001898__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001899
NeilBrown83303b62006-01-06 00:21:06 -08001900static ssize_t
1901rdev_size_show(mdk_rdev_t *rdev, char *page)
1902{
1903 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1904}
1905
1906static ssize_t
1907rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1908{
1909 char *e;
1910 unsigned long long size = simple_strtoull(buf, &e, 10);
1911 if (e==buf || (*e && *e != '\n'))
1912 return -EINVAL;
1913 if (rdev->mddev->pers)
1914 return -EBUSY;
1915 rdev->size = size;
1916 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1917 rdev->mddev->size = size;
1918 return len;
1919}
1920
1921static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07001922__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08001923
NeilBrown86e6ffd2005-11-08 21:39:24 -08001924static struct attribute *rdev_default_attrs[] = {
1925 &rdev_state.attr,
1926 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001927 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001928 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001929 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001930 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001931 NULL,
1932};
1933static ssize_t
1934rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1935{
1936 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1937 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1938
1939 if (!entry->show)
1940 return -EIO;
1941 return entry->show(rdev, page);
1942}
1943
1944static ssize_t
1945rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1946 const char *page, size_t length)
1947{
1948 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1949 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1950
1951 if (!entry->store)
1952 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07001953 if (!capable(CAP_SYS_ADMIN))
1954 return -EACCES;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001955 return entry->store(rdev, page, length);
1956}
1957
1958static void rdev_free(struct kobject *ko)
1959{
1960 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1961 kfree(rdev);
1962}
1963static struct sysfs_ops rdev_sysfs_ops = {
1964 .show = rdev_attr_show,
1965 .store = rdev_attr_store,
1966};
1967static struct kobj_type rdev_ktype = {
1968 .release = rdev_free,
1969 .sysfs_ops = &rdev_sysfs_ops,
1970 .default_attrs = rdev_default_attrs,
1971};
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973/*
1974 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1975 *
1976 * mark the device faulty if:
1977 *
1978 * - the device is nonexistent (zero size)
1979 * - the device has no valid superblock
1980 *
1981 * a faulty rdev _never_ has rdev->sb set.
1982 */
1983static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1984{
1985 char b[BDEVNAME_SIZE];
1986 int err;
1987 mdk_rdev_t *rdev;
1988 sector_t size;
1989
NeilBrown9ffae0c2006-01-06 00:20:32 -08001990 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (!rdev) {
1992 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1993 return ERR_PTR(-ENOMEM);
1994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 if ((err = alloc_disk_sb(rdev)))
1997 goto abort_free;
1998
1999 err = lock_rdev(rdev, newdev);
2000 if (err)
2001 goto abort_free;
2002
NeilBrown86e6ffd2005-11-08 21:39:24 -08002003 rdev->kobj.parent = NULL;
2004 rdev->kobj.ktype = &rdev_ktype;
2005 kobject_init(&rdev->kobj);
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002008 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002009 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002010 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002012 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002014 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002015 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2018 if (!size) {
2019 printk(KERN_WARNING
2020 "md: %s has zero or unknown size, marking faulty!\n",
2021 bdevname(rdev->bdev,b));
2022 err = -EINVAL;
2023 goto abort_free;
2024 }
2025
2026 if (super_format >= 0) {
2027 err = super_types[super_format].
2028 load_super(rdev, NULL, super_minor);
2029 if (err == -EINVAL) {
2030 printk(KERN_WARNING
2031 "md: %s has invalid sb, not importing!\n",
2032 bdevname(rdev->bdev,b));
2033 goto abort_free;
2034 }
2035 if (err < 0) {
2036 printk(KERN_WARNING
2037 "md: could not read %s's sb, not importing!\n",
2038 bdevname(rdev->bdev,b));
2039 goto abort_free;
2040 }
2041 }
2042 INIT_LIST_HEAD(&rdev->same_set);
2043
2044 return rdev;
2045
2046abort_free:
2047 if (rdev->sb_page) {
2048 if (rdev->bdev)
2049 unlock_rdev(rdev);
2050 free_disk_sb(rdev);
2051 }
2052 kfree(rdev);
2053 return ERR_PTR(err);
2054}
2055
2056/*
2057 * Check a full RAID array for plausibility
2058 */
2059
2060
NeilBrowna757e642005-04-16 15:26:42 -07002061static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 int i;
2064 struct list_head *tmp;
2065 mdk_rdev_t *rdev, *freshest;
2066 char b[BDEVNAME_SIZE];
2067
2068 freshest = NULL;
2069 ITERATE_RDEV(mddev,rdev,tmp)
2070 switch (super_types[mddev->major_version].
2071 load_super(rdev, freshest, mddev->minor_version)) {
2072 case 1:
2073 freshest = rdev;
2074 break;
2075 case 0:
2076 break;
2077 default:
2078 printk( KERN_ERR \
2079 "md: fatal superblock inconsistency in %s"
2080 " -- removing from array\n",
2081 bdevname(rdev->bdev,b));
2082 kick_rdev_from_array(rdev);
2083 }
2084
2085
2086 super_types[mddev->major_version].
2087 validate_super(mddev, freshest);
2088
2089 i = 0;
2090 ITERATE_RDEV(mddev,rdev,tmp) {
2091 if (rdev != freshest)
2092 if (super_types[mddev->major_version].
2093 validate_super(mddev, rdev)) {
2094 printk(KERN_WARNING "md: kicking non-fresh %s"
2095 " from array!\n",
2096 bdevname(rdev->bdev,b));
2097 kick_rdev_from_array(rdev);
2098 continue;
2099 }
2100 if (mddev->level == LEVEL_MULTIPATH) {
2101 rdev->desc_nr = i++;
2102 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002103 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105 }
2106
2107
2108
2109 if (mddev->recovery_cp != MaxSector &&
2110 mddev->level >= 1)
2111 printk(KERN_ERR "md: %s: raid array is not clean"
2112 " -- starting background reconstruction\n",
2113 mdname(mddev));
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
NeilBrowneae17012005-11-08 21:39:23 -08002117static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002118safe_delay_show(mddev_t *mddev, char *page)
2119{
2120 int msec = (mddev->safemode_delay*1000)/HZ;
2121 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2122}
2123static ssize_t
2124safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2125{
2126 int scale=1;
2127 int dot=0;
2128 int i;
2129 unsigned long msec;
2130 char buf[30];
2131 char *e;
2132 /* remove a period, and count digits after it */
2133 if (len >= sizeof(buf))
2134 return -EINVAL;
2135 strlcpy(buf, cbuf, len);
2136 buf[len] = 0;
2137 for (i=0; i<len; i++) {
2138 if (dot) {
2139 if (isdigit(buf[i])) {
2140 buf[i-1] = buf[i];
2141 scale *= 10;
2142 }
2143 buf[i] = 0;
2144 } else if (buf[i] == '.') {
2145 dot=1;
2146 buf[i] = 0;
2147 }
2148 }
2149 msec = simple_strtoul(buf, &e, 10);
2150 if (e == buf || (*e && *e != '\n'))
2151 return -EINVAL;
2152 msec = (msec * 1000) / scale;
2153 if (msec == 0)
2154 mddev->safemode_delay = 0;
2155 else {
2156 mddev->safemode_delay = (msec*HZ)/1000;
2157 if (mddev->safemode_delay == 0)
2158 mddev->safemode_delay = 1;
2159 }
2160 return len;
2161}
2162static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002163__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002164
2165static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002166level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002167{
NeilBrown2604b702006-01-06 00:20:36 -08002168 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002169 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002170 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002171 else if (mddev->clevel[0])
2172 return sprintf(page, "%s\n", mddev->clevel);
2173 else if (mddev->level != LEVEL_NONE)
2174 return sprintf(page, "%d\n", mddev->level);
2175 else
2176 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002177}
2178
NeilBrownd9d166c2006-01-06 00:20:51 -08002179static ssize_t
2180level_store(mddev_t *mddev, const char *buf, size_t len)
2181{
2182 int rv = len;
2183 if (mddev->pers)
2184 return -EBUSY;
2185 if (len == 0)
2186 return 0;
2187 if (len >= sizeof(mddev->clevel))
2188 return -ENOSPC;
2189 strncpy(mddev->clevel, buf, len);
2190 if (mddev->clevel[len-1] == '\n')
2191 len--;
2192 mddev->clevel[len] = 0;
2193 mddev->level = LEVEL_NONE;
2194 return rv;
2195}
2196
2197static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002198__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002199
NeilBrownd4dbd022006-06-26 00:27:59 -07002200
2201static ssize_t
2202layout_show(mddev_t *mddev, char *page)
2203{
2204 /* just a number, not meaningful for all levels */
2205 return sprintf(page, "%d\n", mddev->layout);
2206}
2207
2208static ssize_t
2209layout_store(mddev_t *mddev, const char *buf, size_t len)
2210{
2211 char *e;
2212 unsigned long n = simple_strtoul(buf, &e, 10);
2213 if (mddev->pers)
2214 return -EBUSY;
2215
2216 if (!*buf || (*e && *e != '\n'))
2217 return -EINVAL;
2218
2219 mddev->layout = n;
2220 return len;
2221}
2222static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002223__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002224
2225
NeilBrowneae17012005-11-08 21:39:23 -08002226static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002227raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002228{
NeilBrownbb636542005-11-08 21:39:45 -08002229 if (mddev->raid_disks == 0)
2230 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002231 return sprintf(page, "%d\n", mddev->raid_disks);
2232}
2233
NeilBrownda943b92006-01-06 00:20:54 -08002234static int update_raid_disks(mddev_t *mddev, int raid_disks);
2235
2236static ssize_t
2237raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2238{
NeilBrownda943b92006-01-06 00:20:54 -08002239 char *e;
2240 int rv = 0;
2241 unsigned long n = simple_strtoul(buf, &e, 10);
2242
2243 if (!*buf || (*e && *e != '\n'))
2244 return -EINVAL;
2245
2246 if (mddev->pers)
2247 rv = update_raid_disks(mddev, n);
2248 else
2249 mddev->raid_disks = n;
2250 return rv ? rv : len;
2251}
2252static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002253__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002254
NeilBrown24dd4692005-11-08 21:39:26 -08002255static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002256chunk_size_show(mddev_t *mddev, char *page)
2257{
2258 return sprintf(page, "%d\n", mddev->chunk_size);
2259}
2260
2261static ssize_t
2262chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2263{
2264 /* can only set chunk_size if array is not yet active */
2265 char *e;
2266 unsigned long n = simple_strtoul(buf, &e, 10);
2267
2268 if (mddev->pers)
2269 return -EBUSY;
2270 if (!*buf || (*e && *e != '\n'))
2271 return -EINVAL;
2272
2273 mddev->chunk_size = n;
2274 return len;
2275}
2276static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002277__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002278
NeilBrowna94213b2006-06-26 00:28:00 -07002279static ssize_t
2280resync_start_show(mddev_t *mddev, char *page)
2281{
2282 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2283}
2284
2285static ssize_t
2286resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2287{
2288 /* can only set chunk_size if array is not yet active */
2289 char *e;
2290 unsigned long long n = simple_strtoull(buf, &e, 10);
2291
2292 if (mddev->pers)
2293 return -EBUSY;
2294 if (!*buf || (*e && *e != '\n'))
2295 return -EINVAL;
2296
2297 mddev->recovery_cp = n;
2298 return len;
2299}
2300static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002301__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002302
NeilBrown9e653b62006-06-26 00:27:58 -07002303/*
2304 * The array state can be:
2305 *
2306 * clear
2307 * No devices, no size, no level
2308 * Equivalent to STOP_ARRAY ioctl
2309 * inactive
2310 * May have some settings, but array is not active
2311 * all IO results in error
2312 * When written, doesn't tear down array, but just stops it
2313 * suspended (not supported yet)
2314 * All IO requests will block. The array can be reconfigured.
2315 * Writing this, if accepted, will block until array is quiessent
2316 * readonly
2317 * no resync can happen. no superblocks get written.
2318 * write requests fail
2319 * read-auto
2320 * like readonly, but behaves like 'clean' on a write request.
2321 *
2322 * clean - no pending writes, but otherwise active.
2323 * When written to inactive array, starts without resync
2324 * If a write request arrives then
2325 * if metadata is known, mark 'dirty' and switch to 'active'.
2326 * if not known, block and switch to write-pending
2327 * If written to an active array that has pending writes, then fails.
2328 * active
2329 * fully active: IO and resync can be happening.
2330 * When written to inactive array, starts with resync
2331 *
2332 * write-pending
2333 * clean, but writes are blocked waiting for 'active' to be written.
2334 *
2335 * active-idle
2336 * like active, but no writes have been seen for a while (100msec).
2337 *
2338 */
2339enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2340 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002341static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002342 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2343 "write-pending", "active-idle", NULL };
2344
2345static int match_word(const char *word, char **list)
2346{
2347 int n;
2348 for (n=0; list[n]; n++)
2349 if (cmd_match(word, list[n]))
2350 break;
2351 return n;
2352}
2353
2354static ssize_t
2355array_state_show(mddev_t *mddev, char *page)
2356{
2357 enum array_state st = inactive;
2358
2359 if (mddev->pers)
2360 switch(mddev->ro) {
2361 case 1:
2362 st = readonly;
2363 break;
2364 case 2:
2365 st = read_auto;
2366 break;
2367 case 0:
2368 if (mddev->in_sync)
2369 st = clean;
2370 else if (mddev->safemode)
2371 st = active_idle;
2372 else
2373 st = active;
2374 }
2375 else {
2376 if (list_empty(&mddev->disks) &&
2377 mddev->raid_disks == 0 &&
2378 mddev->size == 0)
2379 st = clear;
2380 else
2381 st = inactive;
2382 }
2383 return sprintf(page, "%s\n", array_states[st]);
2384}
2385
2386static int do_md_stop(mddev_t * mddev, int ro);
2387static int do_md_run(mddev_t * mddev);
2388static int restart_array(mddev_t *mddev);
2389
2390static ssize_t
2391array_state_store(mddev_t *mddev, const char *buf, size_t len)
2392{
2393 int err = -EINVAL;
2394 enum array_state st = match_word(buf, array_states);
2395 switch(st) {
2396 case bad_word:
2397 break;
2398 case clear:
2399 /* stopping an active array */
2400 if (mddev->pers) {
2401 if (atomic_read(&mddev->active) > 1)
2402 return -EBUSY;
2403 err = do_md_stop(mddev, 0);
2404 }
2405 break;
2406 case inactive:
2407 /* stopping an active array */
2408 if (mddev->pers) {
2409 if (atomic_read(&mddev->active) > 1)
2410 return -EBUSY;
2411 err = do_md_stop(mddev, 2);
2412 }
2413 break;
2414 case suspended:
2415 break; /* not supported yet */
2416 case readonly:
2417 if (mddev->pers)
2418 err = do_md_stop(mddev, 1);
2419 else {
2420 mddev->ro = 1;
2421 err = do_md_run(mddev);
2422 }
2423 break;
2424 case read_auto:
2425 /* stopping an active array */
2426 if (mddev->pers) {
2427 err = do_md_stop(mddev, 1);
2428 if (err == 0)
2429 mddev->ro = 2; /* FIXME mark devices writable */
2430 } else {
2431 mddev->ro = 2;
2432 err = do_md_run(mddev);
2433 }
2434 break;
2435 case clean:
2436 if (mddev->pers) {
2437 restart_array(mddev);
2438 spin_lock_irq(&mddev->write_lock);
2439 if (atomic_read(&mddev->writes_pending) == 0) {
2440 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07002441 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002442 }
2443 spin_unlock_irq(&mddev->write_lock);
2444 } else {
2445 mddev->ro = 0;
2446 mddev->recovery_cp = MaxSector;
2447 err = do_md_run(mddev);
2448 }
2449 break;
2450 case active:
2451 if (mddev->pers) {
2452 restart_array(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002453 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002454 wake_up(&mddev->sb_wait);
2455 err = 0;
2456 } else {
2457 mddev->ro = 0;
2458 err = do_md_run(mddev);
2459 }
2460 break;
2461 case write_pending:
2462 case active_idle:
2463 /* these cannot be set */
2464 break;
2465 }
2466 if (err)
2467 return err;
2468 else
2469 return len;
2470}
NeilBrown80ca3a42006-07-10 04:44:18 -07002471static struct md_sysfs_entry md_array_state =
2472__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002473
NeilBrown6d7ff732006-01-06 00:21:16 -08002474static ssize_t
2475null_show(mddev_t *mddev, char *page)
2476{
2477 return -EINVAL;
2478}
2479
2480static ssize_t
2481new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2482{
2483 /* buf must be %d:%d\n? giving major and minor numbers */
2484 /* The new device is added to the array.
2485 * If the array has a persistent superblock, we read the
2486 * superblock to initialise info and check validity.
2487 * Otherwise, only checking done is that in bind_rdev_to_array,
2488 * which mainly checks size.
2489 */
2490 char *e;
2491 int major = simple_strtoul(buf, &e, 10);
2492 int minor;
2493 dev_t dev;
2494 mdk_rdev_t *rdev;
2495 int err;
2496
2497 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2498 return -EINVAL;
2499 minor = simple_strtoul(e+1, &e, 10);
2500 if (*e && *e != '\n')
2501 return -EINVAL;
2502 dev = MKDEV(major, minor);
2503 if (major != MAJOR(dev) ||
2504 minor != MINOR(dev))
2505 return -EOVERFLOW;
2506
2507
2508 if (mddev->persistent) {
2509 rdev = md_import_device(dev, mddev->major_version,
2510 mddev->minor_version);
2511 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2512 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2513 mdk_rdev_t, same_set);
2514 err = super_types[mddev->major_version]
2515 .load_super(rdev, rdev0, mddev->minor_version);
2516 if (err < 0)
2517 goto out;
2518 }
2519 } else
2520 rdev = md_import_device(dev, -1, -1);
2521
2522 if (IS_ERR(rdev))
2523 return PTR_ERR(rdev);
2524 err = bind_rdev_to_array(rdev, mddev);
2525 out:
2526 if (err)
2527 export_rdev(rdev);
2528 return err ? err : len;
2529}
2530
2531static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002532__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002533
2534static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002535bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2536{
2537 char *end;
2538 unsigned long chunk, end_chunk;
2539
2540 if (!mddev->bitmap)
2541 goto out;
2542 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2543 while (*buf) {
2544 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2545 if (buf == end) break;
2546 if (*end == '-') { /* range */
2547 buf = end + 1;
2548 end_chunk = simple_strtoul(buf, &end, 0);
2549 if (buf == end) break;
2550 }
2551 if (*end && !isspace(*end)) break;
2552 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2553 buf = end;
2554 while (isspace(*buf)) buf++;
2555 }
2556 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2557out:
2558 return len;
2559}
2560
2561static struct md_sysfs_entry md_bitmap =
2562__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2563
2564static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002565size_show(mddev_t *mddev, char *page)
2566{
2567 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2568}
2569
2570static int update_size(mddev_t *mddev, unsigned long size);
2571
2572static ssize_t
2573size_store(mddev_t *mddev, const char *buf, size_t len)
2574{
2575 /* If array is inactive, we can reduce the component size, but
2576 * not increase it (except from 0).
2577 * If array is active, we can try an on-line resize
2578 */
2579 char *e;
2580 int err = 0;
2581 unsigned long long size = simple_strtoull(buf, &e, 10);
2582 if (!*buf || *buf == '\n' ||
2583 (*e && *e != '\n'))
2584 return -EINVAL;
2585
2586 if (mddev->pers) {
2587 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002588 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002589 } else {
2590 if (mddev->size == 0 ||
2591 mddev->size > size)
2592 mddev->size = size;
2593 else
2594 err = -ENOSPC;
2595 }
2596 return err ? err : len;
2597}
2598
2599static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002600__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002601
NeilBrown8bb93aa2006-01-06 00:20:50 -08002602
2603/* Metdata version.
2604 * This is either 'none' for arrays with externally managed metadata,
2605 * or N.M for internally known formats
2606 */
2607static ssize_t
2608metadata_show(mddev_t *mddev, char *page)
2609{
2610 if (mddev->persistent)
2611 return sprintf(page, "%d.%d\n",
2612 mddev->major_version, mddev->minor_version);
2613 else
2614 return sprintf(page, "none\n");
2615}
2616
2617static ssize_t
2618metadata_store(mddev_t *mddev, const char *buf, size_t len)
2619{
2620 int major, minor;
2621 char *e;
2622 if (!list_empty(&mddev->disks))
2623 return -EBUSY;
2624
2625 if (cmd_match(buf, "none")) {
2626 mddev->persistent = 0;
2627 mddev->major_version = 0;
2628 mddev->minor_version = 90;
2629 return len;
2630 }
2631 major = simple_strtoul(buf, &e, 10);
2632 if (e==buf || *e != '.')
2633 return -EINVAL;
2634 buf = e+1;
2635 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002636 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002637 return -EINVAL;
2638 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2639 super_types[major].name == NULL)
2640 return -ENOENT;
2641 mddev->major_version = major;
2642 mddev->minor_version = minor;
2643 mddev->persistent = 1;
2644 return len;
2645}
2646
2647static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002648__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002649
NeilBrowna35b0d62006-01-06 00:20:49 -08002650static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002651action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002652{
NeilBrown7eec3142005-11-08 21:39:44 -08002653 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002654 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2655 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002656 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2657 type = "reshape";
2658 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002659 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2660 type = "resync";
2661 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2662 type = "check";
2663 else
2664 type = "repair";
2665 } else
2666 type = "recover";
2667 }
2668 return sprintf(page, "%s\n", type);
2669}
2670
2671static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002672action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002673{
NeilBrown7eec3142005-11-08 21:39:44 -08002674 if (!mddev->pers || !mddev->pers->sync_request)
2675 return -EINVAL;
2676
NeilBrownbce74da2006-01-06 00:20:41 -08002677 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002678 if (mddev->sync_thread) {
2679 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2680 md_unregister_thread(mddev->sync_thread);
2681 mddev->sync_thread = NULL;
2682 mddev->recovery = 0;
2683 }
NeilBrown03c902e2006-01-06 00:20:46 -08002684 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2685 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002686 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002687 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002688 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002689 else if (cmd_match(page, "reshape")) {
2690 int err;
2691 if (mddev->pers->start_reshape == NULL)
2692 return -EINVAL;
2693 err = mddev->pers->start_reshape(mddev);
2694 if (err)
2695 return err;
2696 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002697 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002698 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002699 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002700 return -EINVAL;
2701 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2702 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002703 }
NeilBrown03c902e2006-01-06 00:20:46 -08002704 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002705 md_wakeup_thread(mddev->thread);
2706 return len;
2707}
2708
NeilBrown9d888832005-11-08 21:39:26 -08002709static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002710mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002711{
2712 return sprintf(page, "%llu\n",
2713 (unsigned long long) mddev->resync_mismatches);
2714}
2715
NeilBrown80ca3a42006-07-10 04:44:18 -07002716static struct md_sysfs_entry md_scan_mode =
2717__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002718
NeilBrown96de1e62005-11-08 21:39:39 -08002719
NeilBrown80ca3a42006-07-10 04:44:18 -07002720static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002721
NeilBrown88202a02006-01-06 00:21:36 -08002722static ssize_t
2723sync_min_show(mddev_t *mddev, char *page)
2724{
2725 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2726 mddev->sync_speed_min ? "local": "system");
2727}
2728
2729static ssize_t
2730sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2731{
2732 int min;
2733 char *e;
2734 if (strncmp(buf, "system", 6)==0) {
2735 mddev->sync_speed_min = 0;
2736 return len;
2737 }
2738 min = simple_strtoul(buf, &e, 10);
2739 if (buf == e || (*e && *e != '\n') || min <= 0)
2740 return -EINVAL;
2741 mddev->sync_speed_min = min;
2742 return len;
2743}
2744
2745static struct md_sysfs_entry md_sync_min =
2746__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2747
2748static ssize_t
2749sync_max_show(mddev_t *mddev, char *page)
2750{
2751 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2752 mddev->sync_speed_max ? "local": "system");
2753}
2754
2755static ssize_t
2756sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2757{
2758 int max;
2759 char *e;
2760 if (strncmp(buf, "system", 6)==0) {
2761 mddev->sync_speed_max = 0;
2762 return len;
2763 }
2764 max = simple_strtoul(buf, &e, 10);
2765 if (buf == e || (*e && *e != '\n') || max <= 0)
2766 return -EINVAL;
2767 mddev->sync_speed_max = max;
2768 return len;
2769}
2770
2771static struct md_sysfs_entry md_sync_max =
2772__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2773
2774
2775static ssize_t
2776sync_speed_show(mddev_t *mddev, char *page)
2777{
2778 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07002779 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08002780 dt = ((jiffies - mddev->resync_mark) / HZ);
2781 if (!dt) dt++;
2782 db = resync - (mddev->resync_mark_cnt);
2783 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2784}
2785
NeilBrown80ca3a42006-07-10 04:44:18 -07002786static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08002787
2788static ssize_t
2789sync_completed_show(mddev_t *mddev, char *page)
2790{
2791 unsigned long max_blocks, resync;
2792
2793 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2794 max_blocks = mddev->resync_max_sectors;
2795 else
2796 max_blocks = mddev->size << 1;
2797
2798 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2799 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2800}
2801
NeilBrown80ca3a42006-07-10 04:44:18 -07002802static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08002803
NeilBrowne464eaf2006-03-27 01:18:14 -08002804static ssize_t
2805suspend_lo_show(mddev_t *mddev, char *page)
2806{
2807 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2808}
2809
2810static ssize_t
2811suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2812{
2813 char *e;
2814 unsigned long long new = simple_strtoull(buf, &e, 10);
2815
2816 if (mddev->pers->quiesce == NULL)
2817 return -EINVAL;
2818 if (buf == e || (*e && *e != '\n'))
2819 return -EINVAL;
2820 if (new >= mddev->suspend_hi ||
2821 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2822 mddev->suspend_lo = new;
2823 mddev->pers->quiesce(mddev, 2);
2824 return len;
2825 } else
2826 return -EINVAL;
2827}
2828static struct md_sysfs_entry md_suspend_lo =
2829__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2830
2831
2832static ssize_t
2833suspend_hi_show(mddev_t *mddev, char *page)
2834{
2835 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2836}
2837
2838static ssize_t
2839suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2840{
2841 char *e;
2842 unsigned long long new = simple_strtoull(buf, &e, 10);
2843
2844 if (mddev->pers->quiesce == NULL)
2845 return -EINVAL;
2846 if (buf == e || (*e && *e != '\n'))
2847 return -EINVAL;
2848 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2849 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2850 mddev->suspend_hi = new;
2851 mddev->pers->quiesce(mddev, 1);
2852 mddev->pers->quiesce(mddev, 0);
2853 return len;
2854 } else
2855 return -EINVAL;
2856}
2857static struct md_sysfs_entry md_suspend_hi =
2858__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2859
2860
NeilBrowneae17012005-11-08 21:39:23 -08002861static struct attribute *md_default_attrs[] = {
2862 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07002863 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002864 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002865 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002866 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07002867 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002868 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08002869 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07002870 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07002871 &md_array_state.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002872 NULL,
2873};
2874
2875static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002876 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002877 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002878 &md_sync_min.attr,
2879 &md_sync_max.attr,
2880 &md_sync_speed.attr,
2881 &md_sync_completed.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08002882 &md_suspend_lo.attr,
2883 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07002884 &md_bitmap.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002885 NULL,
2886};
NeilBrown411036f2005-11-08 21:39:40 -08002887static struct attribute_group md_redundancy_group = {
2888 .name = NULL,
2889 .attrs = md_redundancy_attrs,
2890};
2891
NeilBrowneae17012005-11-08 21:39:23 -08002892
2893static ssize_t
2894md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2895{
2896 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2897 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002898 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002899
2900 if (!entry->show)
2901 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002902 rv = mddev_lock(mddev);
2903 if (!rv) {
2904 rv = entry->show(mddev, page);
2905 mddev_unlock(mddev);
2906 }
NeilBrown96de1e62005-11-08 21:39:39 -08002907 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002908}
2909
2910static ssize_t
2911md_attr_store(struct kobject *kobj, struct attribute *attr,
2912 const char *page, size_t length)
2913{
2914 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2915 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002916 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002917
2918 if (!entry->store)
2919 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002920 if (!capable(CAP_SYS_ADMIN))
2921 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002922 rv = mddev_lock(mddev);
2923 if (!rv) {
2924 rv = entry->store(mddev, page, length);
2925 mddev_unlock(mddev);
2926 }
NeilBrown96de1e62005-11-08 21:39:39 -08002927 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002928}
2929
2930static void md_free(struct kobject *ko)
2931{
2932 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2933 kfree(mddev);
2934}
2935
2936static struct sysfs_ops md_sysfs_ops = {
2937 .show = md_attr_show,
2938 .store = md_attr_store,
2939};
2940static struct kobj_type md_ktype = {
2941 .release = md_free,
2942 .sysfs_ops = &md_sysfs_ops,
2943 .default_attrs = md_default_attrs,
2944};
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946int mdp_major = 0;
2947
2948static struct kobject *md_probe(dev_t dev, int *part, void *data)
2949{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002950 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 mddev_t *mddev = mddev_find(dev);
2952 struct gendisk *disk;
2953 int partitioned = (MAJOR(dev) != MD_MAJOR);
2954 int shift = partitioned ? MdpMinorShift : 0;
2955 int unit = MINOR(dev) >> shift;
2956
2957 if (!mddev)
2958 return NULL;
2959
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002960 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002962 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 mddev_put(mddev);
2964 return NULL;
2965 }
2966 disk = alloc_disk(1 << shift);
2967 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002968 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 mddev_put(mddev);
2970 return NULL;
2971 }
2972 disk->major = MAJOR(dev);
2973 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07002974 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07002976 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 disk->fops = &md_fops;
2979 disk->private_data = mddev;
2980 disk->queue = mddev->queue;
2981 add_disk(disk);
2982 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002983 mutex_unlock(&disks_mutex);
NeilBrown9c791972005-11-08 21:39:28 -08002984 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08002985 mddev->kobj.k_name = NULL;
2986 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2987 mddev->kobj.ktype = &md_ktype;
2988 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return NULL;
2990}
2991
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992static void md_safemode_timeout(unsigned long data)
2993{
2994 mddev_t *mddev = (mddev_t *) data;
2995
2996 mddev->safemode = 1;
2997 md_wakeup_thread(mddev->thread);
2998}
2999
NeilBrown6ff8d8e2006-01-06 00:20:15 -08003000static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
3002static int do_md_run(mddev_t * mddev)
3003{
NeilBrown2604b702006-01-06 00:20:36 -08003004 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 int chunk_size;
3006 struct list_head *tmp;
3007 mdk_rdev_t *rdev;
3008 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003009 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 char b[BDEVNAME_SIZE];
3011
NeilBrowna757e642005-04-16 15:26:42 -07003012 if (list_empty(&mddev->disks))
3013 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
3016 if (mddev->pers)
3017 return -EBUSY;
3018
3019 /*
3020 * Analyze all RAID superblock(s)
3021 */
NeilBrowna757e642005-04-16 15:26:42 -07003022 if (!mddev->raid_disks)
3023 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
3025 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
NeilBrown2604b702006-01-06 00:20:36 -08003027 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 if (chunk_size > MAX_CHUNK_SIZE) {
3029 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3030 chunk_size, MAX_CHUNK_SIZE);
3031 return -EINVAL;
3032 }
3033 /*
3034 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3035 */
3036 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003037 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return -EINVAL;
3039 }
3040 if (chunk_size < PAGE_SIZE) {
3041 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3042 chunk_size, PAGE_SIZE);
3043 return -EINVAL;
3044 }
3045
3046 /* devices must have minimum size of one chunk */
3047 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003048 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 continue;
3050 if (rdev->size < chunk_size / 1024) {
3051 printk(KERN_WARNING
3052 "md: Dev %s smaller than chunk_size:"
3053 " %lluk < %dk\n",
3054 bdevname(rdev->bdev,b),
3055 (unsigned long long)rdev->size,
3056 chunk_size / 1024);
3057 return -EINVAL;
3058 }
3059 }
3060 }
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003063 if (mddev->level != LEVEL_NONE)
3064 request_module("md-level-%d", mddev->level);
3065 else if (mddev->clevel[0])
3066 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067#endif
3068
3069 /*
3070 * Drop all container device buffers, from now on
3071 * the only valid external interface is through the md
3072 * device.
3073 * Also find largest hardsector size
3074 */
3075 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003076 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 continue;
3078 sync_blockdev(rdev->bdev);
3079 invalidate_bdev(rdev->bdev, 0);
3080 }
3081
3082 md_probe(mddev->unit, NULL, NULL);
3083 disk = mddev->gendisk;
3084 if (!disk)
3085 return -ENOMEM;
3086
3087 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003088 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003089 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003091 if (mddev->level != LEVEL_NONE)
3092 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3093 mddev->level);
3094 else
3095 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3096 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return -EINVAL;
3098 }
NeilBrown2604b702006-01-06 00:20:36 -08003099 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003101 mddev->level = pers->level;
3102 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
NeilBrownf6705572006-03-27 01:18:11 -08003104 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003105 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003106 /* This personality cannot handle reshaping... */
3107 mddev->pers = NULL;
3108 module_put(pers->owner);
3109 return -EINVAL;
3110 }
3111
NeilBrown657390d2005-08-26 18:34:16 -07003112 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003114 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08003115 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
NeilBrownf91de922005-11-08 21:39:36 -08003117 if (start_readonly)
3118 mddev->ro = 2; /* read-only, but switch on first write */
3119
NeilBrownb15c2e52006-01-06 00:20:16 -08003120 err = mddev->pers->run(mddev);
3121 if (!err && mddev->pers->sync_request) {
3122 err = bitmap_create(mddev);
3123 if (err) {
3124 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3125 mdname(mddev), err);
3126 mddev->pers->stop(mddev);
3127 }
3128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (err) {
3130 printk(KERN_ERR "md: pers->run() failed ...\n");
3131 module_put(mddev->pers->owner);
3132 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003133 bitmap_destroy(mddev);
3134 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 }
NeilBrown411036f2005-11-08 21:39:40 -08003136 if (mddev->pers->sync_request)
3137 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08003138 else if (mddev->ro == 2) /* auto-readonly not meaningful */
3139 mddev->ro = 0;
3140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 atomic_set(&mddev->writes_pending,0);
3142 mddev->safemode = 0;
3143 mddev->safemode_timer.function = md_safemode_timeout;
3144 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003145 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003147
3148 ITERATE_RDEV(mddev,rdev,tmp)
3149 if (rdev->raid_disk >= 0) {
3150 char nm[20];
3151 sprintf(nm, "rd%d", rdev->raid_disk);
3152 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3156
NeilBrown850b2b42006-10-03 01:15:46 -07003157 if (mddev->flags)
3158 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160 set_capacity(disk, mddev->array_size<<1);
3161
3162 /* If we call blk_queue_make_request here, it will
3163 * re-initialise max_sectors etc which may have been
3164 * refined inside -> run. So just set the bits we need to set.
3165 * Most initialisation happended when we called
3166 * blk_queue_make_request(..., md_fail_request)
3167 * earlier.
3168 */
3169 mddev->queue->queuedata = mddev;
3170 mddev->queue->make_request_fn = mddev->pers->make_request;
3171
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003172 /* If there is a partially-recovered drive we need to
3173 * start recovery here. If we leave it to md_check_recovery,
3174 * it will remove the drives and not do the right thing
3175 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003176 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003177 struct list_head *rtmp;
3178 int spares = 0;
3179 ITERATE_RDEV(mddev,rdev,rtmp)
3180 if (rdev->raid_disk >= 0 &&
3181 !test_bit(In_sync, &rdev->flags) &&
3182 !test_bit(Faulty, &rdev->flags))
3183 /* complete an interrupted recovery */
3184 spares++;
3185 if (spares && mddev->pers->sync_request) {
3186 mddev->recovery = 0;
3187 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3188 mddev->sync_thread = md_register_thread(md_do_sync,
3189 mddev,
3190 "%s_resync");
3191 if (!mddev->sync_thread) {
3192 printk(KERN_ERR "%s: could not start resync"
3193 " thread...\n",
3194 mdname(mddev));
3195 /* leave the spares where they are, it shouldn't hurt */
3196 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003197 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003198 }
3199 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003200 md_wakeup_thread(mddev->thread);
3201 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003204 md_new_event(mddev);
NeilBrown2f471302006-11-08 17:44:47 -08003205 kobject_uevent(&mddev->gendisk->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 return 0;
3207}
3208
3209static int restart_array(mddev_t *mddev)
3210{
3211 struct gendisk *disk = mddev->gendisk;
3212 int err;
3213
3214 /*
3215 * Complain if it has no devices
3216 */
3217 err = -ENXIO;
3218 if (list_empty(&mddev->disks))
3219 goto out;
3220
3221 if (mddev->pers) {
3222 err = -EBUSY;
3223 if (!mddev->ro)
3224 goto out;
3225
3226 mddev->safemode = 0;
3227 mddev->ro = 0;
3228 set_disk_ro(disk, 0);
3229
3230 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3231 mdname(mddev));
3232 /*
3233 * Kick recovery or resync if necessary
3234 */
3235 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3236 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003237 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003239 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242out:
3243 return err;
3244}
3245
NeilBrownacc55e22006-06-26 00:27:47 -07003246/* similar to deny_write_access, but accounts for our holding a reference
3247 * to the file ourselves */
3248static int deny_bitmap_write_access(struct file * file)
3249{
3250 struct inode *inode = file->f_mapping->host;
3251
3252 spin_lock(&inode->i_lock);
3253 if (atomic_read(&inode->i_writecount) > 1) {
3254 spin_unlock(&inode->i_lock);
3255 return -ETXTBSY;
3256 }
3257 atomic_set(&inode->i_writecount, -1);
3258 spin_unlock(&inode->i_lock);
3259
3260 return 0;
3261}
3262
3263static void restore_bitmap_write_access(struct file *file)
3264{
3265 struct inode *inode = file->f_mapping->host;
3266
3267 spin_lock(&inode->i_lock);
3268 atomic_set(&inode->i_writecount, 1);
3269 spin_unlock(&inode->i_lock);
3270}
3271
NeilBrown9e653b62006-06-26 00:27:58 -07003272/* mode:
3273 * 0 - completely stop and dis-assemble array
3274 * 1 - switch to readonly
3275 * 2 - stop but do not disassemble array
3276 */
3277static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
3279 int err = 0;
3280 struct gendisk *disk = mddev->gendisk;
3281
3282 if (mddev->pers) {
3283 if (atomic_read(&mddev->active)>2) {
3284 printk("md: %s still in use.\n",mdname(mddev));
3285 return -EBUSY;
3286 }
3287
3288 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003289 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3291 md_unregister_thread(mddev->sync_thread);
3292 mddev->sync_thread = NULL;
3293 }
3294
3295 del_timer_sync(&mddev->safemode_timer);
3296
3297 invalidate_partition(disk, 0);
3298
NeilBrown9e653b62006-06-26 00:27:58 -07003299 switch(mode) {
3300 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003302 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 goto out;
3304 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003305 break;
3306 case 0: /* disassemble */
3307 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003308 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003309 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 if (mddev->ro)
3311 set_disk_ro(disk, 0);
3312 blk_queue_make_request(mddev->queue, md_fail_request);
3313 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08003314 if (mddev->pers->sync_request)
3315 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 module_put(mddev->pers->owner);
3318 mddev->pers = NULL;
NeilBrown0d4ca602006-12-10 02:20:44 -08003319
3320 set_capacity(disk, 0);
3321 mddev->changed = 1;
3322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (mddev->ro)
3324 mddev->ro = 0;
3325 }
NeilBrown850b2b42006-10-03 01:15:46 -07003326 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 /* mark array as shutdown cleanly */
3328 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003329 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 }
NeilBrown9e653b62006-06-26 00:27:58 -07003331 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003333 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
NeilBrown32a76272005-06-21 17:17:14 -07003335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 /*
3337 * Free resources if final stop
3338 */
NeilBrown9e653b62006-06-26 00:27:58 -07003339 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003340 mdk_rdev_t *rdev;
3341 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3344
NeilBrown978f9462006-02-02 14:28:05 -08003345 bitmap_destroy(mddev);
3346 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003347 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003348 fput(mddev->bitmap_file);
3349 mddev->bitmap_file = NULL;
3350 }
3351 mddev->bitmap_offset = 0;
3352
NeilBrown86e6ffd2005-11-08 21:39:24 -08003353 ITERATE_RDEV(mddev,rdev,tmp)
3354 if (rdev->raid_disk >= 0) {
3355 char nm[20];
3356 sprintf(nm, "rd%d", rdev->raid_disk);
3357 sysfs_remove_link(&mddev->kobj, nm);
3358 }
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 export_array(mddev);
3361
3362 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003363 mddev->size = 0;
3364 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003365 mddev->recovery_cp = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003366
NeilBrowna8a55c32006-06-26 00:27:39 -07003367 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3369 mdname(mddev));
3370 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003371 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372out:
3373 return err;
3374}
3375
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003376#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377static void autorun_array(mddev_t *mddev)
3378{
3379 mdk_rdev_t *rdev;
3380 struct list_head *tmp;
3381 int err;
3382
NeilBrowna757e642005-04-16 15:26:42 -07003383 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
3386 printk(KERN_INFO "md: running: ");
3387
3388 ITERATE_RDEV(mddev,rdev,tmp) {
3389 char b[BDEVNAME_SIZE];
3390 printk("<%s>", bdevname(rdev->bdev,b));
3391 }
3392 printk("\n");
3393
3394 err = do_md_run (mddev);
3395 if (err) {
3396 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3397 do_md_stop (mddev, 0);
3398 }
3399}
3400
3401/*
3402 * lets try to run arrays based on all disks that have arrived
3403 * until now. (those are in pending_raid_disks)
3404 *
3405 * the method: pick the first pending disk, collect all disks with
3406 * the same UUID, remove all from the pending list and put them into
3407 * the 'same_array' list. Then order this list based on superblock
3408 * update time (freshest comes first), kick out 'old' disks and
3409 * compare superblocks. If everything's fine then run it.
3410 *
3411 * If "unit" is allocated, then bump its reference count
3412 */
3413static void autorun_devices(int part)
3414{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 struct list_head *tmp;
3416 mdk_rdev_t *rdev0, *rdev;
3417 mddev_t *mddev;
3418 char b[BDEVNAME_SIZE];
3419
3420 printk(KERN_INFO "md: autorun ...\n");
3421 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003422 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003424 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 rdev0 = list_entry(pending_raid_disks.next,
3426 mdk_rdev_t, same_set);
3427
3428 printk(KERN_INFO "md: considering %s ...\n",
3429 bdevname(rdev0->bdev,b));
3430 INIT_LIST_HEAD(&candidates);
3431 ITERATE_RDEV_PENDING(rdev,tmp)
3432 if (super_90_load(rdev, rdev0, 0) >= 0) {
3433 printk(KERN_INFO "md: adding %s ...\n",
3434 bdevname(rdev->bdev,b));
3435 list_move(&rdev->same_set, &candidates);
3436 }
3437 /*
3438 * now we have a set of devices, with all of them having
3439 * mostly sane superblocks. It's time to allocate the
3440 * mddev.
3441 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003442 if (part) {
3443 dev = MKDEV(mdp_major,
3444 rdev0->preferred_minor << MdpMinorShift);
3445 unit = MINOR(dev) >> MdpMinorShift;
3446 } else {
3447 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3448 unit = MINOR(dev);
3449 }
3450 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3452 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3453 break;
3454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
3456 md_probe(dev, NULL, NULL);
3457 mddev = mddev_find(dev);
3458 if (!mddev) {
3459 printk(KERN_ERR
3460 "md: cannot allocate memory for md drive.\n");
3461 break;
3462 }
3463 if (mddev_lock(mddev))
3464 printk(KERN_WARNING "md: %s locked, cannot run\n",
3465 mdname(mddev));
3466 else if (mddev->raid_disks || mddev->major_version
3467 || !list_empty(&mddev->disks)) {
3468 printk(KERN_WARNING
3469 "md: %s already running, cannot run %s\n",
3470 mdname(mddev), bdevname(rdev0->bdev,b));
3471 mddev_unlock(mddev);
3472 } else {
3473 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3474 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3475 list_del_init(&rdev->same_set);
3476 if (bind_rdev_to_array(rdev, mddev))
3477 export_rdev(rdev);
3478 }
3479 autorun_array(mddev);
3480 mddev_unlock(mddev);
3481 }
3482 /* on success, candidates will be empty, on error
3483 * it won't...
3484 */
3485 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3486 export_rdev(rdev);
3487 mddev_put(mddev);
3488 }
3489 printk(KERN_INFO "md: ... autorun DONE.\n");
3490}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003491#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493static int get_version(void __user * arg)
3494{
3495 mdu_version_t ver;
3496
3497 ver.major = MD_MAJOR_VERSION;
3498 ver.minor = MD_MINOR_VERSION;
3499 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3500
3501 if (copy_to_user(arg, &ver, sizeof(ver)))
3502 return -EFAULT;
3503
3504 return 0;
3505}
3506
3507static int get_array_info(mddev_t * mddev, void __user * arg)
3508{
3509 mdu_array_info_t info;
3510 int nr,working,active,failed,spare;
3511 mdk_rdev_t *rdev;
3512 struct list_head *tmp;
3513
3514 nr=working=active=failed=spare=0;
3515 ITERATE_RDEV(mddev,rdev,tmp) {
3516 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003517 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 failed++;
3519 else {
3520 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003521 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 active++;
3523 else
3524 spare++;
3525 }
3526 }
3527
3528 info.major_version = mddev->major_version;
3529 info.minor_version = mddev->minor_version;
3530 info.patch_version = MD_PATCHLEVEL_VERSION;
3531 info.ctime = mddev->ctime;
3532 info.level = mddev->level;
3533 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003534 if (info.size != mddev->size) /* overflow */
3535 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 info.nr_disks = nr;
3537 info.raid_disks = mddev->raid_disks;
3538 info.md_minor = mddev->md_minor;
3539 info.not_persistent= !mddev->persistent;
3540
3541 info.utime = mddev->utime;
3542 info.state = 0;
3543 if (mddev->in_sync)
3544 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003545 if (mddev->bitmap && mddev->bitmap_offset)
3546 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 info.active_disks = active;
3548 info.working_disks = working;
3549 info.failed_disks = failed;
3550 info.spare_disks = spare;
3551
3552 info.layout = mddev->layout;
3553 info.chunk_size = mddev->chunk_size;
3554
3555 if (copy_to_user(arg, &info, sizeof(info)))
3556 return -EFAULT;
3557
3558 return 0;
3559}
3560
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003561static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003562{
3563 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3564 char *ptr, *buf = NULL;
3565 int err = -ENOMEM;
3566
NeilBrown2a2275d2007-01-26 00:57:11 -08003567 md_allow_write(mddev);
3568
NeilBrown32a76272005-06-21 17:17:14 -07003569 file = kmalloc(sizeof(*file), GFP_KERNEL);
3570 if (!file)
3571 goto out;
3572
3573 /* bitmap disabled, zero the first byte and copy out */
3574 if (!mddev->bitmap || !mddev->bitmap->file) {
3575 file->pathname[0] = '\0';
3576 goto copy_out;
3577 }
3578
3579 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3580 if (!buf)
3581 goto out;
3582
3583 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3584 if (!ptr)
3585 goto out;
3586
3587 strcpy(file->pathname, ptr);
3588
3589copy_out:
3590 err = 0;
3591 if (copy_to_user(arg, file, sizeof(*file)))
3592 err = -EFAULT;
3593out:
3594 kfree(buf);
3595 kfree(file);
3596 return err;
3597}
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599static int get_disk_info(mddev_t * mddev, void __user * arg)
3600{
3601 mdu_disk_info_t info;
3602 unsigned int nr;
3603 mdk_rdev_t *rdev;
3604
3605 if (copy_from_user(&info, arg, sizeof(info)))
3606 return -EFAULT;
3607
3608 nr = info.number;
3609
3610 rdev = find_rdev_nr(mddev, nr);
3611 if (rdev) {
3612 info.major = MAJOR(rdev->bdev->bd_dev);
3613 info.minor = MINOR(rdev->bdev->bd_dev);
3614 info.raid_disk = rdev->raid_disk;
3615 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003616 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003618 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 info.state |= (1<<MD_DISK_ACTIVE);
3620 info.state |= (1<<MD_DISK_SYNC);
3621 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003622 if (test_bit(WriteMostly, &rdev->flags))
3623 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 } else {
3625 info.major = info.minor = 0;
3626 info.raid_disk = -1;
3627 info.state = (1<<MD_DISK_REMOVED);
3628 }
3629
3630 if (copy_to_user(arg, &info, sizeof(info)))
3631 return -EFAULT;
3632
3633 return 0;
3634}
3635
3636static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3637{
3638 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3639 mdk_rdev_t *rdev;
3640 dev_t dev = MKDEV(info->major,info->minor);
3641
3642 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3643 return -EOVERFLOW;
3644
3645 if (!mddev->raid_disks) {
3646 int err;
3647 /* expecting a device which has a superblock */
3648 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3649 if (IS_ERR(rdev)) {
3650 printk(KERN_WARNING
3651 "md: md_import_device returned %ld\n",
3652 PTR_ERR(rdev));
3653 return PTR_ERR(rdev);
3654 }
3655 if (!list_empty(&mddev->disks)) {
3656 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3657 mdk_rdev_t, same_set);
3658 int err = super_types[mddev->major_version]
3659 .load_super(rdev, rdev0, mddev->minor_version);
3660 if (err < 0) {
3661 printk(KERN_WARNING
3662 "md: %s has different UUID to %s\n",
3663 bdevname(rdev->bdev,b),
3664 bdevname(rdev0->bdev,b2));
3665 export_rdev(rdev);
3666 return -EINVAL;
3667 }
3668 }
3669 err = bind_rdev_to_array(rdev, mddev);
3670 if (err)
3671 export_rdev(rdev);
3672 return err;
3673 }
3674
3675 /*
3676 * add_new_disk can be used once the array is assembled
3677 * to add "hot spares". They must already have a superblock
3678 * written
3679 */
3680 if (mddev->pers) {
3681 int err;
3682 if (!mddev->pers->hot_add_disk) {
3683 printk(KERN_WARNING
3684 "%s: personality does not support diskops!\n",
3685 mdname(mddev));
3686 return -EINVAL;
3687 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003688 if (mddev->persistent)
3689 rdev = md_import_device(dev, mddev->major_version,
3690 mddev->minor_version);
3691 else
3692 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 if (IS_ERR(rdev)) {
3694 printk(KERN_WARNING
3695 "md: md_import_device returned %ld\n",
3696 PTR_ERR(rdev));
3697 return PTR_ERR(rdev);
3698 }
NeilBrown41158c72005-06-21 17:17:25 -07003699 /* set save_raid_disk if appropriate */
3700 if (!mddev->persistent) {
3701 if (info->state & (1<<MD_DISK_SYNC) &&
3702 info->raid_disk < mddev->raid_disks)
3703 rdev->raid_disk = info->raid_disk;
3704 else
3705 rdev->raid_disk = -1;
3706 } else
3707 super_types[mddev->major_version].
3708 validate_super(mddev, rdev);
3709 rdev->saved_raid_disk = rdev->raid_disk;
3710
NeilBrownb2d444d2005-11-08 21:39:31 -08003711 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003712 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3713 set_bit(WriteMostly, &rdev->flags);
3714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 rdev->raid_disk = -1;
3716 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07003717 if (!err && !mddev->pers->hot_remove_disk) {
3718 /* If there is hot_add_disk but no hot_remove_disk
3719 * then added disks for geometry changes,
3720 * and should be added immediately.
3721 */
3722 super_types[mddev->major_version].
3723 validate_super(mddev, rdev);
3724 err = mddev->pers->hot_add_disk(mddev, rdev);
3725 if (err)
3726 unbind_rdev_from_array(rdev);
3727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 if (err)
3729 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003730
NeilBrown17571282006-12-10 02:20:52 -08003731 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07003732 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003733 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 return err;
3735 }
3736
3737 /* otherwise, add_new_disk is only allowed
3738 * for major_version==0 superblocks
3739 */
3740 if (mddev->major_version != 0) {
3741 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3742 mdname(mddev));
3743 return -EINVAL;
3744 }
3745
3746 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3747 int err;
3748 rdev = md_import_device (dev, -1, 0);
3749 if (IS_ERR(rdev)) {
3750 printk(KERN_WARNING
3751 "md: error, md_import_device() returned %ld\n",
3752 PTR_ERR(rdev));
3753 return PTR_ERR(rdev);
3754 }
3755 rdev->desc_nr = info->number;
3756 if (info->raid_disk < mddev->raid_disks)
3757 rdev->raid_disk = info->raid_disk;
3758 else
3759 rdev->raid_disk = -1;
3760
NeilBrownb2d444d2005-11-08 21:39:31 -08003761 rdev->flags = 0;
3762
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003764 if (info->state & (1<<MD_DISK_SYNC))
3765 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003767 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3768 set_bit(WriteMostly, &rdev->flags);
3769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 if (!mddev->persistent) {
3771 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3772 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3773 } else
3774 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3775 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3776
NeilBrown2bf071b2006-01-06 00:20:55 -08003777 err = bind_rdev_to_array(rdev, mddev);
3778 if (err) {
3779 export_rdev(rdev);
3780 return err;
3781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 }
3783
3784 return 0;
3785}
3786
3787static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3788{
3789 char b[BDEVNAME_SIZE];
3790 mdk_rdev_t *rdev;
3791
3792 if (!mddev->pers)
3793 return -ENODEV;
3794
3795 rdev = find_rdev(mddev, dev);
3796 if (!rdev)
3797 return -ENXIO;
3798
3799 if (rdev->raid_disk >= 0)
3800 goto busy;
3801
3802 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07003803 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08003804 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
3806 return 0;
3807busy:
3808 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3809 bdevname(rdev->bdev,b), mdname(mddev));
3810 return -EBUSY;
3811}
3812
3813static int hot_add_disk(mddev_t * mddev, dev_t dev)
3814{
3815 char b[BDEVNAME_SIZE];
3816 int err;
3817 unsigned int size;
3818 mdk_rdev_t *rdev;
3819
3820 if (!mddev->pers)
3821 return -ENODEV;
3822
3823 if (mddev->major_version != 0) {
3824 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3825 " version-0 superblocks.\n",
3826 mdname(mddev));
3827 return -EINVAL;
3828 }
3829 if (!mddev->pers->hot_add_disk) {
3830 printk(KERN_WARNING
3831 "%s: personality does not support diskops!\n",
3832 mdname(mddev));
3833 return -EINVAL;
3834 }
3835
3836 rdev = md_import_device (dev, -1, 0);
3837 if (IS_ERR(rdev)) {
3838 printk(KERN_WARNING
3839 "md: error, md_import_device() returned %ld\n",
3840 PTR_ERR(rdev));
3841 return -EINVAL;
3842 }
3843
3844 if (mddev->persistent)
3845 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3846 else
3847 rdev->sb_offset =
3848 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3849
3850 size = calc_dev_size(rdev, mddev->chunk_size);
3851 rdev->size = size;
3852
NeilBrownb2d444d2005-11-08 21:39:31 -08003853 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 printk(KERN_WARNING
3855 "md: can not hot-add faulty %s disk to %s!\n",
3856 bdevname(rdev->bdev,b), mdname(mddev));
3857 err = -EINVAL;
3858 goto abort_export;
3859 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003860 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07003862 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08003863 err = bind_rdev_to_array(rdev, mddev);
3864 if (err)
3865 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
3867 /*
3868 * The rest should better be atomic, we can have disk failures
3869 * noticed in interrupt contexts ...
3870 */
3871
3872 if (rdev->desc_nr == mddev->max_disks) {
3873 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3874 mdname(mddev));
3875 err = -EBUSY;
3876 goto abort_unbind_export;
3877 }
3878
3879 rdev->raid_disk = -1;
3880
NeilBrown850b2b42006-10-03 01:15:46 -07003881 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
3883 /*
3884 * Kick recovery, maybe this spare has to be added to the
3885 * array immediately.
3886 */
3887 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3888 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08003889 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 return 0;
3891
3892abort_unbind_export:
3893 unbind_rdev_from_array(rdev);
3894
3895abort_export:
3896 export_rdev(rdev);
3897 return err;
3898}
3899
NeilBrown32a76272005-06-21 17:17:14 -07003900static int set_bitmap_file(mddev_t *mddev, int fd)
3901{
3902 int err;
3903
NeilBrown36fa3062005-09-09 16:23:45 -07003904 if (mddev->pers) {
3905 if (!mddev->pers->quiesce)
3906 return -EBUSY;
3907 if (mddev->recovery || mddev->sync_thread)
3908 return -EBUSY;
3909 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07003910 }
3911
NeilBrown36fa3062005-09-09 16:23:45 -07003912
3913 if (fd >= 0) {
3914 if (mddev->bitmap)
3915 return -EEXIST; /* cannot add when bitmap is present */
3916 mddev->bitmap_file = fget(fd);
3917
3918 if (mddev->bitmap_file == NULL) {
3919 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3920 mdname(mddev));
3921 return -EBADF;
3922 }
3923
3924 err = deny_bitmap_write_access(mddev->bitmap_file);
3925 if (err) {
3926 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3927 mdname(mddev));
3928 fput(mddev->bitmap_file);
3929 mddev->bitmap_file = NULL;
3930 return err;
3931 }
NeilBrowna654b9d82005-06-21 17:17:27 -07003932 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07003933 } else if (mddev->bitmap == NULL)
3934 return -ENOENT; /* cannot remove what isn't there */
3935 err = 0;
3936 if (mddev->pers) {
3937 mddev->pers->quiesce(mddev, 1);
3938 if (fd >= 0)
3939 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07003940 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07003941 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07003942 fd = -1; /* make sure to put the file */
3943 }
NeilBrown36fa3062005-09-09 16:23:45 -07003944 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07003945 }
3946 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07003947 if (mddev->bitmap_file) {
3948 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07003949 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07003950 }
NeilBrown36fa3062005-09-09 16:23:45 -07003951 mddev->bitmap_file = NULL;
3952 }
3953
NeilBrown32a76272005-06-21 17:17:14 -07003954 return err;
3955}
3956
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957/*
3958 * set_array_info is used two different ways
3959 * The original usage is when creating a new array.
3960 * In this usage, raid_disks is > 0 and it together with
3961 * level, size, not_persistent,layout,chunksize determine the
3962 * shape of the array.
3963 * This will always create an array with a type-0.90.0 superblock.
3964 * The newer usage is when assembling an array.
3965 * In this case raid_disks will be 0, and the major_version field is
3966 * use to determine which style super-blocks are to be found on the devices.
3967 * The minor and patch _version numbers are also kept incase the
3968 * super_block handler wishes to interpret them.
3969 */
3970static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3971{
3972
3973 if (info->raid_disks == 0) {
3974 /* just setting version number for superblock loading */
3975 if (info->major_version < 0 ||
3976 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3977 super_types[info->major_version].name == NULL) {
3978 /* maybe try to auto-load a module? */
3979 printk(KERN_INFO
3980 "md: superblock version %d not known\n",
3981 info->major_version);
3982 return -EINVAL;
3983 }
3984 mddev->major_version = info->major_version;
3985 mddev->minor_version = info->minor_version;
3986 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08003987 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 return 0;
3989 }
3990 mddev->major_version = MD_MAJOR_VERSION;
3991 mddev->minor_version = MD_MINOR_VERSION;
3992 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3993 mddev->ctime = get_seconds();
3994
3995 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08003996 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 mddev->size = info->size;
3998 mddev->raid_disks = info->raid_disks;
3999 /* don't set md_minor, it is determined by which /dev/md* was
4000 * openned
4001 */
4002 if (info->state & (1<<MD_SB_CLEAN))
4003 mddev->recovery_cp = MaxSector;
4004 else
4005 mddev->recovery_cp = 0;
4006 mddev->persistent = ! info->not_persistent;
4007
4008 mddev->layout = info->layout;
4009 mddev->chunk_size = info->chunk_size;
4010
4011 mddev->max_disks = MD_SB_DISKS;
4012
NeilBrown850b2b42006-10-03 01:15:46 -07004013 mddev->flags = 0;
4014 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
NeilBrownb2a27032005-11-28 13:44:12 -08004016 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4017 mddev->bitmap_offset = 0;
4018
NeilBrownf6705572006-03-27 01:18:11 -08004019 mddev->reshape_position = MaxSector;
4020
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 /*
4022 * Generate a 128 bit UUID
4023 */
4024 get_random_bytes(mddev->uuid, 16);
4025
NeilBrownf6705572006-03-27 01:18:11 -08004026 mddev->new_level = mddev->level;
4027 mddev->new_chunk = mddev->chunk_size;
4028 mddev->new_layout = mddev->layout;
4029 mddev->delta_disks = 0;
4030
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 return 0;
4032}
4033
NeilBrowna35b0d62006-01-06 00:20:49 -08004034static int update_size(mddev_t *mddev, unsigned long size)
4035{
4036 mdk_rdev_t * rdev;
4037 int rv;
4038 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004039 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004040
4041 if (mddev->pers->resize == NULL)
4042 return -EINVAL;
4043 /* The "size" is the amount of each device that is used.
4044 * This can only make sense for arrays with redundancy.
4045 * linear and raid0 always use whatever space is available
4046 * We can only consider changing the size if no resync
4047 * or reconstruction is happening, and if the new size
4048 * is acceptable. It must fit before the sb_offset or,
4049 * if that is <data_offset, it must fit before the
4050 * size of each device.
4051 * If size is zero, we find the largest size that fits.
4052 */
4053 if (mddev->sync_thread)
4054 return -EBUSY;
4055 ITERATE_RDEV(mddev,rdev,tmp) {
4056 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004057 avail = rdev->size * 2;
4058
NeilBrowna35b0d62006-01-06 00:20:49 -08004059 if (fit && (size == 0 || size > avail/2))
4060 size = avail/2;
4061 if (avail < ((sector_t)size << 1))
4062 return -ENOSPC;
4063 }
4064 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4065 if (!rv) {
4066 struct block_device *bdev;
4067
4068 bdev = bdget_disk(mddev->gendisk, 0);
4069 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004070 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004071 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004072 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004073 bdput(bdev);
4074 }
4075 }
4076 return rv;
4077}
4078
NeilBrownda943b92006-01-06 00:20:54 -08004079static int update_raid_disks(mddev_t *mddev, int raid_disks)
4080{
4081 int rv;
4082 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004083 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08004084 return -EINVAL;
4085 if (raid_disks <= 0 ||
4086 raid_disks >= mddev->max_disks)
4087 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004088 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08004089 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004090 mddev->delta_disks = raid_disks - mddev->raid_disks;
4091
4092 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b92006-01-06 00:20:54 -08004093 return rv;
4094}
4095
4096
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097/*
4098 * update_array_info is used to change the configuration of an
4099 * on-line array.
4100 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4101 * fields in the info are checked against the array.
4102 * Any differences that cannot be handled will cause an error.
4103 * Normally, only one change can be managed at a time.
4104 */
4105static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4106{
4107 int rv = 0;
4108 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004109 int state = 0;
4110
4111 /* calculate expected state,ignoring low bits */
4112 if (mddev->bitmap && mddev->bitmap_offset)
4113 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
4115 if (mddev->major_version != info->major_version ||
4116 mddev->minor_version != info->minor_version ||
4117/* mddev->patch_version != info->patch_version || */
4118 mddev->ctime != info->ctime ||
4119 mddev->level != info->level ||
4120/* mddev->layout != info->layout || */
4121 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004122 mddev->chunk_size != info->chunk_size ||
4123 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4124 ((state^info->state) & 0xfffffe00)
4125 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 return -EINVAL;
4127 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004128 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 if (mddev->raid_disks != info->raid_disks) cnt++;
4130 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004131 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 if (cnt == 0) return 0;
4133 if (cnt > 1) return -EINVAL;
4134
4135 if (mddev->layout != info->layout) {
4136 /* Change layout
4137 * we don't need to do anything at the md level, the
4138 * personality will take care of it all.
4139 */
4140 if (mddev->pers->reconfig == NULL)
4141 return -EINVAL;
4142 else
4143 return mddev->pers->reconfig(mddev, info->layout, -1);
4144 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004145 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004146 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
NeilBrownda943b92006-01-06 00:20:54 -08004148 if (mddev->raid_disks != info->raid_disks)
4149 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
NeilBrown36fa3062005-09-09 16:23:45 -07004151 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4152 if (mddev->pers->quiesce == NULL)
4153 return -EINVAL;
4154 if (mddev->recovery || mddev->sync_thread)
4155 return -EBUSY;
4156 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4157 /* add the bitmap */
4158 if (mddev->bitmap)
4159 return -EEXIST;
4160 if (mddev->default_bitmap_offset == 0)
4161 return -EINVAL;
4162 mddev->bitmap_offset = mddev->default_bitmap_offset;
4163 mddev->pers->quiesce(mddev, 1);
4164 rv = bitmap_create(mddev);
4165 if (rv)
4166 bitmap_destroy(mddev);
4167 mddev->pers->quiesce(mddev, 0);
4168 } else {
4169 /* remove the bitmap */
4170 if (!mddev->bitmap)
4171 return -ENOENT;
4172 if (mddev->bitmap->file)
4173 return -EINVAL;
4174 mddev->pers->quiesce(mddev, 1);
4175 bitmap_destroy(mddev);
4176 mddev->pers->quiesce(mddev, 0);
4177 mddev->bitmap_offset = 0;
4178 }
4179 }
NeilBrown850b2b42006-10-03 01:15:46 -07004180 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return rv;
4182}
4183
4184static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4185{
4186 mdk_rdev_t *rdev;
4187
4188 if (mddev->pers == NULL)
4189 return -ENODEV;
4190
4191 rdev = find_rdev(mddev, dev);
4192 if (!rdev)
4193 return -ENODEV;
4194
4195 md_error(mddev, rdev);
4196 return 0;
4197}
4198
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004199static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4200{
4201 mddev_t *mddev = bdev->bd_disk->private_data;
4202
4203 geo->heads = 2;
4204 geo->sectors = 4;
4205 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4206 return 0;
4207}
4208
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209static int md_ioctl(struct inode *inode, struct file *file,
4210 unsigned int cmd, unsigned long arg)
4211{
4212 int err = 0;
4213 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 mddev_t *mddev = NULL;
4215
4216 if (!capable(CAP_SYS_ADMIN))
4217 return -EACCES;
4218
4219 /*
4220 * Commands dealing with the RAID driver but not any
4221 * particular array:
4222 */
4223 switch (cmd)
4224 {
4225 case RAID_VERSION:
4226 err = get_version(argp);
4227 goto done;
4228
4229 case PRINT_RAID_DEBUG:
4230 err = 0;
4231 md_print_devices();
4232 goto done;
4233
4234#ifndef MODULE
4235 case RAID_AUTORUN:
4236 err = 0;
4237 autostart_arrays(arg);
4238 goto done;
4239#endif
4240 default:;
4241 }
4242
4243 /*
4244 * Commands creating/starting a new array:
4245 */
4246
4247 mddev = inode->i_bdev->bd_disk->private_data;
4248
4249 if (!mddev) {
4250 BUG();
4251 goto abort;
4252 }
4253
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 err = mddev_lock(mddev);
4255 if (err) {
4256 printk(KERN_INFO
4257 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4258 err, cmd);
4259 goto abort;
4260 }
4261
4262 switch (cmd)
4263 {
4264 case SET_ARRAY_INFO:
4265 {
4266 mdu_array_info_t info;
4267 if (!arg)
4268 memset(&info, 0, sizeof(info));
4269 else if (copy_from_user(&info, argp, sizeof(info))) {
4270 err = -EFAULT;
4271 goto abort_unlock;
4272 }
4273 if (mddev->pers) {
4274 err = update_array_info(mddev, &info);
4275 if (err) {
4276 printk(KERN_WARNING "md: couldn't update"
4277 " array info. %d\n", err);
4278 goto abort_unlock;
4279 }
4280 goto done_unlock;
4281 }
4282 if (!list_empty(&mddev->disks)) {
4283 printk(KERN_WARNING
4284 "md: array %s already has disks!\n",
4285 mdname(mddev));
4286 err = -EBUSY;
4287 goto abort_unlock;
4288 }
4289 if (mddev->raid_disks) {
4290 printk(KERN_WARNING
4291 "md: array %s already initialised!\n",
4292 mdname(mddev));
4293 err = -EBUSY;
4294 goto abort_unlock;
4295 }
4296 err = set_array_info(mddev, &info);
4297 if (err) {
4298 printk(KERN_WARNING "md: couldn't set"
4299 " array info. %d\n", err);
4300 goto abort_unlock;
4301 }
4302 }
4303 goto done_unlock;
4304
4305 default:;
4306 }
4307
4308 /*
4309 * Commands querying/configuring an existing array:
4310 */
NeilBrown32a76272005-06-21 17:17:14 -07004311 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004312 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrown32a76272005-06-21 17:17:14 -07004313 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
NeilBrown3f9d7b02006-12-22 01:11:41 -08004314 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4315 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 err = -ENODEV;
4317 goto abort_unlock;
4318 }
4319
4320 /*
4321 * Commands even a read-only array can execute:
4322 */
4323 switch (cmd)
4324 {
4325 case GET_ARRAY_INFO:
4326 err = get_array_info(mddev, argp);
4327 goto done_unlock;
4328
NeilBrown32a76272005-06-21 17:17:14 -07004329 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004330 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004331 goto done_unlock;
4332
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 case GET_DISK_INFO:
4334 err = get_disk_info(mddev, argp);
4335 goto done_unlock;
4336
4337 case RESTART_ARRAY_RW:
4338 err = restart_array(mddev);
4339 goto done_unlock;
4340
4341 case STOP_ARRAY:
4342 err = do_md_stop (mddev, 0);
4343 goto done_unlock;
4344
4345 case STOP_ARRAY_RO:
4346 err = do_md_stop (mddev, 1);
4347 goto done_unlock;
4348
4349 /*
4350 * We have a problem here : there is no easy way to give a CHS
4351 * virtual geometry. We currently pretend that we have a 2 heads
4352 * 4 sectors (with a BIG number of cylinders...). This drives
4353 * dosfs just mad... ;-)
4354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 }
4356
4357 /*
4358 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004359 * superblock, so we do not allow them on read-only arrays.
4360 * However non-MD ioctls (e.g. get-size) will still come through
4361 * here and hit the 'default' below, so only disallow
4362 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 */
NeilBrownf91de922005-11-08 21:39:36 -08004364 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4365 mddev->ro && mddev->pers) {
4366 if (mddev->ro == 2) {
4367 mddev->ro = 0;
4368 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4369 md_wakeup_thread(mddev->thread);
4370
4371 } else {
4372 err = -EROFS;
4373 goto abort_unlock;
4374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 }
4376
4377 switch (cmd)
4378 {
4379 case ADD_NEW_DISK:
4380 {
4381 mdu_disk_info_t info;
4382 if (copy_from_user(&info, argp, sizeof(info)))
4383 err = -EFAULT;
4384 else
4385 err = add_new_disk(mddev, &info);
4386 goto done_unlock;
4387 }
4388
4389 case HOT_REMOVE_DISK:
4390 err = hot_remove_disk(mddev, new_decode_dev(arg));
4391 goto done_unlock;
4392
4393 case HOT_ADD_DISK:
4394 err = hot_add_disk(mddev, new_decode_dev(arg));
4395 goto done_unlock;
4396
4397 case SET_DISK_FAULTY:
4398 err = set_disk_faulty(mddev, new_decode_dev(arg));
4399 goto done_unlock;
4400
4401 case RUN_ARRAY:
4402 err = do_md_run (mddev);
4403 goto done_unlock;
4404
NeilBrown32a76272005-06-21 17:17:14 -07004405 case SET_BITMAP_FILE:
4406 err = set_bitmap_file(mddev, (int)arg);
4407 goto done_unlock;
4408
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 err = -EINVAL;
4411 goto abort_unlock;
4412 }
4413
4414done_unlock:
4415abort_unlock:
4416 mddev_unlock(mddev);
4417
4418 return err;
4419done:
4420 if (err)
4421 MD_BUG();
4422abort:
4423 return err;
4424}
4425
4426static int md_open(struct inode *inode, struct file *file)
4427{
4428 /*
4429 * Succeed if we can lock the mddev, which confirms that
4430 * it isn't being stopped right now.
4431 */
4432 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4433 int err;
4434
NeilBrownd63a5a72006-12-08 02:36:17 -08004435 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 goto out;
4437
4438 err = 0;
4439 mddev_get(mddev);
4440 mddev_unlock(mddev);
4441
4442 check_disk_change(inode->i_bdev);
4443 out:
4444 return err;
4445}
4446
4447static int md_release(struct inode *inode, struct file * file)
4448{
4449 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4450
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004451 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 mddev_put(mddev);
4453
4454 return 0;
4455}
4456
4457static int md_media_changed(struct gendisk *disk)
4458{
4459 mddev_t *mddev = disk->private_data;
4460
4461 return mddev->changed;
4462}
4463
4464static int md_revalidate(struct gendisk *disk)
4465{
4466 mddev_t *mddev = disk->private_data;
4467
4468 mddev->changed = 0;
4469 return 0;
4470}
4471static struct block_device_operations md_fops =
4472{
4473 .owner = THIS_MODULE,
4474 .open = md_open,
4475 .release = md_release,
4476 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004477 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 .media_changed = md_media_changed,
4479 .revalidate_disk= md_revalidate,
4480};
4481
Adrian Bunk75c96f82005-05-05 16:16:09 -07004482static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483{
4484 mdk_thread_t *thread = arg;
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 /*
4487 * md_thread is a 'system-thread', it's priority should be very
4488 * high. We avoid resource deadlocks individually in each
4489 * raid personality. (RAID5 does preallocation) We also use RR and
4490 * the very same RT priority as kswapd, thus we will never get
4491 * into a priority inversion deadlock.
4492 *
4493 * we definitely have to have equal or higher priority than
4494 * bdflush, otherwise bdflush will deadlock if there are too
4495 * many dirty RAID5 blocks.
4496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Rafael J. Wysocki4b438a22006-11-08 17:44:48 -08004498 current->flags |= PF_NOFREEZE;
NeilBrown6985c432005-10-19 21:23:47 -07004499 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004500 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
NeilBrown93588e22005-11-15 00:09:12 -08004502 /* We need to wait INTERRUPTIBLE so that
4503 * we don't add to the load-average.
4504 * That means we need to be sure no signals are
4505 * pending
4506 */
4507 if (signal_pending(current))
4508 flush_signals(current);
4509
4510 wait_event_interruptible_timeout
4511 (thread->wqueue,
4512 test_bit(THREAD_WAKEUP, &thread->flags)
4513 || kthread_should_stop(),
4514 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
4516 clear_bit(THREAD_WAKEUP, &thread->flags);
4517
NeilBrown787453c2005-11-08 21:39:43 -08004518 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 return 0;
4522}
4523
4524void md_wakeup_thread(mdk_thread_t *thread)
4525{
4526 if (thread) {
4527 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4528 set_bit(THREAD_WAKEUP, &thread->flags);
4529 wake_up(&thread->wqueue);
4530 }
4531}
4532
4533mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4534 const char *name)
4535{
4536 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
NeilBrown9ffae0c2006-01-06 00:20:32 -08004538 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 if (!thread)
4540 return NULL;
4541
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 init_waitqueue_head(&thread->wqueue);
4543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 thread->run = run;
4545 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004546 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004547 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004548 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 kfree(thread);
4550 return NULL;
4551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 return thread;
4553}
4554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555void md_unregister_thread(mdk_thread_t *thread)
4556{
NeilBrownd28446f2005-04-16 15:26:41 -07004557 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07004558
4559 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 kfree(thread);
4561}
4562
4563void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4564{
4565 if (!mddev) {
4566 MD_BUG();
4567 return;
4568 }
4569
NeilBrownb2d444d2005-11-08 21:39:31 -08004570 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 return;
NeilBrown32a76272005-06-21 17:17:14 -07004572/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4574 mdname(mddev),
4575 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4576 __builtin_return_address(0),__builtin_return_address(1),
4577 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004578*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004579 if (!mddev->pers)
4580 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 if (!mddev->pers->error_handler)
4582 return;
4583 mddev->pers->error_handler(mddev,rdev);
4584 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4585 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4586 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004587 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588}
4589
4590/* seq_file implementation /proc/mdstat */
4591
4592static void status_unused(struct seq_file *seq)
4593{
4594 int i = 0;
4595 mdk_rdev_t *rdev;
4596 struct list_head *tmp;
4597
4598 seq_printf(seq, "unused devices: ");
4599
4600 ITERATE_RDEV_PENDING(rdev,tmp) {
4601 char b[BDEVNAME_SIZE];
4602 i++;
4603 seq_printf(seq, "%s ",
4604 bdevname(rdev->bdev,b));
4605 }
4606 if (!i)
4607 seq_printf(seq, "<none>");
4608
4609 seq_printf(seq, "\n");
4610}
4611
4612
4613static void status_resync(struct seq_file *seq, mddev_t * mddev)
4614{
NeilBrown4588b422006-03-27 01:18:04 -08004615 sector_t max_blocks, resync, res;
4616 unsigned long dt, db, rt;
4617 int scale;
4618 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
4620 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4621
4622 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4623 max_blocks = mddev->resync_max_sectors >> 1;
4624 else
4625 max_blocks = mddev->size;
4626
4627 /*
4628 * Should not happen.
4629 */
4630 if (!max_blocks) {
4631 MD_BUG();
4632 return;
4633 }
NeilBrown4588b422006-03-27 01:18:04 -08004634 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4635 * in a sector_t, and (max_blocks>>scale) will fit in a
4636 * u32, as those are the requirements for sector_div.
4637 * Thus 'scale' must be at least 10
4638 */
4639 scale = 10;
4640 if (sizeof(sector_t) > sizeof(unsigned long)) {
4641 while ( max_blocks/2 > (1ULL<<(scale+32)))
4642 scale++;
4643 }
4644 res = (resync>>scale)*1000;
4645 sector_div(res, (u32)((max_blocks>>scale)+1));
4646
4647 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 {
NeilBrown4588b422006-03-27 01:18:04 -08004649 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 seq_printf(seq, "[");
4651 for (i = 0; i < x; i++)
4652 seq_printf(seq, "=");
4653 seq_printf(seq, ">");
4654 for (i = 0; i < y; i++)
4655 seq_printf(seq, ".");
4656 seq_printf(seq, "] ");
4657 }
NeilBrown4588b422006-03-27 01:18:04 -08004658 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004659 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4660 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07004661 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
4662 "check" :
4663 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4664 "resync" : "recovery"))),
4665 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08004666 (unsigned long long) resync,
4667 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
4669 /*
4670 * We do not want to overflow, so the order of operands and
4671 * the * 100 / 100 trick are important. We do a +1 to be
4672 * safe against division by zero. We only estimate anyway.
4673 *
4674 * dt: time from mark until now
4675 * db: blocks written from mark until now
4676 * rt: remaining time
4677 */
4678 dt = ((jiffies - mddev->resync_mark) / HZ);
4679 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07004680 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
4681 - mddev->resync_mark_cnt;
4682 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
4684 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4685
NeilBrownff4e8d92006-07-10 04:44:16 -07004686 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687}
4688
4689static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4690{
4691 struct list_head *tmp;
4692 loff_t l = *pos;
4693 mddev_t *mddev;
4694
4695 if (l >= 0x10000)
4696 return NULL;
4697 if (!l--)
4698 /* header */
4699 return (void*)1;
4700
4701 spin_lock(&all_mddevs_lock);
4702 list_for_each(tmp,&all_mddevs)
4703 if (!l--) {
4704 mddev = list_entry(tmp, mddev_t, all_mddevs);
4705 mddev_get(mddev);
4706 spin_unlock(&all_mddevs_lock);
4707 return mddev;
4708 }
4709 spin_unlock(&all_mddevs_lock);
4710 if (!l--)
4711 return (void*)2;/* tail */
4712 return NULL;
4713}
4714
4715static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4716{
4717 struct list_head *tmp;
4718 mddev_t *next_mddev, *mddev = v;
4719
4720 ++*pos;
4721 if (v == (void*)2)
4722 return NULL;
4723
4724 spin_lock(&all_mddevs_lock);
4725 if (v == (void*)1)
4726 tmp = all_mddevs.next;
4727 else
4728 tmp = mddev->all_mddevs.next;
4729 if (tmp != &all_mddevs)
4730 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4731 else {
4732 next_mddev = (void*)2;
4733 *pos = 0x10000;
4734 }
4735 spin_unlock(&all_mddevs_lock);
4736
4737 if (v != (void*)1)
4738 mddev_put(mddev);
4739 return next_mddev;
4740
4741}
4742
4743static void md_seq_stop(struct seq_file *seq, void *v)
4744{
4745 mddev_t *mddev = v;
4746
4747 if (mddev && v != (void*)1 && v != (void*)2)
4748 mddev_put(mddev);
4749}
4750
NeilBrownd7603b72006-01-06 00:20:30 -08004751struct mdstat_info {
4752 int event;
4753};
4754
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755static int md_seq_show(struct seq_file *seq, void *v)
4756{
4757 mddev_t *mddev = v;
4758 sector_t size;
4759 struct list_head *tmp2;
4760 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004761 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004762 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
4764 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004765 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 seq_printf(seq, "Personalities : ");
4767 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004768 list_for_each_entry(pers, &pers_list, list)
4769 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
4771 spin_unlock(&pers_lock);
4772 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004773 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 return 0;
4775 }
4776 if (v == (void*)2) {
4777 status_unused(seq);
4778 return 0;
4779 }
4780
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004781 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004783
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4785 seq_printf(seq, "%s : %sactive", mdname(mddev),
4786 mddev->pers ? "" : "in");
4787 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004788 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004790 if (mddev->ro==2)
4791 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 seq_printf(seq, " %s", mddev->pers->name);
4793 }
4794
4795 size = 0;
4796 ITERATE_RDEV(mddev,rdev,tmp2) {
4797 char b[BDEVNAME_SIZE];
4798 seq_printf(seq, " %s[%d]",
4799 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004800 if (test_bit(WriteMostly, &rdev->flags))
4801 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004802 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 seq_printf(seq, "(F)");
4804 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004805 } else if (rdev->raid_disk < 0)
4806 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 size += rdev->size;
4808 }
4809
4810 if (!list_empty(&mddev->disks)) {
4811 if (mddev->pers)
4812 seq_printf(seq, "\n %llu blocks",
4813 (unsigned long long)mddev->array_size);
4814 else
4815 seq_printf(seq, "\n %llu blocks",
4816 (unsigned long long)size);
4817 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004818 if (mddev->persistent) {
4819 if (mddev->major_version != 0 ||
4820 mddev->minor_version != 90) {
4821 seq_printf(seq," super %d.%d",
4822 mddev->major_version,
4823 mddev->minor_version);
4824 }
4825 } else
4826 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
4828 if (mddev->pers) {
4829 mddev->pers->status (seq, mddev);
4830 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004831 if (mddev->pers->sync_request) {
4832 if (mddev->curr_resync > 2) {
4833 status_resync (seq, mddev);
4834 seq_printf(seq, "\n ");
4835 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4836 seq_printf(seq, "\tresync=DELAYED\n ");
4837 else if (mddev->recovery_cp < MaxSector)
4838 seq_printf(seq, "\tresync=PENDING\n ");
4839 }
NeilBrown32a76272005-06-21 17:17:14 -07004840 } else
4841 seq_printf(seq, "\n ");
4842
4843 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07004844 unsigned long chunk_kb;
4845 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07004846 spin_lock_irqsave(&bitmap->lock, flags);
4847 chunk_kb = bitmap->chunksize >> 10;
4848 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4849 "%lu%s chunk",
4850 bitmap->pages - bitmap->missing_pages,
4851 bitmap->pages,
4852 (bitmap->pages - bitmap->missing_pages)
4853 << (PAGE_SHIFT - 10),
4854 chunk_kb ? chunk_kb : bitmap->chunksize,
4855 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07004856 if (bitmap->file) {
4857 seq_printf(seq, ", file: ");
Josef Sipekc649bb92006-12-08 02:37:19 -08004858 seq_path(seq, bitmap->file->f_path.mnt,
4859 bitmap->file->f_path.dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07004860 }
NeilBrown78d742d2005-06-21 17:17:15 -07004861
NeilBrown32a76272005-06-21 17:17:14 -07004862 seq_printf(seq, "\n");
4863 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 }
4865
4866 seq_printf(seq, "\n");
4867 }
4868 mddev_unlock(mddev);
4869
4870 return 0;
4871}
4872
4873static struct seq_operations md_seq_ops = {
4874 .start = md_seq_start,
4875 .next = md_seq_next,
4876 .stop = md_seq_stop,
4877 .show = md_seq_show,
4878};
4879
4880static int md_seq_open(struct inode *inode, struct file *file)
4881{
4882 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08004883 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4884 if (mi == NULL)
4885 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
4887 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08004888 if (error)
4889 kfree(mi);
4890 else {
4891 struct seq_file *p = file->private_data;
4892 p->private = mi;
4893 mi->event = atomic_read(&md_event_count);
4894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 return error;
4896}
4897
NeilBrownd7603b72006-01-06 00:20:30 -08004898static int md_seq_release(struct inode *inode, struct file *file)
4899{
4900 struct seq_file *m = file->private_data;
4901 struct mdstat_info *mi = m->private;
4902 m->private = NULL;
4903 kfree(mi);
4904 return seq_release(inode, file);
4905}
4906
4907static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4908{
4909 struct seq_file *m = filp->private_data;
4910 struct mdstat_info *mi = m->private;
4911 int mask;
4912
4913 poll_wait(filp, &md_event_waiters, wait);
4914
4915 /* always allow read */
4916 mask = POLLIN | POLLRDNORM;
4917
4918 if (mi->event != atomic_read(&md_event_count))
4919 mask |= POLLERR | POLLPRI;
4920 return mask;
4921}
4922
Arjan van de Venfa027c22007-02-12 00:55:33 -08004923static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07004924 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 .open = md_seq_open,
4926 .read = seq_read,
4927 .llseek = seq_lseek,
NeilBrownd7603b72006-01-06 00:20:30 -08004928 .release = md_seq_release,
4929 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930};
4931
NeilBrown2604b702006-01-06 00:20:36 -08004932int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004935 list_add_tail(&p->list, &pers_list);
4936 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 spin_unlock(&pers_lock);
4938 return 0;
4939}
4940
NeilBrown2604b702006-01-06 00:20:36 -08004941int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942{
NeilBrown2604b702006-01-06 00:20:36 -08004943 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004945 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 spin_unlock(&pers_lock);
4947 return 0;
4948}
4949
4950static int is_mddev_idle(mddev_t *mddev)
4951{
4952 mdk_rdev_t * rdev;
4953 struct list_head *tmp;
4954 int idle;
4955 unsigned long curr_events;
4956
4957 idle = 1;
4958 ITERATE_RDEV(mddev,rdev,tmp) {
4959 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01004960 curr_events = disk_stat_read(disk, sectors[0]) +
4961 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08004963 /* The difference between curr_events and last_events
4964 * will be affected by any new non-sync IO (making
4965 * curr_events bigger) and any difference in the amount of
4966 * in-flight syncio (making current_events bigger or smaller)
4967 * The amount in-flight is currently limited to
4968 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4969 * which is at most 4096 sectors.
4970 * These numbers are fairly fragile and should be made
4971 * more robust, probably by enforcing the
4972 * 'window size' that md_do_sync sort-of uses.
4973 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 * Note: the following is an unsigned comparison.
4975 */
NeilBrownc0e48522005-11-18 01:11:01 -08004976 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 rdev->last_events = curr_events;
4978 idle = 0;
4979 }
4980 }
4981 return idle;
4982}
4983
4984void md_done_sync(mddev_t *mddev, int blocks, int ok)
4985{
4986 /* another "blocks" (512byte) blocks have been synced */
4987 atomic_sub(blocks, &mddev->recovery_active);
4988 wake_up(&mddev->recovery_wait);
4989 if (!ok) {
4990 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4991 md_wakeup_thread(mddev->thread);
4992 // stop recovery, signal do_sync ....
4993 }
4994}
4995
4996
NeilBrown06d91a52005-06-21 17:17:12 -07004997/* md_write_start(mddev, bi)
4998 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07004999 * in superblock) before writing, schedule a superblock update
5000 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005001 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005002void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003{
NeilBrown06d91a52005-06-21 17:17:12 -07005004 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005005 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005006
NeilBrownf91de922005-11-08 21:39:36 -08005007 BUG_ON(mddev->ro == 1);
5008 if (mddev->ro == 2) {
5009 /* need to switch to read/write */
5010 mddev->ro = 0;
5011 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5012 md_wakeup_thread(mddev->thread);
5013 }
NeilBrown06d91a52005-06-21 17:17:12 -07005014 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005015 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005016 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005017 if (mddev->in_sync) {
5018 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005019 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005020 md_wakeup_thread(mddev->thread);
5021 }
NeilBrowna9701a32005-11-08 21:39:34 -08005022 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005023 }
NeilBrown850b2b42006-10-03 01:15:46 -07005024 wait_event(mddev->sb_wait, mddev->flags==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025}
5026
5027void md_write_end(mddev_t *mddev)
5028{
5029 if (atomic_dec_and_test(&mddev->writes_pending)) {
5030 if (mddev->safemode == 2)
5031 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005032 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5034 }
5035}
5036
NeilBrown2a2275d2007-01-26 00:57:11 -08005037/* md_allow_write(mddev)
5038 * Calling this ensures that the array is marked 'active' so that writes
5039 * may proceed without blocking. It is important to call this before
5040 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5041 * Must be called with mddev_lock held.
5042 */
5043void md_allow_write(mddev_t *mddev)
5044{
5045 if (!mddev->pers)
5046 return;
5047 if (mddev->ro)
5048 return;
5049
5050 spin_lock_irq(&mddev->write_lock);
5051 if (mddev->in_sync) {
5052 mddev->in_sync = 0;
5053 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5054 if (mddev->safemode_delay &&
5055 mddev->safemode == 0)
5056 mddev->safemode = 1;
5057 spin_unlock_irq(&mddev->write_lock);
5058 md_update_sb(mddev, 0);
5059 } else
5060 spin_unlock_irq(&mddev->write_lock);
5061}
5062EXPORT_SYMBOL_GPL(md_allow_write);
5063
Adrian Bunk75c96f82005-05-05 16:16:09 -07005064static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
5066#define SYNC_MARKS 10
5067#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005068void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069{
5070 mddev_t *mddev2;
5071 unsigned int currspeed = 0,
5072 window;
NeilBrown57afd892005-06-21 17:17:13 -07005073 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 unsigned long mark[SYNC_MARKS];
5075 sector_t mark_cnt[SYNC_MARKS];
5076 int last_mark,m;
5077 struct list_head *tmp;
5078 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005079 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005080 struct list_head *rtmp;
5081 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005082 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
5084 /* just incase thread restarts... */
5085 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5086 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005087 if (mddev->ro) /* never try to sync a read-only array */
5088 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
NeilBrown61df9d92006-10-03 01:15:57 -07005090 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5091 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5092 desc = "data-check";
5093 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5094 desc = "requested-resync";
5095 else
5096 desc = "resync";
5097 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5098 desc = "reshape";
5099 else
5100 desc = "recovery";
5101
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 /* we overload curr_resync somewhat here.
5103 * 0 == not engaged in resync at all
5104 * 2 == checking that there is no conflict with another sync
5105 * 1 == like 2, but have yielded to allow conflicting resync to
5106 * commense
5107 * other == active in resync - this many blocks
5108 *
5109 * Before starting a resync we must have set curr_resync to
5110 * 2, and then checked that every "conflicting" array has curr_resync
5111 * less than ours. When we find one that is the same or higher
5112 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5113 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5114 * This will mean we have to start checking from the beginning again.
5115 *
5116 */
5117
5118 do {
5119 mddev->curr_resync = 2;
5120
5121 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005122 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005123 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 goto skip;
5125 }
5126 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 if (mddev2 == mddev)
5128 continue;
5129 if (mddev2->curr_resync &&
5130 match_mddev_units(mddev,mddev2)) {
5131 DEFINE_WAIT(wq);
5132 if (mddev < mddev2 && mddev->curr_resync == 2) {
5133 /* arbitrarily yield */
5134 mddev->curr_resync = 1;
5135 wake_up(&resync_wait);
5136 }
5137 if (mddev > mddev2 && mddev->curr_resync == 1)
5138 /* no need to wait here, we can wait the next
5139 * time 'round when curr_resync == 2
5140 */
5141 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005142 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5143 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005144 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005145 printk(KERN_INFO "md: delaying %s of %s"
5146 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005148 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 mddev_put(mddev2);
5150 schedule();
5151 finish_wait(&resync_wait, &wq);
5152 goto try_again;
5153 }
5154 finish_wait(&resync_wait, &wq);
5155 }
5156 }
5157 } while (mddev->curr_resync < 2);
5158
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005159 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005160 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005162 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 */
5164 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005165 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005166 /* we don't use the checkpoint if there's a bitmap */
5167 if (!mddev->bitmap &&
5168 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5169 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005170 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5171 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005172 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 /* recovery follows the physical size of devices */
5174 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005175 j = MaxSector;
5176 ITERATE_RDEV(mddev,rdev,rtmp)
5177 if (rdev->raid_disk >= 0 &&
5178 !test_bit(Faulty, &rdev->flags) &&
5179 !test_bit(In_sync, &rdev->flags) &&
5180 rdev->recovery_offset < j)
5181 j = rdev->recovery_offset;
5182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
NeilBrown61df9d92006-10-03 01:15:57 -07005184 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5185 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5186 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005187 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005188 "(but not more than %d KB/sec) for %s.\n",
5189 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
5191 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005192
NeilBrown57afd892005-06-21 17:17:13 -07005193 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 for (m = 0; m < SYNC_MARKS; m++) {
5195 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005196 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 }
5198 last_mark = 0;
5199 mddev->resync_mark = mark[last_mark];
5200 mddev->resync_mark_cnt = mark_cnt[last_mark];
5201
5202 /*
5203 * Tune reconstruction:
5204 */
5205 window = 32*(PAGE_SIZE/512);
5206 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5207 window/2,(unsigned long long) max_sectors/2);
5208
5209 atomic_set(&mddev->recovery_active, 0);
5210 init_waitqueue_head(&mddev->recovery_wait);
5211 last_check = 0;
5212
5213 if (j>2) {
5214 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005215 "md: resuming %s of %s from checkpoint.\n",
5216 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 mddev->curr_resync = j;
5218 }
5219
5220 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005221 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222
NeilBrown57afd892005-06-21 17:17:13 -07005223 skipped = 0;
5224 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08005225 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005226 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5228 goto out;
5229 }
NeilBrown57afd892005-06-21 17:17:13 -07005230
5231 if (!skipped) { /* actual IO requested */
5232 io_sectors += sectors;
5233 atomic_add(sectors, &mddev->recovery_active);
5234 }
5235
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 j += sectors;
5237 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005238 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005239 if (last_check == 0)
5240 /* this is the earliers that rebuilt will be
5241 * visible in /proc/mdstat
5242 */
5243 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005244
5245 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 continue;
5247
NeilBrown57afd892005-06-21 17:17:13 -07005248 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
5250 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5251 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5252 break;
5253
5254 repeat:
5255 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5256 /* step marks */
5257 int next = (last_mark+1) % SYNC_MARKS;
5258
5259 mddev->resync_mark = mark[next];
5260 mddev->resync_mark_cnt = mark_cnt[next];
5261 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005262 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 last_mark = next;
5264 }
5265
5266
NeilBrown787453c2005-11-08 21:39:43 -08005267 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 /*
5269 * got a signal, exit.
5270 */
5271 printk(KERN_INFO
5272 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5274 goto out;
5275 }
5276
5277 /*
5278 * this loop exits only if either when we are slower than
5279 * the 'hard' speed limit, or the system was IO-idle for
5280 * a jiffy.
5281 * the system might be non-idle CPU-wise, but we only care
5282 * about not overloading the IO subsystem. (things like an
5283 * e2fsck being done on the RAID array should execute fast)
5284 */
5285 mddev->queue->unplug_fn(mddev->queue);
5286 cond_resched();
5287
NeilBrown57afd892005-06-21 17:17:13 -07005288 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5289 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
NeilBrown88202a02006-01-06 00:21:36 -08005291 if (currspeed > speed_min(mddev)) {
5292 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005294 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 goto repeat;
5296 }
5297 }
5298 }
NeilBrown61df9d92006-10-03 01:15:57 -07005299 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 /*
5301 * this also signals 'finished resyncing' to md_stop
5302 */
5303 out:
5304 mddev->queue->unplug_fn(mddev->queue);
5305
5306 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5307
5308 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005309 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
5311 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005312 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005313 mddev->curr_resync > 2) {
5314 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5315 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5316 if (mddev->curr_resync >= mddev->recovery_cp) {
5317 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005318 "md: checkpointing %s of %s.\n",
5319 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005320 mddev->recovery_cp = mddev->curr_resync;
5321 }
5322 } else
5323 mddev->recovery_cp = MaxSector;
5324 } else {
5325 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5326 mddev->curr_resync = MaxSector;
5327 ITERATE_RDEV(mddev,rdev,rtmp)
5328 if (rdev->raid_disk >= 0 &&
5329 !test_bit(Faulty, &rdev->flags) &&
5330 !test_bit(In_sync, &rdev->flags) &&
5331 rdev->recovery_offset < mddev->curr_resync)
5332 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 }
NeilBrown17571282006-12-10 02:20:52 -08005335 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 skip:
5338 mddev->curr_resync = 0;
5339 wake_up(&resync_wait);
5340 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5341 md_wakeup_thread(mddev->thread);
5342}
NeilBrown29269552006-03-27 01:18:10 -08005343EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
5345
5346/*
5347 * This routine is regularly called by all per-raid-array threads to
5348 * deal with generic issues like resync and super-block update.
5349 * Raid personalities that don't have a thread (linear/raid0) do not
5350 * need this as they never do any recovery or update the superblock.
5351 *
5352 * It does not do any resync itself, but rather "forks" off other threads
5353 * to do that as needed.
5354 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5355 * "->recovery" and create a thread at ->sync_thread.
5356 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5357 * and wakeups up this thread which will reap the thread and finish up.
5358 * This thread also removes any faulty devices (with nr_pending == 0).
5359 *
5360 * The overall approach is:
5361 * 1/ if the superblock needs updating, update it.
5362 * 2/ If a recovery thread is running, don't do anything else.
5363 * 3/ If recovery has finished, clean up, possibly marking spares active.
5364 * 4/ If there are any faulty devices, remove them.
5365 * 5/ If array is degraded, try to add spares devices
5366 * 6/ If array has spares or is not in-sync, start a resync thread.
5367 */
5368void md_check_recovery(mddev_t *mddev)
5369{
5370 mdk_rdev_t *rdev;
5371 struct list_head *rtmp;
5372
5373
NeilBrown5f404022005-06-21 17:17:16 -07005374 if (mddev->bitmap)
5375 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377 if (mddev->ro)
5378 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005379
5380 if (signal_pending(current)) {
5381 if (mddev->pers->sync_request) {
5382 printk(KERN_INFO "md: %s in immediate safe mode\n",
5383 mdname(mddev));
5384 mddev->safemode = 2;
5385 }
5386 flush_signals(current);
5387 }
5388
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 if ( ! (
NeilBrown850b2b42006-10-03 01:15:46 -07005390 mddev->flags ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005392 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5393 (mddev->safemode == 1) ||
5394 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5395 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 ))
5397 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005398
NeilBrowndf5b89b2006-03-27 01:18:20 -08005399 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07005401
NeilBrowna9701a32005-11-08 21:39:34 -08005402 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005403 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5404 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5405 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07005406 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownfca4d842005-06-21 17:17:11 -07005407 }
5408 if (mddev->safemode == 1)
5409 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005410 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005411
NeilBrown850b2b42006-10-03 01:15:46 -07005412 if (mddev->flags)
5413 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005414
NeilBrown06d91a52005-06-21 17:17:12 -07005415
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5417 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5418 /* resync/recovery still happening */
5419 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5420 goto unlock;
5421 }
5422 if (mddev->sync_thread) {
5423 /* resync has finished, collect result */
5424 md_unregister_thread(mddev->sync_thread);
5425 mddev->sync_thread = NULL;
5426 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5427 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5428 /* success...*/
5429 /* activate any spares */
5430 mddev->pers->spare_active(mddev);
5431 }
NeilBrown850b2b42006-10-03 01:15:46 -07005432 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005433
5434 /* if array is no-longer degraded, then any saved_raid_disk
5435 * information must be scrapped
5436 */
5437 if (!mddev->degraded)
5438 ITERATE_RDEV(mddev,rdev,rtmp)
5439 rdev->saved_raid_disk = -1;
5440
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 mddev->recovery = 0;
5442 /* flag recovery needed just to double check */
5443 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005444 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 goto unlock;
5446 }
NeilBrown24dd4692005-11-08 21:39:26 -08005447 /* Clear some bits that don't mean anything, but
5448 * might be left set
5449 */
5450 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5451 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5452 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5453 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005455 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5456 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 /* no recovery is running.
5458 * remove any failed drives, then
5459 * add spares if possible.
5460 * Spare are also removed and re-added, to allow
5461 * the personality to fail the re-add.
5462 */
5463 ITERATE_RDEV(mddev,rdev,rtmp)
5464 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005465 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08005467 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
5468 char nm[20];
5469 sprintf(nm,"rd%d", rdev->raid_disk);
5470 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08005472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 }
5474
5475 if (mddev->degraded) {
5476 ITERATE_RDEV(mddev,rdev,rtmp)
5477 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08005478 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005479 rdev->recovery_offset = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08005480 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5481 char nm[20];
5482 sprintf(nm, "rd%d", rdev->raid_disk);
5483 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 spares++;
NeilBrownd7603b72006-01-06 00:20:30 -08005485 md_new_event(mddev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08005486 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 break;
5488 }
5489 }
5490
NeilBrown24dd4692005-11-08 21:39:26 -08005491 if (spares) {
5492 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5493 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5494 } else if (mddev->recovery_cp < MaxSector) {
5495 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5496 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5497 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005499
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 if (mddev->pers->sync_request) {
5501 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005502 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5503 /* We are adding a device or devices to an array
5504 * which has the bitmap stored on all devices.
5505 * So make sure all bitmap pages get written
5506 */
5507 bitmap_write_all(mddev->bitmap);
5508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 mddev->sync_thread = md_register_thread(md_do_sync,
5510 mddev,
5511 "%s_resync");
5512 if (!mddev->sync_thread) {
5513 printk(KERN_ERR "%s: could not start resync"
5514 " thread...\n",
5515 mdname(mddev));
5516 /* leave the spares where they are, it shouldn't hurt */
5517 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005518 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005520 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 }
5522 unlock:
5523 mddev_unlock(mddev);
5524 }
5525}
5526
Adrian Bunk75c96f82005-05-05 16:16:09 -07005527static int md_notify_reboot(struct notifier_block *this,
5528 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529{
5530 struct list_head *tmp;
5531 mddev_t *mddev;
5532
5533 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5534
5535 printk(KERN_INFO "md: stopping all md devices.\n");
5536
5537 ITERATE_MDDEV(mddev,tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005538 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005540 mddev_unlock(mddev);
5541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 /*
5543 * certain more exotic SCSI devices are known to be
5544 * volatile wrt too early system reboots. While the
5545 * right place to handle this issue is the given
5546 * driver, we do want to have a safe RAID driver ...
5547 */
5548 mdelay(1000*1);
5549 }
5550 return NOTIFY_DONE;
5551}
5552
Adrian Bunk75c96f82005-05-05 16:16:09 -07005553static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 .notifier_call = md_notify_reboot,
5555 .next = NULL,
5556 .priority = INT_MAX, /* before any real devices */
5557};
5558
5559static void md_geninit(void)
5560{
5561 struct proc_dir_entry *p;
5562
5563 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5564
5565 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5566 if (p)
5567 p->proc_fops = &md_seq_fops;
5568}
5569
Adrian Bunk75c96f82005-05-05 16:16:09 -07005570static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 if (register_blkdev(MAJOR_NR, "md"))
5573 return -1;
5574 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5575 unregister_blkdev(MAJOR_NR, "md");
5576 return -1;
5577 }
NeilBrowne8703fe2006-10-03 01:15:59 -07005578 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
5579 md_probe, NULL, NULL);
5580 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 md_probe, NULL, NULL);
5582
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08005584 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
5586 md_geninit();
5587 return (0);
5588}
5589
5590
5591#ifndef MODULE
5592
5593/*
5594 * Searches all registered partitions for autorun RAID arrays
5595 * at boot time.
5596 */
5597static dev_t detected_devices[128];
5598static int dev_cnt;
5599
5600void md_autodetect_dev(dev_t dev)
5601{
5602 if (dev_cnt >= 0 && dev_cnt < 127)
5603 detected_devices[dev_cnt++] = dev;
5604}
5605
5606
5607static void autostart_arrays(int part)
5608{
5609 mdk_rdev_t *rdev;
5610 int i;
5611
5612 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5613
5614 for (i = 0; i < dev_cnt; i++) {
5615 dev_t dev = detected_devices[i];
5616
5617 rdev = md_import_device(dev,0, 0);
5618 if (IS_ERR(rdev))
5619 continue;
5620
NeilBrownb2d444d2005-11-08 21:39:31 -08005621 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 MD_BUG();
5623 continue;
5624 }
5625 list_add(&rdev->same_set, &pending_raid_disks);
5626 }
5627 dev_cnt = 0;
5628
5629 autorun_devices(part);
5630}
5631
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005632#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
5634static __exit void md_exit(void)
5635{
5636 mddev_t *mddev;
5637 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07005638
NeilBrowne8703fe2006-10-03 01:15:59 -07005639 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
5640 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
5642 unregister_blkdev(MAJOR_NR,"md");
5643 unregister_blkdev(mdp_major, "mdp");
5644 unregister_reboot_notifier(&md_notifier);
5645 unregister_sysctl_table(raid_table_header);
5646 remove_proc_entry("mdstat", NULL);
5647 ITERATE_MDDEV(mddev,tmp) {
5648 struct gendisk *disk = mddev->gendisk;
5649 if (!disk)
5650 continue;
5651 export_array(mddev);
5652 del_gendisk(disk);
5653 put_disk(disk);
5654 mddev->gendisk = NULL;
5655 mddev_put(mddev);
5656 }
5657}
5658
5659module_init(md_init)
5660module_exit(md_exit)
5661
NeilBrownf91de922005-11-08 21:39:36 -08005662static int get_ro(char *buffer, struct kernel_param *kp)
5663{
5664 return sprintf(buffer, "%d", start_readonly);
5665}
5666static int set_ro(const char *val, struct kernel_param *kp)
5667{
5668 char *e;
5669 int num = simple_strtoul(val, &e, 10);
5670 if (*val && (*e == '\0' || *e == '\n')) {
5671 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005672 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005673 }
5674 return -EINVAL;
5675}
5676
NeilBrown80ca3a42006-07-10 04:44:18 -07005677module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
5678module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005679
NeilBrownf91de922005-11-08 21:39:36 -08005680
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681EXPORT_SYMBOL(register_md_personality);
5682EXPORT_SYMBOL(unregister_md_personality);
5683EXPORT_SYMBOL(md_error);
5684EXPORT_SYMBOL(md_done_sync);
5685EXPORT_SYMBOL(md_write_start);
5686EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687EXPORT_SYMBOL(md_register_thread);
5688EXPORT_SYMBOL(md_unregister_thread);
5689EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690EXPORT_SYMBOL(md_check_recovery);
5691MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005692MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005693MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);