blob: 6c06e825cff5af8f070ff9d370f987da7c64f34b [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};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1301{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001302 struct list_head *tmp, *tmp2;
1303 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 ITERATE_RDEV(mddev1,rdev,tmp)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001306 ITERATE_RDEV(mddev2, rdev2, tmp2)
1307 if (rdev->bdev->bd_contains ==
1308 rdev2->bdev->bd_contains)
1309 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 return 0;
1312}
1313
1314static LIST_HEAD(pending_raid_disks);
1315
1316static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1317{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001318 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001319 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001320 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 if (rdev->mddev) {
1323 MD_BUG();
1324 return -EINVAL;
1325 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001326 /* make sure rdev->size exceeds mddev->size */
1327 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1328 if (mddev->pers)
1329 /* Cannot change size, so fail */
1330 return -ENOSPC;
1331 else
1332 mddev->size = rdev->size;
1333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 /* Verify rdev->desc_nr is unique.
1336 * If it is -1, assign a free number, else
1337 * check number is not in use
1338 */
1339 if (rdev->desc_nr < 0) {
1340 int choice = 0;
1341 if (mddev->pers) choice = mddev->raid_disks;
1342 while (find_rdev_nr(mddev, choice))
1343 choice++;
1344 rdev->desc_nr = choice;
1345 } else {
1346 if (find_rdev_nr(mddev, rdev->desc_nr))
1347 return -EBUSY;
1348 }
NeilBrown19133a42005-11-08 21:39:35 -08001349 bdevname(rdev->bdev,b);
1350 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1351 return -ENOMEM;
Neil Brown1edf80d2006-01-12 01:05:23 -08001352 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1353 *s = '!';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 list_add(&rdev->same_set, &mddev->disks);
1356 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001357 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001358
NeilBrown9c791972005-11-08 21:39:28 -08001359 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001360 kobject_add(&rdev->kobj);
1361
NeilBrownf637b9f2005-11-08 21:39:37 -08001362 if (rdev->bdev->bd_part)
1363 ko = &rdev->bdev->bd_part->kobj;
1364 else
1365 ko = &rdev->bdev->bd_disk->kobj;
1366 sysfs_create_link(&rdev->kobj, ko, "block");
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001367 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return 0;
1369}
1370
1371static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1372{
1373 char b[BDEVNAME_SIZE];
1374 if (!rdev->mddev) {
1375 MD_BUG();
1376 return;
1377 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001378 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 list_del_init(&rdev->same_set);
1380 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1381 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001382 sysfs_remove_link(&rdev->kobj, "block");
1383 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386/*
1387 * prevent the device from being mounted, repartitioned or
1388 * otherwise reused by a RAID array (or any other kernel
1389 * subsystem), by bd_claiming the device.
1390 */
1391static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1392{
1393 int err = 0;
1394 struct block_device *bdev;
1395 char b[BDEVNAME_SIZE];
1396
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001397 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (IS_ERR(bdev)) {
1399 printk(KERN_ERR "md: could not open %s.\n",
1400 __bdevname(dev, b));
1401 return PTR_ERR(bdev);
1402 }
1403 err = bd_claim(bdev, rdev);
1404 if (err) {
1405 printk(KERN_ERR "md: could not bd_claim %s.\n",
1406 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001407 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return err;
1409 }
1410 rdev->bdev = bdev;
1411 return err;
1412}
1413
1414static void unlock_rdev(mdk_rdev_t *rdev)
1415{
1416 struct block_device *bdev = rdev->bdev;
1417 rdev->bdev = NULL;
1418 if (!bdev)
1419 MD_BUG();
1420 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001421 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424void md_autodetect_dev(dev_t dev);
1425
1426static void export_rdev(mdk_rdev_t * rdev)
1427{
1428 char b[BDEVNAME_SIZE];
1429 printk(KERN_INFO "md: export_rdev(%s)\n",
1430 bdevname(rdev->bdev,b));
1431 if (rdev->mddev)
1432 MD_BUG();
1433 free_disk_sb(rdev);
1434 list_del_init(&rdev->same_set);
1435#ifndef MODULE
1436 md_autodetect_dev(rdev->bdev->bd_dev);
1437#endif
1438 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001439 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
1442static void kick_rdev_from_array(mdk_rdev_t * rdev)
1443{
1444 unbind_rdev_from_array(rdev);
1445 export_rdev(rdev);
1446}
1447
1448static void export_array(mddev_t *mddev)
1449{
1450 struct list_head *tmp;
1451 mdk_rdev_t *rdev;
1452
1453 ITERATE_RDEV(mddev,rdev,tmp) {
1454 if (!rdev->mddev) {
1455 MD_BUG();
1456 continue;
1457 }
1458 kick_rdev_from_array(rdev);
1459 }
1460 if (!list_empty(&mddev->disks))
1461 MD_BUG();
1462 mddev->raid_disks = 0;
1463 mddev->major_version = 0;
1464}
1465
1466static void print_desc(mdp_disk_t *desc)
1467{
1468 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1469 desc->major,desc->minor,desc->raid_disk,desc->state);
1470}
1471
1472static void print_sb(mdp_super_t *sb)
1473{
1474 int i;
1475
1476 printk(KERN_INFO
1477 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1478 sb->major_version, sb->minor_version, sb->patch_version,
1479 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1480 sb->ctime);
1481 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1482 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1483 sb->md_minor, sb->layout, sb->chunk_size);
1484 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1485 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1486 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1487 sb->failed_disks, sb->spare_disks,
1488 sb->sb_csum, (unsigned long)sb->events_lo);
1489
1490 printk(KERN_INFO);
1491 for (i = 0; i < MD_SB_DISKS; i++) {
1492 mdp_disk_t *desc;
1493
1494 desc = sb->disks + i;
1495 if (desc->number || desc->major || desc->minor ||
1496 desc->raid_disk || (desc->state && (desc->state != 4))) {
1497 printk(" D %2d: ", i);
1498 print_desc(desc);
1499 }
1500 }
1501 printk(KERN_INFO "md: THIS: ");
1502 print_desc(&sb->this_disk);
1503
1504}
1505
1506static void print_rdev(mdk_rdev_t *rdev)
1507{
1508 char b[BDEVNAME_SIZE];
1509 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1510 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001511 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1512 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (rdev->sb_loaded) {
1514 printk(KERN_INFO "md: rdev superblock:\n");
1515 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1516 } else
1517 printk(KERN_INFO "md: no rdev superblock!\n");
1518}
1519
Adrian Bunk5e563412006-06-26 00:27:42 -07001520static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 struct list_head *tmp, *tmp2;
1523 mdk_rdev_t *rdev;
1524 mddev_t *mddev;
1525 char b[BDEVNAME_SIZE];
1526
1527 printk("\n");
1528 printk("md: **********************************\n");
1529 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1530 printk("md: **********************************\n");
1531 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
NeilBrown32a76272005-06-21 17:17:14 -07001533 if (mddev->bitmap)
1534 bitmap_print_sb(mddev->bitmap);
1535 else
1536 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 ITERATE_RDEV(mddev,rdev,tmp2)
1538 printk("<%s>", bdevname(rdev->bdev,b));
1539 printk("\n");
1540
1541 ITERATE_RDEV(mddev,rdev,tmp2)
1542 print_rdev(rdev);
1543 }
1544 printk("md: **********************************\n");
1545 printk("\n");
1546}
1547
1548
NeilBrown42543762006-06-26 00:27:57 -07001549static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
NeilBrown42543762006-06-26 00:27:57 -07001551 /* Update each superblock (in-memory image), but
1552 * if we are allowed to, skip spares which already
1553 * have the right event counter, or have one earlier
1554 * (which would mean they aren't being marked as dirty
1555 * with the rest of the array)
1556 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 mdk_rdev_t *rdev;
1558 struct list_head *tmp;
1559
1560 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrown42543762006-06-26 00:27:57 -07001561 if (rdev->sb_events == mddev->events ||
1562 (nospares &&
1563 rdev->raid_disk < 0 &&
1564 (rdev->sb_events&1)==0 &&
1565 rdev->sb_events+1 == mddev->events)) {
1566 /* Don't update this superblock */
1567 rdev->sb_loaded = 2;
1568 } else {
1569 super_types[mddev->major_version].
1570 sync_super(mddev, rdev);
1571 rdev->sb_loaded = 1;
1572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574}
1575
NeilBrown850b2b42006-10-03 01:15:46 -07001576static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001578 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct list_head *tmp;
1580 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001581 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001582 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001585 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001586
NeilBrown850b2b42006-10-03 01:15:46 -07001587 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1588 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1589 force_change = 1;
1590 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1591 /* just a clean<-> dirty transition, possibly leave spares alone,
1592 * though if events isn't the right even/odd, we will have to do
1593 * spares after all
1594 */
1595 nospares = 1;
1596 if (force_change)
1597 nospares = 0;
1598 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001599 /* If the array is degraded, then skipping spares is both
1600 * dangerous and fairly pointless.
1601 * Dangerous because a device that was removed from the array
1602 * might have a event_count that still looks up-to-date,
1603 * so it can be re-added without a resync.
1604 * Pointless because if there are any spares to skip,
1605 * then a recovery will happen and soon that array won't
1606 * be degraded any more and the spare can go back to sleep then.
1607 */
NeilBrown850b2b42006-10-03 01:15:46 -07001608 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001609
NeilBrown06d91a52005-06-21 17:17:12 -07001610 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001612
1613 /* If this is just a dirty<->clean transition, and the array is clean
1614 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001615 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001616 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001617 && (mddev->events & 1)
1618 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001619 mddev->events--;
1620 else {
1621 /* otherwise we have to go forward and ... */
1622 mddev->events ++;
1623 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1624 /* .. if the array isn't clean, insist on an odd 'events' */
1625 if ((mddev->events&1)==0) {
1626 mddev->events++;
1627 nospares = 0;
1628 }
1629 } else {
1630 /* otherwise insist on an even 'events' (for clean states) */
1631 if ((mddev->events&1)) {
1632 mddev->events++;
1633 nospares = 0;
1634 }
1635 }
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 if (!mddev->events) {
1639 /*
1640 * oops, this 64-bit counter should never wrap.
1641 * Either we are in around ~1 trillion A.C., assuming
1642 * 1 reboot per second, or we have a bug:
1643 */
1644 MD_BUG();
1645 mddev->events --;
1646 }
NeilBrown42543762006-06-26 00:27:57 -07001647 sync_sbs(mddev, nospares);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /*
1650 * do not write anything to disk if using
1651 * nonpersistent superblocks
1652 */
NeilBrown06d91a52005-06-21 17:17:12 -07001653 if (!mddev->persistent) {
NeilBrown850b2b42006-10-03 01:15:46 -07001654 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001655 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001656 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001658 }
NeilBrowna9701a32005-11-08 21:39:34 -08001659 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 dprintk(KERN_INFO
1662 "md: updating %s RAID superblock on device (in sync %d)\n",
1663 mdname(mddev),mddev->in_sync);
1664
NeilBrown32a76272005-06-21 17:17:14 -07001665 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 ITERATE_RDEV(mddev,rdev,tmp) {
1667 char b[BDEVNAME_SIZE];
1668 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001669 if (rdev->sb_loaded != 1)
1670 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001671 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 dprintk("(skipping faulty ");
1673
1674 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001675 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001676 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001677 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001678 rdev->sb_page);
1679 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1680 bdevname(rdev->bdev,b),
1681 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001682 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 } else
1685 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001686 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /* only need to write one superblock... */
1688 break;
1689 }
NeilBrowna9701a32005-11-08 21:39:34 -08001690 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001691 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001692
NeilBrowna9701a32005-11-08 21:39:34 -08001693 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001694 if (mddev->in_sync != sync_req ||
1695 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001696 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001697 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001698 goto repeat;
1699 }
NeilBrown850b2b42006-10-03 01:15:46 -07001700 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001701 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001702 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
NeilBrownbce74da2006-01-06 00:20:41 -08001706/* words written to sysfs files may, or my not, be \n terminated.
1707 * We want to accept with case. For this we use cmd_match.
1708 */
1709static int cmd_match(const char *cmd, const char *str)
1710{
1711 /* See if cmd, written into a sysfs file, matches
1712 * str. They must either be the same, or cmd can
1713 * have a trailing newline
1714 */
1715 while (*cmd && *str && *cmd == *str) {
1716 cmd++;
1717 str++;
1718 }
1719 if (*cmd == '\n')
1720 cmd++;
1721 if (*str || *cmd)
1722 return 0;
1723 return 1;
1724}
1725
NeilBrown86e6ffd2005-11-08 21:39:24 -08001726struct rdev_sysfs_entry {
1727 struct attribute attr;
1728 ssize_t (*show)(mdk_rdev_t *, char *);
1729 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1730};
1731
1732static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001733state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001734{
1735 char *sep = "";
1736 int len=0;
1737
NeilBrownb2d444d2005-11-08 21:39:31 -08001738 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001739 len+= sprintf(page+len, "%sfaulty",sep);
1740 sep = ",";
1741 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001742 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001743 len += sprintf(page+len, "%sin_sync",sep);
1744 sep = ",";
1745 }
NeilBrownf6556752006-06-26 00:28:01 -07001746 if (test_bit(WriteMostly, &rdev->flags)) {
1747 len += sprintf(page+len, "%swrite_mostly",sep);
1748 sep = ",";
1749 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001750 if (!test_bit(Faulty, &rdev->flags) &&
1751 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001752 len += sprintf(page+len, "%sspare", sep);
1753 sep = ",";
1754 }
1755 return len+sprintf(page+len, "\n");
1756}
1757
NeilBrown45dc2de2006-06-26 00:27:58 -07001758static ssize_t
1759state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1760{
1761 /* can write
1762 * faulty - simulates and error
1763 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001764 * writemostly - sets write_mostly
1765 * -writemostly - clears write_mostly
NeilBrown45dc2de2006-06-26 00:27:58 -07001766 */
1767 int err = -EINVAL;
1768 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1769 md_error(rdev->mddev, rdev);
1770 err = 0;
1771 } else if (cmd_match(buf, "remove")) {
1772 if (rdev->raid_disk >= 0)
1773 err = -EBUSY;
1774 else {
1775 mddev_t *mddev = rdev->mddev;
1776 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001777 if (mddev->pers)
1778 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001779 md_new_event(mddev);
1780 err = 0;
1781 }
NeilBrownf6556752006-06-26 00:28:01 -07001782 } else if (cmd_match(buf, "writemostly")) {
1783 set_bit(WriteMostly, &rdev->flags);
1784 err = 0;
1785 } else if (cmd_match(buf, "-writemostly")) {
1786 clear_bit(WriteMostly, &rdev->flags);
1787 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001788 }
1789 return err ? err : len;
1790}
NeilBrown80ca3a42006-07-10 04:44:18 -07001791static struct rdev_sysfs_entry rdev_state =
1792__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001793
1794static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001795super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001796{
1797 if (rdev->sb_loaded && rdev->sb_size) {
1798 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1799 return rdev->sb_size;
1800 } else
1801 return 0;
1802}
NeilBrown96de1e62005-11-08 21:39:39 -08001803static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1804
NeilBrown4dbcdc72006-01-06 00:20:52 -08001805static ssize_t
1806errors_show(mdk_rdev_t *rdev, char *page)
1807{
1808 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1809}
1810
1811static ssize_t
1812errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1813{
1814 char *e;
1815 unsigned long n = simple_strtoul(buf, &e, 10);
1816 if (*buf && (*e == 0 || *e == '\n')) {
1817 atomic_set(&rdev->corrected_errors, n);
1818 return len;
1819 }
1820 return -EINVAL;
1821}
1822static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001823__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001824
NeilBrown014236d2006-01-06 00:20:55 -08001825static ssize_t
1826slot_show(mdk_rdev_t *rdev, char *page)
1827{
1828 if (rdev->raid_disk < 0)
1829 return sprintf(page, "none\n");
1830 else
1831 return sprintf(page, "%d\n", rdev->raid_disk);
1832}
1833
1834static ssize_t
1835slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1836{
1837 char *e;
1838 int slot = simple_strtoul(buf, &e, 10);
1839 if (strncmp(buf, "none", 4)==0)
1840 slot = -1;
1841 else if (e==buf || (*e && *e!= '\n'))
1842 return -EINVAL;
1843 if (rdev->mddev->pers)
1844 /* Cannot set slot in active array (yet) */
1845 return -EBUSY;
1846 if (slot >= rdev->mddev->raid_disks)
1847 return -ENOSPC;
1848 rdev->raid_disk = slot;
1849 /* assume it is working */
1850 rdev->flags = 0;
1851 set_bit(In_sync, &rdev->flags);
1852 return len;
1853}
1854
1855
1856static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001857__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001858
NeilBrown93c8cad2006-01-06 00:20:56 -08001859static ssize_t
1860offset_show(mdk_rdev_t *rdev, char *page)
1861{
Andrew Morton6961ece2006-01-06 00:20:59 -08001862 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001863}
1864
1865static ssize_t
1866offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1867{
1868 char *e;
1869 unsigned long long offset = simple_strtoull(buf, &e, 10);
1870 if (e==buf || (*e && *e != '\n'))
1871 return -EINVAL;
1872 if (rdev->mddev->pers)
1873 return -EBUSY;
1874 rdev->data_offset = offset;
1875 return len;
1876}
1877
1878static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001879__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001880
NeilBrown83303b62006-01-06 00:21:06 -08001881static ssize_t
1882rdev_size_show(mdk_rdev_t *rdev, char *page)
1883{
1884 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1885}
1886
1887static ssize_t
1888rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1889{
1890 char *e;
1891 unsigned long long size = simple_strtoull(buf, &e, 10);
1892 if (e==buf || (*e && *e != '\n'))
1893 return -EINVAL;
1894 if (rdev->mddev->pers)
1895 return -EBUSY;
1896 rdev->size = size;
1897 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1898 rdev->mddev->size = size;
1899 return len;
1900}
1901
1902static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07001903__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08001904
NeilBrown86e6ffd2005-11-08 21:39:24 -08001905static struct attribute *rdev_default_attrs[] = {
1906 &rdev_state.attr,
1907 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001908 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001909 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001910 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001911 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001912 NULL,
1913};
1914static ssize_t
1915rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1916{
1917 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1918 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1919
1920 if (!entry->show)
1921 return -EIO;
1922 return entry->show(rdev, page);
1923}
1924
1925static ssize_t
1926rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1927 const char *page, size_t length)
1928{
1929 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1930 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1931
1932 if (!entry->store)
1933 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07001934 if (!capable(CAP_SYS_ADMIN))
1935 return -EACCES;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001936 return entry->store(rdev, page, length);
1937}
1938
1939static void rdev_free(struct kobject *ko)
1940{
1941 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1942 kfree(rdev);
1943}
1944static struct sysfs_ops rdev_sysfs_ops = {
1945 .show = rdev_attr_show,
1946 .store = rdev_attr_store,
1947};
1948static struct kobj_type rdev_ktype = {
1949 .release = rdev_free,
1950 .sysfs_ops = &rdev_sysfs_ops,
1951 .default_attrs = rdev_default_attrs,
1952};
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954/*
1955 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1956 *
1957 * mark the device faulty if:
1958 *
1959 * - the device is nonexistent (zero size)
1960 * - the device has no valid superblock
1961 *
1962 * a faulty rdev _never_ has rdev->sb set.
1963 */
1964static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1965{
1966 char b[BDEVNAME_SIZE];
1967 int err;
1968 mdk_rdev_t *rdev;
1969 sector_t size;
1970
NeilBrown9ffae0c2006-01-06 00:20:32 -08001971 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (!rdev) {
1973 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1974 return ERR_PTR(-ENOMEM);
1975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 if ((err = alloc_disk_sb(rdev)))
1978 goto abort_free;
1979
1980 err = lock_rdev(rdev, newdev);
1981 if (err)
1982 goto abort_free;
1983
NeilBrown86e6ffd2005-11-08 21:39:24 -08001984 rdev->kobj.parent = NULL;
1985 rdev->kobj.ktype = &rdev_ktype;
1986 kobject_init(&rdev->kobj);
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07001989 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08001990 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001991 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07001993 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001995 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001996 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1999 if (!size) {
2000 printk(KERN_WARNING
2001 "md: %s has zero or unknown size, marking faulty!\n",
2002 bdevname(rdev->bdev,b));
2003 err = -EINVAL;
2004 goto abort_free;
2005 }
2006
2007 if (super_format >= 0) {
2008 err = super_types[super_format].
2009 load_super(rdev, NULL, super_minor);
2010 if (err == -EINVAL) {
2011 printk(KERN_WARNING
2012 "md: %s has invalid sb, not importing!\n",
2013 bdevname(rdev->bdev,b));
2014 goto abort_free;
2015 }
2016 if (err < 0) {
2017 printk(KERN_WARNING
2018 "md: could not read %s's sb, not importing!\n",
2019 bdevname(rdev->bdev,b));
2020 goto abort_free;
2021 }
2022 }
2023 INIT_LIST_HEAD(&rdev->same_set);
2024
2025 return rdev;
2026
2027abort_free:
2028 if (rdev->sb_page) {
2029 if (rdev->bdev)
2030 unlock_rdev(rdev);
2031 free_disk_sb(rdev);
2032 }
2033 kfree(rdev);
2034 return ERR_PTR(err);
2035}
2036
2037/*
2038 * Check a full RAID array for plausibility
2039 */
2040
2041
NeilBrowna757e642005-04-16 15:26:42 -07002042static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 int i;
2045 struct list_head *tmp;
2046 mdk_rdev_t *rdev, *freshest;
2047 char b[BDEVNAME_SIZE];
2048
2049 freshest = NULL;
2050 ITERATE_RDEV(mddev,rdev,tmp)
2051 switch (super_types[mddev->major_version].
2052 load_super(rdev, freshest, mddev->minor_version)) {
2053 case 1:
2054 freshest = rdev;
2055 break;
2056 case 0:
2057 break;
2058 default:
2059 printk( KERN_ERR \
2060 "md: fatal superblock inconsistency in %s"
2061 " -- removing from array\n",
2062 bdevname(rdev->bdev,b));
2063 kick_rdev_from_array(rdev);
2064 }
2065
2066
2067 super_types[mddev->major_version].
2068 validate_super(mddev, freshest);
2069
2070 i = 0;
2071 ITERATE_RDEV(mddev,rdev,tmp) {
2072 if (rdev != freshest)
2073 if (super_types[mddev->major_version].
2074 validate_super(mddev, rdev)) {
2075 printk(KERN_WARNING "md: kicking non-fresh %s"
2076 " from array!\n",
2077 bdevname(rdev->bdev,b));
2078 kick_rdev_from_array(rdev);
2079 continue;
2080 }
2081 if (mddev->level == LEVEL_MULTIPATH) {
2082 rdev->desc_nr = i++;
2083 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002084 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086 }
2087
2088
2089
2090 if (mddev->recovery_cp != MaxSector &&
2091 mddev->level >= 1)
2092 printk(KERN_ERR "md: %s: raid array is not clean"
2093 " -- starting background reconstruction\n",
2094 mdname(mddev));
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
NeilBrowneae17012005-11-08 21:39:23 -08002098static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002099safe_delay_show(mddev_t *mddev, char *page)
2100{
2101 int msec = (mddev->safemode_delay*1000)/HZ;
2102 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2103}
2104static ssize_t
2105safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2106{
2107 int scale=1;
2108 int dot=0;
2109 int i;
2110 unsigned long msec;
2111 char buf[30];
2112 char *e;
2113 /* remove a period, and count digits after it */
2114 if (len >= sizeof(buf))
2115 return -EINVAL;
2116 strlcpy(buf, cbuf, len);
2117 buf[len] = 0;
2118 for (i=0; i<len; i++) {
2119 if (dot) {
2120 if (isdigit(buf[i])) {
2121 buf[i-1] = buf[i];
2122 scale *= 10;
2123 }
2124 buf[i] = 0;
2125 } else if (buf[i] == '.') {
2126 dot=1;
2127 buf[i] = 0;
2128 }
2129 }
2130 msec = simple_strtoul(buf, &e, 10);
2131 if (e == buf || (*e && *e != '\n'))
2132 return -EINVAL;
2133 msec = (msec * 1000) / scale;
2134 if (msec == 0)
2135 mddev->safemode_delay = 0;
2136 else {
2137 mddev->safemode_delay = (msec*HZ)/1000;
2138 if (mddev->safemode_delay == 0)
2139 mddev->safemode_delay = 1;
2140 }
2141 return len;
2142}
2143static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002144__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002145
2146static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002147level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002148{
NeilBrown2604b702006-01-06 00:20:36 -08002149 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002150 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002151 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002152 else if (mddev->clevel[0])
2153 return sprintf(page, "%s\n", mddev->clevel);
2154 else if (mddev->level != LEVEL_NONE)
2155 return sprintf(page, "%d\n", mddev->level);
2156 else
2157 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002158}
2159
NeilBrownd9d166c2006-01-06 00:20:51 -08002160static ssize_t
2161level_store(mddev_t *mddev, const char *buf, size_t len)
2162{
2163 int rv = len;
2164 if (mddev->pers)
2165 return -EBUSY;
2166 if (len == 0)
2167 return 0;
2168 if (len >= sizeof(mddev->clevel))
2169 return -ENOSPC;
2170 strncpy(mddev->clevel, buf, len);
2171 if (mddev->clevel[len-1] == '\n')
2172 len--;
2173 mddev->clevel[len] = 0;
2174 mddev->level = LEVEL_NONE;
2175 return rv;
2176}
2177
2178static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002179__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002180
NeilBrownd4dbd022006-06-26 00:27:59 -07002181
2182static ssize_t
2183layout_show(mddev_t *mddev, char *page)
2184{
2185 /* just a number, not meaningful for all levels */
2186 return sprintf(page, "%d\n", mddev->layout);
2187}
2188
2189static ssize_t
2190layout_store(mddev_t *mddev, const char *buf, size_t len)
2191{
2192 char *e;
2193 unsigned long n = simple_strtoul(buf, &e, 10);
2194 if (mddev->pers)
2195 return -EBUSY;
2196
2197 if (!*buf || (*e && *e != '\n'))
2198 return -EINVAL;
2199
2200 mddev->layout = n;
2201 return len;
2202}
2203static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002204__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002205
2206
NeilBrowneae17012005-11-08 21:39:23 -08002207static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002208raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002209{
NeilBrownbb636542005-11-08 21:39:45 -08002210 if (mddev->raid_disks == 0)
2211 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002212 return sprintf(page, "%d\n", mddev->raid_disks);
2213}
2214
NeilBrownda943b92006-01-06 00:20:54 -08002215static int update_raid_disks(mddev_t *mddev, int raid_disks);
2216
2217static ssize_t
2218raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2219{
NeilBrownda943b92006-01-06 00:20:54 -08002220 char *e;
2221 int rv = 0;
2222 unsigned long n = simple_strtoul(buf, &e, 10);
2223
2224 if (!*buf || (*e && *e != '\n'))
2225 return -EINVAL;
2226
2227 if (mddev->pers)
2228 rv = update_raid_disks(mddev, n);
2229 else
2230 mddev->raid_disks = n;
2231 return rv ? rv : len;
2232}
2233static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002234__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002235
NeilBrown24dd4692005-11-08 21:39:26 -08002236static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002237chunk_size_show(mddev_t *mddev, char *page)
2238{
2239 return sprintf(page, "%d\n", mddev->chunk_size);
2240}
2241
2242static ssize_t
2243chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2244{
2245 /* can only set chunk_size if array is not yet active */
2246 char *e;
2247 unsigned long n = simple_strtoul(buf, &e, 10);
2248
2249 if (mddev->pers)
2250 return -EBUSY;
2251 if (!*buf || (*e && *e != '\n'))
2252 return -EINVAL;
2253
2254 mddev->chunk_size = n;
2255 return len;
2256}
2257static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002258__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002259
NeilBrowna94213b2006-06-26 00:28:00 -07002260static ssize_t
2261resync_start_show(mddev_t *mddev, char *page)
2262{
2263 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2264}
2265
2266static ssize_t
2267resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2268{
2269 /* can only set chunk_size if array is not yet active */
2270 char *e;
2271 unsigned long long n = simple_strtoull(buf, &e, 10);
2272
2273 if (mddev->pers)
2274 return -EBUSY;
2275 if (!*buf || (*e && *e != '\n'))
2276 return -EINVAL;
2277
2278 mddev->recovery_cp = n;
2279 return len;
2280}
2281static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002282__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002283
NeilBrown9e653b62006-06-26 00:27:58 -07002284/*
2285 * The array state can be:
2286 *
2287 * clear
2288 * No devices, no size, no level
2289 * Equivalent to STOP_ARRAY ioctl
2290 * inactive
2291 * May have some settings, but array is not active
2292 * all IO results in error
2293 * When written, doesn't tear down array, but just stops it
2294 * suspended (not supported yet)
2295 * All IO requests will block. The array can be reconfigured.
2296 * Writing this, if accepted, will block until array is quiessent
2297 * readonly
2298 * no resync can happen. no superblocks get written.
2299 * write requests fail
2300 * read-auto
2301 * like readonly, but behaves like 'clean' on a write request.
2302 *
2303 * clean - no pending writes, but otherwise active.
2304 * When written to inactive array, starts without resync
2305 * If a write request arrives then
2306 * if metadata is known, mark 'dirty' and switch to 'active'.
2307 * if not known, block and switch to write-pending
2308 * If written to an active array that has pending writes, then fails.
2309 * active
2310 * fully active: IO and resync can be happening.
2311 * When written to inactive array, starts with resync
2312 *
2313 * write-pending
2314 * clean, but writes are blocked waiting for 'active' to be written.
2315 *
2316 * active-idle
2317 * like active, but no writes have been seen for a while (100msec).
2318 *
2319 */
2320enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2321 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002322static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002323 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2324 "write-pending", "active-idle", NULL };
2325
2326static int match_word(const char *word, char **list)
2327{
2328 int n;
2329 for (n=0; list[n]; n++)
2330 if (cmd_match(word, list[n]))
2331 break;
2332 return n;
2333}
2334
2335static ssize_t
2336array_state_show(mddev_t *mddev, char *page)
2337{
2338 enum array_state st = inactive;
2339
2340 if (mddev->pers)
2341 switch(mddev->ro) {
2342 case 1:
2343 st = readonly;
2344 break;
2345 case 2:
2346 st = read_auto;
2347 break;
2348 case 0:
2349 if (mddev->in_sync)
2350 st = clean;
2351 else if (mddev->safemode)
2352 st = active_idle;
2353 else
2354 st = active;
2355 }
2356 else {
2357 if (list_empty(&mddev->disks) &&
2358 mddev->raid_disks == 0 &&
2359 mddev->size == 0)
2360 st = clear;
2361 else
2362 st = inactive;
2363 }
2364 return sprintf(page, "%s\n", array_states[st]);
2365}
2366
2367static int do_md_stop(mddev_t * mddev, int ro);
2368static int do_md_run(mddev_t * mddev);
2369static int restart_array(mddev_t *mddev);
2370
2371static ssize_t
2372array_state_store(mddev_t *mddev, const char *buf, size_t len)
2373{
2374 int err = -EINVAL;
2375 enum array_state st = match_word(buf, array_states);
2376 switch(st) {
2377 case bad_word:
2378 break;
2379 case clear:
2380 /* stopping an active array */
2381 if (mddev->pers) {
2382 if (atomic_read(&mddev->active) > 1)
2383 return -EBUSY;
2384 err = do_md_stop(mddev, 0);
2385 }
2386 break;
2387 case inactive:
2388 /* stopping an active array */
2389 if (mddev->pers) {
2390 if (atomic_read(&mddev->active) > 1)
2391 return -EBUSY;
2392 err = do_md_stop(mddev, 2);
2393 }
2394 break;
2395 case suspended:
2396 break; /* not supported yet */
2397 case readonly:
2398 if (mddev->pers)
2399 err = do_md_stop(mddev, 1);
2400 else {
2401 mddev->ro = 1;
2402 err = do_md_run(mddev);
2403 }
2404 break;
2405 case read_auto:
2406 /* stopping an active array */
2407 if (mddev->pers) {
2408 err = do_md_stop(mddev, 1);
2409 if (err == 0)
2410 mddev->ro = 2; /* FIXME mark devices writable */
2411 } else {
2412 mddev->ro = 2;
2413 err = do_md_run(mddev);
2414 }
2415 break;
2416 case clean:
2417 if (mddev->pers) {
2418 restart_array(mddev);
2419 spin_lock_irq(&mddev->write_lock);
2420 if (atomic_read(&mddev->writes_pending) == 0) {
2421 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07002422 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002423 }
2424 spin_unlock_irq(&mddev->write_lock);
2425 } else {
2426 mddev->ro = 0;
2427 mddev->recovery_cp = MaxSector;
2428 err = do_md_run(mddev);
2429 }
2430 break;
2431 case active:
2432 if (mddev->pers) {
2433 restart_array(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002434 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002435 wake_up(&mddev->sb_wait);
2436 err = 0;
2437 } else {
2438 mddev->ro = 0;
2439 err = do_md_run(mddev);
2440 }
2441 break;
2442 case write_pending:
2443 case active_idle:
2444 /* these cannot be set */
2445 break;
2446 }
2447 if (err)
2448 return err;
2449 else
2450 return len;
2451}
NeilBrown80ca3a42006-07-10 04:44:18 -07002452static struct md_sysfs_entry md_array_state =
2453__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002454
NeilBrown6d7ff732006-01-06 00:21:16 -08002455static ssize_t
2456null_show(mddev_t *mddev, char *page)
2457{
2458 return -EINVAL;
2459}
2460
2461static ssize_t
2462new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2463{
2464 /* buf must be %d:%d\n? giving major and minor numbers */
2465 /* The new device is added to the array.
2466 * If the array has a persistent superblock, we read the
2467 * superblock to initialise info and check validity.
2468 * Otherwise, only checking done is that in bind_rdev_to_array,
2469 * which mainly checks size.
2470 */
2471 char *e;
2472 int major = simple_strtoul(buf, &e, 10);
2473 int minor;
2474 dev_t dev;
2475 mdk_rdev_t *rdev;
2476 int err;
2477
2478 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2479 return -EINVAL;
2480 minor = simple_strtoul(e+1, &e, 10);
2481 if (*e && *e != '\n')
2482 return -EINVAL;
2483 dev = MKDEV(major, minor);
2484 if (major != MAJOR(dev) ||
2485 minor != MINOR(dev))
2486 return -EOVERFLOW;
2487
2488
2489 if (mddev->persistent) {
2490 rdev = md_import_device(dev, mddev->major_version,
2491 mddev->minor_version);
2492 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2493 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2494 mdk_rdev_t, same_set);
2495 err = super_types[mddev->major_version]
2496 .load_super(rdev, rdev0, mddev->minor_version);
2497 if (err < 0)
2498 goto out;
2499 }
2500 } else
2501 rdev = md_import_device(dev, -1, -1);
2502
2503 if (IS_ERR(rdev))
2504 return PTR_ERR(rdev);
2505 err = bind_rdev_to_array(rdev, mddev);
2506 out:
2507 if (err)
2508 export_rdev(rdev);
2509 return err ? err : len;
2510}
2511
2512static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002513__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002514
2515static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002516bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2517{
2518 char *end;
2519 unsigned long chunk, end_chunk;
2520
2521 if (!mddev->bitmap)
2522 goto out;
2523 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2524 while (*buf) {
2525 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2526 if (buf == end) break;
2527 if (*end == '-') { /* range */
2528 buf = end + 1;
2529 end_chunk = simple_strtoul(buf, &end, 0);
2530 if (buf == end) break;
2531 }
2532 if (*end && !isspace(*end)) break;
2533 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2534 buf = end;
2535 while (isspace(*buf)) buf++;
2536 }
2537 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2538out:
2539 return len;
2540}
2541
2542static struct md_sysfs_entry md_bitmap =
2543__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2544
2545static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002546size_show(mddev_t *mddev, char *page)
2547{
2548 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2549}
2550
2551static int update_size(mddev_t *mddev, unsigned long size);
2552
2553static ssize_t
2554size_store(mddev_t *mddev, const char *buf, size_t len)
2555{
2556 /* If array is inactive, we can reduce the component size, but
2557 * not increase it (except from 0).
2558 * If array is active, we can try an on-line resize
2559 */
2560 char *e;
2561 int err = 0;
2562 unsigned long long size = simple_strtoull(buf, &e, 10);
2563 if (!*buf || *buf == '\n' ||
2564 (*e && *e != '\n'))
2565 return -EINVAL;
2566
2567 if (mddev->pers) {
2568 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002569 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002570 } else {
2571 if (mddev->size == 0 ||
2572 mddev->size > size)
2573 mddev->size = size;
2574 else
2575 err = -ENOSPC;
2576 }
2577 return err ? err : len;
2578}
2579
2580static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002581__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002582
NeilBrown8bb93aa2006-01-06 00:20:50 -08002583
2584/* Metdata version.
2585 * This is either 'none' for arrays with externally managed metadata,
2586 * or N.M for internally known formats
2587 */
2588static ssize_t
2589metadata_show(mddev_t *mddev, char *page)
2590{
2591 if (mddev->persistent)
2592 return sprintf(page, "%d.%d\n",
2593 mddev->major_version, mddev->minor_version);
2594 else
2595 return sprintf(page, "none\n");
2596}
2597
2598static ssize_t
2599metadata_store(mddev_t *mddev, const char *buf, size_t len)
2600{
2601 int major, minor;
2602 char *e;
2603 if (!list_empty(&mddev->disks))
2604 return -EBUSY;
2605
2606 if (cmd_match(buf, "none")) {
2607 mddev->persistent = 0;
2608 mddev->major_version = 0;
2609 mddev->minor_version = 90;
2610 return len;
2611 }
2612 major = simple_strtoul(buf, &e, 10);
2613 if (e==buf || *e != '.')
2614 return -EINVAL;
2615 buf = e+1;
2616 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002617 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002618 return -EINVAL;
2619 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2620 super_types[major].name == NULL)
2621 return -ENOENT;
2622 mddev->major_version = major;
2623 mddev->minor_version = minor;
2624 mddev->persistent = 1;
2625 return len;
2626}
2627
2628static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002629__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002630
NeilBrowna35b0d62006-01-06 00:20:49 -08002631static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002632action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002633{
NeilBrown7eec3142005-11-08 21:39:44 -08002634 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002635 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2636 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002637 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2638 type = "reshape";
2639 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002640 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2641 type = "resync";
2642 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2643 type = "check";
2644 else
2645 type = "repair";
2646 } else
2647 type = "recover";
2648 }
2649 return sprintf(page, "%s\n", type);
2650}
2651
2652static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002653action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002654{
NeilBrown7eec3142005-11-08 21:39:44 -08002655 if (!mddev->pers || !mddev->pers->sync_request)
2656 return -EINVAL;
2657
NeilBrownbce74da2006-01-06 00:20:41 -08002658 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002659 if (mddev->sync_thread) {
2660 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2661 md_unregister_thread(mddev->sync_thread);
2662 mddev->sync_thread = NULL;
2663 mddev->recovery = 0;
2664 }
NeilBrown03c902e2006-01-06 00:20:46 -08002665 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2666 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002667 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002668 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002669 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002670 else if (cmd_match(page, "reshape")) {
2671 int err;
2672 if (mddev->pers->start_reshape == NULL)
2673 return -EINVAL;
2674 err = mddev->pers->start_reshape(mddev);
2675 if (err)
2676 return err;
2677 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002678 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002679 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002680 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002681 return -EINVAL;
2682 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2683 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002684 }
NeilBrown03c902e2006-01-06 00:20:46 -08002685 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002686 md_wakeup_thread(mddev->thread);
2687 return len;
2688}
2689
NeilBrown9d888832005-11-08 21:39:26 -08002690static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002691mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002692{
2693 return sprintf(page, "%llu\n",
2694 (unsigned long long) mddev->resync_mismatches);
2695}
2696
NeilBrown80ca3a42006-07-10 04:44:18 -07002697static struct md_sysfs_entry md_scan_mode =
2698__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002699
NeilBrown96de1e62005-11-08 21:39:39 -08002700
NeilBrown80ca3a42006-07-10 04:44:18 -07002701static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002702
NeilBrown88202a02006-01-06 00:21:36 -08002703static ssize_t
2704sync_min_show(mddev_t *mddev, char *page)
2705{
2706 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2707 mddev->sync_speed_min ? "local": "system");
2708}
2709
2710static ssize_t
2711sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2712{
2713 int min;
2714 char *e;
2715 if (strncmp(buf, "system", 6)==0) {
2716 mddev->sync_speed_min = 0;
2717 return len;
2718 }
2719 min = simple_strtoul(buf, &e, 10);
2720 if (buf == e || (*e && *e != '\n') || min <= 0)
2721 return -EINVAL;
2722 mddev->sync_speed_min = min;
2723 return len;
2724}
2725
2726static struct md_sysfs_entry md_sync_min =
2727__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2728
2729static ssize_t
2730sync_max_show(mddev_t *mddev, char *page)
2731{
2732 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2733 mddev->sync_speed_max ? "local": "system");
2734}
2735
2736static ssize_t
2737sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2738{
2739 int max;
2740 char *e;
2741 if (strncmp(buf, "system", 6)==0) {
2742 mddev->sync_speed_max = 0;
2743 return len;
2744 }
2745 max = simple_strtoul(buf, &e, 10);
2746 if (buf == e || (*e && *e != '\n') || max <= 0)
2747 return -EINVAL;
2748 mddev->sync_speed_max = max;
2749 return len;
2750}
2751
2752static struct md_sysfs_entry md_sync_max =
2753__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2754
2755
2756static ssize_t
2757sync_speed_show(mddev_t *mddev, char *page)
2758{
2759 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07002760 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08002761 dt = ((jiffies - mddev->resync_mark) / HZ);
2762 if (!dt) dt++;
2763 db = resync - (mddev->resync_mark_cnt);
2764 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2765}
2766
NeilBrown80ca3a42006-07-10 04:44:18 -07002767static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08002768
2769static ssize_t
2770sync_completed_show(mddev_t *mddev, char *page)
2771{
2772 unsigned long max_blocks, resync;
2773
2774 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2775 max_blocks = mddev->resync_max_sectors;
2776 else
2777 max_blocks = mddev->size << 1;
2778
2779 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2780 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2781}
2782
NeilBrown80ca3a42006-07-10 04:44:18 -07002783static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08002784
NeilBrowne464eaf2006-03-27 01:18:14 -08002785static ssize_t
2786suspend_lo_show(mddev_t *mddev, char *page)
2787{
2788 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2789}
2790
2791static ssize_t
2792suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2793{
2794 char *e;
2795 unsigned long long new = simple_strtoull(buf, &e, 10);
2796
2797 if (mddev->pers->quiesce == NULL)
2798 return -EINVAL;
2799 if (buf == e || (*e && *e != '\n'))
2800 return -EINVAL;
2801 if (new >= mddev->suspend_hi ||
2802 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2803 mddev->suspend_lo = new;
2804 mddev->pers->quiesce(mddev, 2);
2805 return len;
2806 } else
2807 return -EINVAL;
2808}
2809static struct md_sysfs_entry md_suspend_lo =
2810__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2811
2812
2813static ssize_t
2814suspend_hi_show(mddev_t *mddev, char *page)
2815{
2816 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2817}
2818
2819static ssize_t
2820suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2821{
2822 char *e;
2823 unsigned long long new = simple_strtoull(buf, &e, 10);
2824
2825 if (mddev->pers->quiesce == NULL)
2826 return -EINVAL;
2827 if (buf == e || (*e && *e != '\n'))
2828 return -EINVAL;
2829 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2830 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2831 mddev->suspend_hi = new;
2832 mddev->pers->quiesce(mddev, 1);
2833 mddev->pers->quiesce(mddev, 0);
2834 return len;
2835 } else
2836 return -EINVAL;
2837}
2838static struct md_sysfs_entry md_suspend_hi =
2839__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2840
2841
NeilBrowneae17012005-11-08 21:39:23 -08002842static struct attribute *md_default_attrs[] = {
2843 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07002844 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002845 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002846 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002847 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07002848 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002849 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08002850 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07002851 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07002852 &md_array_state.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002853 NULL,
2854};
2855
2856static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002857 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002858 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002859 &md_sync_min.attr,
2860 &md_sync_max.attr,
2861 &md_sync_speed.attr,
2862 &md_sync_completed.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08002863 &md_suspend_lo.attr,
2864 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07002865 &md_bitmap.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002866 NULL,
2867};
NeilBrown411036f2005-11-08 21:39:40 -08002868static struct attribute_group md_redundancy_group = {
2869 .name = NULL,
2870 .attrs = md_redundancy_attrs,
2871};
2872
NeilBrowneae17012005-11-08 21:39:23 -08002873
2874static ssize_t
2875md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2876{
2877 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2878 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002879 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002880
2881 if (!entry->show)
2882 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002883 rv = mddev_lock(mddev);
2884 if (!rv) {
2885 rv = entry->show(mddev, page);
2886 mddev_unlock(mddev);
2887 }
NeilBrown96de1e62005-11-08 21:39:39 -08002888 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002889}
2890
2891static ssize_t
2892md_attr_store(struct kobject *kobj, struct attribute *attr,
2893 const char *page, size_t length)
2894{
2895 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2896 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002897 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002898
2899 if (!entry->store)
2900 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002901 if (!capable(CAP_SYS_ADMIN))
2902 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002903 rv = mddev_lock(mddev);
2904 if (!rv) {
2905 rv = entry->store(mddev, page, length);
2906 mddev_unlock(mddev);
2907 }
NeilBrown96de1e62005-11-08 21:39:39 -08002908 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002909}
2910
2911static void md_free(struct kobject *ko)
2912{
2913 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2914 kfree(mddev);
2915}
2916
2917static struct sysfs_ops md_sysfs_ops = {
2918 .show = md_attr_show,
2919 .store = md_attr_store,
2920};
2921static struct kobj_type md_ktype = {
2922 .release = md_free,
2923 .sysfs_ops = &md_sysfs_ops,
2924 .default_attrs = md_default_attrs,
2925};
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927int mdp_major = 0;
2928
2929static struct kobject *md_probe(dev_t dev, int *part, void *data)
2930{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002931 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 mddev_t *mddev = mddev_find(dev);
2933 struct gendisk *disk;
2934 int partitioned = (MAJOR(dev) != MD_MAJOR);
2935 int shift = partitioned ? MdpMinorShift : 0;
2936 int unit = MINOR(dev) >> shift;
2937
2938 if (!mddev)
2939 return NULL;
2940
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002941 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002943 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 mddev_put(mddev);
2945 return NULL;
2946 }
2947 disk = alloc_disk(1 << shift);
2948 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002949 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 mddev_put(mddev);
2951 return NULL;
2952 }
2953 disk->major = MAJOR(dev);
2954 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07002955 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07002957 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 disk->fops = &md_fops;
2960 disk->private_data = mddev;
2961 disk->queue = mddev->queue;
2962 add_disk(disk);
2963 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002964 mutex_unlock(&disks_mutex);
NeilBrown9c791972005-11-08 21:39:28 -08002965 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08002966 mddev->kobj.k_name = NULL;
2967 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2968 mddev->kobj.ktype = &md_ktype;
2969 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return NULL;
2971}
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973static void md_safemode_timeout(unsigned long data)
2974{
2975 mddev_t *mddev = (mddev_t *) data;
2976
2977 mddev->safemode = 1;
2978 md_wakeup_thread(mddev->thread);
2979}
2980
NeilBrown6ff8d8e2006-01-06 00:20:15 -08002981static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983static int do_md_run(mddev_t * mddev)
2984{
NeilBrown2604b702006-01-06 00:20:36 -08002985 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 int chunk_size;
2987 struct list_head *tmp;
2988 mdk_rdev_t *rdev;
2989 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08002990 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 char b[BDEVNAME_SIZE];
2992
NeilBrowna757e642005-04-16 15:26:42 -07002993 if (list_empty(&mddev->disks))
2994 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 if (mddev->pers)
2998 return -EBUSY;
2999
3000 /*
3001 * Analyze all RAID superblock(s)
3002 */
NeilBrowna757e642005-04-16 15:26:42 -07003003 if (!mddev->raid_disks)
3004 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
3006 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
NeilBrown2604b702006-01-06 00:20:36 -08003008 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 if (chunk_size > MAX_CHUNK_SIZE) {
3010 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3011 chunk_size, MAX_CHUNK_SIZE);
3012 return -EINVAL;
3013 }
3014 /*
3015 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3016 */
3017 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003018 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 return -EINVAL;
3020 }
3021 if (chunk_size < PAGE_SIZE) {
3022 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3023 chunk_size, PAGE_SIZE);
3024 return -EINVAL;
3025 }
3026
3027 /* devices must have minimum size of one chunk */
3028 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003029 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 continue;
3031 if (rdev->size < chunk_size / 1024) {
3032 printk(KERN_WARNING
3033 "md: Dev %s smaller than chunk_size:"
3034 " %lluk < %dk\n",
3035 bdevname(rdev->bdev,b),
3036 (unsigned long long)rdev->size,
3037 chunk_size / 1024);
3038 return -EINVAL;
3039 }
3040 }
3041 }
3042
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003044 if (mddev->level != LEVEL_NONE)
3045 request_module("md-level-%d", mddev->level);
3046 else if (mddev->clevel[0])
3047 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048#endif
3049
3050 /*
3051 * Drop all container device buffers, from now on
3052 * the only valid external interface is through the md
3053 * device.
3054 * Also find largest hardsector size
3055 */
3056 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003057 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 continue;
3059 sync_blockdev(rdev->bdev);
3060 invalidate_bdev(rdev->bdev, 0);
3061 }
3062
3063 md_probe(mddev->unit, NULL, NULL);
3064 disk = mddev->gendisk;
3065 if (!disk)
3066 return -ENOMEM;
3067
3068 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003069 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003070 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003072 if (mddev->level != LEVEL_NONE)
3073 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3074 mddev->level);
3075 else
3076 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3077 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return -EINVAL;
3079 }
NeilBrown2604b702006-01-06 00:20:36 -08003080 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003082 mddev->level = pers->level;
3083 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
NeilBrownf6705572006-03-27 01:18:11 -08003085 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003086 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003087 /* This personality cannot handle reshaping... */
3088 mddev->pers = NULL;
3089 module_put(pers->owner);
3090 return -EINVAL;
3091 }
3092
NeilBrown7dd5e7c2007-02-28 20:11:35 -08003093 if (pers->sync_request) {
3094 /* Warn if this is a potentially silly
3095 * configuration.
3096 */
3097 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3098 mdk_rdev_t *rdev2;
3099 struct list_head *tmp2;
3100 int warned = 0;
3101 ITERATE_RDEV(mddev, rdev, tmp) {
3102 ITERATE_RDEV(mddev, rdev2, tmp2) {
3103 if (rdev < rdev2 &&
3104 rdev->bdev->bd_contains ==
3105 rdev2->bdev->bd_contains) {
3106 printk(KERN_WARNING
3107 "%s: WARNING: %s appears to be"
3108 " on the same physical disk as"
3109 " %s.\n",
3110 mdname(mddev),
3111 bdevname(rdev->bdev,b),
3112 bdevname(rdev2->bdev,b2));
3113 warned = 1;
3114 }
3115 }
3116 }
3117 if (warned)
3118 printk(KERN_WARNING
3119 "True protection against single-disk"
3120 " failure might be compromised.\n");
3121 }
3122
NeilBrown657390d2005-08-26 18:34:16 -07003123 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003125 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08003126 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
NeilBrownf91de922005-11-08 21:39:36 -08003128 if (start_readonly)
3129 mddev->ro = 2; /* read-only, but switch on first write */
3130
NeilBrownb15c2e52006-01-06 00:20:16 -08003131 err = mddev->pers->run(mddev);
3132 if (!err && mddev->pers->sync_request) {
3133 err = bitmap_create(mddev);
3134 if (err) {
3135 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3136 mdname(mddev), err);
3137 mddev->pers->stop(mddev);
3138 }
3139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (err) {
3141 printk(KERN_ERR "md: pers->run() failed ...\n");
3142 module_put(mddev->pers->owner);
3143 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003144 bitmap_destroy(mddev);
3145 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
NeilBrown411036f2005-11-08 21:39:40 -08003147 if (mddev->pers->sync_request)
3148 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08003149 else if (mddev->ro == 2) /* auto-readonly not meaningful */
3150 mddev->ro = 0;
3151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 atomic_set(&mddev->writes_pending,0);
3153 mddev->safemode = 0;
3154 mddev->safemode_timer.function = md_safemode_timeout;
3155 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003156 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003158
3159 ITERATE_RDEV(mddev,rdev,tmp)
3160 if (rdev->raid_disk >= 0) {
3161 char nm[20];
3162 sprintf(nm, "rd%d", rdev->raid_disk);
3163 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3167
NeilBrown850b2b42006-10-03 01:15:46 -07003168 if (mddev->flags)
3169 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
3171 set_capacity(disk, mddev->array_size<<1);
3172
3173 /* If we call blk_queue_make_request here, it will
3174 * re-initialise max_sectors etc which may have been
3175 * refined inside -> run. So just set the bits we need to set.
3176 * Most initialisation happended when we called
3177 * blk_queue_make_request(..., md_fail_request)
3178 * earlier.
3179 */
3180 mddev->queue->queuedata = mddev;
3181 mddev->queue->make_request_fn = mddev->pers->make_request;
3182
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003183 /* If there is a partially-recovered drive we need to
3184 * start recovery here. If we leave it to md_check_recovery,
3185 * it will remove the drives and not do the right thing
3186 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003187 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003188 struct list_head *rtmp;
3189 int spares = 0;
3190 ITERATE_RDEV(mddev,rdev,rtmp)
3191 if (rdev->raid_disk >= 0 &&
3192 !test_bit(In_sync, &rdev->flags) &&
3193 !test_bit(Faulty, &rdev->flags))
3194 /* complete an interrupted recovery */
3195 spares++;
3196 if (spares && mddev->pers->sync_request) {
3197 mddev->recovery = 0;
3198 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3199 mddev->sync_thread = md_register_thread(md_do_sync,
3200 mddev,
3201 "%s_resync");
3202 if (!mddev->sync_thread) {
3203 printk(KERN_ERR "%s: could not start resync"
3204 " thread...\n",
3205 mdname(mddev));
3206 /* leave the spares where they are, it shouldn't hurt */
3207 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003208 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003209 }
3210 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003211 md_wakeup_thread(mddev->thread);
3212 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003215 md_new_event(mddev);
NeilBrown2f471302006-11-08 17:44:47 -08003216 kobject_uevent(&mddev->gendisk->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 return 0;
3218}
3219
3220static int restart_array(mddev_t *mddev)
3221{
3222 struct gendisk *disk = mddev->gendisk;
3223 int err;
3224
3225 /*
3226 * Complain if it has no devices
3227 */
3228 err = -ENXIO;
3229 if (list_empty(&mddev->disks))
3230 goto out;
3231
3232 if (mddev->pers) {
3233 err = -EBUSY;
3234 if (!mddev->ro)
3235 goto out;
3236
3237 mddev->safemode = 0;
3238 mddev->ro = 0;
3239 set_disk_ro(disk, 0);
3240
3241 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3242 mdname(mddev));
3243 /*
3244 * Kick recovery or resync if necessary
3245 */
3246 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3247 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003248 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003250 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253out:
3254 return err;
3255}
3256
NeilBrownacc55e22006-06-26 00:27:47 -07003257/* similar to deny_write_access, but accounts for our holding a reference
3258 * to the file ourselves */
3259static int deny_bitmap_write_access(struct file * file)
3260{
3261 struct inode *inode = file->f_mapping->host;
3262
3263 spin_lock(&inode->i_lock);
3264 if (atomic_read(&inode->i_writecount) > 1) {
3265 spin_unlock(&inode->i_lock);
3266 return -ETXTBSY;
3267 }
3268 atomic_set(&inode->i_writecount, -1);
3269 spin_unlock(&inode->i_lock);
3270
3271 return 0;
3272}
3273
3274static void restore_bitmap_write_access(struct file *file)
3275{
3276 struct inode *inode = file->f_mapping->host;
3277
3278 spin_lock(&inode->i_lock);
3279 atomic_set(&inode->i_writecount, 1);
3280 spin_unlock(&inode->i_lock);
3281}
3282
NeilBrown9e653b62006-06-26 00:27:58 -07003283/* mode:
3284 * 0 - completely stop and dis-assemble array
3285 * 1 - switch to readonly
3286 * 2 - stop but do not disassemble array
3287 */
3288static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
3290 int err = 0;
3291 struct gendisk *disk = mddev->gendisk;
3292
3293 if (mddev->pers) {
3294 if (atomic_read(&mddev->active)>2) {
3295 printk("md: %s still in use.\n",mdname(mddev));
3296 return -EBUSY;
3297 }
3298
3299 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003300 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3302 md_unregister_thread(mddev->sync_thread);
3303 mddev->sync_thread = NULL;
3304 }
3305
3306 del_timer_sync(&mddev->safemode_timer);
3307
3308 invalidate_partition(disk, 0);
3309
NeilBrown9e653b62006-06-26 00:27:58 -07003310 switch(mode) {
3311 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003313 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 goto out;
3315 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003316 break;
3317 case 0: /* disassemble */
3318 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003319 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003320 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 if (mddev->ro)
3322 set_disk_ro(disk, 0);
3323 blk_queue_make_request(mddev->queue, md_fail_request);
3324 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003325 mddev->queue->merge_bvec_fn = NULL;
3326 mddev->queue->unplug_fn = NULL;
3327 mddev->queue->issue_flush_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003328 if (mddev->pers->sync_request)
3329 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 module_put(mddev->pers->owner);
3332 mddev->pers = NULL;
NeilBrown0d4ca602006-12-10 02:20:44 -08003333
3334 set_capacity(disk, 0);
3335 mddev->changed = 1;
3336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 if (mddev->ro)
3338 mddev->ro = 0;
3339 }
NeilBrown850b2b42006-10-03 01:15:46 -07003340 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 /* mark array as shutdown cleanly */
3342 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003343 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 }
NeilBrown9e653b62006-06-26 00:27:58 -07003345 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003347 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 }
NeilBrown32a76272005-06-21 17:17:14 -07003349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 /*
3351 * Free resources if final stop
3352 */
NeilBrown9e653b62006-06-26 00:27:58 -07003353 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003354 mdk_rdev_t *rdev;
3355 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3358
NeilBrown978f9462006-02-02 14:28:05 -08003359 bitmap_destroy(mddev);
3360 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003361 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003362 fput(mddev->bitmap_file);
3363 mddev->bitmap_file = NULL;
3364 }
3365 mddev->bitmap_offset = 0;
3366
NeilBrown86e6ffd2005-11-08 21:39:24 -08003367 ITERATE_RDEV(mddev,rdev,tmp)
3368 if (rdev->raid_disk >= 0) {
3369 char nm[20];
3370 sprintf(nm, "rd%d", rdev->raid_disk);
3371 sysfs_remove_link(&mddev->kobj, nm);
3372 }
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 export_array(mddev);
3375
3376 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003377 mddev->size = 0;
3378 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003379 mddev->recovery_cp = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003380
NeilBrowna8a55c32006-06-26 00:27:39 -07003381 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3383 mdname(mddev));
3384 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003385 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386out:
3387 return err;
3388}
3389
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003390#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391static void autorun_array(mddev_t *mddev)
3392{
3393 mdk_rdev_t *rdev;
3394 struct list_head *tmp;
3395 int err;
3396
NeilBrowna757e642005-04-16 15:26:42 -07003397 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
3400 printk(KERN_INFO "md: running: ");
3401
3402 ITERATE_RDEV(mddev,rdev,tmp) {
3403 char b[BDEVNAME_SIZE];
3404 printk("<%s>", bdevname(rdev->bdev,b));
3405 }
3406 printk("\n");
3407
3408 err = do_md_run (mddev);
3409 if (err) {
3410 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3411 do_md_stop (mddev, 0);
3412 }
3413}
3414
3415/*
3416 * lets try to run arrays based on all disks that have arrived
3417 * until now. (those are in pending_raid_disks)
3418 *
3419 * the method: pick the first pending disk, collect all disks with
3420 * the same UUID, remove all from the pending list and put them into
3421 * the 'same_array' list. Then order this list based on superblock
3422 * update time (freshest comes first), kick out 'old' disks and
3423 * compare superblocks. If everything's fine then run it.
3424 *
3425 * If "unit" is allocated, then bump its reference count
3426 */
3427static void autorun_devices(int part)
3428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 struct list_head *tmp;
3430 mdk_rdev_t *rdev0, *rdev;
3431 mddev_t *mddev;
3432 char b[BDEVNAME_SIZE];
3433
3434 printk(KERN_INFO "md: autorun ...\n");
3435 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003436 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003438 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 rdev0 = list_entry(pending_raid_disks.next,
3440 mdk_rdev_t, same_set);
3441
3442 printk(KERN_INFO "md: considering %s ...\n",
3443 bdevname(rdev0->bdev,b));
3444 INIT_LIST_HEAD(&candidates);
3445 ITERATE_RDEV_PENDING(rdev,tmp)
3446 if (super_90_load(rdev, rdev0, 0) >= 0) {
3447 printk(KERN_INFO "md: adding %s ...\n",
3448 bdevname(rdev->bdev,b));
3449 list_move(&rdev->same_set, &candidates);
3450 }
3451 /*
3452 * now we have a set of devices, with all of them having
3453 * mostly sane superblocks. It's time to allocate the
3454 * mddev.
3455 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003456 if (part) {
3457 dev = MKDEV(mdp_major,
3458 rdev0->preferred_minor << MdpMinorShift);
3459 unit = MINOR(dev) >> MdpMinorShift;
3460 } else {
3461 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3462 unit = MINOR(dev);
3463 }
3464 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3466 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3467 break;
3468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470 md_probe(dev, NULL, NULL);
3471 mddev = mddev_find(dev);
3472 if (!mddev) {
3473 printk(KERN_ERR
3474 "md: cannot allocate memory for md drive.\n");
3475 break;
3476 }
3477 if (mddev_lock(mddev))
3478 printk(KERN_WARNING "md: %s locked, cannot run\n",
3479 mdname(mddev));
3480 else if (mddev->raid_disks || mddev->major_version
3481 || !list_empty(&mddev->disks)) {
3482 printk(KERN_WARNING
3483 "md: %s already running, cannot run %s\n",
3484 mdname(mddev), bdevname(rdev0->bdev,b));
3485 mddev_unlock(mddev);
3486 } else {
3487 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3488 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3489 list_del_init(&rdev->same_set);
3490 if (bind_rdev_to_array(rdev, mddev))
3491 export_rdev(rdev);
3492 }
3493 autorun_array(mddev);
3494 mddev_unlock(mddev);
3495 }
3496 /* on success, candidates will be empty, on error
3497 * it won't...
3498 */
3499 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3500 export_rdev(rdev);
3501 mddev_put(mddev);
3502 }
3503 printk(KERN_INFO "md: ... autorun DONE.\n");
3504}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003505#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507static int get_version(void __user * arg)
3508{
3509 mdu_version_t ver;
3510
3511 ver.major = MD_MAJOR_VERSION;
3512 ver.minor = MD_MINOR_VERSION;
3513 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3514
3515 if (copy_to_user(arg, &ver, sizeof(ver)))
3516 return -EFAULT;
3517
3518 return 0;
3519}
3520
3521static int get_array_info(mddev_t * mddev, void __user * arg)
3522{
3523 mdu_array_info_t info;
3524 int nr,working,active,failed,spare;
3525 mdk_rdev_t *rdev;
3526 struct list_head *tmp;
3527
3528 nr=working=active=failed=spare=0;
3529 ITERATE_RDEV(mddev,rdev,tmp) {
3530 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003531 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 failed++;
3533 else {
3534 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003535 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 active++;
3537 else
3538 spare++;
3539 }
3540 }
3541
3542 info.major_version = mddev->major_version;
3543 info.minor_version = mddev->minor_version;
3544 info.patch_version = MD_PATCHLEVEL_VERSION;
3545 info.ctime = mddev->ctime;
3546 info.level = mddev->level;
3547 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003548 if (info.size != mddev->size) /* overflow */
3549 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 info.nr_disks = nr;
3551 info.raid_disks = mddev->raid_disks;
3552 info.md_minor = mddev->md_minor;
3553 info.not_persistent= !mddev->persistent;
3554
3555 info.utime = mddev->utime;
3556 info.state = 0;
3557 if (mddev->in_sync)
3558 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003559 if (mddev->bitmap && mddev->bitmap_offset)
3560 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 info.active_disks = active;
3562 info.working_disks = working;
3563 info.failed_disks = failed;
3564 info.spare_disks = spare;
3565
3566 info.layout = mddev->layout;
3567 info.chunk_size = mddev->chunk_size;
3568
3569 if (copy_to_user(arg, &info, sizeof(info)))
3570 return -EFAULT;
3571
3572 return 0;
3573}
3574
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003575static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003576{
3577 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3578 char *ptr, *buf = NULL;
3579 int err = -ENOMEM;
3580
NeilBrown2a2275d2007-01-26 00:57:11 -08003581 md_allow_write(mddev);
3582
NeilBrown32a76272005-06-21 17:17:14 -07003583 file = kmalloc(sizeof(*file), GFP_KERNEL);
3584 if (!file)
3585 goto out;
3586
3587 /* bitmap disabled, zero the first byte and copy out */
3588 if (!mddev->bitmap || !mddev->bitmap->file) {
3589 file->pathname[0] = '\0';
3590 goto copy_out;
3591 }
3592
3593 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3594 if (!buf)
3595 goto out;
3596
3597 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3598 if (!ptr)
3599 goto out;
3600
3601 strcpy(file->pathname, ptr);
3602
3603copy_out:
3604 err = 0;
3605 if (copy_to_user(arg, file, sizeof(*file)))
3606 err = -EFAULT;
3607out:
3608 kfree(buf);
3609 kfree(file);
3610 return err;
3611}
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613static int get_disk_info(mddev_t * mddev, void __user * arg)
3614{
3615 mdu_disk_info_t info;
3616 unsigned int nr;
3617 mdk_rdev_t *rdev;
3618
3619 if (copy_from_user(&info, arg, sizeof(info)))
3620 return -EFAULT;
3621
3622 nr = info.number;
3623
3624 rdev = find_rdev_nr(mddev, nr);
3625 if (rdev) {
3626 info.major = MAJOR(rdev->bdev->bd_dev);
3627 info.minor = MINOR(rdev->bdev->bd_dev);
3628 info.raid_disk = rdev->raid_disk;
3629 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003630 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003632 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 info.state |= (1<<MD_DISK_ACTIVE);
3634 info.state |= (1<<MD_DISK_SYNC);
3635 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003636 if (test_bit(WriteMostly, &rdev->flags))
3637 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 } else {
3639 info.major = info.minor = 0;
3640 info.raid_disk = -1;
3641 info.state = (1<<MD_DISK_REMOVED);
3642 }
3643
3644 if (copy_to_user(arg, &info, sizeof(info)))
3645 return -EFAULT;
3646
3647 return 0;
3648}
3649
3650static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3651{
3652 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3653 mdk_rdev_t *rdev;
3654 dev_t dev = MKDEV(info->major,info->minor);
3655
3656 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3657 return -EOVERFLOW;
3658
3659 if (!mddev->raid_disks) {
3660 int err;
3661 /* expecting a device which has a superblock */
3662 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3663 if (IS_ERR(rdev)) {
3664 printk(KERN_WARNING
3665 "md: md_import_device returned %ld\n",
3666 PTR_ERR(rdev));
3667 return PTR_ERR(rdev);
3668 }
3669 if (!list_empty(&mddev->disks)) {
3670 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3671 mdk_rdev_t, same_set);
3672 int err = super_types[mddev->major_version]
3673 .load_super(rdev, rdev0, mddev->minor_version);
3674 if (err < 0) {
3675 printk(KERN_WARNING
3676 "md: %s has different UUID to %s\n",
3677 bdevname(rdev->bdev,b),
3678 bdevname(rdev0->bdev,b2));
3679 export_rdev(rdev);
3680 return -EINVAL;
3681 }
3682 }
3683 err = bind_rdev_to_array(rdev, mddev);
3684 if (err)
3685 export_rdev(rdev);
3686 return err;
3687 }
3688
3689 /*
3690 * add_new_disk can be used once the array is assembled
3691 * to add "hot spares". They must already have a superblock
3692 * written
3693 */
3694 if (mddev->pers) {
3695 int err;
3696 if (!mddev->pers->hot_add_disk) {
3697 printk(KERN_WARNING
3698 "%s: personality does not support diskops!\n",
3699 mdname(mddev));
3700 return -EINVAL;
3701 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003702 if (mddev->persistent)
3703 rdev = md_import_device(dev, mddev->major_version,
3704 mddev->minor_version);
3705 else
3706 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 if (IS_ERR(rdev)) {
3708 printk(KERN_WARNING
3709 "md: md_import_device returned %ld\n",
3710 PTR_ERR(rdev));
3711 return PTR_ERR(rdev);
3712 }
NeilBrown41158c72005-06-21 17:17:25 -07003713 /* set save_raid_disk if appropriate */
3714 if (!mddev->persistent) {
3715 if (info->state & (1<<MD_DISK_SYNC) &&
3716 info->raid_disk < mddev->raid_disks)
3717 rdev->raid_disk = info->raid_disk;
3718 else
3719 rdev->raid_disk = -1;
3720 } else
3721 super_types[mddev->major_version].
3722 validate_super(mddev, rdev);
3723 rdev->saved_raid_disk = rdev->raid_disk;
3724
NeilBrownb2d444d2005-11-08 21:39:31 -08003725 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003726 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3727 set_bit(WriteMostly, &rdev->flags);
3728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 rdev->raid_disk = -1;
3730 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07003731 if (!err && !mddev->pers->hot_remove_disk) {
3732 /* If there is hot_add_disk but no hot_remove_disk
3733 * then added disks for geometry changes,
3734 * and should be added immediately.
3735 */
3736 super_types[mddev->major_version].
3737 validate_super(mddev, rdev);
3738 err = mddev->pers->hot_add_disk(mddev, rdev);
3739 if (err)
3740 unbind_rdev_from_array(rdev);
3741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 if (err)
3743 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003744
NeilBrown17571282006-12-10 02:20:52 -08003745 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07003746 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003747 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 return err;
3749 }
3750
3751 /* otherwise, add_new_disk is only allowed
3752 * for major_version==0 superblocks
3753 */
3754 if (mddev->major_version != 0) {
3755 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3756 mdname(mddev));
3757 return -EINVAL;
3758 }
3759
3760 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3761 int err;
3762 rdev = md_import_device (dev, -1, 0);
3763 if (IS_ERR(rdev)) {
3764 printk(KERN_WARNING
3765 "md: error, md_import_device() returned %ld\n",
3766 PTR_ERR(rdev));
3767 return PTR_ERR(rdev);
3768 }
3769 rdev->desc_nr = info->number;
3770 if (info->raid_disk < mddev->raid_disks)
3771 rdev->raid_disk = info->raid_disk;
3772 else
3773 rdev->raid_disk = -1;
3774
NeilBrownb2d444d2005-11-08 21:39:31 -08003775 rdev->flags = 0;
3776
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003778 if (info->state & (1<<MD_DISK_SYNC))
3779 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003781 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3782 set_bit(WriteMostly, &rdev->flags);
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 if (!mddev->persistent) {
3785 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3786 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3787 } else
3788 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3789 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3790
NeilBrown2bf071b2006-01-06 00:20:55 -08003791 err = bind_rdev_to_array(rdev, mddev);
3792 if (err) {
3793 export_rdev(rdev);
3794 return err;
3795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 }
3797
3798 return 0;
3799}
3800
3801static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3802{
3803 char b[BDEVNAME_SIZE];
3804 mdk_rdev_t *rdev;
3805
3806 if (!mddev->pers)
3807 return -ENODEV;
3808
3809 rdev = find_rdev(mddev, dev);
3810 if (!rdev)
3811 return -ENXIO;
3812
3813 if (rdev->raid_disk >= 0)
3814 goto busy;
3815
3816 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07003817 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08003818 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
3820 return 0;
3821busy:
3822 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3823 bdevname(rdev->bdev,b), mdname(mddev));
3824 return -EBUSY;
3825}
3826
3827static int hot_add_disk(mddev_t * mddev, dev_t dev)
3828{
3829 char b[BDEVNAME_SIZE];
3830 int err;
3831 unsigned int size;
3832 mdk_rdev_t *rdev;
3833
3834 if (!mddev->pers)
3835 return -ENODEV;
3836
3837 if (mddev->major_version != 0) {
3838 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3839 " version-0 superblocks.\n",
3840 mdname(mddev));
3841 return -EINVAL;
3842 }
3843 if (!mddev->pers->hot_add_disk) {
3844 printk(KERN_WARNING
3845 "%s: personality does not support diskops!\n",
3846 mdname(mddev));
3847 return -EINVAL;
3848 }
3849
3850 rdev = md_import_device (dev, -1, 0);
3851 if (IS_ERR(rdev)) {
3852 printk(KERN_WARNING
3853 "md: error, md_import_device() returned %ld\n",
3854 PTR_ERR(rdev));
3855 return -EINVAL;
3856 }
3857
3858 if (mddev->persistent)
3859 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3860 else
3861 rdev->sb_offset =
3862 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3863
3864 size = calc_dev_size(rdev, mddev->chunk_size);
3865 rdev->size = size;
3866
NeilBrownb2d444d2005-11-08 21:39:31 -08003867 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 printk(KERN_WARNING
3869 "md: can not hot-add faulty %s disk to %s!\n",
3870 bdevname(rdev->bdev,b), mdname(mddev));
3871 err = -EINVAL;
3872 goto abort_export;
3873 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003874 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07003876 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08003877 err = bind_rdev_to_array(rdev, mddev);
3878 if (err)
3879 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
3881 /*
3882 * The rest should better be atomic, we can have disk failures
3883 * noticed in interrupt contexts ...
3884 */
3885
3886 if (rdev->desc_nr == mddev->max_disks) {
3887 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3888 mdname(mddev));
3889 err = -EBUSY;
3890 goto abort_unbind_export;
3891 }
3892
3893 rdev->raid_disk = -1;
3894
NeilBrown850b2b42006-10-03 01:15:46 -07003895 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
3897 /*
3898 * Kick recovery, maybe this spare has to be added to the
3899 * array immediately.
3900 */
3901 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3902 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08003903 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 return 0;
3905
3906abort_unbind_export:
3907 unbind_rdev_from_array(rdev);
3908
3909abort_export:
3910 export_rdev(rdev);
3911 return err;
3912}
3913
NeilBrown32a76272005-06-21 17:17:14 -07003914static int set_bitmap_file(mddev_t *mddev, int fd)
3915{
3916 int err;
3917
NeilBrown36fa3062005-09-09 16:23:45 -07003918 if (mddev->pers) {
3919 if (!mddev->pers->quiesce)
3920 return -EBUSY;
3921 if (mddev->recovery || mddev->sync_thread)
3922 return -EBUSY;
3923 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07003924 }
3925
NeilBrown36fa3062005-09-09 16:23:45 -07003926
3927 if (fd >= 0) {
3928 if (mddev->bitmap)
3929 return -EEXIST; /* cannot add when bitmap is present */
3930 mddev->bitmap_file = fget(fd);
3931
3932 if (mddev->bitmap_file == NULL) {
3933 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3934 mdname(mddev));
3935 return -EBADF;
3936 }
3937
3938 err = deny_bitmap_write_access(mddev->bitmap_file);
3939 if (err) {
3940 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3941 mdname(mddev));
3942 fput(mddev->bitmap_file);
3943 mddev->bitmap_file = NULL;
3944 return err;
3945 }
NeilBrowna654b9d82005-06-21 17:17:27 -07003946 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07003947 } else if (mddev->bitmap == NULL)
3948 return -ENOENT; /* cannot remove what isn't there */
3949 err = 0;
3950 if (mddev->pers) {
3951 mddev->pers->quiesce(mddev, 1);
3952 if (fd >= 0)
3953 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07003954 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07003955 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07003956 fd = -1; /* make sure to put the file */
3957 }
NeilBrown36fa3062005-09-09 16:23:45 -07003958 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07003959 }
3960 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07003961 if (mddev->bitmap_file) {
3962 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07003963 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07003964 }
NeilBrown36fa3062005-09-09 16:23:45 -07003965 mddev->bitmap_file = NULL;
3966 }
3967
NeilBrown32a76272005-06-21 17:17:14 -07003968 return err;
3969}
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971/*
3972 * set_array_info is used two different ways
3973 * The original usage is when creating a new array.
3974 * In this usage, raid_disks is > 0 and it together with
3975 * level, size, not_persistent,layout,chunksize determine the
3976 * shape of the array.
3977 * This will always create an array with a type-0.90.0 superblock.
3978 * The newer usage is when assembling an array.
3979 * In this case raid_disks will be 0, and the major_version field is
3980 * use to determine which style super-blocks are to be found on the devices.
3981 * The minor and patch _version numbers are also kept incase the
3982 * super_block handler wishes to interpret them.
3983 */
3984static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3985{
3986
3987 if (info->raid_disks == 0) {
3988 /* just setting version number for superblock loading */
3989 if (info->major_version < 0 ||
3990 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3991 super_types[info->major_version].name == NULL) {
3992 /* maybe try to auto-load a module? */
3993 printk(KERN_INFO
3994 "md: superblock version %d not known\n",
3995 info->major_version);
3996 return -EINVAL;
3997 }
3998 mddev->major_version = info->major_version;
3999 mddev->minor_version = info->minor_version;
4000 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004001 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 return 0;
4003 }
4004 mddev->major_version = MD_MAJOR_VERSION;
4005 mddev->minor_version = MD_MINOR_VERSION;
4006 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4007 mddev->ctime = get_seconds();
4008
4009 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004010 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 mddev->size = info->size;
4012 mddev->raid_disks = info->raid_disks;
4013 /* don't set md_minor, it is determined by which /dev/md* was
4014 * openned
4015 */
4016 if (info->state & (1<<MD_SB_CLEAN))
4017 mddev->recovery_cp = MaxSector;
4018 else
4019 mddev->recovery_cp = 0;
4020 mddev->persistent = ! info->not_persistent;
4021
4022 mddev->layout = info->layout;
4023 mddev->chunk_size = info->chunk_size;
4024
4025 mddev->max_disks = MD_SB_DISKS;
4026
NeilBrown850b2b42006-10-03 01:15:46 -07004027 mddev->flags = 0;
4028 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
NeilBrownb2a27032005-11-28 13:44:12 -08004030 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4031 mddev->bitmap_offset = 0;
4032
NeilBrownf6705572006-03-27 01:18:11 -08004033 mddev->reshape_position = MaxSector;
4034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 /*
4036 * Generate a 128 bit UUID
4037 */
4038 get_random_bytes(mddev->uuid, 16);
4039
NeilBrownf6705572006-03-27 01:18:11 -08004040 mddev->new_level = mddev->level;
4041 mddev->new_chunk = mddev->chunk_size;
4042 mddev->new_layout = mddev->layout;
4043 mddev->delta_disks = 0;
4044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 return 0;
4046}
4047
NeilBrowna35b0d62006-01-06 00:20:49 -08004048static int update_size(mddev_t *mddev, unsigned long size)
4049{
4050 mdk_rdev_t * rdev;
4051 int rv;
4052 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004053 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004054
4055 if (mddev->pers->resize == NULL)
4056 return -EINVAL;
4057 /* The "size" is the amount of each device that is used.
4058 * This can only make sense for arrays with redundancy.
4059 * linear and raid0 always use whatever space is available
4060 * We can only consider changing the size if no resync
4061 * or reconstruction is happening, and if the new size
4062 * is acceptable. It must fit before the sb_offset or,
4063 * if that is <data_offset, it must fit before the
4064 * size of each device.
4065 * If size is zero, we find the largest size that fits.
4066 */
4067 if (mddev->sync_thread)
4068 return -EBUSY;
4069 ITERATE_RDEV(mddev,rdev,tmp) {
4070 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004071 avail = rdev->size * 2;
4072
NeilBrowna35b0d62006-01-06 00:20:49 -08004073 if (fit && (size == 0 || size > avail/2))
4074 size = avail/2;
4075 if (avail < ((sector_t)size << 1))
4076 return -ENOSPC;
4077 }
4078 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4079 if (!rv) {
4080 struct block_device *bdev;
4081
4082 bdev = bdget_disk(mddev->gendisk, 0);
4083 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004084 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004085 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004086 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004087 bdput(bdev);
4088 }
4089 }
4090 return rv;
4091}
4092
NeilBrownda943b92006-01-06 00:20:54 -08004093static int update_raid_disks(mddev_t *mddev, int raid_disks)
4094{
4095 int rv;
4096 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004097 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08004098 return -EINVAL;
4099 if (raid_disks <= 0 ||
4100 raid_disks >= mddev->max_disks)
4101 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004102 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08004103 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004104 mddev->delta_disks = raid_disks - mddev->raid_disks;
4105
4106 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b92006-01-06 00:20:54 -08004107 return rv;
4108}
4109
4110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111/*
4112 * update_array_info is used to change the configuration of an
4113 * on-line array.
4114 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4115 * fields in the info are checked against the array.
4116 * Any differences that cannot be handled will cause an error.
4117 * Normally, only one change can be managed at a time.
4118 */
4119static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4120{
4121 int rv = 0;
4122 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004123 int state = 0;
4124
4125 /* calculate expected state,ignoring low bits */
4126 if (mddev->bitmap && mddev->bitmap_offset)
4127 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
4129 if (mddev->major_version != info->major_version ||
4130 mddev->minor_version != info->minor_version ||
4131/* mddev->patch_version != info->patch_version || */
4132 mddev->ctime != info->ctime ||
4133 mddev->level != info->level ||
4134/* mddev->layout != info->layout || */
4135 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004136 mddev->chunk_size != info->chunk_size ||
4137 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4138 ((state^info->state) & 0xfffffe00)
4139 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 return -EINVAL;
4141 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004142 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 if (mddev->raid_disks != info->raid_disks) cnt++;
4144 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004145 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 if (cnt == 0) return 0;
4147 if (cnt > 1) return -EINVAL;
4148
4149 if (mddev->layout != info->layout) {
4150 /* Change layout
4151 * we don't need to do anything at the md level, the
4152 * personality will take care of it all.
4153 */
4154 if (mddev->pers->reconfig == NULL)
4155 return -EINVAL;
4156 else
4157 return mddev->pers->reconfig(mddev, info->layout, -1);
4158 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004159 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004160 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
NeilBrownda943b92006-01-06 00:20:54 -08004162 if (mddev->raid_disks != info->raid_disks)
4163 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
NeilBrown36fa3062005-09-09 16:23:45 -07004165 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4166 if (mddev->pers->quiesce == NULL)
4167 return -EINVAL;
4168 if (mddev->recovery || mddev->sync_thread)
4169 return -EBUSY;
4170 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4171 /* add the bitmap */
4172 if (mddev->bitmap)
4173 return -EEXIST;
4174 if (mddev->default_bitmap_offset == 0)
4175 return -EINVAL;
4176 mddev->bitmap_offset = mddev->default_bitmap_offset;
4177 mddev->pers->quiesce(mddev, 1);
4178 rv = bitmap_create(mddev);
4179 if (rv)
4180 bitmap_destroy(mddev);
4181 mddev->pers->quiesce(mddev, 0);
4182 } else {
4183 /* remove the bitmap */
4184 if (!mddev->bitmap)
4185 return -ENOENT;
4186 if (mddev->bitmap->file)
4187 return -EINVAL;
4188 mddev->pers->quiesce(mddev, 1);
4189 bitmap_destroy(mddev);
4190 mddev->pers->quiesce(mddev, 0);
4191 mddev->bitmap_offset = 0;
4192 }
4193 }
NeilBrown850b2b42006-10-03 01:15:46 -07004194 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 return rv;
4196}
4197
4198static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4199{
4200 mdk_rdev_t *rdev;
4201
4202 if (mddev->pers == NULL)
4203 return -ENODEV;
4204
4205 rdev = find_rdev(mddev, dev);
4206 if (!rdev)
4207 return -ENODEV;
4208
4209 md_error(mddev, rdev);
4210 return 0;
4211}
4212
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004213static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4214{
4215 mddev_t *mddev = bdev->bd_disk->private_data;
4216
4217 geo->heads = 2;
4218 geo->sectors = 4;
4219 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4220 return 0;
4221}
4222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223static int md_ioctl(struct inode *inode, struct file *file,
4224 unsigned int cmd, unsigned long arg)
4225{
4226 int err = 0;
4227 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 mddev_t *mddev = NULL;
4229
4230 if (!capable(CAP_SYS_ADMIN))
4231 return -EACCES;
4232
4233 /*
4234 * Commands dealing with the RAID driver but not any
4235 * particular array:
4236 */
4237 switch (cmd)
4238 {
4239 case RAID_VERSION:
4240 err = get_version(argp);
4241 goto done;
4242
4243 case PRINT_RAID_DEBUG:
4244 err = 0;
4245 md_print_devices();
4246 goto done;
4247
4248#ifndef MODULE
4249 case RAID_AUTORUN:
4250 err = 0;
4251 autostart_arrays(arg);
4252 goto done;
4253#endif
4254 default:;
4255 }
4256
4257 /*
4258 * Commands creating/starting a new array:
4259 */
4260
4261 mddev = inode->i_bdev->bd_disk->private_data;
4262
4263 if (!mddev) {
4264 BUG();
4265 goto abort;
4266 }
4267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 err = mddev_lock(mddev);
4269 if (err) {
4270 printk(KERN_INFO
4271 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4272 err, cmd);
4273 goto abort;
4274 }
4275
4276 switch (cmd)
4277 {
4278 case SET_ARRAY_INFO:
4279 {
4280 mdu_array_info_t info;
4281 if (!arg)
4282 memset(&info, 0, sizeof(info));
4283 else if (copy_from_user(&info, argp, sizeof(info))) {
4284 err = -EFAULT;
4285 goto abort_unlock;
4286 }
4287 if (mddev->pers) {
4288 err = update_array_info(mddev, &info);
4289 if (err) {
4290 printk(KERN_WARNING "md: couldn't update"
4291 " array info. %d\n", err);
4292 goto abort_unlock;
4293 }
4294 goto done_unlock;
4295 }
4296 if (!list_empty(&mddev->disks)) {
4297 printk(KERN_WARNING
4298 "md: array %s already has disks!\n",
4299 mdname(mddev));
4300 err = -EBUSY;
4301 goto abort_unlock;
4302 }
4303 if (mddev->raid_disks) {
4304 printk(KERN_WARNING
4305 "md: array %s already initialised!\n",
4306 mdname(mddev));
4307 err = -EBUSY;
4308 goto abort_unlock;
4309 }
4310 err = set_array_info(mddev, &info);
4311 if (err) {
4312 printk(KERN_WARNING "md: couldn't set"
4313 " array info. %d\n", err);
4314 goto abort_unlock;
4315 }
4316 }
4317 goto done_unlock;
4318
4319 default:;
4320 }
4321
4322 /*
4323 * Commands querying/configuring an existing array:
4324 */
NeilBrown32a76272005-06-21 17:17:14 -07004325 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004326 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrown32a76272005-06-21 17:17:14 -07004327 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
NeilBrown3f9d7b02006-12-22 01:11:41 -08004328 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4329 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 err = -ENODEV;
4331 goto abort_unlock;
4332 }
4333
4334 /*
4335 * Commands even a read-only array can execute:
4336 */
4337 switch (cmd)
4338 {
4339 case GET_ARRAY_INFO:
4340 err = get_array_info(mddev, argp);
4341 goto done_unlock;
4342
NeilBrown32a76272005-06-21 17:17:14 -07004343 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004344 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004345 goto done_unlock;
4346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 case GET_DISK_INFO:
4348 err = get_disk_info(mddev, argp);
4349 goto done_unlock;
4350
4351 case RESTART_ARRAY_RW:
4352 err = restart_array(mddev);
4353 goto done_unlock;
4354
4355 case STOP_ARRAY:
4356 err = do_md_stop (mddev, 0);
4357 goto done_unlock;
4358
4359 case STOP_ARRAY_RO:
4360 err = do_md_stop (mddev, 1);
4361 goto done_unlock;
4362
4363 /*
4364 * We have a problem here : there is no easy way to give a CHS
4365 * virtual geometry. We currently pretend that we have a 2 heads
4366 * 4 sectors (with a BIG number of cylinders...). This drives
4367 * dosfs just mad... ;-)
4368 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 }
4370
4371 /*
4372 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004373 * superblock, so we do not allow them on read-only arrays.
4374 * However non-MD ioctls (e.g. get-size) will still come through
4375 * here and hit the 'default' below, so only disallow
4376 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 */
NeilBrownf91de922005-11-08 21:39:36 -08004378 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4379 mddev->ro && mddev->pers) {
4380 if (mddev->ro == 2) {
4381 mddev->ro = 0;
4382 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4383 md_wakeup_thread(mddev->thread);
4384
4385 } else {
4386 err = -EROFS;
4387 goto abort_unlock;
4388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 }
4390
4391 switch (cmd)
4392 {
4393 case ADD_NEW_DISK:
4394 {
4395 mdu_disk_info_t info;
4396 if (copy_from_user(&info, argp, sizeof(info)))
4397 err = -EFAULT;
4398 else
4399 err = add_new_disk(mddev, &info);
4400 goto done_unlock;
4401 }
4402
4403 case HOT_REMOVE_DISK:
4404 err = hot_remove_disk(mddev, new_decode_dev(arg));
4405 goto done_unlock;
4406
4407 case HOT_ADD_DISK:
4408 err = hot_add_disk(mddev, new_decode_dev(arg));
4409 goto done_unlock;
4410
4411 case SET_DISK_FAULTY:
4412 err = set_disk_faulty(mddev, new_decode_dev(arg));
4413 goto done_unlock;
4414
4415 case RUN_ARRAY:
4416 err = do_md_run (mddev);
4417 goto done_unlock;
4418
NeilBrown32a76272005-06-21 17:17:14 -07004419 case SET_BITMAP_FILE:
4420 err = set_bitmap_file(mddev, (int)arg);
4421 goto done_unlock;
4422
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 err = -EINVAL;
4425 goto abort_unlock;
4426 }
4427
4428done_unlock:
4429abort_unlock:
4430 mddev_unlock(mddev);
4431
4432 return err;
4433done:
4434 if (err)
4435 MD_BUG();
4436abort:
4437 return err;
4438}
4439
4440static int md_open(struct inode *inode, struct file *file)
4441{
4442 /*
4443 * Succeed if we can lock the mddev, which confirms that
4444 * it isn't being stopped right now.
4445 */
4446 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4447 int err;
4448
NeilBrownd63a5a72006-12-08 02:36:17 -08004449 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 goto out;
4451
4452 err = 0;
4453 mddev_get(mddev);
4454 mddev_unlock(mddev);
4455
4456 check_disk_change(inode->i_bdev);
4457 out:
4458 return err;
4459}
4460
4461static int md_release(struct inode *inode, struct file * file)
4462{
4463 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4464
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004465 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 mddev_put(mddev);
4467
4468 return 0;
4469}
4470
4471static int md_media_changed(struct gendisk *disk)
4472{
4473 mddev_t *mddev = disk->private_data;
4474
4475 return mddev->changed;
4476}
4477
4478static int md_revalidate(struct gendisk *disk)
4479{
4480 mddev_t *mddev = disk->private_data;
4481
4482 mddev->changed = 0;
4483 return 0;
4484}
4485static struct block_device_operations md_fops =
4486{
4487 .owner = THIS_MODULE,
4488 .open = md_open,
4489 .release = md_release,
4490 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004491 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 .media_changed = md_media_changed,
4493 .revalidate_disk= md_revalidate,
4494};
4495
Adrian Bunk75c96f82005-05-05 16:16:09 -07004496static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497{
4498 mdk_thread_t *thread = arg;
4499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 /*
4501 * md_thread is a 'system-thread', it's priority should be very
4502 * high. We avoid resource deadlocks individually in each
4503 * raid personality. (RAID5 does preallocation) We also use RR and
4504 * the very same RT priority as kswapd, thus we will never get
4505 * into a priority inversion deadlock.
4506 *
4507 * we definitely have to have equal or higher priority than
4508 * bdflush, otherwise bdflush will deadlock if there are too
4509 * many dirty RAID5 blocks.
4510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
Rafael J. Wysocki4b438a22006-11-08 17:44:48 -08004512 current->flags |= PF_NOFREEZE;
NeilBrown6985c432005-10-19 21:23:47 -07004513 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004514 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
NeilBrown93588e22005-11-15 00:09:12 -08004516 /* We need to wait INTERRUPTIBLE so that
4517 * we don't add to the load-average.
4518 * That means we need to be sure no signals are
4519 * pending
4520 */
4521 if (signal_pending(current))
4522 flush_signals(current);
4523
4524 wait_event_interruptible_timeout
4525 (thread->wqueue,
4526 test_bit(THREAD_WAKEUP, &thread->flags)
4527 || kthread_should_stop(),
4528 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
4530 clear_bit(THREAD_WAKEUP, &thread->flags);
4531
NeilBrown787453c2005-11-08 21:39:43 -08004532 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004534
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 return 0;
4536}
4537
4538void md_wakeup_thread(mdk_thread_t *thread)
4539{
4540 if (thread) {
4541 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4542 set_bit(THREAD_WAKEUP, &thread->flags);
4543 wake_up(&thread->wqueue);
4544 }
4545}
4546
4547mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4548 const char *name)
4549{
4550 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
NeilBrown9ffae0c2006-01-06 00:20:32 -08004552 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 if (!thread)
4554 return NULL;
4555
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 init_waitqueue_head(&thread->wqueue);
4557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 thread->run = run;
4559 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004560 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004561 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004562 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 kfree(thread);
4564 return NULL;
4565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 return thread;
4567}
4568
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569void md_unregister_thread(mdk_thread_t *thread)
4570{
NeilBrownd28446f2005-04-16 15:26:41 -07004571 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07004572
4573 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 kfree(thread);
4575}
4576
4577void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4578{
4579 if (!mddev) {
4580 MD_BUG();
4581 return;
4582 }
4583
NeilBrownb2d444d2005-11-08 21:39:31 -08004584 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 return;
NeilBrown32a76272005-06-21 17:17:14 -07004586/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4588 mdname(mddev),
4589 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4590 __builtin_return_address(0),__builtin_return_address(1),
4591 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004592*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004593 if (!mddev->pers)
4594 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 if (!mddev->pers->error_handler)
4596 return;
4597 mddev->pers->error_handler(mddev,rdev);
4598 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4599 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4600 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004601 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602}
4603
4604/* seq_file implementation /proc/mdstat */
4605
4606static void status_unused(struct seq_file *seq)
4607{
4608 int i = 0;
4609 mdk_rdev_t *rdev;
4610 struct list_head *tmp;
4611
4612 seq_printf(seq, "unused devices: ");
4613
4614 ITERATE_RDEV_PENDING(rdev,tmp) {
4615 char b[BDEVNAME_SIZE];
4616 i++;
4617 seq_printf(seq, "%s ",
4618 bdevname(rdev->bdev,b));
4619 }
4620 if (!i)
4621 seq_printf(seq, "<none>");
4622
4623 seq_printf(seq, "\n");
4624}
4625
4626
4627static void status_resync(struct seq_file *seq, mddev_t * mddev)
4628{
NeilBrown4588b422006-03-27 01:18:04 -08004629 sector_t max_blocks, resync, res;
4630 unsigned long dt, db, rt;
4631 int scale;
4632 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
4634 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4635
4636 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4637 max_blocks = mddev->resync_max_sectors >> 1;
4638 else
4639 max_blocks = mddev->size;
4640
4641 /*
4642 * Should not happen.
4643 */
4644 if (!max_blocks) {
4645 MD_BUG();
4646 return;
4647 }
NeilBrown4588b422006-03-27 01:18:04 -08004648 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4649 * in a sector_t, and (max_blocks>>scale) will fit in a
4650 * u32, as those are the requirements for sector_div.
4651 * Thus 'scale' must be at least 10
4652 */
4653 scale = 10;
4654 if (sizeof(sector_t) > sizeof(unsigned long)) {
4655 while ( max_blocks/2 > (1ULL<<(scale+32)))
4656 scale++;
4657 }
4658 res = (resync>>scale)*1000;
4659 sector_div(res, (u32)((max_blocks>>scale)+1));
4660
4661 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 {
NeilBrown4588b422006-03-27 01:18:04 -08004663 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 seq_printf(seq, "[");
4665 for (i = 0; i < x; i++)
4666 seq_printf(seq, "=");
4667 seq_printf(seq, ">");
4668 for (i = 0; i < y; i++)
4669 seq_printf(seq, ".");
4670 seq_printf(seq, "] ");
4671 }
NeilBrown4588b422006-03-27 01:18:04 -08004672 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004673 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4674 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07004675 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
4676 "check" :
4677 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4678 "resync" : "recovery"))),
4679 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08004680 (unsigned long long) resync,
4681 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
4683 /*
4684 * We do not want to overflow, so the order of operands and
4685 * the * 100 / 100 trick are important. We do a +1 to be
4686 * safe against division by zero. We only estimate anyway.
4687 *
4688 * dt: time from mark until now
4689 * db: blocks written from mark until now
4690 * rt: remaining time
4691 */
4692 dt = ((jiffies - mddev->resync_mark) / HZ);
4693 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07004694 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
4695 - mddev->resync_mark_cnt;
4696 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
4698 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4699
NeilBrownff4e8d92006-07-10 04:44:16 -07004700 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701}
4702
4703static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4704{
4705 struct list_head *tmp;
4706 loff_t l = *pos;
4707 mddev_t *mddev;
4708
4709 if (l >= 0x10000)
4710 return NULL;
4711 if (!l--)
4712 /* header */
4713 return (void*)1;
4714
4715 spin_lock(&all_mddevs_lock);
4716 list_for_each(tmp,&all_mddevs)
4717 if (!l--) {
4718 mddev = list_entry(tmp, mddev_t, all_mddevs);
4719 mddev_get(mddev);
4720 spin_unlock(&all_mddevs_lock);
4721 return mddev;
4722 }
4723 spin_unlock(&all_mddevs_lock);
4724 if (!l--)
4725 return (void*)2;/* tail */
4726 return NULL;
4727}
4728
4729static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4730{
4731 struct list_head *tmp;
4732 mddev_t *next_mddev, *mddev = v;
4733
4734 ++*pos;
4735 if (v == (void*)2)
4736 return NULL;
4737
4738 spin_lock(&all_mddevs_lock);
4739 if (v == (void*)1)
4740 tmp = all_mddevs.next;
4741 else
4742 tmp = mddev->all_mddevs.next;
4743 if (tmp != &all_mddevs)
4744 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4745 else {
4746 next_mddev = (void*)2;
4747 *pos = 0x10000;
4748 }
4749 spin_unlock(&all_mddevs_lock);
4750
4751 if (v != (void*)1)
4752 mddev_put(mddev);
4753 return next_mddev;
4754
4755}
4756
4757static void md_seq_stop(struct seq_file *seq, void *v)
4758{
4759 mddev_t *mddev = v;
4760
4761 if (mddev && v != (void*)1 && v != (void*)2)
4762 mddev_put(mddev);
4763}
4764
NeilBrownd7603b72006-01-06 00:20:30 -08004765struct mdstat_info {
4766 int event;
4767};
4768
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769static int md_seq_show(struct seq_file *seq, void *v)
4770{
4771 mddev_t *mddev = v;
4772 sector_t size;
4773 struct list_head *tmp2;
4774 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004775 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004776 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
4778 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004779 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 seq_printf(seq, "Personalities : ");
4781 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004782 list_for_each_entry(pers, &pers_list, list)
4783 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
4785 spin_unlock(&pers_lock);
4786 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004787 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 return 0;
4789 }
4790 if (v == (void*)2) {
4791 status_unused(seq);
4792 return 0;
4793 }
4794
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004795 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004797
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4799 seq_printf(seq, "%s : %sactive", mdname(mddev),
4800 mddev->pers ? "" : "in");
4801 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004802 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004804 if (mddev->ro==2)
4805 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 seq_printf(seq, " %s", mddev->pers->name);
4807 }
4808
4809 size = 0;
4810 ITERATE_RDEV(mddev,rdev,tmp2) {
4811 char b[BDEVNAME_SIZE];
4812 seq_printf(seq, " %s[%d]",
4813 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004814 if (test_bit(WriteMostly, &rdev->flags))
4815 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004816 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 seq_printf(seq, "(F)");
4818 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004819 } else if (rdev->raid_disk < 0)
4820 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 size += rdev->size;
4822 }
4823
4824 if (!list_empty(&mddev->disks)) {
4825 if (mddev->pers)
4826 seq_printf(seq, "\n %llu blocks",
4827 (unsigned long long)mddev->array_size);
4828 else
4829 seq_printf(seq, "\n %llu blocks",
4830 (unsigned long long)size);
4831 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004832 if (mddev->persistent) {
4833 if (mddev->major_version != 0 ||
4834 mddev->minor_version != 90) {
4835 seq_printf(seq," super %d.%d",
4836 mddev->major_version,
4837 mddev->minor_version);
4838 }
4839 } else
4840 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
4842 if (mddev->pers) {
4843 mddev->pers->status (seq, mddev);
4844 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004845 if (mddev->pers->sync_request) {
4846 if (mddev->curr_resync > 2) {
4847 status_resync (seq, mddev);
4848 seq_printf(seq, "\n ");
4849 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4850 seq_printf(seq, "\tresync=DELAYED\n ");
4851 else if (mddev->recovery_cp < MaxSector)
4852 seq_printf(seq, "\tresync=PENDING\n ");
4853 }
NeilBrown32a76272005-06-21 17:17:14 -07004854 } else
4855 seq_printf(seq, "\n ");
4856
4857 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07004858 unsigned long chunk_kb;
4859 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07004860 spin_lock_irqsave(&bitmap->lock, flags);
4861 chunk_kb = bitmap->chunksize >> 10;
4862 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4863 "%lu%s chunk",
4864 bitmap->pages - bitmap->missing_pages,
4865 bitmap->pages,
4866 (bitmap->pages - bitmap->missing_pages)
4867 << (PAGE_SHIFT - 10),
4868 chunk_kb ? chunk_kb : bitmap->chunksize,
4869 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07004870 if (bitmap->file) {
4871 seq_printf(seq, ", file: ");
Josef Sipekc649bb92006-12-08 02:37:19 -08004872 seq_path(seq, bitmap->file->f_path.mnt,
4873 bitmap->file->f_path.dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07004874 }
NeilBrown78d742d2005-06-21 17:17:15 -07004875
NeilBrown32a76272005-06-21 17:17:14 -07004876 seq_printf(seq, "\n");
4877 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 }
4879
4880 seq_printf(seq, "\n");
4881 }
4882 mddev_unlock(mddev);
4883
4884 return 0;
4885}
4886
4887static struct seq_operations md_seq_ops = {
4888 .start = md_seq_start,
4889 .next = md_seq_next,
4890 .stop = md_seq_stop,
4891 .show = md_seq_show,
4892};
4893
4894static int md_seq_open(struct inode *inode, struct file *file)
4895{
4896 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08004897 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4898 if (mi == NULL)
4899 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
4901 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08004902 if (error)
4903 kfree(mi);
4904 else {
4905 struct seq_file *p = file->private_data;
4906 p->private = mi;
4907 mi->event = atomic_read(&md_event_count);
4908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 return error;
4910}
4911
NeilBrownd7603b72006-01-06 00:20:30 -08004912static int md_seq_release(struct inode *inode, struct file *file)
4913{
4914 struct seq_file *m = file->private_data;
4915 struct mdstat_info *mi = m->private;
4916 m->private = NULL;
4917 kfree(mi);
4918 return seq_release(inode, file);
4919}
4920
4921static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4922{
4923 struct seq_file *m = filp->private_data;
4924 struct mdstat_info *mi = m->private;
4925 int mask;
4926
4927 poll_wait(filp, &md_event_waiters, wait);
4928
4929 /* always allow read */
4930 mask = POLLIN | POLLRDNORM;
4931
4932 if (mi->event != atomic_read(&md_event_count))
4933 mask |= POLLERR | POLLPRI;
4934 return mask;
4935}
4936
Arjan van de Venfa027c22007-02-12 00:55:33 -08004937static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07004938 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 .open = md_seq_open,
4940 .read = seq_read,
4941 .llseek = seq_lseek,
NeilBrownd7603b72006-01-06 00:20:30 -08004942 .release = md_seq_release,
4943 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944};
4945
NeilBrown2604b702006-01-06 00:20:36 -08004946int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004949 list_add_tail(&p->list, &pers_list);
4950 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 spin_unlock(&pers_lock);
4952 return 0;
4953}
4954
NeilBrown2604b702006-01-06 00:20:36 -08004955int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956{
NeilBrown2604b702006-01-06 00:20:36 -08004957 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004959 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 spin_unlock(&pers_lock);
4961 return 0;
4962}
4963
4964static int is_mddev_idle(mddev_t *mddev)
4965{
4966 mdk_rdev_t * rdev;
4967 struct list_head *tmp;
4968 int idle;
4969 unsigned long curr_events;
4970
4971 idle = 1;
4972 ITERATE_RDEV(mddev,rdev,tmp) {
4973 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01004974 curr_events = disk_stat_read(disk, sectors[0]) +
4975 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08004977 /* The difference between curr_events and last_events
4978 * will be affected by any new non-sync IO (making
4979 * curr_events bigger) and any difference in the amount of
4980 * in-flight syncio (making current_events bigger or smaller)
4981 * The amount in-flight is currently limited to
4982 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4983 * which is at most 4096 sectors.
4984 * These numbers are fairly fragile and should be made
4985 * more robust, probably by enforcing the
4986 * 'window size' that md_do_sync sort-of uses.
4987 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 * Note: the following is an unsigned comparison.
4989 */
NeilBrownc0e48522005-11-18 01:11:01 -08004990 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 rdev->last_events = curr_events;
4992 idle = 0;
4993 }
4994 }
4995 return idle;
4996}
4997
4998void md_done_sync(mddev_t *mddev, int blocks, int ok)
4999{
5000 /* another "blocks" (512byte) blocks have been synced */
5001 atomic_sub(blocks, &mddev->recovery_active);
5002 wake_up(&mddev->recovery_wait);
5003 if (!ok) {
5004 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5005 md_wakeup_thread(mddev->thread);
5006 // stop recovery, signal do_sync ....
5007 }
5008}
5009
5010
NeilBrown06d91a52005-06-21 17:17:12 -07005011/* md_write_start(mddev, bi)
5012 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005013 * in superblock) before writing, schedule a superblock update
5014 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005015 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005016void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017{
NeilBrown06d91a52005-06-21 17:17:12 -07005018 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005019 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005020
NeilBrownf91de922005-11-08 21:39:36 -08005021 BUG_ON(mddev->ro == 1);
5022 if (mddev->ro == 2) {
5023 /* need to switch to read/write */
5024 mddev->ro = 0;
5025 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5026 md_wakeup_thread(mddev->thread);
5027 }
NeilBrown06d91a52005-06-21 17:17:12 -07005028 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005029 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005030 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005031 if (mddev->in_sync) {
5032 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005033 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005034 md_wakeup_thread(mddev->thread);
5035 }
NeilBrowna9701a32005-11-08 21:39:34 -08005036 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005037 }
NeilBrown850b2b42006-10-03 01:15:46 -07005038 wait_event(mddev->sb_wait, mddev->flags==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039}
5040
5041void md_write_end(mddev_t *mddev)
5042{
5043 if (atomic_dec_and_test(&mddev->writes_pending)) {
5044 if (mddev->safemode == 2)
5045 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005046 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5048 }
5049}
5050
NeilBrown2a2275d2007-01-26 00:57:11 -08005051/* md_allow_write(mddev)
5052 * Calling this ensures that the array is marked 'active' so that writes
5053 * may proceed without blocking. It is important to call this before
5054 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5055 * Must be called with mddev_lock held.
5056 */
5057void md_allow_write(mddev_t *mddev)
5058{
5059 if (!mddev->pers)
5060 return;
5061 if (mddev->ro)
5062 return;
5063
5064 spin_lock_irq(&mddev->write_lock);
5065 if (mddev->in_sync) {
5066 mddev->in_sync = 0;
5067 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5068 if (mddev->safemode_delay &&
5069 mddev->safemode == 0)
5070 mddev->safemode = 1;
5071 spin_unlock_irq(&mddev->write_lock);
5072 md_update_sb(mddev, 0);
5073 } else
5074 spin_unlock_irq(&mddev->write_lock);
5075}
5076EXPORT_SYMBOL_GPL(md_allow_write);
5077
Adrian Bunk75c96f82005-05-05 16:16:09 -07005078static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
5080#define SYNC_MARKS 10
5081#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005082void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083{
5084 mddev_t *mddev2;
5085 unsigned int currspeed = 0,
5086 window;
NeilBrown57afd892005-06-21 17:17:13 -07005087 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 unsigned long mark[SYNC_MARKS];
5089 sector_t mark_cnt[SYNC_MARKS];
5090 int last_mark,m;
5091 struct list_head *tmp;
5092 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005093 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005094 struct list_head *rtmp;
5095 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005096 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
5098 /* just incase thread restarts... */
5099 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5100 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005101 if (mddev->ro) /* never try to sync a read-only array */
5102 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
NeilBrown61df9d92006-10-03 01:15:57 -07005104 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5105 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5106 desc = "data-check";
5107 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5108 desc = "requested-resync";
5109 else
5110 desc = "resync";
5111 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5112 desc = "reshape";
5113 else
5114 desc = "recovery";
5115
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 /* we overload curr_resync somewhat here.
5117 * 0 == not engaged in resync at all
5118 * 2 == checking that there is no conflict with another sync
5119 * 1 == like 2, but have yielded to allow conflicting resync to
5120 * commense
5121 * other == active in resync - this many blocks
5122 *
5123 * Before starting a resync we must have set curr_resync to
5124 * 2, and then checked that every "conflicting" array has curr_resync
5125 * less than ours. When we find one that is the same or higher
5126 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5127 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5128 * This will mean we have to start checking from the beginning again.
5129 *
5130 */
5131
5132 do {
5133 mddev->curr_resync = 2;
5134
5135 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005136 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005137 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 goto skip;
5139 }
5140 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 if (mddev2 == mddev)
5142 continue;
5143 if (mddev2->curr_resync &&
5144 match_mddev_units(mddev,mddev2)) {
5145 DEFINE_WAIT(wq);
5146 if (mddev < mddev2 && mddev->curr_resync == 2) {
5147 /* arbitrarily yield */
5148 mddev->curr_resync = 1;
5149 wake_up(&resync_wait);
5150 }
5151 if (mddev > mddev2 && mddev->curr_resync == 1)
5152 /* no need to wait here, we can wait the next
5153 * time 'round when curr_resync == 2
5154 */
5155 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005156 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5157 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005158 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005159 printk(KERN_INFO "md: delaying %s of %s"
5160 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005162 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 mddev_put(mddev2);
5164 schedule();
5165 finish_wait(&resync_wait, &wq);
5166 goto try_again;
5167 }
5168 finish_wait(&resync_wait, &wq);
5169 }
5170 }
5171 } while (mddev->curr_resync < 2);
5172
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005173 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005174 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005176 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 */
5178 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005179 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005180 /* we don't use the checkpoint if there's a bitmap */
5181 if (!mddev->bitmap &&
5182 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5183 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005184 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5185 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005186 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 /* recovery follows the physical size of devices */
5188 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005189 j = MaxSector;
5190 ITERATE_RDEV(mddev,rdev,rtmp)
5191 if (rdev->raid_disk >= 0 &&
5192 !test_bit(Faulty, &rdev->flags) &&
5193 !test_bit(In_sync, &rdev->flags) &&
5194 rdev->recovery_offset < j)
5195 j = rdev->recovery_offset;
5196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
NeilBrown61df9d92006-10-03 01:15:57 -07005198 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5199 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5200 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005201 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005202 "(but not more than %d KB/sec) for %s.\n",
5203 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204
5205 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005206
NeilBrown57afd892005-06-21 17:17:13 -07005207 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 for (m = 0; m < SYNC_MARKS; m++) {
5209 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005210 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 }
5212 last_mark = 0;
5213 mddev->resync_mark = mark[last_mark];
5214 mddev->resync_mark_cnt = mark_cnt[last_mark];
5215
5216 /*
5217 * Tune reconstruction:
5218 */
5219 window = 32*(PAGE_SIZE/512);
5220 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5221 window/2,(unsigned long long) max_sectors/2);
5222
5223 atomic_set(&mddev->recovery_active, 0);
5224 init_waitqueue_head(&mddev->recovery_wait);
5225 last_check = 0;
5226
5227 if (j>2) {
5228 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005229 "md: resuming %s of %s from checkpoint.\n",
5230 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 mddev->curr_resync = j;
5232 }
5233
5234 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005235 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
NeilBrown57afd892005-06-21 17:17:13 -07005237 skipped = 0;
5238 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08005239 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005240 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5242 goto out;
5243 }
NeilBrown57afd892005-06-21 17:17:13 -07005244
5245 if (!skipped) { /* actual IO requested */
5246 io_sectors += sectors;
5247 atomic_add(sectors, &mddev->recovery_active);
5248 }
5249
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 j += sectors;
5251 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005252 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005253 if (last_check == 0)
5254 /* this is the earliers that rebuilt will be
5255 * visible in /proc/mdstat
5256 */
5257 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005258
5259 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 continue;
5261
NeilBrown57afd892005-06-21 17:17:13 -07005262 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
5264 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5265 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5266 break;
5267
5268 repeat:
5269 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5270 /* step marks */
5271 int next = (last_mark+1) % SYNC_MARKS;
5272
5273 mddev->resync_mark = mark[next];
5274 mddev->resync_mark_cnt = mark_cnt[next];
5275 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005276 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 last_mark = next;
5278 }
5279
5280
NeilBrown787453c2005-11-08 21:39:43 -08005281 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 /*
5283 * got a signal, exit.
5284 */
5285 printk(KERN_INFO
5286 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5288 goto out;
5289 }
5290
5291 /*
5292 * this loop exits only if either when we are slower than
5293 * the 'hard' speed limit, or the system was IO-idle for
5294 * a jiffy.
5295 * the system might be non-idle CPU-wise, but we only care
5296 * about not overloading the IO subsystem. (things like an
5297 * e2fsck being done on the RAID array should execute fast)
5298 */
5299 mddev->queue->unplug_fn(mddev->queue);
5300 cond_resched();
5301
NeilBrown57afd892005-06-21 17:17:13 -07005302 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5303 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
NeilBrown88202a02006-01-06 00:21:36 -08005305 if (currspeed > speed_min(mddev)) {
5306 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005308 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 goto repeat;
5310 }
5311 }
5312 }
NeilBrown61df9d92006-10-03 01:15:57 -07005313 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 /*
5315 * this also signals 'finished resyncing' to md_stop
5316 */
5317 out:
5318 mddev->queue->unplug_fn(mddev->queue);
5319
5320 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5321
5322 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005323 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
5325 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005326 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005327 mddev->curr_resync > 2) {
5328 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5329 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5330 if (mddev->curr_resync >= mddev->recovery_cp) {
5331 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005332 "md: checkpointing %s of %s.\n",
5333 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005334 mddev->recovery_cp = mddev->curr_resync;
5335 }
5336 } else
5337 mddev->recovery_cp = MaxSector;
5338 } else {
5339 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5340 mddev->curr_resync = MaxSector;
5341 ITERATE_RDEV(mddev,rdev,rtmp)
5342 if (rdev->raid_disk >= 0 &&
5343 !test_bit(Faulty, &rdev->flags) &&
5344 !test_bit(In_sync, &rdev->flags) &&
5345 rdev->recovery_offset < mddev->curr_resync)
5346 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 }
NeilBrown17571282006-12-10 02:20:52 -08005349 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 skip:
5352 mddev->curr_resync = 0;
5353 wake_up(&resync_wait);
5354 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5355 md_wakeup_thread(mddev->thread);
5356}
NeilBrown29269552006-03-27 01:18:10 -08005357EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
5359
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005360static int remove_and_add_spares(mddev_t *mddev)
5361{
5362 mdk_rdev_t *rdev;
5363 struct list_head *rtmp;
5364 int spares = 0;
5365
5366 ITERATE_RDEV(mddev,rdev,rtmp)
5367 if (rdev->raid_disk >= 0 &&
5368 (test_bit(Faulty, &rdev->flags) ||
5369 ! test_bit(In_sync, &rdev->flags)) &&
5370 atomic_read(&rdev->nr_pending)==0) {
5371 if (mddev->pers->hot_remove_disk(
5372 mddev, rdev->raid_disk)==0) {
5373 char nm[20];
5374 sprintf(nm,"rd%d", rdev->raid_disk);
5375 sysfs_remove_link(&mddev->kobj, nm);
5376 rdev->raid_disk = -1;
5377 }
5378 }
5379
5380 if (mddev->degraded) {
5381 ITERATE_RDEV(mddev,rdev,rtmp)
5382 if (rdev->raid_disk < 0
5383 && !test_bit(Faulty, &rdev->flags)) {
5384 rdev->recovery_offset = 0;
5385 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5386 char nm[20];
5387 sprintf(nm, "rd%d", rdev->raid_disk);
5388 sysfs_create_link(&mddev->kobj,
5389 &rdev->kobj, nm);
5390 spares++;
5391 md_new_event(mddev);
5392 } else
5393 break;
5394 }
5395 }
5396 return spares;
5397}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398/*
5399 * This routine is regularly called by all per-raid-array threads to
5400 * deal with generic issues like resync and super-block update.
5401 * Raid personalities that don't have a thread (linear/raid0) do not
5402 * need this as they never do any recovery or update the superblock.
5403 *
5404 * It does not do any resync itself, but rather "forks" off other threads
5405 * to do that as needed.
5406 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5407 * "->recovery" and create a thread at ->sync_thread.
5408 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5409 * and wakeups up this thread which will reap the thread and finish up.
5410 * This thread also removes any faulty devices (with nr_pending == 0).
5411 *
5412 * The overall approach is:
5413 * 1/ if the superblock needs updating, update it.
5414 * 2/ If a recovery thread is running, don't do anything else.
5415 * 3/ If recovery has finished, clean up, possibly marking spares active.
5416 * 4/ If there are any faulty devices, remove them.
5417 * 5/ If array is degraded, try to add spares devices
5418 * 6/ If array has spares or is not in-sync, start a resync thread.
5419 */
5420void md_check_recovery(mddev_t *mddev)
5421{
5422 mdk_rdev_t *rdev;
5423 struct list_head *rtmp;
5424
5425
NeilBrown5f404022005-06-21 17:17:16 -07005426 if (mddev->bitmap)
5427 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428
5429 if (mddev->ro)
5430 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005431
5432 if (signal_pending(current)) {
5433 if (mddev->pers->sync_request) {
5434 printk(KERN_INFO "md: %s in immediate safe mode\n",
5435 mdname(mddev));
5436 mddev->safemode = 2;
5437 }
5438 flush_signals(current);
5439 }
5440
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 if ( ! (
NeilBrown850b2b42006-10-03 01:15:46 -07005442 mddev->flags ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005444 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5445 (mddev->safemode == 1) ||
5446 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5447 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 ))
5449 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005450
NeilBrowndf5b89b2006-03-27 01:18:20 -08005451 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005452 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07005453
NeilBrowna9701a32005-11-08 21:39:34 -08005454 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005455 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5456 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5457 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07005458 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownfca4d842005-06-21 17:17:11 -07005459 }
5460 if (mddev->safemode == 1)
5461 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005462 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005463
NeilBrown850b2b42006-10-03 01:15:46 -07005464 if (mddev->flags)
5465 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005466
NeilBrown06d91a52005-06-21 17:17:12 -07005467
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5469 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5470 /* resync/recovery still happening */
5471 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5472 goto unlock;
5473 }
5474 if (mddev->sync_thread) {
5475 /* resync has finished, collect result */
5476 md_unregister_thread(mddev->sync_thread);
5477 mddev->sync_thread = NULL;
5478 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5479 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5480 /* success...*/
5481 /* activate any spares */
5482 mddev->pers->spare_active(mddev);
5483 }
NeilBrown850b2b42006-10-03 01:15:46 -07005484 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005485
5486 /* if array is no-longer degraded, then any saved_raid_disk
5487 * information must be scrapped
5488 */
5489 if (!mddev->degraded)
5490 ITERATE_RDEV(mddev,rdev,rtmp)
5491 rdev->saved_raid_disk = -1;
5492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 mddev->recovery = 0;
5494 /* flag recovery needed just to double check */
5495 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005496 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 goto unlock;
5498 }
NeilBrown24dd4692005-11-08 21:39:26 -08005499 /* Clear some bits that don't mean anything, but
5500 * might be left set
5501 */
5502 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5503 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5504 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5505 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005507 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5508 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 /* no recovery is running.
5510 * remove any failed drives, then
5511 * add spares if possible.
5512 * Spare are also removed and re-added, to allow
5513 * the personality to fail the re-add.
5514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005516 if (mddev->reshape_position != MaxSector) {
5517 if (mddev->pers->check_reshape(mddev) != 0)
5518 /* Cannot proceed */
5519 goto unlock;
5520 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5521 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08005522 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5523 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5524 } else if (mddev->recovery_cp < MaxSector) {
5525 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5526 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5527 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005529
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 if (mddev->pers->sync_request) {
5531 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005532 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5533 /* We are adding a device or devices to an array
5534 * which has the bitmap stored on all devices.
5535 * So make sure all bitmap pages get written
5536 */
5537 bitmap_write_all(mddev->bitmap);
5538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 mddev->sync_thread = md_register_thread(md_do_sync,
5540 mddev,
5541 "%s_resync");
5542 if (!mddev->sync_thread) {
5543 printk(KERN_ERR "%s: could not start resync"
5544 " thread...\n",
5545 mdname(mddev));
5546 /* leave the spares where they are, it shouldn't hurt */
5547 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005548 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005550 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 }
5552 unlock:
5553 mddev_unlock(mddev);
5554 }
5555}
5556
Adrian Bunk75c96f82005-05-05 16:16:09 -07005557static int md_notify_reboot(struct notifier_block *this,
5558 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559{
5560 struct list_head *tmp;
5561 mddev_t *mddev;
5562
5563 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5564
5565 printk(KERN_INFO "md: stopping all md devices.\n");
5566
5567 ITERATE_MDDEV(mddev,tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005568 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005570 mddev_unlock(mddev);
5571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 /*
5573 * certain more exotic SCSI devices are known to be
5574 * volatile wrt too early system reboots. While the
5575 * right place to handle this issue is the given
5576 * driver, we do want to have a safe RAID driver ...
5577 */
5578 mdelay(1000*1);
5579 }
5580 return NOTIFY_DONE;
5581}
5582
Adrian Bunk75c96f82005-05-05 16:16:09 -07005583static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 .notifier_call = md_notify_reboot,
5585 .next = NULL,
5586 .priority = INT_MAX, /* before any real devices */
5587};
5588
5589static void md_geninit(void)
5590{
5591 struct proc_dir_entry *p;
5592
5593 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5594
5595 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5596 if (p)
5597 p->proc_fops = &md_seq_fops;
5598}
5599
Adrian Bunk75c96f82005-05-05 16:16:09 -07005600static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 if (register_blkdev(MAJOR_NR, "md"))
5603 return -1;
5604 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5605 unregister_blkdev(MAJOR_NR, "md");
5606 return -1;
5607 }
NeilBrowne8703fe2006-10-03 01:15:59 -07005608 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
5609 md_probe, NULL, NULL);
5610 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 md_probe, NULL, NULL);
5612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08005614 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
5616 md_geninit();
5617 return (0);
5618}
5619
5620
5621#ifndef MODULE
5622
5623/*
5624 * Searches all registered partitions for autorun RAID arrays
5625 * at boot time.
5626 */
5627static dev_t detected_devices[128];
5628static int dev_cnt;
5629
5630void md_autodetect_dev(dev_t dev)
5631{
5632 if (dev_cnt >= 0 && dev_cnt < 127)
5633 detected_devices[dev_cnt++] = dev;
5634}
5635
5636
5637static void autostart_arrays(int part)
5638{
5639 mdk_rdev_t *rdev;
5640 int i;
5641
5642 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5643
5644 for (i = 0; i < dev_cnt; i++) {
5645 dev_t dev = detected_devices[i];
5646
5647 rdev = md_import_device(dev,0, 0);
5648 if (IS_ERR(rdev))
5649 continue;
5650
NeilBrownb2d444d2005-11-08 21:39:31 -08005651 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 MD_BUG();
5653 continue;
5654 }
5655 list_add(&rdev->same_set, &pending_raid_disks);
5656 }
5657 dev_cnt = 0;
5658
5659 autorun_devices(part);
5660}
5661
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005662#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
5664static __exit void md_exit(void)
5665{
5666 mddev_t *mddev;
5667 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07005668
NeilBrowne8703fe2006-10-03 01:15:59 -07005669 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
5670 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
5672 unregister_blkdev(MAJOR_NR,"md");
5673 unregister_blkdev(mdp_major, "mdp");
5674 unregister_reboot_notifier(&md_notifier);
5675 unregister_sysctl_table(raid_table_header);
5676 remove_proc_entry("mdstat", NULL);
5677 ITERATE_MDDEV(mddev,tmp) {
5678 struct gendisk *disk = mddev->gendisk;
5679 if (!disk)
5680 continue;
5681 export_array(mddev);
5682 del_gendisk(disk);
5683 put_disk(disk);
5684 mddev->gendisk = NULL;
5685 mddev_put(mddev);
5686 }
5687}
5688
5689module_init(md_init)
5690module_exit(md_exit)
5691
NeilBrownf91de922005-11-08 21:39:36 -08005692static int get_ro(char *buffer, struct kernel_param *kp)
5693{
5694 return sprintf(buffer, "%d", start_readonly);
5695}
5696static int set_ro(const char *val, struct kernel_param *kp)
5697{
5698 char *e;
5699 int num = simple_strtoul(val, &e, 10);
5700 if (*val && (*e == '\0' || *e == '\n')) {
5701 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005702 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005703 }
5704 return -EINVAL;
5705}
5706
NeilBrown80ca3a42006-07-10 04:44:18 -07005707module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
5708module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005709
NeilBrownf91de922005-11-08 21:39:36 -08005710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711EXPORT_SYMBOL(register_md_personality);
5712EXPORT_SYMBOL(unregister_md_personality);
5713EXPORT_SYMBOL(md_error);
5714EXPORT_SYMBOL(md_done_sync);
5715EXPORT_SYMBOL(md_write_start);
5716EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717EXPORT_SYMBOL(md_register_thread);
5718EXPORT_SYMBOL(md_unregister_thread);
5719EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720EXPORT_SYMBOL(md_check_recovery);
5721MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005722MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005723MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);