blob: 8603c0692eab8c0add1ff68db61d2562cb9735a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
35#include <linux/module.h>
36#include <linux/config.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070037#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/linkage.h>
39#include <linux/raid/md.h>
NeilBrown32a76272005-06-21 17:17:14 -070040#include <linux/raid/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/sysctl.h>
42#include <linux/devfs_fs_kernel.h>
43#include <linux/buffer_head.h> /* for invalidate_bdev */
44#include <linux/suspend.h>
NeilBrownd7603b72006-01-06 00:20:30 -080045#include <linux/poll.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080046#include <linux/mutex.h>
NeilBrown16f17b32006-06-26 00:27:37 -070047#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <linux/init.h>
50
NeilBrown32a76272005-06-21 17:17:14 -070051#include <linux/file.h>
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#ifdef CONFIG_KMOD
54#include <linux/kmod.h>
55#endif
56
57#include <asm/unaligned.h>
58
59#define MAJOR_NR MD_MAJOR
60#define MD_DRIVER
61
62/* 63 partitions with the alternate major number (mdp) */
63#define MdpMinorShift 6
64
65#define DEBUG 0
66#define dprintk(x...) ((void)(DEBUG && printk(x)))
67
68
69#ifndef MODULE
70static void autostart_arrays (int part);
71#endif
72
NeilBrown2604b702006-01-06 00:20:36 -080073static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static DEFINE_SPINLOCK(pers_lock);
75
Adrian Bunk5e563412006-06-26 00:27:42 -070076static void md_print_devices(void);
77
78#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
82 * is 1000 KB/sec, so the extra system load does not show up that much.
83 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070084 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * subsystem is idle. There is also an 'absolute maximum' reconstruction
86 * speed limit - in case reconstruction slows down your system despite
87 * idle IO detection.
88 *
89 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080090 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
92
93static int sysctl_speed_limit_min = 1000;
94static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080095static inline int speed_min(mddev_t *mddev)
96{
97 return mddev->sync_speed_min ?
98 mddev->sync_speed_min : sysctl_speed_limit_min;
99}
100
101static inline int speed_max(mddev_t *mddev)
102{
103 return mddev->sync_speed_max ?
104 mddev->sync_speed_max : sysctl_speed_limit_max;
105}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107static struct ctl_table_header *raid_table_header;
108
109static ctl_table raid_table[] = {
110 {
111 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
112 .procname = "speed_limit_min",
113 .data = &sysctl_speed_limit_min,
114 .maxlen = sizeof(int),
115 .mode = 0644,
116 .proc_handler = &proc_dointvec,
117 },
118 {
119 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
120 .procname = "speed_limit_max",
121 .data = &sysctl_speed_limit_max,
122 .maxlen = sizeof(int),
123 .mode = 0644,
124 .proc_handler = &proc_dointvec,
125 },
126 { .ctl_name = 0 }
127};
128
129static ctl_table raid_dir_table[] = {
130 {
131 .ctl_name = DEV_RAID,
132 .procname = "raid",
133 .maxlen = 0,
134 .mode = 0555,
135 .child = raid_table,
136 },
137 { .ctl_name = 0 }
138};
139
140static ctl_table raid_root_table[] = {
141 {
142 .ctl_name = CTL_DEV,
143 .procname = "dev",
144 .maxlen = 0,
145 .mode = 0555,
146 .child = raid_dir_table,
147 },
148 { .ctl_name = 0 }
149};
150
151static struct block_device_operations md_fops;
152
NeilBrownf91de922005-11-08 21:39:36 -0800153static int start_readonly;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
NeilBrownd7603b72006-01-06 00:20:30 -0800156 * We have a system wide 'event count' that is incremented
157 * on any 'interesting' event, and readers of /proc/mdstat
158 * can use 'poll' or 'select' to find out when the event
159 * count increases.
160 *
161 * Events are:
162 * start array, stop array, error, add device, remove device,
163 * start build, activate spare
164 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800165static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800166static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800167void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800168{
169 atomic_inc(&md_event_count);
170 wake_up(&md_event_waiters);
NeilBrown4508a7a2006-03-20 17:53:53 +1100171 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -0800172}
NeilBrown29269552006-03-27 01:18:10 -0800173EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800174
NeilBrownc331eb02006-05-30 21:27:13 -0700175/* Alternate version that can be called from interrupts
176 * when calling sysfs_notify isn't needed.
177 */
178void md_new_event_inintr(mddev_t *mddev)
179{
180 atomic_inc(&md_event_count);
181 wake_up(&md_event_waiters);
182}
183
NeilBrownd7603b72006-01-06 00:20:30 -0800184/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * Enables to iterate over all existing md arrays
186 * all_mddevs_lock protects this list.
187 */
188static LIST_HEAD(all_mddevs);
189static DEFINE_SPINLOCK(all_mddevs_lock);
190
191
192/*
193 * iterates through all used mddevs in the system.
194 * We take care to grab the all_mddevs_lock whenever navigating
195 * the list, and to always hold a refcount when unlocked.
196 * Any code which breaks out of this loop while own
197 * a reference to the current mddev and must mddev_put it.
198 */
199#define ITERATE_MDDEV(mddev,tmp) \
200 \
201 for (({ spin_lock(&all_mddevs_lock); \
202 tmp = all_mddevs.next; \
203 mddev = NULL;}); \
204 ({ if (tmp != &all_mddevs) \
205 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
206 spin_unlock(&all_mddevs_lock); \
207 if (mddev) mddev_put(mddev); \
208 mddev = list_entry(tmp, mddev_t, all_mddevs); \
209 tmp != &all_mddevs;}); \
210 ({ spin_lock(&all_mddevs_lock); \
211 tmp = tmp->next;}) \
212 )
213
214
215static int md_fail_request (request_queue_t *q, struct bio *bio)
216{
217 bio_io_error(bio, bio->bi_size);
218 return 0;
219}
220
221static inline mddev_t *mddev_get(mddev_t *mddev)
222{
223 atomic_inc(&mddev->active);
224 return mddev;
225}
226
227static void mddev_put(mddev_t *mddev)
228{
229 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
230 return;
231 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
232 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800233 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500234 blk_cleanup_queue(mddev->queue);
NeilBrowneae17012005-11-08 21:39:23 -0800235 kobject_unregister(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800236 } else
237 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240static mddev_t * mddev_find(dev_t unit)
241{
242 mddev_t *mddev, *new = NULL;
243
244 retry:
245 spin_lock(&all_mddevs_lock);
246 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
247 if (mddev->unit == unit) {
248 mddev_get(mddev);
249 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700250 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return mddev;
252 }
253
254 if (new) {
255 list_add(&new->all_mddevs, &all_mddevs);
256 spin_unlock(&all_mddevs_lock);
257 return new;
258 }
259 spin_unlock(&all_mddevs_lock);
260
NeilBrown9ffae0c2006-01-06 00:20:32 -0800261 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!new)
263 return NULL;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 new->unit = unit;
266 if (MAJOR(unit) == MD_MAJOR)
267 new->md_minor = MINOR(unit);
268 else
269 new->md_minor = MINOR(unit) >> MdpMinorShift;
270
NeilBrowndf5b89b2006-03-27 01:18:20 -0800271 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 INIT_LIST_HEAD(&new->disks);
273 INIT_LIST_HEAD(&new->all_mddevs);
274 init_timer(&new->safemode_timer);
275 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700276 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700277 init_waitqueue_head(&new->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 new->queue = blk_alloc_queue(GFP_KERNEL);
280 if (!new->queue) {
281 kfree(new);
282 return NULL;
283 }
NeilBrown89e5c8b2006-03-27 01:18:02 -0800284 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 blk_queue_make_request(new->queue, md_fail_request);
287
288 goto retry;
289}
290
291static inline int mddev_lock(mddev_t * mddev)
292{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800293 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static inline int mddev_trylock(mddev_t * mddev)
297{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800298 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301static inline void mddev_unlock(mddev_t * mddev)
302{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800303 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
NeilBrown005eca52005-08-22 13:11:08 -0700305 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
NeilBrown2989ddb2006-01-06 00:20:43 -0800308static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 mdk_rdev_t * rdev;
311 struct list_head *tmp;
312
313 ITERATE_RDEV(mddev,rdev,tmp) {
314 if (rdev->desc_nr == nr)
315 return rdev;
316 }
317 return NULL;
318}
319
320static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
321{
322 struct list_head *tmp;
323 mdk_rdev_t *rdev;
324
325 ITERATE_RDEV(mddev,rdev,tmp) {
326 if (rdev->bdev->bd_dev == dev)
327 return rdev;
328 }
329 return NULL;
330}
331
NeilBrownd9d166c2006-01-06 00:20:51 -0800332static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800333{
334 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800335 list_for_each_entry(pers, &pers_list, list) {
336 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800337 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800338 if (strcmp(pers->name, clevel)==0)
339 return pers;
340 }
NeilBrown2604b702006-01-06 00:20:36 -0800341 return NULL;
342}
343
Jesper Juhl77933d72005-07-27 11:46:09 -0700344static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
347 return MD_NEW_SIZE_BLOCKS(size);
348}
349
350static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
351{
352 sector_t size;
353
354 size = rdev->sb_offset;
355
356 if (chunk_size)
357 size &= ~((sector_t)chunk_size/1024 - 1);
358 return size;
359}
360
361static int alloc_disk_sb(mdk_rdev_t * rdev)
362{
363 if (rdev->sb_page)
364 MD_BUG();
365
366 rdev->sb_page = alloc_page(GFP_KERNEL);
367 if (!rdev->sb_page) {
368 printk(KERN_ALERT "md: out of memory.\n");
369 return -EINVAL;
370 }
371
372 return 0;
373}
374
375static void free_disk_sb(mdk_rdev_t * rdev)
376{
377 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800378 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 rdev->sb_loaded = 0;
380 rdev->sb_page = NULL;
381 rdev->sb_offset = 0;
382 rdev->size = 0;
383 }
384}
385
386
NeilBrown7bfa19f2005-06-21 17:17:28 -0700387static int super_written(struct bio *bio, unsigned int bytes_done, int error)
388{
389 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800390 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700391 if (bio->bi_size)
392 return 1;
393
394 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
NeilBrowna9701a32005-11-08 21:39:34 -0800395 md_error(mddev, rdev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700396
NeilBrowna9701a32005-11-08 21:39:34 -0800397 if (atomic_dec_and_test(&mddev->pending_writes))
398 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700399 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700400 return 0;
401}
402
NeilBrowna9701a32005-11-08 21:39:34 -0800403static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
404{
405 struct bio *bio2 = bio->bi_private;
406 mdk_rdev_t *rdev = bio2->bi_private;
407 mddev_t *mddev = rdev->mddev;
408 if (bio->bi_size)
409 return 1;
410
411 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
412 error == -EOPNOTSUPP) {
413 unsigned long flags;
414 /* barriers don't appear to be supported :-( */
415 set_bit(BarriersNotsupp, &rdev->flags);
416 mddev->barriers_work = 0;
417 spin_lock_irqsave(&mddev->write_lock, flags);
418 bio2->bi_next = mddev->biolist;
419 mddev->biolist = bio2;
420 spin_unlock_irqrestore(&mddev->write_lock, flags);
421 wake_up(&mddev->sb_wait);
422 bio_put(bio);
423 return 0;
424 }
425 bio_put(bio2);
426 bio->bi_private = rdev;
427 return super_written(bio, bytes_done, error);
428}
429
NeilBrown7bfa19f2005-06-21 17:17:28 -0700430void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
431 sector_t sector, int size, struct page *page)
432{
433 /* write first size bytes of page to sector of rdev
434 * Increment mddev->pending_writes before returning
435 * and decrement it on completion, waking up sb_wait
436 * if zero is reached.
437 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800438 *
439 * As we might need to resubmit the request if BIO_RW_BARRIER
440 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700441 */
442 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800443 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700444
445 bio->bi_bdev = rdev->bdev;
446 bio->bi_sector = sector;
447 bio_add_page(bio, page, size, 0);
448 bio->bi_private = rdev;
449 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800450 bio->bi_rw = rw;
451
NeilBrown7bfa19f2005-06-21 17:17:28 -0700452 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800453 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
454 struct bio *rbio;
455 rw |= (1<<BIO_RW_BARRIER);
456 rbio = bio_clone(bio, GFP_NOIO);
457 rbio->bi_private = bio;
458 rbio->bi_end_io = super_written_barrier;
459 submit_bio(rw, rbio);
460 } else
461 submit_bio(rw, bio);
462}
463
464void md_super_wait(mddev_t *mddev)
465{
466 /* wait for all superblock writes that were scheduled to complete.
467 * if any had to be retried (due to BARRIER problems), retry them
468 */
469 DEFINE_WAIT(wq);
470 for(;;) {
471 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
472 if (atomic_read(&mddev->pending_writes)==0)
473 break;
474 while (mddev->biolist) {
475 struct bio *bio;
476 spin_lock_irq(&mddev->write_lock);
477 bio = mddev->biolist;
478 mddev->biolist = bio->bi_next ;
479 bio->bi_next = NULL;
480 spin_unlock_irq(&mddev->write_lock);
481 submit_bio(bio->bi_rw, bio);
482 }
483 schedule();
484 }
485 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
489{
490 if (bio->bi_size)
491 return 1;
492
493 complete((struct completion*)bio->bi_private);
494 return 0;
495}
496
NeilBrowna654b9d82005-06-21 17:17:27 -0700497int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct page *page, int rw)
499{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700500 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct completion event;
502 int ret;
503
504 rw |= (1 << BIO_RW_SYNC);
505
506 bio->bi_bdev = bdev;
507 bio->bi_sector = sector;
508 bio_add_page(bio, page, size, 0);
509 init_completion(&event);
510 bio->bi_private = &event;
511 bio->bi_end_io = bi_complete;
512 submit_bio(rw, bio);
513 wait_for_completion(&event);
514
515 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
516 bio_put(bio);
517 return ret;
518}
NeilBrowna8745db2006-01-06 00:20:34 -0800519EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
NeilBrown0002b272005-09-09 16:23:53 -0700521static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 char b[BDEVNAME_SIZE];
524 if (!rdev->sb_page) {
525 MD_BUG();
526 return -EINVAL;
527 }
528 if (rdev->sb_loaded)
529 return 0;
530
531
NeilBrown0002b272005-09-09 16:23:53 -0700532 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto fail;
534 rdev->sb_loaded = 1;
535 return 0;
536
537fail:
538 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
539 bdevname(rdev->bdev,b));
540 return -EINVAL;
541}
542
543static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
544{
545 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
546 (sb1->set_uuid1 == sb2->set_uuid1) &&
547 (sb1->set_uuid2 == sb2->set_uuid2) &&
548 (sb1->set_uuid3 == sb2->set_uuid3))
549
550 return 1;
551
552 return 0;
553}
554
555
556static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
557{
558 int ret;
559 mdp_super_t *tmp1, *tmp2;
560
561 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
562 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
563
564 if (!tmp1 || !tmp2) {
565 ret = 0;
566 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
567 goto abort;
568 }
569
570 *tmp1 = *sb1;
571 *tmp2 = *sb2;
572
573 /*
574 * nr_disks is not constant
575 */
576 tmp1->nr_disks = 0;
577 tmp2->nr_disks = 0;
578
579 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
580 ret = 0;
581 else
582 ret = 1;
583
584abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700585 kfree(tmp1);
586 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return ret;
588}
589
590static unsigned int calc_sb_csum(mdp_super_t * sb)
591{
592 unsigned int disk_csum, csum;
593
594 disk_csum = sb->sb_csum;
595 sb->sb_csum = 0;
596 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
597 sb->sb_csum = disk_csum;
598 return csum;
599}
600
601
602/*
603 * Handle superblock details.
604 * We want to be able to handle multiple superblock formats
605 * so we have a common interface to them all, and an array of
606 * different handlers.
607 * We rely on user-space to write the initial superblock, and support
608 * reading and updating of superblocks.
609 * Interface methods are:
610 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
611 * loads and validates a superblock on dev.
612 * if refdev != NULL, compare superblocks on both devices
613 * Return:
614 * 0 - dev has a superblock that is compatible with refdev
615 * 1 - dev has a superblock that is compatible and newer than refdev
616 * so dev should be used as the refdev in future
617 * -EINVAL superblock incompatible or invalid
618 * -othererror e.g. -EIO
619 *
620 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
621 * Verify that dev is acceptable into mddev.
622 * The first time, mddev->raid_disks will be 0, and data from
623 * dev should be merged in. Subsequent calls check that dev
624 * is new enough. Return 0 or -EINVAL
625 *
626 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
627 * Update the superblock for rdev with data in mddev
628 * This does not write to disc.
629 *
630 */
631
632struct super_type {
633 char *name;
634 struct module *owner;
635 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
636 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
637 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
638};
639
640/*
641 * load_super for 0.90.0
642 */
643static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
644{
645 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
646 mdp_super_t *sb;
647 int ret;
648 sector_t sb_offset;
649
650 /*
651 * Calculate the position of the superblock,
652 * it's at the end of the disk.
653 *
654 * It also happens to be a multiple of 4Kb.
655 */
656 sb_offset = calc_dev_sboffset(rdev->bdev);
657 rdev->sb_offset = sb_offset;
658
NeilBrown0002b272005-09-09 16:23:53 -0700659 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (ret) return ret;
661
662 ret = -EINVAL;
663
664 bdevname(rdev->bdev, b);
665 sb = (mdp_super_t*)page_address(rdev->sb_page);
666
667 if (sb->md_magic != MD_SB_MAGIC) {
668 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
669 b);
670 goto abort;
671 }
672
673 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800674 sb->minor_version < 90 ||
675 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
677 sb->major_version, sb->minor_version,
678 b);
679 goto abort;
680 }
681
682 if (sb->raid_disks <= 0)
683 goto abort;
684
685 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
686 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
687 b);
688 goto abort;
689 }
690
691 rdev->preferred_minor = sb->md_minor;
692 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700693 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (sb->level == LEVEL_MULTIPATH)
696 rdev->desc_nr = -1;
697 else
698 rdev->desc_nr = sb->this_disk.number;
699
700 if (refdev == 0)
701 ret = 1;
702 else {
703 __u64 ev1, ev2;
704 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
705 if (!uuid_equal(refsb, sb)) {
706 printk(KERN_WARNING "md: %s has different UUID to %s\n",
707 b, bdevname(refdev->bdev,b2));
708 goto abort;
709 }
710 if (!sb_equal(refsb, sb)) {
711 printk(KERN_WARNING "md: %s has same UUID"
712 " but different superblock to %s\n",
713 b, bdevname(refdev->bdev, b2));
714 goto abort;
715 }
716 ev1 = md_event(sb);
717 ev2 = md_event(refsb);
718 if (ev1 > ev2)
719 ret = 1;
720 else
721 ret = 0;
722 }
723 rdev->size = calc_dev_size(rdev, sb->chunk_size);
724
NeilBrown2bf071b2006-01-06 00:20:55 -0800725 if (rdev->size < sb->size && sb->level > 1)
726 /* "this cannot possibly happen" ... */
727 ret = -EINVAL;
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 abort:
730 return ret;
731}
732
733/*
734 * validate_super for 0.90.0
735 */
736static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
737{
738 mdp_disk_t *desc;
739 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700740 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
NeilBrown41158c72005-06-21 17:17:25 -0700742 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800743 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (mddev->raid_disks == 0) {
745 mddev->major_version = 0;
746 mddev->minor_version = sb->minor_version;
747 mddev->patch_version = sb->patch_version;
748 mddev->persistent = ! sb->not_persistent;
749 mddev->chunk_size = sb->chunk_size;
750 mddev->ctime = sb->ctime;
751 mddev->utime = sb->utime;
752 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800753 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 mddev->layout = sb->layout;
755 mddev->raid_disks = sb->raid_disks;
756 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700757 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700758 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700759 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
NeilBrownf6705572006-03-27 01:18:11 -0800761 if (mddev->minor_version >= 91) {
762 mddev->reshape_position = sb->reshape_position;
763 mddev->delta_disks = sb->delta_disks;
764 mddev->new_level = sb->new_level;
765 mddev->new_layout = sb->new_layout;
766 mddev->new_chunk = sb->new_chunk;
767 } else {
768 mddev->reshape_position = MaxSector;
769 mddev->delta_disks = 0;
770 mddev->new_level = mddev->level;
771 mddev->new_layout = mddev->layout;
772 mddev->new_chunk = mddev->chunk_size;
773 }
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (sb->state & (1<<MD_SB_CLEAN))
776 mddev->recovery_cp = MaxSector;
777 else {
778 if (sb->events_hi == sb->cp_events_hi &&
779 sb->events_lo == sb->cp_events_lo) {
780 mddev->recovery_cp = sb->recovery_cp;
781 } else
782 mddev->recovery_cp = 0;
783 }
784
785 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
786 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
787 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
788 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
789
790 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700791
792 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
793 mddev->bitmap_file == NULL) {
NeilBrownc5a10f62006-03-27 01:18:03 -0800794 if (mddev->level != 1 && mddev->level != 4
795 && mddev->level != 5 && mddev->level != 6
NeilBrown6cce3b22006-01-06 00:20:16 -0800796 && mddev->level != 10) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700797 /* FIXME use a better test */
NeilBrown6cce3b22006-01-06 00:20:16 -0800798 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -0700799 return -EINVAL;
800 }
NeilBrown36fa3062005-09-09 16:23:45 -0700801 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700802 }
803
NeilBrown41158c72005-06-21 17:17:25 -0700804 } else if (mddev->pers == NULL) {
805 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 ++ev1;
807 if (ev1 < mddev->events)
808 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700809 } else if (mddev->bitmap) {
810 /* if adding to array with a bitmap, then we can accept an
811 * older device ... but not too old.
812 */
NeilBrown41158c72005-06-21 17:17:25 -0700813 if (ev1 < mddev->bitmap->events_cleared)
814 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700815 } else {
816 if (ev1 < mddev->events)
817 /* just a hot-add of a new device, leave raid_disk at -1 */
818 return 0;
819 }
NeilBrown41158c72005-06-21 17:17:25 -0700820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 desc = sb->disks + rdev->desc_nr;
823
824 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800825 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700826 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
827 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800828 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 rdev->raid_disk = desc->raid_disk;
830 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700831 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
832 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700833 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800834 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return 0;
836}
837
838/*
839 * sync_super for 0.90.0
840 */
841static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
842{
843 mdp_super_t *sb;
844 struct list_head *tmp;
845 mdk_rdev_t *rdev2;
846 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* make rdev->sb match mddev data..
850 *
851 * 1/ zero out disks
852 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
853 * 3/ any empty disks < next_spare become removed
854 *
855 * disks[0] gets initialised to REMOVED because
856 * we cannot be sure from other fields if it has
857 * been initialised or not.
858 */
859 int i;
860 int active=0, working=0,failed=0,spare=0,nr_disks=0;
861
NeilBrown61181562005-09-09 16:24:02 -0700862 rdev->sb_size = MD_SB_BYTES;
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 sb = (mdp_super_t*)page_address(rdev->sb_page);
865
866 memset(sb, 0, sizeof(*sb));
867
868 sb->md_magic = MD_SB_MAGIC;
869 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 sb->patch_version = mddev->patch_version;
871 sb->gvalid_words = 0; /* ignored */
872 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
873 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
874 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
875 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
876
877 sb->ctime = mddev->ctime;
878 sb->level = mddev->level;
879 sb->size = mddev->size;
880 sb->raid_disks = mddev->raid_disks;
881 sb->md_minor = mddev->md_minor;
882 sb->not_persistent = !mddev->persistent;
883 sb->utime = mddev->utime;
884 sb->state = 0;
885 sb->events_hi = (mddev->events>>32);
886 sb->events_lo = (u32)mddev->events;
887
NeilBrownf6705572006-03-27 01:18:11 -0800888 if (mddev->reshape_position == MaxSector)
889 sb->minor_version = 90;
890 else {
891 sb->minor_version = 91;
892 sb->reshape_position = mddev->reshape_position;
893 sb->new_level = mddev->new_level;
894 sb->delta_disks = mddev->delta_disks;
895 sb->new_layout = mddev->new_layout;
896 sb->new_chunk = mddev->new_chunk;
897 }
898 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (mddev->in_sync)
900 {
901 sb->recovery_cp = mddev->recovery_cp;
902 sb->cp_events_hi = (mddev->events>>32);
903 sb->cp_events_lo = (u32)mddev->events;
904 if (mddev->recovery_cp == MaxSector)
905 sb->state = (1<< MD_SB_CLEAN);
906 } else
907 sb->recovery_cp = 0;
908
909 sb->layout = mddev->layout;
910 sb->chunk_size = mddev->chunk_size;
911
NeilBrowna654b9d82005-06-21 17:17:27 -0700912 if (mddev->bitmap && mddev->bitmap_file == NULL)
913 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 sb->disks[0].state = (1<<MD_DISK_REMOVED);
916 ITERATE_RDEV(mddev,rdev2,tmp) {
917 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800918 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800919 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
920 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800921 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800923 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800924 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 d = &sb->disks[rdev2->desc_nr];
926 nr_disks++;
927 d->number = rdev2->desc_nr;
928 d->major = MAJOR(rdev2->bdev->bd_dev);
929 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800930 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
931 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 d->raid_disk = rdev2->raid_disk;
933 else
934 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800935 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800937 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 d->state = (1<<MD_DISK_ACTIVE);
939 d->state |= (1<<MD_DISK_SYNC);
940 active++;
941 working++;
942 } else {
943 d->state = 0;
944 spare++;
945 working++;
946 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700947 if (test_bit(WriteMostly, &rdev2->flags))
948 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /* now set the "removed" and "faulty" bits on any missing devices */
951 for (i=0 ; i < mddev->raid_disks ; i++) {
952 mdp_disk_t *d = &sb->disks[i];
953 if (d->state == 0 && d->number == 0) {
954 d->number = i;
955 d->raid_disk = i;
956 d->state = (1<<MD_DISK_REMOVED);
957 d->state |= (1<<MD_DISK_FAULTY);
958 failed++;
959 }
960 }
961 sb->nr_disks = nr_disks;
962 sb->active_disks = active;
963 sb->working_disks = working;
964 sb->failed_disks = failed;
965 sb->spare_disks = spare;
966
967 sb->this_disk = sb->disks[rdev->desc_nr];
968 sb->sb_csum = calc_sb_csum(sb);
969}
970
971/*
972 * version 1 superblock
973 */
974
975static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
976{
977 unsigned int disk_csum, csum;
978 unsigned long long newcsum;
979 int size = 256 + le32_to_cpu(sb->max_dev)*2;
980 unsigned int *isuper = (unsigned int*)sb;
981 int i;
982
983 disk_csum = sb->sb_csum;
984 sb->sb_csum = 0;
985 newcsum = 0;
986 for (i=0; size>=4; size -= 4 )
987 newcsum += le32_to_cpu(*isuper++);
988
989 if (size == 2)
990 newcsum += le16_to_cpu(*(unsigned short*) isuper);
991
992 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
993 sb->sb_csum = disk_csum;
994 return cpu_to_le32(csum);
995}
996
997static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
998{
999 struct mdp_superblock_1 *sb;
1000 int ret;
1001 sector_t sb_offset;
1002 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001003 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 /*
1006 * Calculate the position of the superblock.
1007 * It is always aligned to a 4K boundary and
1008 * depeding on minor_version, it can be:
1009 * 0: At least 8K, but less than 12K, from end of device
1010 * 1: At start of device
1011 * 2: 4K from start of device.
1012 */
1013 switch(minor_version) {
1014 case 0:
1015 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1016 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001017 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /* convert from sectors to K */
1019 sb_offset /= 2;
1020 break;
1021 case 1:
1022 sb_offset = 0;
1023 break;
1024 case 2:
1025 sb_offset = 4;
1026 break;
1027 default:
1028 return -EINVAL;
1029 }
1030 rdev->sb_offset = sb_offset;
1031
NeilBrown0002b272005-09-09 16:23:53 -07001032 /* superblock is rarely larger than 1K, but it can be larger,
1033 * and it is safe to read 4k, so we do that
1034 */
1035 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (ret) return ret;
1037
1038
1039 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1040
1041 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1042 sb->major_version != cpu_to_le32(1) ||
1043 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1044 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001045 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return -EINVAL;
1047
1048 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1049 printk("md: invalid superblock checksum on %s\n",
1050 bdevname(rdev->bdev,b));
1051 return -EINVAL;
1052 }
1053 if (le64_to_cpu(sb->data_size) < 10) {
1054 printk("md: data_size too small on %s\n",
1055 bdevname(rdev->bdev,b));
1056 return -EINVAL;
1057 }
1058 rdev->preferred_minor = 0xffff;
1059 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001060 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
NeilBrown0002b272005-09-09 16:23:53 -07001062 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001063 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001064 if (rdev->sb_size & bmask)
1065 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (refdev == 0)
NeilBrown8ed75462006-02-03 03:03:41 -08001068 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 else {
1070 __u64 ev1, ev2;
1071 struct mdp_superblock_1 *refsb =
1072 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1073
1074 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1075 sb->level != refsb->level ||
1076 sb->layout != refsb->layout ||
1077 sb->chunksize != refsb->chunksize) {
1078 printk(KERN_WARNING "md: %s has strangely different"
1079 " superblock to %s\n",
1080 bdevname(rdev->bdev,b),
1081 bdevname(refdev->bdev,b2));
1082 return -EINVAL;
1083 }
1084 ev1 = le64_to_cpu(sb->events);
1085 ev2 = le64_to_cpu(refsb->events);
1086
1087 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001088 ret = 1;
1089 else
1090 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092 if (minor_version)
1093 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1094 else
1095 rdev->size = rdev->sb_offset;
1096 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1097 return -EINVAL;
1098 rdev->size = le64_to_cpu(sb->data_size)/2;
1099 if (le32_to_cpu(sb->chunksize))
1100 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001101
1102 if (le32_to_cpu(sb->size) > rdev->size*2)
1103 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001104 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1108{
1109 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001110 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
NeilBrown41158c72005-06-21 17:17:25 -07001112 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001113 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (mddev->raid_disks == 0) {
1115 mddev->major_version = 1;
1116 mddev->patch_version = 0;
1117 mddev->persistent = 1;
1118 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1119 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1120 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1121 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001122 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 mddev->layout = le32_to_cpu(sb->layout);
1124 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1125 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001126 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001127 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001128 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1131 memcpy(mddev->uuid, sb->set_uuid, 16);
1132
1133 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001134
NeilBrown71c08052005-09-09 16:23:51 -07001135 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -07001136 mddev->bitmap_file == NULL ) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001137 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1138 && mddev->level != 10) {
1139 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -07001140 return -EINVAL;
1141 }
1142 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1143 }
NeilBrownf6705572006-03-27 01:18:11 -08001144 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1145 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1146 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1147 mddev->new_level = le32_to_cpu(sb->new_level);
1148 mddev->new_layout = le32_to_cpu(sb->new_layout);
1149 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1150 } else {
1151 mddev->reshape_position = MaxSector;
1152 mddev->delta_disks = 0;
1153 mddev->new_level = mddev->level;
1154 mddev->new_layout = mddev->layout;
1155 mddev->new_chunk = mddev->chunk_size;
1156 }
1157
NeilBrown41158c72005-06-21 17:17:25 -07001158 } else if (mddev->pers == NULL) {
1159 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ++ev1;
1161 if (ev1 < mddev->events)
1162 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001163 } else if (mddev->bitmap) {
1164 /* If adding to array with a bitmap, then we can accept an
1165 * older device, but not too old.
1166 */
NeilBrown41158c72005-06-21 17:17:25 -07001167 if (ev1 < mddev->bitmap->events_cleared)
1168 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001169 } else {
1170 if (ev1 < mddev->events)
1171 /* just a hot-add of a new device, leave raid_disk at -1 */
1172 return 0;
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (mddev->level != LEVEL_MULTIPATH) {
1175 int role;
1176 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1177 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1178 switch(role) {
1179 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 break;
1181 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001182 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 break;
1184 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001185 if ((le32_to_cpu(sb->feature_map) &
1186 MD_FEATURE_RECOVERY_OFFSET))
1187 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1188 else
1189 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 rdev->raid_disk = role;
1191 break;
1192 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001193 if (sb->devflags & WriteMostly1)
1194 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001195 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001196 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return 0;
1199}
1200
1201static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1202{
1203 struct mdp_superblock_1 *sb;
1204 struct list_head *tmp;
1205 mdk_rdev_t *rdev2;
1206 int max_dev, i;
1207 /* make rdev->sb match mddev and rdev data. */
1208
1209 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1210
1211 sb->feature_map = 0;
1212 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001213 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 memset(sb->pad1, 0, sizeof(sb->pad1));
1215 memset(sb->pad2, 0, sizeof(sb->pad2));
1216 memset(sb->pad3, 0, sizeof(sb->pad3));
1217
1218 sb->utime = cpu_to_le64((__u64)mddev->utime);
1219 sb->events = cpu_to_le64(mddev->events);
1220 if (mddev->in_sync)
1221 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1222 else
1223 sb->resync_offset = cpu_to_le64(0);
1224
NeilBrown4dbcdc72006-01-06 00:20:52 -08001225 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1226
NeilBrownf0ca3402006-02-02 14:28:04 -08001227 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001228 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001229
NeilBrowna654b9d82005-06-21 17:17:27 -07001230 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1231 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001232 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001233 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001234
1235 if (rdev->raid_disk >= 0 &&
1236 !test_bit(In_sync, &rdev->flags) &&
1237 rdev->recovery_offset > 0) {
1238 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1239 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1240 }
1241
NeilBrownf6705572006-03-27 01:18:11 -08001242 if (mddev->reshape_position != MaxSector) {
1243 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1244 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1245 sb->new_layout = cpu_to_le32(mddev->new_layout);
1246 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1247 sb->new_level = cpu_to_le32(mddev->new_level);
1248 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1249 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 max_dev = 0;
1252 ITERATE_RDEV(mddev,rdev2,tmp)
1253 if (rdev2->desc_nr+1 > max_dev)
1254 max_dev = rdev2->desc_nr+1;
1255
1256 sb->max_dev = cpu_to_le32(max_dev);
1257 for (i=0; i<max_dev;i++)
1258 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1259
1260 ITERATE_RDEV(mddev,rdev2,tmp) {
1261 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001262 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001264 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001266 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1267 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 else
1269 sb->dev_roles[i] = cpu_to_le16(0xffff);
1270 }
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 sb->sb_csum = calc_sb_1_csum(sb);
1273}
1274
1275
Adrian Bunk75c96f82005-05-05 16:16:09 -07001276static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 [0] = {
1278 .name = "0.90.0",
1279 .owner = THIS_MODULE,
1280 .load_super = super_90_load,
1281 .validate_super = super_90_validate,
1282 .sync_super = super_90_sync,
1283 },
1284 [1] = {
1285 .name = "md-1",
1286 .owner = THIS_MODULE,
1287 .load_super = super_1_load,
1288 .validate_super = super_1_validate,
1289 .sync_super = super_1_sync,
1290 },
1291};
1292
1293static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1294{
1295 struct list_head *tmp;
1296 mdk_rdev_t *rdev;
1297
1298 ITERATE_RDEV(mddev,rdev,tmp)
1299 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1300 return rdev;
1301
1302 return NULL;
1303}
1304
1305static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1306{
1307 struct list_head *tmp;
1308 mdk_rdev_t *rdev;
1309
1310 ITERATE_RDEV(mddev1,rdev,tmp)
1311 if (match_dev_unit(mddev2, rdev))
1312 return 1;
1313
1314 return 0;
1315}
1316
1317static LIST_HEAD(pending_raid_disks);
1318
1319static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1320{
1321 mdk_rdev_t *same_pdev;
1322 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001323 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001324 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (rdev->mddev) {
1327 MD_BUG();
1328 return -EINVAL;
1329 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001330 /* make sure rdev->size exceeds mddev->size */
1331 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1332 if (mddev->pers)
1333 /* Cannot change size, so fail */
1334 return -ENOSPC;
1335 else
1336 mddev->size = rdev->size;
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 same_pdev = match_dev_unit(mddev, rdev);
1339 if (same_pdev)
1340 printk(KERN_WARNING
1341 "%s: WARNING: %s appears to be on the same physical"
1342 " disk as %s. True\n protection against single-disk"
1343 " failure might be compromised.\n",
1344 mdname(mddev), bdevname(rdev->bdev,b),
1345 bdevname(same_pdev->bdev,b2));
1346
1347 /* Verify rdev->desc_nr is unique.
1348 * If it is -1, assign a free number, else
1349 * check number is not in use
1350 */
1351 if (rdev->desc_nr < 0) {
1352 int choice = 0;
1353 if (mddev->pers) choice = mddev->raid_disks;
1354 while (find_rdev_nr(mddev, choice))
1355 choice++;
1356 rdev->desc_nr = choice;
1357 } else {
1358 if (find_rdev_nr(mddev, rdev->desc_nr))
1359 return -EBUSY;
1360 }
NeilBrown19133a42005-11-08 21:39:35 -08001361 bdevname(rdev->bdev,b);
1362 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1363 return -ENOMEM;
Neil Brown1edf80d2006-01-12 01:05:23 -08001364 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1365 *s = '!';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 list_add(&rdev->same_set, &mddev->disks);
1368 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001369 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001370
NeilBrown9c791972005-11-08 21:39:28 -08001371 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001372 kobject_add(&rdev->kobj);
1373
NeilBrownf637b9f2005-11-08 21:39:37 -08001374 if (rdev->bdev->bd_part)
1375 ko = &rdev->bdev->bd_part->kobj;
1376 else
1377 ko = &rdev->bdev->bd_disk->kobj;
1378 sysfs_create_link(&rdev->kobj, ko, "block");
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001379 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return 0;
1381}
1382
1383static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1384{
1385 char b[BDEVNAME_SIZE];
1386 if (!rdev->mddev) {
1387 MD_BUG();
1388 return;
1389 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001390 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 list_del_init(&rdev->same_set);
1392 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1393 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001394 sysfs_remove_link(&rdev->kobj, "block");
1395 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
1398/*
1399 * prevent the device from being mounted, repartitioned or
1400 * otherwise reused by a RAID array (or any other kernel
1401 * subsystem), by bd_claiming the device.
1402 */
1403static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1404{
1405 int err = 0;
1406 struct block_device *bdev;
1407 char b[BDEVNAME_SIZE];
1408
1409 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1410 if (IS_ERR(bdev)) {
1411 printk(KERN_ERR "md: could not open %s.\n",
1412 __bdevname(dev, b));
1413 return PTR_ERR(bdev);
1414 }
1415 err = bd_claim(bdev, rdev);
1416 if (err) {
1417 printk(KERN_ERR "md: could not bd_claim %s.\n",
1418 bdevname(bdev, b));
1419 blkdev_put(bdev);
1420 return err;
1421 }
1422 rdev->bdev = bdev;
1423 return err;
1424}
1425
1426static void unlock_rdev(mdk_rdev_t *rdev)
1427{
1428 struct block_device *bdev = rdev->bdev;
1429 rdev->bdev = NULL;
1430 if (!bdev)
1431 MD_BUG();
1432 bd_release(bdev);
1433 blkdev_put(bdev);
1434}
1435
1436void md_autodetect_dev(dev_t dev);
1437
1438static void export_rdev(mdk_rdev_t * rdev)
1439{
1440 char b[BDEVNAME_SIZE];
1441 printk(KERN_INFO "md: export_rdev(%s)\n",
1442 bdevname(rdev->bdev,b));
1443 if (rdev->mddev)
1444 MD_BUG();
1445 free_disk_sb(rdev);
1446 list_del_init(&rdev->same_set);
1447#ifndef MODULE
1448 md_autodetect_dev(rdev->bdev->bd_dev);
1449#endif
1450 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001451 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454static void kick_rdev_from_array(mdk_rdev_t * rdev)
1455{
1456 unbind_rdev_from_array(rdev);
1457 export_rdev(rdev);
1458}
1459
1460static void export_array(mddev_t *mddev)
1461{
1462 struct list_head *tmp;
1463 mdk_rdev_t *rdev;
1464
1465 ITERATE_RDEV(mddev,rdev,tmp) {
1466 if (!rdev->mddev) {
1467 MD_BUG();
1468 continue;
1469 }
1470 kick_rdev_from_array(rdev);
1471 }
1472 if (!list_empty(&mddev->disks))
1473 MD_BUG();
1474 mddev->raid_disks = 0;
1475 mddev->major_version = 0;
1476}
1477
1478static void print_desc(mdp_disk_t *desc)
1479{
1480 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1481 desc->major,desc->minor,desc->raid_disk,desc->state);
1482}
1483
1484static void print_sb(mdp_super_t *sb)
1485{
1486 int i;
1487
1488 printk(KERN_INFO
1489 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1490 sb->major_version, sb->minor_version, sb->patch_version,
1491 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1492 sb->ctime);
1493 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1494 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1495 sb->md_minor, sb->layout, sb->chunk_size);
1496 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1497 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1498 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1499 sb->failed_disks, sb->spare_disks,
1500 sb->sb_csum, (unsigned long)sb->events_lo);
1501
1502 printk(KERN_INFO);
1503 for (i = 0; i < MD_SB_DISKS; i++) {
1504 mdp_disk_t *desc;
1505
1506 desc = sb->disks + i;
1507 if (desc->number || desc->major || desc->minor ||
1508 desc->raid_disk || (desc->state && (desc->state != 4))) {
1509 printk(" D %2d: ", i);
1510 print_desc(desc);
1511 }
1512 }
1513 printk(KERN_INFO "md: THIS: ");
1514 print_desc(&sb->this_disk);
1515
1516}
1517
1518static void print_rdev(mdk_rdev_t *rdev)
1519{
1520 char b[BDEVNAME_SIZE];
1521 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1522 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001523 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1524 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (rdev->sb_loaded) {
1526 printk(KERN_INFO "md: rdev superblock:\n");
1527 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1528 } else
1529 printk(KERN_INFO "md: no rdev superblock!\n");
1530}
1531
Adrian Bunk5e563412006-06-26 00:27:42 -07001532static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 struct list_head *tmp, *tmp2;
1535 mdk_rdev_t *rdev;
1536 mddev_t *mddev;
1537 char b[BDEVNAME_SIZE];
1538
1539 printk("\n");
1540 printk("md: **********************************\n");
1541 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1542 printk("md: **********************************\n");
1543 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
NeilBrown32a76272005-06-21 17:17:14 -07001545 if (mddev->bitmap)
1546 bitmap_print_sb(mddev->bitmap);
1547 else
1548 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 ITERATE_RDEV(mddev,rdev,tmp2)
1550 printk("<%s>", bdevname(rdev->bdev,b));
1551 printk("\n");
1552
1553 ITERATE_RDEV(mddev,rdev,tmp2)
1554 print_rdev(rdev);
1555 }
1556 printk("md: **********************************\n");
1557 printk("\n");
1558}
1559
1560
NeilBrown42543762006-06-26 00:27:57 -07001561static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
NeilBrown42543762006-06-26 00:27:57 -07001563 /* Update each superblock (in-memory image), but
1564 * if we are allowed to, skip spares which already
1565 * have the right event counter, or have one earlier
1566 * (which would mean they aren't being marked as dirty
1567 * with the rest of the array)
1568 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 mdk_rdev_t *rdev;
1570 struct list_head *tmp;
1571
1572 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrown42543762006-06-26 00:27:57 -07001573 if (rdev->sb_events == mddev->events ||
1574 (nospares &&
1575 rdev->raid_disk < 0 &&
1576 (rdev->sb_events&1)==0 &&
1577 rdev->sb_events+1 == mddev->events)) {
1578 /* Don't update this superblock */
1579 rdev->sb_loaded = 2;
1580 } else {
1581 super_types[mddev->major_version].
1582 sync_super(mddev, rdev);
1583 rdev->sb_loaded = 1;
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586}
1587
NeilBrownf6705572006-03-27 01:18:11 -08001588void md_update_sb(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001590 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 struct list_head *tmp;
1592 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001593 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001594 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001597 spin_lock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001598 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001600 if (mddev->sb_dirty == 3)
1601 /* just a clean<-> dirty transition, possibly leave spares alone,
1602 * though if events isn't the right even/odd, we will have to do
1603 * spares after all
1604 */
1605 nospares = 1;
1606
1607 /* If this is just a dirty<->clean transition, and the array is clean
1608 * and 'events' is odd, we can roll back to the previous clean state */
1609 if (mddev->sb_dirty == 3
1610 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1611 && (mddev->events & 1))
1612 mddev->events--;
1613 else {
1614 /* otherwise we have to go forward and ... */
1615 mddev->events ++;
1616 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1617 /* .. if the array isn't clean, insist on an odd 'events' */
1618 if ((mddev->events&1)==0) {
1619 mddev->events++;
1620 nospares = 0;
1621 }
1622 } else {
1623 /* otherwise insist on an even 'events' (for clean states) */
1624 if ((mddev->events&1)) {
1625 mddev->events++;
1626 nospares = 0;
1627 }
1628 }
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 if (!mddev->events) {
1632 /*
1633 * oops, this 64-bit counter should never wrap.
1634 * Either we are in around ~1 trillion A.C., assuming
1635 * 1 reboot per second, or we have a bug:
1636 */
1637 MD_BUG();
1638 mddev->events --;
1639 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001640 mddev->sb_dirty = 2;
NeilBrown42543762006-06-26 00:27:57 -07001641 sync_sbs(mddev, nospares);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 /*
1644 * do not write anything to disk if using
1645 * nonpersistent superblocks
1646 */
NeilBrown06d91a52005-06-21 17:17:12 -07001647 if (!mddev->persistent) {
1648 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001649 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001650 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001652 }
NeilBrowna9701a32005-11-08 21:39:34 -08001653 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 dprintk(KERN_INFO
1656 "md: updating %s RAID superblock on device (in sync %d)\n",
1657 mdname(mddev),mddev->in_sync);
1658
NeilBrown32a76272005-06-21 17:17:14 -07001659 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 ITERATE_RDEV(mddev,rdev,tmp) {
1661 char b[BDEVNAME_SIZE];
1662 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001663 if (rdev->sb_loaded != 1)
1664 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001665 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 dprintk("(skipping faulty ");
1667
1668 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001669 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001670 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001671 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001672 rdev->sb_page);
1673 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1674 bdevname(rdev->bdev,b),
1675 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001676 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 } else
1679 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001680 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /* only need to write one superblock... */
1682 break;
1683 }
NeilBrowna9701a32005-11-08 21:39:34 -08001684 md_super_wait(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001685 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1686
NeilBrowna9701a32005-11-08 21:39:34 -08001687 spin_lock_irq(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001688 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001689 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001690 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001691 goto repeat;
1692 }
1693 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001694 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001695 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
NeilBrownf6705572006-03-27 01:18:11 -08001698EXPORT_SYMBOL_GPL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
NeilBrownbce74da2006-01-06 00:20:41 -08001700/* words written to sysfs files may, or my not, be \n terminated.
1701 * We want to accept with case. For this we use cmd_match.
1702 */
1703static int cmd_match(const char *cmd, const char *str)
1704{
1705 /* See if cmd, written into a sysfs file, matches
1706 * str. They must either be the same, or cmd can
1707 * have a trailing newline
1708 */
1709 while (*cmd && *str && *cmd == *str) {
1710 cmd++;
1711 str++;
1712 }
1713 if (*cmd == '\n')
1714 cmd++;
1715 if (*str || *cmd)
1716 return 0;
1717 return 1;
1718}
1719
NeilBrown86e6ffd2005-11-08 21:39:24 -08001720struct rdev_sysfs_entry {
1721 struct attribute attr;
1722 ssize_t (*show)(mdk_rdev_t *, char *);
1723 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1724};
1725
1726static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001727state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001728{
1729 char *sep = "";
1730 int len=0;
1731
NeilBrownb2d444d2005-11-08 21:39:31 -08001732 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001733 len+= sprintf(page+len, "%sfaulty",sep);
1734 sep = ",";
1735 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001736 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001737 len += sprintf(page+len, "%sin_sync",sep);
1738 sep = ",";
1739 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001740 if (!test_bit(Faulty, &rdev->flags) &&
1741 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001742 len += sprintf(page+len, "%sspare", sep);
1743 sep = ",";
1744 }
1745 return len+sprintf(page+len, "\n");
1746}
1747
NeilBrown45dc2de2006-06-26 00:27:58 -07001748static ssize_t
1749state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1750{
1751 /* can write
1752 * faulty - simulates and error
1753 * remove - disconnects the device
1754 */
1755 int err = -EINVAL;
1756 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1757 md_error(rdev->mddev, rdev);
1758 err = 0;
1759 } else if (cmd_match(buf, "remove")) {
1760 if (rdev->raid_disk >= 0)
1761 err = -EBUSY;
1762 else {
1763 mddev_t *mddev = rdev->mddev;
1764 kick_rdev_from_array(rdev);
1765 md_update_sb(mddev);
1766 md_new_event(mddev);
1767 err = 0;
1768 }
1769 }
1770 return err ? err : len;
1771}
NeilBrown96de1e62005-11-08 21:39:39 -08001772static struct rdev_sysfs_entry
NeilBrown45dc2de2006-06-26 00:27:58 -07001773rdev_state = __ATTR(state, 0644, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001774
1775static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001776super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001777{
1778 if (rdev->sb_loaded && rdev->sb_size) {
1779 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1780 return rdev->sb_size;
1781 } else
1782 return 0;
1783}
NeilBrown96de1e62005-11-08 21:39:39 -08001784static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1785
NeilBrown4dbcdc72006-01-06 00:20:52 -08001786static ssize_t
1787errors_show(mdk_rdev_t *rdev, char *page)
1788{
1789 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1790}
1791
1792static ssize_t
1793errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1794{
1795 char *e;
1796 unsigned long n = simple_strtoul(buf, &e, 10);
1797 if (*buf && (*e == 0 || *e == '\n')) {
1798 atomic_set(&rdev->corrected_errors, n);
1799 return len;
1800 }
1801 return -EINVAL;
1802}
1803static struct rdev_sysfs_entry rdev_errors =
1804__ATTR(errors, 0644, errors_show, errors_store);
1805
NeilBrown014236d2006-01-06 00:20:55 -08001806static ssize_t
1807slot_show(mdk_rdev_t *rdev, char *page)
1808{
1809 if (rdev->raid_disk < 0)
1810 return sprintf(page, "none\n");
1811 else
1812 return sprintf(page, "%d\n", rdev->raid_disk);
1813}
1814
1815static ssize_t
1816slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1817{
1818 char *e;
1819 int slot = simple_strtoul(buf, &e, 10);
1820 if (strncmp(buf, "none", 4)==0)
1821 slot = -1;
1822 else if (e==buf || (*e && *e!= '\n'))
1823 return -EINVAL;
1824 if (rdev->mddev->pers)
1825 /* Cannot set slot in active array (yet) */
1826 return -EBUSY;
1827 if (slot >= rdev->mddev->raid_disks)
1828 return -ENOSPC;
1829 rdev->raid_disk = slot;
1830 /* assume it is working */
1831 rdev->flags = 0;
1832 set_bit(In_sync, &rdev->flags);
1833 return len;
1834}
1835
1836
1837static struct rdev_sysfs_entry rdev_slot =
1838__ATTR(slot, 0644, slot_show, slot_store);
1839
NeilBrown93c8cad2006-01-06 00:20:56 -08001840static ssize_t
1841offset_show(mdk_rdev_t *rdev, char *page)
1842{
Andrew Morton6961ece2006-01-06 00:20:59 -08001843 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001844}
1845
1846static ssize_t
1847offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1848{
1849 char *e;
1850 unsigned long long offset = simple_strtoull(buf, &e, 10);
1851 if (e==buf || (*e && *e != '\n'))
1852 return -EINVAL;
1853 if (rdev->mddev->pers)
1854 return -EBUSY;
1855 rdev->data_offset = offset;
1856 return len;
1857}
1858
1859static struct rdev_sysfs_entry rdev_offset =
1860__ATTR(offset, 0644, offset_show, offset_store);
1861
NeilBrown83303b62006-01-06 00:21:06 -08001862static ssize_t
1863rdev_size_show(mdk_rdev_t *rdev, char *page)
1864{
1865 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1866}
1867
1868static ssize_t
1869rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1870{
1871 char *e;
1872 unsigned long long size = simple_strtoull(buf, &e, 10);
1873 if (e==buf || (*e && *e != '\n'))
1874 return -EINVAL;
1875 if (rdev->mddev->pers)
1876 return -EBUSY;
1877 rdev->size = size;
1878 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1879 rdev->mddev->size = size;
1880 return len;
1881}
1882
1883static struct rdev_sysfs_entry rdev_size =
1884__ATTR(size, 0644, rdev_size_show, rdev_size_store);
1885
NeilBrown86e6ffd2005-11-08 21:39:24 -08001886static struct attribute *rdev_default_attrs[] = {
1887 &rdev_state.attr,
1888 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001889 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001890 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001891 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001892 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001893 NULL,
1894};
1895static ssize_t
1896rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1897{
1898 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1899 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1900
1901 if (!entry->show)
1902 return -EIO;
1903 return entry->show(rdev, page);
1904}
1905
1906static ssize_t
1907rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1908 const char *page, size_t length)
1909{
1910 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1911 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1912
1913 if (!entry->store)
1914 return -EIO;
1915 return entry->store(rdev, page, length);
1916}
1917
1918static void rdev_free(struct kobject *ko)
1919{
1920 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1921 kfree(rdev);
1922}
1923static struct sysfs_ops rdev_sysfs_ops = {
1924 .show = rdev_attr_show,
1925 .store = rdev_attr_store,
1926};
1927static struct kobj_type rdev_ktype = {
1928 .release = rdev_free,
1929 .sysfs_ops = &rdev_sysfs_ops,
1930 .default_attrs = rdev_default_attrs,
1931};
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933/*
1934 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1935 *
1936 * mark the device faulty if:
1937 *
1938 * - the device is nonexistent (zero size)
1939 * - the device has no valid superblock
1940 *
1941 * a faulty rdev _never_ has rdev->sb set.
1942 */
1943static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1944{
1945 char b[BDEVNAME_SIZE];
1946 int err;
1947 mdk_rdev_t *rdev;
1948 sector_t size;
1949
NeilBrown9ffae0c2006-01-06 00:20:32 -08001950 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (!rdev) {
1952 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1953 return ERR_PTR(-ENOMEM);
1954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 if ((err = alloc_disk_sb(rdev)))
1957 goto abort_free;
1958
1959 err = lock_rdev(rdev, newdev);
1960 if (err)
1961 goto abort_free;
1962
NeilBrown86e6ffd2005-11-08 21:39:24 -08001963 rdev->kobj.parent = NULL;
1964 rdev->kobj.ktype = &rdev_ktype;
1965 kobject_init(&rdev->kobj);
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 rdev->desc_nr = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001968 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07001970 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001972 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001973 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1976 if (!size) {
1977 printk(KERN_WARNING
1978 "md: %s has zero or unknown size, marking faulty!\n",
1979 bdevname(rdev->bdev,b));
1980 err = -EINVAL;
1981 goto abort_free;
1982 }
1983
1984 if (super_format >= 0) {
1985 err = super_types[super_format].
1986 load_super(rdev, NULL, super_minor);
1987 if (err == -EINVAL) {
1988 printk(KERN_WARNING
1989 "md: %s has invalid sb, not importing!\n",
1990 bdevname(rdev->bdev,b));
1991 goto abort_free;
1992 }
1993 if (err < 0) {
1994 printk(KERN_WARNING
1995 "md: could not read %s's sb, not importing!\n",
1996 bdevname(rdev->bdev,b));
1997 goto abort_free;
1998 }
1999 }
2000 INIT_LIST_HEAD(&rdev->same_set);
2001
2002 return rdev;
2003
2004abort_free:
2005 if (rdev->sb_page) {
2006 if (rdev->bdev)
2007 unlock_rdev(rdev);
2008 free_disk_sb(rdev);
2009 }
2010 kfree(rdev);
2011 return ERR_PTR(err);
2012}
2013
2014/*
2015 * Check a full RAID array for plausibility
2016 */
2017
2018
NeilBrowna757e642005-04-16 15:26:42 -07002019static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 int i;
2022 struct list_head *tmp;
2023 mdk_rdev_t *rdev, *freshest;
2024 char b[BDEVNAME_SIZE];
2025
2026 freshest = NULL;
2027 ITERATE_RDEV(mddev,rdev,tmp)
2028 switch (super_types[mddev->major_version].
2029 load_super(rdev, freshest, mddev->minor_version)) {
2030 case 1:
2031 freshest = rdev;
2032 break;
2033 case 0:
2034 break;
2035 default:
2036 printk( KERN_ERR \
2037 "md: fatal superblock inconsistency in %s"
2038 " -- removing from array\n",
2039 bdevname(rdev->bdev,b));
2040 kick_rdev_from_array(rdev);
2041 }
2042
2043
2044 super_types[mddev->major_version].
2045 validate_super(mddev, freshest);
2046
2047 i = 0;
2048 ITERATE_RDEV(mddev,rdev,tmp) {
2049 if (rdev != freshest)
2050 if (super_types[mddev->major_version].
2051 validate_super(mddev, rdev)) {
2052 printk(KERN_WARNING "md: kicking non-fresh %s"
2053 " from array!\n",
2054 bdevname(rdev->bdev,b));
2055 kick_rdev_from_array(rdev);
2056 continue;
2057 }
2058 if (mddev->level == LEVEL_MULTIPATH) {
2059 rdev->desc_nr = i++;
2060 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002061 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063 }
2064
2065
2066
2067 if (mddev->recovery_cp != MaxSector &&
2068 mddev->level >= 1)
2069 printk(KERN_ERR "md: %s: raid array is not clean"
2070 " -- starting background reconstruction\n",
2071 mdname(mddev));
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
NeilBrowneae17012005-11-08 21:39:23 -08002075static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002076safe_delay_show(mddev_t *mddev, char *page)
2077{
2078 int msec = (mddev->safemode_delay*1000)/HZ;
2079 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2080}
2081static ssize_t
2082safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2083{
2084 int scale=1;
2085 int dot=0;
2086 int i;
2087 unsigned long msec;
2088 char buf[30];
2089 char *e;
2090 /* remove a period, and count digits after it */
2091 if (len >= sizeof(buf))
2092 return -EINVAL;
2093 strlcpy(buf, cbuf, len);
2094 buf[len] = 0;
2095 for (i=0; i<len; i++) {
2096 if (dot) {
2097 if (isdigit(buf[i])) {
2098 buf[i-1] = buf[i];
2099 scale *= 10;
2100 }
2101 buf[i] = 0;
2102 } else if (buf[i] == '.') {
2103 dot=1;
2104 buf[i] = 0;
2105 }
2106 }
2107 msec = simple_strtoul(buf, &e, 10);
2108 if (e == buf || (*e && *e != '\n'))
2109 return -EINVAL;
2110 msec = (msec * 1000) / scale;
2111 if (msec == 0)
2112 mddev->safemode_delay = 0;
2113 else {
2114 mddev->safemode_delay = (msec*HZ)/1000;
2115 if (mddev->safemode_delay == 0)
2116 mddev->safemode_delay = 1;
2117 }
2118 return len;
2119}
2120static struct md_sysfs_entry md_safe_delay =
2121__ATTR(safe_mode_delay, 0644,safe_delay_show, safe_delay_store);
2122
2123static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002124level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002125{
NeilBrown2604b702006-01-06 00:20:36 -08002126 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002127 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002128 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002129 else if (mddev->clevel[0])
2130 return sprintf(page, "%s\n", mddev->clevel);
2131 else if (mddev->level != LEVEL_NONE)
2132 return sprintf(page, "%d\n", mddev->level);
2133 else
2134 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002135}
2136
NeilBrownd9d166c2006-01-06 00:20:51 -08002137static ssize_t
2138level_store(mddev_t *mddev, const char *buf, size_t len)
2139{
2140 int rv = len;
2141 if (mddev->pers)
2142 return -EBUSY;
2143 if (len == 0)
2144 return 0;
2145 if (len >= sizeof(mddev->clevel))
2146 return -ENOSPC;
2147 strncpy(mddev->clevel, buf, len);
2148 if (mddev->clevel[len-1] == '\n')
2149 len--;
2150 mddev->clevel[len] = 0;
2151 mddev->level = LEVEL_NONE;
2152 return rv;
2153}
2154
2155static struct md_sysfs_entry md_level =
2156__ATTR(level, 0644, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002157
NeilBrownd4dbd022006-06-26 00:27:59 -07002158
2159static ssize_t
2160layout_show(mddev_t *mddev, char *page)
2161{
2162 /* just a number, not meaningful for all levels */
2163 return sprintf(page, "%d\n", mddev->layout);
2164}
2165
2166static ssize_t
2167layout_store(mddev_t *mddev, const char *buf, size_t len)
2168{
2169 char *e;
2170 unsigned long n = simple_strtoul(buf, &e, 10);
2171 if (mddev->pers)
2172 return -EBUSY;
2173
2174 if (!*buf || (*e && *e != '\n'))
2175 return -EINVAL;
2176
2177 mddev->layout = n;
2178 return len;
2179}
2180static struct md_sysfs_entry md_layout =
2181__ATTR(layout, 0655, layout_show, layout_store);
2182
2183
NeilBrowneae17012005-11-08 21:39:23 -08002184static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002185raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002186{
NeilBrownbb636542005-11-08 21:39:45 -08002187 if (mddev->raid_disks == 0)
2188 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002189 return sprintf(page, "%d\n", mddev->raid_disks);
2190}
2191
NeilBrownda943b92006-01-06 00:20:54 -08002192static int update_raid_disks(mddev_t *mddev, int raid_disks);
2193
2194static ssize_t
2195raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2196{
2197 /* can only set raid_disks if array is not yet active */
2198 char *e;
2199 int rv = 0;
2200 unsigned long n = simple_strtoul(buf, &e, 10);
2201
2202 if (!*buf || (*e && *e != '\n'))
2203 return -EINVAL;
2204
2205 if (mddev->pers)
2206 rv = update_raid_disks(mddev, n);
2207 else
2208 mddev->raid_disks = n;
2209 return rv ? rv : len;
2210}
2211static struct md_sysfs_entry md_raid_disks =
2212__ATTR(raid_disks, 0644, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002213
NeilBrown24dd4692005-11-08 21:39:26 -08002214static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002215chunk_size_show(mddev_t *mddev, char *page)
2216{
2217 return sprintf(page, "%d\n", mddev->chunk_size);
2218}
2219
2220static ssize_t
2221chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2222{
2223 /* can only set chunk_size if array is not yet active */
2224 char *e;
2225 unsigned long n = simple_strtoul(buf, &e, 10);
2226
2227 if (mddev->pers)
2228 return -EBUSY;
2229 if (!*buf || (*e && *e != '\n'))
2230 return -EINVAL;
2231
2232 mddev->chunk_size = n;
2233 return len;
2234}
2235static struct md_sysfs_entry md_chunk_size =
2236__ATTR(chunk_size, 0644, chunk_size_show, chunk_size_store);
2237
NeilBrowna94213b2006-06-26 00:28:00 -07002238static ssize_t
2239resync_start_show(mddev_t *mddev, char *page)
2240{
2241 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2242}
2243
2244static ssize_t
2245resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2246{
2247 /* can only set chunk_size if array is not yet active */
2248 char *e;
2249 unsigned long long n = simple_strtoull(buf, &e, 10);
2250
2251 if (mddev->pers)
2252 return -EBUSY;
2253 if (!*buf || (*e && *e != '\n'))
2254 return -EINVAL;
2255
2256 mddev->recovery_cp = n;
2257 return len;
2258}
2259static struct md_sysfs_entry md_resync_start =
2260__ATTR(resync_start, 0644, resync_start_show, resync_start_store);
2261
NeilBrown9e653b62006-06-26 00:27:58 -07002262/*
2263 * The array state can be:
2264 *
2265 * clear
2266 * No devices, no size, no level
2267 * Equivalent to STOP_ARRAY ioctl
2268 * inactive
2269 * May have some settings, but array is not active
2270 * all IO results in error
2271 * When written, doesn't tear down array, but just stops it
2272 * suspended (not supported yet)
2273 * All IO requests will block. The array can be reconfigured.
2274 * Writing this, if accepted, will block until array is quiessent
2275 * readonly
2276 * no resync can happen. no superblocks get written.
2277 * write requests fail
2278 * read-auto
2279 * like readonly, but behaves like 'clean' on a write request.
2280 *
2281 * clean - no pending writes, but otherwise active.
2282 * When written to inactive array, starts without resync
2283 * If a write request arrives then
2284 * if metadata is known, mark 'dirty' and switch to 'active'.
2285 * if not known, block and switch to write-pending
2286 * If written to an active array that has pending writes, then fails.
2287 * active
2288 * fully active: IO and resync can be happening.
2289 * When written to inactive array, starts with resync
2290 *
2291 * write-pending
2292 * clean, but writes are blocked waiting for 'active' to be written.
2293 *
2294 * active-idle
2295 * like active, but no writes have been seen for a while (100msec).
2296 *
2297 */
2298enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2299 write_pending, active_idle, bad_word};
2300char *array_states[] = {
2301 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2302 "write-pending", "active-idle", NULL };
2303
2304static int match_word(const char *word, char **list)
2305{
2306 int n;
2307 for (n=0; list[n]; n++)
2308 if (cmd_match(word, list[n]))
2309 break;
2310 return n;
2311}
2312
2313static ssize_t
2314array_state_show(mddev_t *mddev, char *page)
2315{
2316 enum array_state st = inactive;
2317
2318 if (mddev->pers)
2319 switch(mddev->ro) {
2320 case 1:
2321 st = readonly;
2322 break;
2323 case 2:
2324 st = read_auto;
2325 break;
2326 case 0:
2327 if (mddev->in_sync)
2328 st = clean;
2329 else if (mddev->safemode)
2330 st = active_idle;
2331 else
2332 st = active;
2333 }
2334 else {
2335 if (list_empty(&mddev->disks) &&
2336 mddev->raid_disks == 0 &&
2337 mddev->size == 0)
2338 st = clear;
2339 else
2340 st = inactive;
2341 }
2342 return sprintf(page, "%s\n", array_states[st]);
2343}
2344
2345static int do_md_stop(mddev_t * mddev, int ro);
2346static int do_md_run(mddev_t * mddev);
2347static int restart_array(mddev_t *mddev);
2348
2349static ssize_t
2350array_state_store(mddev_t *mddev, const char *buf, size_t len)
2351{
2352 int err = -EINVAL;
2353 enum array_state st = match_word(buf, array_states);
2354 switch(st) {
2355 case bad_word:
2356 break;
2357 case clear:
2358 /* stopping an active array */
2359 if (mddev->pers) {
2360 if (atomic_read(&mddev->active) > 1)
2361 return -EBUSY;
2362 err = do_md_stop(mddev, 0);
2363 }
2364 break;
2365 case inactive:
2366 /* stopping an active array */
2367 if (mddev->pers) {
2368 if (atomic_read(&mddev->active) > 1)
2369 return -EBUSY;
2370 err = do_md_stop(mddev, 2);
2371 }
2372 break;
2373 case suspended:
2374 break; /* not supported yet */
2375 case readonly:
2376 if (mddev->pers)
2377 err = do_md_stop(mddev, 1);
2378 else {
2379 mddev->ro = 1;
2380 err = do_md_run(mddev);
2381 }
2382 break;
2383 case read_auto:
2384 /* stopping an active array */
2385 if (mddev->pers) {
2386 err = do_md_stop(mddev, 1);
2387 if (err == 0)
2388 mddev->ro = 2; /* FIXME mark devices writable */
2389 } else {
2390 mddev->ro = 2;
2391 err = do_md_run(mddev);
2392 }
2393 break;
2394 case clean:
2395 if (mddev->pers) {
2396 restart_array(mddev);
2397 spin_lock_irq(&mddev->write_lock);
2398 if (atomic_read(&mddev->writes_pending) == 0) {
2399 mddev->in_sync = 1;
2400 mddev->sb_dirty = 1;
2401 }
2402 spin_unlock_irq(&mddev->write_lock);
2403 } else {
2404 mddev->ro = 0;
2405 mddev->recovery_cp = MaxSector;
2406 err = do_md_run(mddev);
2407 }
2408 break;
2409 case active:
2410 if (mddev->pers) {
2411 restart_array(mddev);
2412 mddev->sb_dirty = 0;
2413 wake_up(&mddev->sb_wait);
2414 err = 0;
2415 } else {
2416 mddev->ro = 0;
2417 err = do_md_run(mddev);
2418 }
2419 break;
2420 case write_pending:
2421 case active_idle:
2422 /* these cannot be set */
2423 break;
2424 }
2425 if (err)
2426 return err;
2427 else
2428 return len;
2429}
2430static struct md_sysfs_entry md_array_state = __ATTR(array_state, 0644, array_state_show, array_state_store);
2431
NeilBrown6d7ff732006-01-06 00:21:16 -08002432static ssize_t
2433null_show(mddev_t *mddev, char *page)
2434{
2435 return -EINVAL;
2436}
2437
2438static ssize_t
2439new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2440{
2441 /* buf must be %d:%d\n? giving major and minor numbers */
2442 /* The new device is added to the array.
2443 * If the array has a persistent superblock, we read the
2444 * superblock to initialise info and check validity.
2445 * Otherwise, only checking done is that in bind_rdev_to_array,
2446 * which mainly checks size.
2447 */
2448 char *e;
2449 int major = simple_strtoul(buf, &e, 10);
2450 int minor;
2451 dev_t dev;
2452 mdk_rdev_t *rdev;
2453 int err;
2454
2455 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2456 return -EINVAL;
2457 minor = simple_strtoul(e+1, &e, 10);
2458 if (*e && *e != '\n')
2459 return -EINVAL;
2460 dev = MKDEV(major, minor);
2461 if (major != MAJOR(dev) ||
2462 minor != MINOR(dev))
2463 return -EOVERFLOW;
2464
2465
2466 if (mddev->persistent) {
2467 rdev = md_import_device(dev, mddev->major_version,
2468 mddev->minor_version);
2469 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2470 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2471 mdk_rdev_t, same_set);
2472 err = super_types[mddev->major_version]
2473 .load_super(rdev, rdev0, mddev->minor_version);
2474 if (err < 0)
2475 goto out;
2476 }
2477 } else
2478 rdev = md_import_device(dev, -1, -1);
2479
2480 if (IS_ERR(rdev))
2481 return PTR_ERR(rdev);
2482 err = bind_rdev_to_array(rdev, mddev);
2483 out:
2484 if (err)
2485 export_rdev(rdev);
2486 return err ? err : len;
2487}
2488
2489static struct md_sysfs_entry md_new_device =
2490__ATTR(new_dev, 0200, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002491
2492static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002493size_show(mddev_t *mddev, char *page)
2494{
2495 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2496}
2497
2498static int update_size(mddev_t *mddev, unsigned long size);
2499
2500static ssize_t
2501size_store(mddev_t *mddev, const char *buf, size_t len)
2502{
2503 /* If array is inactive, we can reduce the component size, but
2504 * not increase it (except from 0).
2505 * If array is active, we can try an on-line resize
2506 */
2507 char *e;
2508 int err = 0;
2509 unsigned long long size = simple_strtoull(buf, &e, 10);
2510 if (!*buf || *buf == '\n' ||
2511 (*e && *e != '\n'))
2512 return -EINVAL;
2513
2514 if (mddev->pers) {
2515 err = update_size(mddev, size);
2516 md_update_sb(mddev);
2517 } else {
2518 if (mddev->size == 0 ||
2519 mddev->size > size)
2520 mddev->size = size;
2521 else
2522 err = -ENOSPC;
2523 }
2524 return err ? err : len;
2525}
2526
2527static struct md_sysfs_entry md_size =
2528__ATTR(component_size, 0644, size_show, size_store);
2529
NeilBrown8bb93aa2006-01-06 00:20:50 -08002530
2531/* Metdata version.
2532 * This is either 'none' for arrays with externally managed metadata,
2533 * or N.M for internally known formats
2534 */
2535static ssize_t
2536metadata_show(mddev_t *mddev, char *page)
2537{
2538 if (mddev->persistent)
2539 return sprintf(page, "%d.%d\n",
2540 mddev->major_version, mddev->minor_version);
2541 else
2542 return sprintf(page, "none\n");
2543}
2544
2545static ssize_t
2546metadata_store(mddev_t *mddev, const char *buf, size_t len)
2547{
2548 int major, minor;
2549 char *e;
2550 if (!list_empty(&mddev->disks))
2551 return -EBUSY;
2552
2553 if (cmd_match(buf, "none")) {
2554 mddev->persistent = 0;
2555 mddev->major_version = 0;
2556 mddev->minor_version = 90;
2557 return len;
2558 }
2559 major = simple_strtoul(buf, &e, 10);
2560 if (e==buf || *e != '.')
2561 return -EINVAL;
2562 buf = e+1;
2563 minor = simple_strtoul(buf, &e, 10);
2564 if (e==buf || *e != '\n')
2565 return -EINVAL;
2566 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2567 super_types[major].name == NULL)
2568 return -ENOENT;
2569 mddev->major_version = major;
2570 mddev->minor_version = minor;
2571 mddev->persistent = 1;
2572 return len;
2573}
2574
2575static struct md_sysfs_entry md_metadata =
2576__ATTR(metadata_version, 0644, metadata_show, metadata_store);
2577
NeilBrowna35b0d62006-01-06 00:20:49 -08002578static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002579action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002580{
NeilBrown7eec3142005-11-08 21:39:44 -08002581 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002582 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2583 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002584 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2585 type = "reshape";
2586 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002587 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2588 type = "resync";
2589 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2590 type = "check";
2591 else
2592 type = "repair";
2593 } else
2594 type = "recover";
2595 }
2596 return sprintf(page, "%s\n", type);
2597}
2598
2599static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002600action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002601{
NeilBrown7eec3142005-11-08 21:39:44 -08002602 if (!mddev->pers || !mddev->pers->sync_request)
2603 return -EINVAL;
2604
NeilBrownbce74da2006-01-06 00:20:41 -08002605 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002606 if (mddev->sync_thread) {
2607 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2608 md_unregister_thread(mddev->sync_thread);
2609 mddev->sync_thread = NULL;
2610 mddev->recovery = 0;
2611 }
NeilBrown03c902e2006-01-06 00:20:46 -08002612 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2613 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002614 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002615 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002616 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002617 else if (cmd_match(page, "reshape")) {
2618 int err;
2619 if (mddev->pers->start_reshape == NULL)
2620 return -EINVAL;
2621 err = mddev->pers->start_reshape(mddev);
2622 if (err)
2623 return err;
2624 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002625 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002626 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002627 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002628 return -EINVAL;
2629 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2630 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002631 }
NeilBrown03c902e2006-01-06 00:20:46 -08002632 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002633 md_wakeup_thread(mddev->thread);
2634 return len;
2635}
2636
NeilBrown9d888832005-11-08 21:39:26 -08002637static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002638mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002639{
2640 return sprintf(page, "%llu\n",
2641 (unsigned long long) mddev->resync_mismatches);
2642}
2643
NeilBrown96de1e62005-11-08 21:39:39 -08002644static struct md_sysfs_entry
NeilBrown7eec3142005-11-08 21:39:44 -08002645md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002646
NeilBrown96de1e62005-11-08 21:39:39 -08002647
2648static struct md_sysfs_entry
2649md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002650
NeilBrown88202a02006-01-06 00:21:36 -08002651static ssize_t
2652sync_min_show(mddev_t *mddev, char *page)
2653{
2654 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2655 mddev->sync_speed_min ? "local": "system");
2656}
2657
2658static ssize_t
2659sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2660{
2661 int min;
2662 char *e;
2663 if (strncmp(buf, "system", 6)==0) {
2664 mddev->sync_speed_min = 0;
2665 return len;
2666 }
2667 min = simple_strtoul(buf, &e, 10);
2668 if (buf == e || (*e && *e != '\n') || min <= 0)
2669 return -EINVAL;
2670 mddev->sync_speed_min = min;
2671 return len;
2672}
2673
2674static struct md_sysfs_entry md_sync_min =
2675__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2676
2677static ssize_t
2678sync_max_show(mddev_t *mddev, char *page)
2679{
2680 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2681 mddev->sync_speed_max ? "local": "system");
2682}
2683
2684static ssize_t
2685sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2686{
2687 int max;
2688 char *e;
2689 if (strncmp(buf, "system", 6)==0) {
2690 mddev->sync_speed_max = 0;
2691 return len;
2692 }
2693 max = simple_strtoul(buf, &e, 10);
2694 if (buf == e || (*e && *e != '\n') || max <= 0)
2695 return -EINVAL;
2696 mddev->sync_speed_max = max;
2697 return len;
2698}
2699
2700static struct md_sysfs_entry md_sync_max =
2701__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2702
2703
2704static ssize_t
2705sync_speed_show(mddev_t *mddev, char *page)
2706{
2707 unsigned long resync, dt, db;
2708 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2709 dt = ((jiffies - mddev->resync_mark) / HZ);
2710 if (!dt) dt++;
2711 db = resync - (mddev->resync_mark_cnt);
2712 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2713}
2714
2715static struct md_sysfs_entry
2716md_sync_speed = __ATTR_RO(sync_speed);
2717
2718static ssize_t
2719sync_completed_show(mddev_t *mddev, char *page)
2720{
2721 unsigned long max_blocks, resync;
2722
2723 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2724 max_blocks = mddev->resync_max_sectors;
2725 else
2726 max_blocks = mddev->size << 1;
2727
2728 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2729 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2730}
2731
2732static struct md_sysfs_entry
2733md_sync_completed = __ATTR_RO(sync_completed);
2734
NeilBrowne464eaf2006-03-27 01:18:14 -08002735static ssize_t
2736suspend_lo_show(mddev_t *mddev, char *page)
2737{
2738 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2739}
2740
2741static ssize_t
2742suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2743{
2744 char *e;
2745 unsigned long long new = simple_strtoull(buf, &e, 10);
2746
2747 if (mddev->pers->quiesce == NULL)
2748 return -EINVAL;
2749 if (buf == e || (*e && *e != '\n'))
2750 return -EINVAL;
2751 if (new >= mddev->suspend_hi ||
2752 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2753 mddev->suspend_lo = new;
2754 mddev->pers->quiesce(mddev, 2);
2755 return len;
2756 } else
2757 return -EINVAL;
2758}
2759static struct md_sysfs_entry md_suspend_lo =
2760__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2761
2762
2763static ssize_t
2764suspend_hi_show(mddev_t *mddev, char *page)
2765{
2766 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2767}
2768
2769static ssize_t
2770suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2771{
2772 char *e;
2773 unsigned long long new = simple_strtoull(buf, &e, 10);
2774
2775 if (mddev->pers->quiesce == NULL)
2776 return -EINVAL;
2777 if (buf == e || (*e && *e != '\n'))
2778 return -EINVAL;
2779 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2780 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2781 mddev->suspend_hi = new;
2782 mddev->pers->quiesce(mddev, 1);
2783 mddev->pers->quiesce(mddev, 0);
2784 return len;
2785 } else
2786 return -EINVAL;
2787}
2788static struct md_sysfs_entry md_suspend_hi =
2789__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2790
2791
NeilBrowneae17012005-11-08 21:39:23 -08002792static struct attribute *md_default_attrs[] = {
2793 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07002794 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002795 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002796 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002797 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07002798 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002799 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08002800 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07002801 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07002802 &md_array_state.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002803 NULL,
2804};
2805
2806static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002807 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002808 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002809 &md_sync_min.attr,
2810 &md_sync_max.attr,
2811 &md_sync_speed.attr,
2812 &md_sync_completed.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08002813 &md_suspend_lo.attr,
2814 &md_suspend_hi.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002815 NULL,
2816};
NeilBrown411036f2005-11-08 21:39:40 -08002817static struct attribute_group md_redundancy_group = {
2818 .name = NULL,
2819 .attrs = md_redundancy_attrs,
2820};
2821
NeilBrowneae17012005-11-08 21:39:23 -08002822
2823static ssize_t
2824md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2825{
2826 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2827 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002828 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002829
2830 if (!entry->show)
2831 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002832 rv = mddev_lock(mddev);
2833 if (!rv) {
2834 rv = entry->show(mddev, page);
2835 mddev_unlock(mddev);
2836 }
NeilBrown96de1e62005-11-08 21:39:39 -08002837 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002838}
2839
2840static ssize_t
2841md_attr_store(struct kobject *kobj, struct attribute *attr,
2842 const char *page, size_t length)
2843{
2844 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2845 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002846 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002847
2848 if (!entry->store)
2849 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002850 rv = mddev_lock(mddev);
2851 if (!rv) {
2852 rv = entry->store(mddev, page, length);
2853 mddev_unlock(mddev);
2854 }
NeilBrown96de1e62005-11-08 21:39:39 -08002855 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002856}
2857
2858static void md_free(struct kobject *ko)
2859{
2860 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2861 kfree(mddev);
2862}
2863
2864static struct sysfs_ops md_sysfs_ops = {
2865 .show = md_attr_show,
2866 .store = md_attr_store,
2867};
2868static struct kobj_type md_ktype = {
2869 .release = md_free,
2870 .sysfs_ops = &md_sysfs_ops,
2871 .default_attrs = md_default_attrs,
2872};
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874int mdp_major = 0;
2875
2876static struct kobject *md_probe(dev_t dev, int *part, void *data)
2877{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002878 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 mddev_t *mddev = mddev_find(dev);
2880 struct gendisk *disk;
2881 int partitioned = (MAJOR(dev) != MD_MAJOR);
2882 int shift = partitioned ? MdpMinorShift : 0;
2883 int unit = MINOR(dev) >> shift;
2884
2885 if (!mddev)
2886 return NULL;
2887
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002888 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002890 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 mddev_put(mddev);
2892 return NULL;
2893 }
2894 disk = alloc_disk(1 << shift);
2895 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002896 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 mddev_put(mddev);
2898 return NULL;
2899 }
2900 disk->major = MAJOR(dev);
2901 disk->first_minor = unit << shift;
2902 if (partitioned) {
2903 sprintf(disk->disk_name, "md_d%d", unit);
2904 sprintf(disk->devfs_name, "md/d%d", unit);
2905 } else {
2906 sprintf(disk->disk_name, "md%d", unit);
2907 sprintf(disk->devfs_name, "md/%d", unit);
2908 }
2909 disk->fops = &md_fops;
2910 disk->private_data = mddev;
2911 disk->queue = mddev->queue;
2912 add_disk(disk);
2913 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002914 mutex_unlock(&disks_mutex);
NeilBrown9c791972005-11-08 21:39:28 -08002915 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08002916 mddev->kobj.k_name = NULL;
2917 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2918 mddev->kobj.ktype = &md_ktype;
2919 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return NULL;
2921}
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923static void md_safemode_timeout(unsigned long data)
2924{
2925 mddev_t *mddev = (mddev_t *) data;
2926
2927 mddev->safemode = 1;
2928 md_wakeup_thread(mddev->thread);
2929}
2930
NeilBrown6ff8d8e2006-01-06 00:20:15 -08002931static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933static int do_md_run(mddev_t * mddev)
2934{
NeilBrown2604b702006-01-06 00:20:36 -08002935 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 int chunk_size;
2937 struct list_head *tmp;
2938 mdk_rdev_t *rdev;
2939 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08002940 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 char b[BDEVNAME_SIZE];
2942
NeilBrowna757e642005-04-16 15:26:42 -07002943 if (list_empty(&mddev->disks))
2944 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947 if (mddev->pers)
2948 return -EBUSY;
2949
2950 /*
2951 * Analyze all RAID superblock(s)
2952 */
NeilBrowna757e642005-04-16 15:26:42 -07002953 if (!mddev->raid_disks)
2954 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
NeilBrown2604b702006-01-06 00:20:36 -08002958 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 if (chunk_size > MAX_CHUNK_SIZE) {
2960 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2961 chunk_size, MAX_CHUNK_SIZE);
2962 return -EINVAL;
2963 }
2964 /*
2965 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2966 */
2967 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07002968 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 return -EINVAL;
2970 }
2971 if (chunk_size < PAGE_SIZE) {
2972 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2973 chunk_size, PAGE_SIZE);
2974 return -EINVAL;
2975 }
2976
2977 /* devices must have minimum size of one chunk */
2978 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002979 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 continue;
2981 if (rdev->size < chunk_size / 1024) {
2982 printk(KERN_WARNING
2983 "md: Dev %s smaller than chunk_size:"
2984 " %lluk < %dk\n",
2985 bdevname(rdev->bdev,b),
2986 (unsigned long long)rdev->size,
2987 chunk_size / 1024);
2988 return -EINVAL;
2989 }
2990 }
2991 }
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08002994 if (mddev->level != LEVEL_NONE)
2995 request_module("md-level-%d", mddev->level);
2996 else if (mddev->clevel[0])
2997 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998#endif
2999
3000 /*
3001 * Drop all container device buffers, from now on
3002 * the only valid external interface is through the md
3003 * device.
3004 * Also find largest hardsector size
3005 */
3006 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003007 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 continue;
3009 sync_blockdev(rdev->bdev);
3010 invalidate_bdev(rdev->bdev, 0);
3011 }
3012
3013 md_probe(mddev->unit, NULL, NULL);
3014 disk = mddev->gendisk;
3015 if (!disk)
3016 return -ENOMEM;
3017
3018 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003019 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003020 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003022 if (mddev->level != LEVEL_NONE)
3023 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3024 mddev->level);
3025 else
3026 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3027 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return -EINVAL;
3029 }
NeilBrown2604b702006-01-06 00:20:36 -08003030 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003032 mddev->level = pers->level;
3033 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
NeilBrownf6705572006-03-27 01:18:11 -08003035 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003036 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003037 /* This personality cannot handle reshaping... */
3038 mddev->pers = NULL;
3039 module_put(pers->owner);
3040 return -EINVAL;
3041 }
3042
NeilBrown657390d2005-08-26 18:34:16 -07003043 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003045 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08003046 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
NeilBrownf91de922005-11-08 21:39:36 -08003048 if (start_readonly)
3049 mddev->ro = 2; /* read-only, but switch on first write */
3050
NeilBrownb15c2e52006-01-06 00:20:16 -08003051 err = mddev->pers->run(mddev);
3052 if (!err && mddev->pers->sync_request) {
3053 err = bitmap_create(mddev);
3054 if (err) {
3055 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3056 mdname(mddev), err);
3057 mddev->pers->stop(mddev);
3058 }
3059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 if (err) {
3061 printk(KERN_ERR "md: pers->run() failed ...\n");
3062 module_put(mddev->pers->owner);
3063 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003064 bitmap_destroy(mddev);
3065 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 }
NeilBrown411036f2005-11-08 21:39:40 -08003067 if (mddev->pers->sync_request)
3068 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08003069 else if (mddev->ro == 2) /* auto-readonly not meaningful */
3070 mddev->ro = 0;
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 atomic_set(&mddev->writes_pending,0);
3073 mddev->safemode = 0;
3074 mddev->safemode_timer.function = md_safemode_timeout;
3075 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003076 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003078
3079 ITERATE_RDEV(mddev,rdev,tmp)
3080 if (rdev->raid_disk >= 0) {
3081 char nm[20];
3082 sprintf(nm, "rd%d", rdev->raid_disk);
3083 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
3086 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003087 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089 if (mddev->sb_dirty)
3090 md_update_sb(mddev);
3091
3092 set_capacity(disk, mddev->array_size<<1);
3093
3094 /* If we call blk_queue_make_request here, it will
3095 * re-initialise max_sectors etc which may have been
3096 * refined inside -> run. So just set the bits we need to set.
3097 * Most initialisation happended when we called
3098 * blk_queue_make_request(..., md_fail_request)
3099 * earlier.
3100 */
3101 mddev->queue->queuedata = mddev;
3102 mddev->queue->make_request_fn = mddev->pers->make_request;
3103
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003104 /* If there is a partially-recovered drive we need to
3105 * start recovery here. If we leave it to md_check_recovery,
3106 * it will remove the drives and not do the right thing
3107 */
3108 if (mddev->degraded) {
3109 struct list_head *rtmp;
3110 int spares = 0;
3111 ITERATE_RDEV(mddev,rdev,rtmp)
3112 if (rdev->raid_disk >= 0 &&
3113 !test_bit(In_sync, &rdev->flags) &&
3114 !test_bit(Faulty, &rdev->flags))
3115 /* complete an interrupted recovery */
3116 spares++;
3117 if (spares && mddev->pers->sync_request) {
3118 mddev->recovery = 0;
3119 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3120 mddev->sync_thread = md_register_thread(md_do_sync,
3121 mddev,
3122 "%s_resync");
3123 if (!mddev->sync_thread) {
3124 printk(KERN_ERR "%s: could not start resync"
3125 " thread...\n",
3126 mdname(mddev));
3127 /* leave the spares where they are, it shouldn't hurt */
3128 mddev->recovery = 0;
3129 } else
3130 md_wakeup_thread(mddev->sync_thread);
3131 }
3132 }
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003135 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 return 0;
3137}
3138
3139static int restart_array(mddev_t *mddev)
3140{
3141 struct gendisk *disk = mddev->gendisk;
3142 int err;
3143
3144 /*
3145 * Complain if it has no devices
3146 */
3147 err = -ENXIO;
3148 if (list_empty(&mddev->disks))
3149 goto out;
3150
3151 if (mddev->pers) {
3152 err = -EBUSY;
3153 if (!mddev->ro)
3154 goto out;
3155
3156 mddev->safemode = 0;
3157 mddev->ro = 0;
3158 set_disk_ro(disk, 0);
3159
3160 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3161 mdname(mddev));
3162 /*
3163 * Kick recovery or resync if necessary
3164 */
3165 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3166 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003167 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003169 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172out:
3173 return err;
3174}
3175
NeilBrownacc55e22006-06-26 00:27:47 -07003176/* similar to deny_write_access, but accounts for our holding a reference
3177 * to the file ourselves */
3178static int deny_bitmap_write_access(struct file * file)
3179{
3180 struct inode *inode = file->f_mapping->host;
3181
3182 spin_lock(&inode->i_lock);
3183 if (atomic_read(&inode->i_writecount) > 1) {
3184 spin_unlock(&inode->i_lock);
3185 return -ETXTBSY;
3186 }
3187 atomic_set(&inode->i_writecount, -1);
3188 spin_unlock(&inode->i_lock);
3189
3190 return 0;
3191}
3192
3193static void restore_bitmap_write_access(struct file *file)
3194{
3195 struct inode *inode = file->f_mapping->host;
3196
3197 spin_lock(&inode->i_lock);
3198 atomic_set(&inode->i_writecount, 1);
3199 spin_unlock(&inode->i_lock);
3200}
3201
NeilBrown9e653b62006-06-26 00:27:58 -07003202/* mode:
3203 * 0 - completely stop and dis-assemble array
3204 * 1 - switch to readonly
3205 * 2 - stop but do not disassemble array
3206 */
3207static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
3209 int err = 0;
3210 struct gendisk *disk = mddev->gendisk;
3211
3212 if (mddev->pers) {
3213 if (atomic_read(&mddev->active)>2) {
3214 printk("md: %s still in use.\n",mdname(mddev));
3215 return -EBUSY;
3216 }
3217
3218 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003219 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3221 md_unregister_thread(mddev->sync_thread);
3222 mddev->sync_thread = NULL;
3223 }
3224
3225 del_timer_sync(&mddev->safemode_timer);
3226
3227 invalidate_partition(disk, 0);
3228
NeilBrown9e653b62006-06-26 00:27:58 -07003229 switch(mode) {
3230 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003232 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 goto out;
3234 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003235 break;
3236 case 0: /* disassemble */
3237 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003238 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003239 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (mddev->ro)
3241 set_disk_ro(disk, 0);
3242 blk_queue_make_request(mddev->queue, md_fail_request);
3243 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08003244 if (mddev->pers->sync_request)
3245 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3246
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 module_put(mddev->pers->owner);
3248 mddev->pers = NULL;
3249 if (mddev->ro)
3250 mddev->ro = 0;
3251 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003252 if (!mddev->in_sync || mddev->sb_dirty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 /* mark array as shutdown cleanly */
3254 mddev->in_sync = 1;
3255 md_update_sb(mddev);
3256 }
NeilBrown9e653b62006-06-26 00:27:58 -07003257 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003259 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 }
NeilBrown32a76272005-06-21 17:17:14 -07003261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 /*
3263 * Free resources if final stop
3264 */
NeilBrown9e653b62006-06-26 00:27:58 -07003265 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003266 mdk_rdev_t *rdev;
3267 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 struct gendisk *disk;
3269 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3270
NeilBrown978f9462006-02-02 14:28:05 -08003271 bitmap_destroy(mddev);
3272 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003273 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003274 fput(mddev->bitmap_file);
3275 mddev->bitmap_file = NULL;
3276 }
3277 mddev->bitmap_offset = 0;
3278
NeilBrown86e6ffd2005-11-08 21:39:24 -08003279 ITERATE_RDEV(mddev,rdev,tmp)
3280 if (rdev->raid_disk >= 0) {
3281 char nm[20];
3282 sprintf(nm, "rd%d", rdev->raid_disk);
3283 sysfs_remove_link(&mddev->kobj, nm);
3284 }
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 export_array(mddev);
3287
3288 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003289 mddev->size = 0;
3290 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003291 mddev->recovery_cp = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 disk = mddev->gendisk;
3294 if (disk)
3295 set_capacity(disk, 0);
3296 mddev->changed = 1;
NeilBrowna8a55c32006-06-26 00:27:39 -07003297 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3299 mdname(mddev));
3300 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003301 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302out:
3303 return err;
3304}
3305
3306static void autorun_array(mddev_t *mddev)
3307{
3308 mdk_rdev_t *rdev;
3309 struct list_head *tmp;
3310 int err;
3311
NeilBrowna757e642005-04-16 15:26:42 -07003312 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 printk(KERN_INFO "md: running: ");
3316
3317 ITERATE_RDEV(mddev,rdev,tmp) {
3318 char b[BDEVNAME_SIZE];
3319 printk("<%s>", bdevname(rdev->bdev,b));
3320 }
3321 printk("\n");
3322
3323 err = do_md_run (mddev);
3324 if (err) {
3325 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3326 do_md_stop (mddev, 0);
3327 }
3328}
3329
3330/*
3331 * lets try to run arrays based on all disks that have arrived
3332 * until now. (those are in pending_raid_disks)
3333 *
3334 * the method: pick the first pending disk, collect all disks with
3335 * the same UUID, remove all from the pending list and put them into
3336 * the 'same_array' list. Then order this list based on superblock
3337 * update time (freshest comes first), kick out 'old' disks and
3338 * compare superblocks. If everything's fine then run it.
3339 *
3340 * If "unit" is allocated, then bump its reference count
3341 */
3342static void autorun_devices(int part)
3343{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 struct list_head *tmp;
3345 mdk_rdev_t *rdev0, *rdev;
3346 mddev_t *mddev;
3347 char b[BDEVNAME_SIZE];
3348
3349 printk(KERN_INFO "md: autorun ...\n");
3350 while (!list_empty(&pending_raid_disks)) {
3351 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003352 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 rdev0 = list_entry(pending_raid_disks.next,
3354 mdk_rdev_t, same_set);
3355
3356 printk(KERN_INFO "md: considering %s ...\n",
3357 bdevname(rdev0->bdev,b));
3358 INIT_LIST_HEAD(&candidates);
3359 ITERATE_RDEV_PENDING(rdev,tmp)
3360 if (super_90_load(rdev, rdev0, 0) >= 0) {
3361 printk(KERN_INFO "md: adding %s ...\n",
3362 bdevname(rdev->bdev,b));
3363 list_move(&rdev->same_set, &candidates);
3364 }
3365 /*
3366 * now we have a set of devices, with all of them having
3367 * mostly sane superblocks. It's time to allocate the
3368 * mddev.
3369 */
3370 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
3371 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3372 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3373 break;
3374 }
3375 if (part)
3376 dev = MKDEV(mdp_major,
3377 rdev0->preferred_minor << MdpMinorShift);
3378 else
3379 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3380
3381 md_probe(dev, NULL, NULL);
3382 mddev = mddev_find(dev);
3383 if (!mddev) {
3384 printk(KERN_ERR
3385 "md: cannot allocate memory for md drive.\n");
3386 break;
3387 }
3388 if (mddev_lock(mddev))
3389 printk(KERN_WARNING "md: %s locked, cannot run\n",
3390 mdname(mddev));
3391 else if (mddev->raid_disks || mddev->major_version
3392 || !list_empty(&mddev->disks)) {
3393 printk(KERN_WARNING
3394 "md: %s already running, cannot run %s\n",
3395 mdname(mddev), bdevname(rdev0->bdev,b));
3396 mddev_unlock(mddev);
3397 } else {
3398 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3399 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3400 list_del_init(&rdev->same_set);
3401 if (bind_rdev_to_array(rdev, mddev))
3402 export_rdev(rdev);
3403 }
3404 autorun_array(mddev);
3405 mddev_unlock(mddev);
3406 }
3407 /* on success, candidates will be empty, on error
3408 * it won't...
3409 */
3410 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3411 export_rdev(rdev);
3412 mddev_put(mddev);
3413 }
3414 printk(KERN_INFO "md: ... autorun DONE.\n");
3415}
3416
3417/*
3418 * import RAID devices based on one partition
3419 * if possible, the array gets run as well.
3420 */
3421
3422static int autostart_array(dev_t startdev)
3423{
3424 char b[BDEVNAME_SIZE];
3425 int err = -EINVAL, i;
3426 mdp_super_t *sb = NULL;
3427 mdk_rdev_t *start_rdev = NULL, *rdev;
3428
3429 start_rdev = md_import_device(startdev, 0, 0);
3430 if (IS_ERR(start_rdev))
3431 return err;
3432
3433
3434 /* NOTE: this can only work for 0.90.0 superblocks */
3435 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
3436 if (sb->major_version != 0 ||
3437 sb->minor_version != 90 ) {
3438 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
3439 export_rdev(start_rdev);
3440 return err;
3441 }
3442
NeilBrownb2d444d2005-11-08 21:39:31 -08003443 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 printk(KERN_WARNING
3445 "md: can not autostart based on faulty %s!\n",
3446 bdevname(start_rdev->bdev,b));
3447 export_rdev(start_rdev);
3448 return err;
3449 }
3450 list_add(&start_rdev->same_set, &pending_raid_disks);
3451
3452 for (i = 0; i < MD_SB_DISKS; i++) {
3453 mdp_disk_t *desc = sb->disks + i;
3454 dev_t dev = MKDEV(desc->major, desc->minor);
3455
3456 if (!dev)
3457 continue;
3458 if (dev == startdev)
3459 continue;
3460 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
3461 continue;
3462 rdev = md_import_device(dev, 0, 0);
3463 if (IS_ERR(rdev))
3464 continue;
3465
3466 list_add(&rdev->same_set, &pending_raid_disks);
3467 }
3468
3469 /*
3470 * possibly return codes
3471 */
3472 autorun_devices(0);
3473 return 0;
3474
3475}
3476
3477
3478static int get_version(void __user * arg)
3479{
3480 mdu_version_t ver;
3481
3482 ver.major = MD_MAJOR_VERSION;
3483 ver.minor = MD_MINOR_VERSION;
3484 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3485
3486 if (copy_to_user(arg, &ver, sizeof(ver)))
3487 return -EFAULT;
3488
3489 return 0;
3490}
3491
3492static int get_array_info(mddev_t * mddev, void __user * arg)
3493{
3494 mdu_array_info_t info;
3495 int nr,working,active,failed,spare;
3496 mdk_rdev_t *rdev;
3497 struct list_head *tmp;
3498
3499 nr=working=active=failed=spare=0;
3500 ITERATE_RDEV(mddev,rdev,tmp) {
3501 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003502 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 failed++;
3504 else {
3505 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003506 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 active++;
3508 else
3509 spare++;
3510 }
3511 }
3512
3513 info.major_version = mddev->major_version;
3514 info.minor_version = mddev->minor_version;
3515 info.patch_version = MD_PATCHLEVEL_VERSION;
3516 info.ctime = mddev->ctime;
3517 info.level = mddev->level;
3518 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003519 if (info.size != mddev->size) /* overflow */
3520 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 info.nr_disks = nr;
3522 info.raid_disks = mddev->raid_disks;
3523 info.md_minor = mddev->md_minor;
3524 info.not_persistent= !mddev->persistent;
3525
3526 info.utime = mddev->utime;
3527 info.state = 0;
3528 if (mddev->in_sync)
3529 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003530 if (mddev->bitmap && mddev->bitmap_offset)
3531 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 info.active_disks = active;
3533 info.working_disks = working;
3534 info.failed_disks = failed;
3535 info.spare_disks = spare;
3536
3537 info.layout = mddev->layout;
3538 info.chunk_size = mddev->chunk_size;
3539
3540 if (copy_to_user(arg, &info, sizeof(info)))
3541 return -EFAULT;
3542
3543 return 0;
3544}
3545
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003546static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003547{
3548 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3549 char *ptr, *buf = NULL;
3550 int err = -ENOMEM;
3551
3552 file = kmalloc(sizeof(*file), GFP_KERNEL);
3553 if (!file)
3554 goto out;
3555
3556 /* bitmap disabled, zero the first byte and copy out */
3557 if (!mddev->bitmap || !mddev->bitmap->file) {
3558 file->pathname[0] = '\0';
3559 goto copy_out;
3560 }
3561
3562 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3563 if (!buf)
3564 goto out;
3565
3566 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3567 if (!ptr)
3568 goto out;
3569
3570 strcpy(file->pathname, ptr);
3571
3572copy_out:
3573 err = 0;
3574 if (copy_to_user(arg, file, sizeof(*file)))
3575 err = -EFAULT;
3576out:
3577 kfree(buf);
3578 kfree(file);
3579 return err;
3580}
3581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582static int get_disk_info(mddev_t * mddev, void __user * arg)
3583{
3584 mdu_disk_info_t info;
3585 unsigned int nr;
3586 mdk_rdev_t *rdev;
3587
3588 if (copy_from_user(&info, arg, sizeof(info)))
3589 return -EFAULT;
3590
3591 nr = info.number;
3592
3593 rdev = find_rdev_nr(mddev, nr);
3594 if (rdev) {
3595 info.major = MAJOR(rdev->bdev->bd_dev);
3596 info.minor = MINOR(rdev->bdev->bd_dev);
3597 info.raid_disk = rdev->raid_disk;
3598 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003599 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003601 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 info.state |= (1<<MD_DISK_ACTIVE);
3603 info.state |= (1<<MD_DISK_SYNC);
3604 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003605 if (test_bit(WriteMostly, &rdev->flags))
3606 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 } else {
3608 info.major = info.minor = 0;
3609 info.raid_disk = -1;
3610 info.state = (1<<MD_DISK_REMOVED);
3611 }
3612
3613 if (copy_to_user(arg, &info, sizeof(info)))
3614 return -EFAULT;
3615
3616 return 0;
3617}
3618
3619static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3620{
3621 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3622 mdk_rdev_t *rdev;
3623 dev_t dev = MKDEV(info->major,info->minor);
3624
3625 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3626 return -EOVERFLOW;
3627
3628 if (!mddev->raid_disks) {
3629 int err;
3630 /* expecting a device which has a superblock */
3631 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3632 if (IS_ERR(rdev)) {
3633 printk(KERN_WARNING
3634 "md: md_import_device returned %ld\n",
3635 PTR_ERR(rdev));
3636 return PTR_ERR(rdev);
3637 }
3638 if (!list_empty(&mddev->disks)) {
3639 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3640 mdk_rdev_t, same_set);
3641 int err = super_types[mddev->major_version]
3642 .load_super(rdev, rdev0, mddev->minor_version);
3643 if (err < 0) {
3644 printk(KERN_WARNING
3645 "md: %s has different UUID to %s\n",
3646 bdevname(rdev->bdev,b),
3647 bdevname(rdev0->bdev,b2));
3648 export_rdev(rdev);
3649 return -EINVAL;
3650 }
3651 }
3652 err = bind_rdev_to_array(rdev, mddev);
3653 if (err)
3654 export_rdev(rdev);
3655 return err;
3656 }
3657
3658 /*
3659 * add_new_disk can be used once the array is assembled
3660 * to add "hot spares". They must already have a superblock
3661 * written
3662 */
3663 if (mddev->pers) {
3664 int err;
3665 if (!mddev->pers->hot_add_disk) {
3666 printk(KERN_WARNING
3667 "%s: personality does not support diskops!\n",
3668 mdname(mddev));
3669 return -EINVAL;
3670 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003671 if (mddev->persistent)
3672 rdev = md_import_device(dev, mddev->major_version,
3673 mddev->minor_version);
3674 else
3675 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 if (IS_ERR(rdev)) {
3677 printk(KERN_WARNING
3678 "md: md_import_device returned %ld\n",
3679 PTR_ERR(rdev));
3680 return PTR_ERR(rdev);
3681 }
NeilBrown41158c72005-06-21 17:17:25 -07003682 /* set save_raid_disk if appropriate */
3683 if (!mddev->persistent) {
3684 if (info->state & (1<<MD_DISK_SYNC) &&
3685 info->raid_disk < mddev->raid_disks)
3686 rdev->raid_disk = info->raid_disk;
3687 else
3688 rdev->raid_disk = -1;
3689 } else
3690 super_types[mddev->major_version].
3691 validate_super(mddev, rdev);
3692 rdev->saved_raid_disk = rdev->raid_disk;
3693
NeilBrownb2d444d2005-11-08 21:39:31 -08003694 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003695 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3696 set_bit(WriteMostly, &rdev->flags);
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 rdev->raid_disk = -1;
3699 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07003700 if (!err && !mddev->pers->hot_remove_disk) {
3701 /* If there is hot_add_disk but no hot_remove_disk
3702 * then added disks for geometry changes,
3703 * and should be added immediately.
3704 */
3705 super_types[mddev->major_version].
3706 validate_super(mddev, rdev);
3707 err = mddev->pers->hot_add_disk(mddev, rdev);
3708 if (err)
3709 unbind_rdev_from_array(rdev);
3710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 if (err)
3712 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003713
3714 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003715 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 return err;
3717 }
3718
3719 /* otherwise, add_new_disk is only allowed
3720 * for major_version==0 superblocks
3721 */
3722 if (mddev->major_version != 0) {
3723 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3724 mdname(mddev));
3725 return -EINVAL;
3726 }
3727
3728 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3729 int err;
3730 rdev = md_import_device (dev, -1, 0);
3731 if (IS_ERR(rdev)) {
3732 printk(KERN_WARNING
3733 "md: error, md_import_device() returned %ld\n",
3734 PTR_ERR(rdev));
3735 return PTR_ERR(rdev);
3736 }
3737 rdev->desc_nr = info->number;
3738 if (info->raid_disk < mddev->raid_disks)
3739 rdev->raid_disk = info->raid_disk;
3740 else
3741 rdev->raid_disk = -1;
3742
NeilBrownb2d444d2005-11-08 21:39:31 -08003743 rdev->flags = 0;
3744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003746 if (info->state & (1<<MD_DISK_SYNC))
3747 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003749 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3750 set_bit(WriteMostly, &rdev->flags);
3751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 if (!mddev->persistent) {
3753 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3754 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3755 } else
3756 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3757 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3758
NeilBrown2bf071b2006-01-06 00:20:55 -08003759 err = bind_rdev_to_array(rdev, mddev);
3760 if (err) {
3761 export_rdev(rdev);
3762 return err;
3763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 }
3765
3766 return 0;
3767}
3768
3769static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3770{
3771 char b[BDEVNAME_SIZE];
3772 mdk_rdev_t *rdev;
3773
3774 if (!mddev->pers)
3775 return -ENODEV;
3776
3777 rdev = find_rdev(mddev, dev);
3778 if (!rdev)
3779 return -ENXIO;
3780
3781 if (rdev->raid_disk >= 0)
3782 goto busy;
3783
3784 kick_rdev_from_array(rdev);
3785 md_update_sb(mddev);
NeilBrownd7603b72006-01-06 00:20:30 -08003786 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
3788 return 0;
3789busy:
3790 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3791 bdevname(rdev->bdev,b), mdname(mddev));
3792 return -EBUSY;
3793}
3794
3795static int hot_add_disk(mddev_t * mddev, dev_t dev)
3796{
3797 char b[BDEVNAME_SIZE];
3798 int err;
3799 unsigned int size;
3800 mdk_rdev_t *rdev;
3801
3802 if (!mddev->pers)
3803 return -ENODEV;
3804
3805 if (mddev->major_version != 0) {
3806 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3807 " version-0 superblocks.\n",
3808 mdname(mddev));
3809 return -EINVAL;
3810 }
3811 if (!mddev->pers->hot_add_disk) {
3812 printk(KERN_WARNING
3813 "%s: personality does not support diskops!\n",
3814 mdname(mddev));
3815 return -EINVAL;
3816 }
3817
3818 rdev = md_import_device (dev, -1, 0);
3819 if (IS_ERR(rdev)) {
3820 printk(KERN_WARNING
3821 "md: error, md_import_device() returned %ld\n",
3822 PTR_ERR(rdev));
3823 return -EINVAL;
3824 }
3825
3826 if (mddev->persistent)
3827 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3828 else
3829 rdev->sb_offset =
3830 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3831
3832 size = calc_dev_size(rdev, mddev->chunk_size);
3833 rdev->size = size;
3834
NeilBrownb2d444d2005-11-08 21:39:31 -08003835 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 printk(KERN_WARNING
3837 "md: can not hot-add faulty %s disk to %s!\n",
3838 bdevname(rdev->bdev,b), mdname(mddev));
3839 err = -EINVAL;
3840 goto abort_export;
3841 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003842 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 rdev->desc_nr = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08003844 err = bind_rdev_to_array(rdev, mddev);
3845 if (err)
3846 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
3848 /*
3849 * The rest should better be atomic, we can have disk failures
3850 * noticed in interrupt contexts ...
3851 */
3852
3853 if (rdev->desc_nr == mddev->max_disks) {
3854 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3855 mdname(mddev));
3856 err = -EBUSY;
3857 goto abort_unbind_export;
3858 }
3859
3860 rdev->raid_disk = -1;
3861
3862 md_update_sb(mddev);
3863
3864 /*
3865 * Kick recovery, maybe this spare has to be added to the
3866 * array immediately.
3867 */
3868 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3869 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08003870 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 return 0;
3872
3873abort_unbind_export:
3874 unbind_rdev_from_array(rdev);
3875
3876abort_export:
3877 export_rdev(rdev);
3878 return err;
3879}
3880
NeilBrown32a76272005-06-21 17:17:14 -07003881static int set_bitmap_file(mddev_t *mddev, int fd)
3882{
3883 int err;
3884
NeilBrown36fa3062005-09-09 16:23:45 -07003885 if (mddev->pers) {
3886 if (!mddev->pers->quiesce)
3887 return -EBUSY;
3888 if (mddev->recovery || mddev->sync_thread)
3889 return -EBUSY;
3890 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07003891 }
3892
NeilBrown36fa3062005-09-09 16:23:45 -07003893
3894 if (fd >= 0) {
3895 if (mddev->bitmap)
3896 return -EEXIST; /* cannot add when bitmap is present */
3897 mddev->bitmap_file = fget(fd);
3898
3899 if (mddev->bitmap_file == NULL) {
3900 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3901 mdname(mddev));
3902 return -EBADF;
3903 }
3904
3905 err = deny_bitmap_write_access(mddev->bitmap_file);
3906 if (err) {
3907 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3908 mdname(mddev));
3909 fput(mddev->bitmap_file);
3910 mddev->bitmap_file = NULL;
3911 return err;
3912 }
NeilBrowna654b9d82005-06-21 17:17:27 -07003913 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07003914 } else if (mddev->bitmap == NULL)
3915 return -ENOENT; /* cannot remove what isn't there */
3916 err = 0;
3917 if (mddev->pers) {
3918 mddev->pers->quiesce(mddev, 1);
3919 if (fd >= 0)
3920 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07003921 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07003922 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07003923 fd = -1; /* make sure to put the file */
3924 }
NeilBrown36fa3062005-09-09 16:23:45 -07003925 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07003926 }
3927 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07003928 if (mddev->bitmap_file) {
3929 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07003930 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07003931 }
NeilBrown36fa3062005-09-09 16:23:45 -07003932 mddev->bitmap_file = NULL;
3933 }
3934
NeilBrown32a76272005-06-21 17:17:14 -07003935 return err;
3936}
3937
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938/*
3939 * set_array_info is used two different ways
3940 * The original usage is when creating a new array.
3941 * In this usage, raid_disks is > 0 and it together with
3942 * level, size, not_persistent,layout,chunksize determine the
3943 * shape of the array.
3944 * This will always create an array with a type-0.90.0 superblock.
3945 * The newer usage is when assembling an array.
3946 * In this case raid_disks will be 0, and the major_version field is
3947 * use to determine which style super-blocks are to be found on the devices.
3948 * The minor and patch _version numbers are also kept incase the
3949 * super_block handler wishes to interpret them.
3950 */
3951static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3952{
3953
3954 if (info->raid_disks == 0) {
3955 /* just setting version number for superblock loading */
3956 if (info->major_version < 0 ||
3957 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3958 super_types[info->major_version].name == NULL) {
3959 /* maybe try to auto-load a module? */
3960 printk(KERN_INFO
3961 "md: superblock version %d not known\n",
3962 info->major_version);
3963 return -EINVAL;
3964 }
3965 mddev->major_version = info->major_version;
3966 mddev->minor_version = info->minor_version;
3967 mddev->patch_version = info->patch_version;
3968 return 0;
3969 }
3970 mddev->major_version = MD_MAJOR_VERSION;
3971 mddev->minor_version = MD_MINOR_VERSION;
3972 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3973 mddev->ctime = get_seconds();
3974
3975 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08003976 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 mddev->size = info->size;
3978 mddev->raid_disks = info->raid_disks;
3979 /* don't set md_minor, it is determined by which /dev/md* was
3980 * openned
3981 */
3982 if (info->state & (1<<MD_SB_CLEAN))
3983 mddev->recovery_cp = MaxSector;
3984 else
3985 mddev->recovery_cp = 0;
3986 mddev->persistent = ! info->not_persistent;
3987
3988 mddev->layout = info->layout;
3989 mddev->chunk_size = info->chunk_size;
3990
3991 mddev->max_disks = MD_SB_DISKS;
3992
3993 mddev->sb_dirty = 1;
3994
NeilBrownb2a27032005-11-28 13:44:12 -08003995 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
3996 mddev->bitmap_offset = 0;
3997
NeilBrownf6705572006-03-27 01:18:11 -08003998 mddev->reshape_position = MaxSector;
3999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 /*
4001 * Generate a 128 bit UUID
4002 */
4003 get_random_bytes(mddev->uuid, 16);
4004
NeilBrownf6705572006-03-27 01:18:11 -08004005 mddev->new_level = mddev->level;
4006 mddev->new_chunk = mddev->chunk_size;
4007 mddev->new_layout = mddev->layout;
4008 mddev->delta_disks = 0;
4009
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 return 0;
4011}
4012
NeilBrowna35b0d62006-01-06 00:20:49 -08004013static int update_size(mddev_t *mddev, unsigned long size)
4014{
4015 mdk_rdev_t * rdev;
4016 int rv;
4017 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004018 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004019
4020 if (mddev->pers->resize == NULL)
4021 return -EINVAL;
4022 /* The "size" is the amount of each device that is used.
4023 * This can only make sense for arrays with redundancy.
4024 * linear and raid0 always use whatever space is available
4025 * We can only consider changing the size if no resync
4026 * or reconstruction is happening, and if the new size
4027 * is acceptable. It must fit before the sb_offset or,
4028 * if that is <data_offset, it must fit before the
4029 * size of each device.
4030 * If size is zero, we find the largest size that fits.
4031 */
4032 if (mddev->sync_thread)
4033 return -EBUSY;
4034 ITERATE_RDEV(mddev,rdev,tmp) {
4035 sector_t avail;
NeilBrowna35b0d62006-01-06 00:20:49 -08004036 if (rdev->sb_offset > rdev->data_offset)
4037 avail = (rdev->sb_offset*2) - rdev->data_offset;
4038 else
4039 avail = get_capacity(rdev->bdev->bd_disk)
4040 - rdev->data_offset;
4041 if (fit && (size == 0 || size > avail/2))
4042 size = avail/2;
4043 if (avail < ((sector_t)size << 1))
4044 return -ENOSPC;
4045 }
4046 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4047 if (!rv) {
4048 struct block_device *bdev;
4049
4050 bdev = bdget_disk(mddev->gendisk, 0);
4051 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004052 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004053 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004054 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004055 bdput(bdev);
4056 }
4057 }
4058 return rv;
4059}
4060
NeilBrownda943b92006-01-06 00:20:54 -08004061static int update_raid_disks(mddev_t *mddev, int raid_disks)
4062{
4063 int rv;
4064 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004065 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08004066 return -EINVAL;
4067 if (raid_disks <= 0 ||
4068 raid_disks >= mddev->max_disks)
4069 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004070 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08004071 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004072 mddev->delta_disks = raid_disks - mddev->raid_disks;
4073
4074 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b92006-01-06 00:20:54 -08004075 return rv;
4076}
4077
4078
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079/*
4080 * update_array_info is used to change the configuration of an
4081 * on-line array.
4082 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4083 * fields in the info are checked against the array.
4084 * Any differences that cannot be handled will cause an error.
4085 * Normally, only one change can be managed at a time.
4086 */
4087static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4088{
4089 int rv = 0;
4090 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004091 int state = 0;
4092
4093 /* calculate expected state,ignoring low bits */
4094 if (mddev->bitmap && mddev->bitmap_offset)
4095 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
4097 if (mddev->major_version != info->major_version ||
4098 mddev->minor_version != info->minor_version ||
4099/* mddev->patch_version != info->patch_version || */
4100 mddev->ctime != info->ctime ||
4101 mddev->level != info->level ||
4102/* mddev->layout != info->layout || */
4103 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004104 mddev->chunk_size != info->chunk_size ||
4105 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4106 ((state^info->state) & 0xfffffe00)
4107 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 return -EINVAL;
4109 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004110 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 if (mddev->raid_disks != info->raid_disks) cnt++;
4112 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004113 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 if (cnt == 0) return 0;
4115 if (cnt > 1) return -EINVAL;
4116
4117 if (mddev->layout != info->layout) {
4118 /* Change layout
4119 * we don't need to do anything at the md level, the
4120 * personality will take care of it all.
4121 */
4122 if (mddev->pers->reconfig == NULL)
4123 return -EINVAL;
4124 else
4125 return mddev->pers->reconfig(mddev, info->layout, -1);
4126 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004127 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004128 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
NeilBrownda943b92006-01-06 00:20:54 -08004130 if (mddev->raid_disks != info->raid_disks)
4131 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
NeilBrown36fa3062005-09-09 16:23:45 -07004133 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4134 if (mddev->pers->quiesce == NULL)
4135 return -EINVAL;
4136 if (mddev->recovery || mddev->sync_thread)
4137 return -EBUSY;
4138 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4139 /* add the bitmap */
4140 if (mddev->bitmap)
4141 return -EEXIST;
4142 if (mddev->default_bitmap_offset == 0)
4143 return -EINVAL;
4144 mddev->bitmap_offset = mddev->default_bitmap_offset;
4145 mddev->pers->quiesce(mddev, 1);
4146 rv = bitmap_create(mddev);
4147 if (rv)
4148 bitmap_destroy(mddev);
4149 mddev->pers->quiesce(mddev, 0);
4150 } else {
4151 /* remove the bitmap */
4152 if (!mddev->bitmap)
4153 return -ENOENT;
4154 if (mddev->bitmap->file)
4155 return -EINVAL;
4156 mddev->pers->quiesce(mddev, 1);
4157 bitmap_destroy(mddev);
4158 mddev->pers->quiesce(mddev, 0);
4159 mddev->bitmap_offset = 0;
4160 }
4161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 md_update_sb(mddev);
4163 return rv;
4164}
4165
4166static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4167{
4168 mdk_rdev_t *rdev;
4169
4170 if (mddev->pers == NULL)
4171 return -ENODEV;
4172
4173 rdev = find_rdev(mddev, dev);
4174 if (!rdev)
4175 return -ENODEV;
4176
4177 md_error(mddev, rdev);
4178 return 0;
4179}
4180
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004181static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4182{
4183 mddev_t *mddev = bdev->bd_disk->private_data;
4184
4185 geo->heads = 2;
4186 geo->sectors = 4;
4187 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4188 return 0;
4189}
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191static int md_ioctl(struct inode *inode, struct file *file,
4192 unsigned int cmd, unsigned long arg)
4193{
4194 int err = 0;
4195 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 mddev_t *mddev = NULL;
4197
4198 if (!capable(CAP_SYS_ADMIN))
4199 return -EACCES;
4200
4201 /*
4202 * Commands dealing with the RAID driver but not any
4203 * particular array:
4204 */
4205 switch (cmd)
4206 {
4207 case RAID_VERSION:
4208 err = get_version(argp);
4209 goto done;
4210
4211 case PRINT_RAID_DEBUG:
4212 err = 0;
4213 md_print_devices();
4214 goto done;
4215
4216#ifndef MODULE
4217 case RAID_AUTORUN:
4218 err = 0;
4219 autostart_arrays(arg);
4220 goto done;
4221#endif
4222 default:;
4223 }
4224
4225 /*
4226 * Commands creating/starting a new array:
4227 */
4228
4229 mddev = inode->i_bdev->bd_disk->private_data;
4230
4231 if (!mddev) {
4232 BUG();
4233 goto abort;
4234 }
4235
4236
4237 if (cmd == START_ARRAY) {
4238 /* START_ARRAY doesn't need to lock the array as autostart_array
4239 * does the locking, and it could even be a different array
4240 */
4241 static int cnt = 3;
4242 if (cnt > 0 ) {
4243 printk(KERN_WARNING
4244 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08004245 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 current->comm, current->pid);
4247 cnt--;
4248 }
4249 err = autostart_array(new_decode_dev(arg));
4250 if (err) {
4251 printk(KERN_WARNING "md: autostart failed!\n");
4252 goto abort;
4253 }
4254 goto done;
4255 }
4256
4257 err = mddev_lock(mddev);
4258 if (err) {
4259 printk(KERN_INFO
4260 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4261 err, cmd);
4262 goto abort;
4263 }
4264
4265 switch (cmd)
4266 {
4267 case SET_ARRAY_INFO:
4268 {
4269 mdu_array_info_t info;
4270 if (!arg)
4271 memset(&info, 0, sizeof(info));
4272 else if (copy_from_user(&info, argp, sizeof(info))) {
4273 err = -EFAULT;
4274 goto abort_unlock;
4275 }
4276 if (mddev->pers) {
4277 err = update_array_info(mddev, &info);
4278 if (err) {
4279 printk(KERN_WARNING "md: couldn't update"
4280 " array info. %d\n", err);
4281 goto abort_unlock;
4282 }
4283 goto done_unlock;
4284 }
4285 if (!list_empty(&mddev->disks)) {
4286 printk(KERN_WARNING
4287 "md: array %s already has disks!\n",
4288 mdname(mddev));
4289 err = -EBUSY;
4290 goto abort_unlock;
4291 }
4292 if (mddev->raid_disks) {
4293 printk(KERN_WARNING
4294 "md: array %s already initialised!\n",
4295 mdname(mddev));
4296 err = -EBUSY;
4297 goto abort_unlock;
4298 }
4299 err = set_array_info(mddev, &info);
4300 if (err) {
4301 printk(KERN_WARNING "md: couldn't set"
4302 " array info. %d\n", err);
4303 goto abort_unlock;
4304 }
4305 }
4306 goto done_unlock;
4307
4308 default:;
4309 }
4310
4311 /*
4312 * Commands querying/configuring an existing array:
4313 */
NeilBrown32a76272005-06-21 17:17:14 -07004314 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4315 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
4316 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4317 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 err = -ENODEV;
4319 goto abort_unlock;
4320 }
4321
4322 /*
4323 * Commands even a read-only array can execute:
4324 */
4325 switch (cmd)
4326 {
4327 case GET_ARRAY_INFO:
4328 err = get_array_info(mddev, argp);
4329 goto done_unlock;
4330
NeilBrown32a76272005-06-21 17:17:14 -07004331 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004332 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004333 goto done_unlock;
4334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 case GET_DISK_INFO:
4336 err = get_disk_info(mddev, argp);
4337 goto done_unlock;
4338
4339 case RESTART_ARRAY_RW:
4340 err = restart_array(mddev);
4341 goto done_unlock;
4342
4343 case STOP_ARRAY:
4344 err = do_md_stop (mddev, 0);
4345 goto done_unlock;
4346
4347 case STOP_ARRAY_RO:
4348 err = do_md_stop (mddev, 1);
4349 goto done_unlock;
4350
4351 /*
4352 * We have a problem here : there is no easy way to give a CHS
4353 * virtual geometry. We currently pretend that we have a 2 heads
4354 * 4 sectors (with a BIG number of cylinders...). This drives
4355 * dosfs just mad... ;-)
4356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 }
4358
4359 /*
4360 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004361 * superblock, so we do not allow them on read-only arrays.
4362 * However non-MD ioctls (e.g. get-size) will still come through
4363 * here and hit the 'default' below, so only disallow
4364 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 */
NeilBrownf91de922005-11-08 21:39:36 -08004366 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4367 mddev->ro && mddev->pers) {
4368 if (mddev->ro == 2) {
4369 mddev->ro = 0;
4370 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4371 md_wakeup_thread(mddev->thread);
4372
4373 } else {
4374 err = -EROFS;
4375 goto abort_unlock;
4376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 }
4378
4379 switch (cmd)
4380 {
4381 case ADD_NEW_DISK:
4382 {
4383 mdu_disk_info_t info;
4384 if (copy_from_user(&info, argp, sizeof(info)))
4385 err = -EFAULT;
4386 else
4387 err = add_new_disk(mddev, &info);
4388 goto done_unlock;
4389 }
4390
4391 case HOT_REMOVE_DISK:
4392 err = hot_remove_disk(mddev, new_decode_dev(arg));
4393 goto done_unlock;
4394
4395 case HOT_ADD_DISK:
4396 err = hot_add_disk(mddev, new_decode_dev(arg));
4397 goto done_unlock;
4398
4399 case SET_DISK_FAULTY:
4400 err = set_disk_faulty(mddev, new_decode_dev(arg));
4401 goto done_unlock;
4402
4403 case RUN_ARRAY:
4404 err = do_md_run (mddev);
4405 goto done_unlock;
4406
NeilBrown32a76272005-06-21 17:17:14 -07004407 case SET_BITMAP_FILE:
4408 err = set_bitmap_file(mddev, (int)arg);
4409 goto done_unlock;
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 err = -EINVAL;
4413 goto abort_unlock;
4414 }
4415
4416done_unlock:
4417abort_unlock:
4418 mddev_unlock(mddev);
4419
4420 return err;
4421done:
4422 if (err)
4423 MD_BUG();
4424abort:
4425 return err;
4426}
4427
4428static int md_open(struct inode *inode, struct file *file)
4429{
4430 /*
4431 * Succeed if we can lock the mddev, which confirms that
4432 * it isn't being stopped right now.
4433 */
4434 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4435 int err;
4436
4437 if ((err = mddev_lock(mddev)))
4438 goto out;
4439
4440 err = 0;
4441 mddev_get(mddev);
4442 mddev_unlock(mddev);
4443
4444 check_disk_change(inode->i_bdev);
4445 out:
4446 return err;
4447}
4448
4449static int md_release(struct inode *inode, struct file * file)
4450{
4451 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4452
4453 if (!mddev)
4454 BUG();
4455 mddev_put(mddev);
4456
4457 return 0;
4458}
4459
4460static int md_media_changed(struct gendisk *disk)
4461{
4462 mddev_t *mddev = disk->private_data;
4463
4464 return mddev->changed;
4465}
4466
4467static int md_revalidate(struct gendisk *disk)
4468{
4469 mddev_t *mddev = disk->private_data;
4470
4471 mddev->changed = 0;
4472 return 0;
4473}
4474static struct block_device_operations md_fops =
4475{
4476 .owner = THIS_MODULE,
4477 .open = md_open,
4478 .release = md_release,
4479 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004480 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 .media_changed = md_media_changed,
4482 .revalidate_disk= md_revalidate,
4483};
4484
Adrian Bunk75c96f82005-05-05 16:16:09 -07004485static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486{
4487 mdk_thread_t *thread = arg;
4488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 /*
4490 * md_thread is a 'system-thread', it's priority should be very
4491 * high. We avoid resource deadlocks individually in each
4492 * raid personality. (RAID5 does preallocation) We also use RR and
4493 * the very same RT priority as kswapd, thus we will never get
4494 * into a priority inversion deadlock.
4495 *
4496 * we definitely have to have equal or higher priority than
4497 * bdflush, otherwise bdflush will deadlock if there are too
4498 * many dirty RAID5 blocks.
4499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
NeilBrown6985c432005-10-19 21:23:47 -07004501 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004502 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
NeilBrown93588e22005-11-15 00:09:12 -08004504 /* We need to wait INTERRUPTIBLE so that
4505 * we don't add to the load-average.
4506 * That means we need to be sure no signals are
4507 * pending
4508 */
4509 if (signal_pending(current))
4510 flush_signals(current);
4511
4512 wait_event_interruptible_timeout
4513 (thread->wqueue,
4514 test_bit(THREAD_WAKEUP, &thread->flags)
4515 || kthread_should_stop(),
4516 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07004517 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
4519 clear_bit(THREAD_WAKEUP, &thread->flags);
4520
NeilBrown787453c2005-11-08 21:39:43 -08004521 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004523
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 return 0;
4525}
4526
4527void md_wakeup_thread(mdk_thread_t *thread)
4528{
4529 if (thread) {
4530 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4531 set_bit(THREAD_WAKEUP, &thread->flags);
4532 wake_up(&thread->wqueue);
4533 }
4534}
4535
4536mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4537 const char *name)
4538{
4539 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
NeilBrown9ffae0c2006-01-06 00:20:32 -08004541 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 if (!thread)
4543 return NULL;
4544
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 init_waitqueue_head(&thread->wqueue);
4546
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 thread->run = run;
4548 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004549 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004550 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004551 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 kfree(thread);
4553 return NULL;
4554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 return thread;
4556}
4557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558void md_unregister_thread(mdk_thread_t *thread)
4559{
NeilBrownd28446f2005-04-16 15:26:41 -07004560 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07004561
4562 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 kfree(thread);
4564}
4565
4566void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4567{
4568 if (!mddev) {
4569 MD_BUG();
4570 return;
4571 }
4572
NeilBrownb2d444d2005-11-08 21:39:31 -08004573 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 return;
NeilBrown32a76272005-06-21 17:17:14 -07004575/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4577 mdname(mddev),
4578 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4579 __builtin_return_address(0),__builtin_return_address(1),
4580 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004581*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (!mddev->pers->error_handler)
4583 return;
4584 mddev->pers->error_handler(mddev,rdev);
4585 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4586 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4587 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004588 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589}
4590
4591/* seq_file implementation /proc/mdstat */
4592
4593static void status_unused(struct seq_file *seq)
4594{
4595 int i = 0;
4596 mdk_rdev_t *rdev;
4597 struct list_head *tmp;
4598
4599 seq_printf(seq, "unused devices: ");
4600
4601 ITERATE_RDEV_PENDING(rdev,tmp) {
4602 char b[BDEVNAME_SIZE];
4603 i++;
4604 seq_printf(seq, "%s ",
4605 bdevname(rdev->bdev,b));
4606 }
4607 if (!i)
4608 seq_printf(seq, "<none>");
4609
4610 seq_printf(seq, "\n");
4611}
4612
4613
4614static void status_resync(struct seq_file *seq, mddev_t * mddev)
4615{
NeilBrown4588b422006-03-27 01:18:04 -08004616 sector_t max_blocks, resync, res;
4617 unsigned long dt, db, rt;
4618 int scale;
4619 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
4621 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4622
4623 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4624 max_blocks = mddev->resync_max_sectors >> 1;
4625 else
4626 max_blocks = mddev->size;
4627
4628 /*
4629 * Should not happen.
4630 */
4631 if (!max_blocks) {
4632 MD_BUG();
4633 return;
4634 }
NeilBrown4588b422006-03-27 01:18:04 -08004635 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4636 * in a sector_t, and (max_blocks>>scale) will fit in a
4637 * u32, as those are the requirements for sector_div.
4638 * Thus 'scale' must be at least 10
4639 */
4640 scale = 10;
4641 if (sizeof(sector_t) > sizeof(unsigned long)) {
4642 while ( max_blocks/2 > (1ULL<<(scale+32)))
4643 scale++;
4644 }
4645 res = (resync>>scale)*1000;
4646 sector_div(res, (u32)((max_blocks>>scale)+1));
4647
4648 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 {
NeilBrown4588b422006-03-27 01:18:04 -08004650 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 seq_printf(seq, "[");
4652 for (i = 0; i < x; i++)
4653 seq_printf(seq, "=");
4654 seq_printf(seq, ">");
4655 for (i = 0; i < y; i++)
4656 seq_printf(seq, ".");
4657 seq_printf(seq, "] ");
4658 }
NeilBrown4588b422006-03-27 01:18:04 -08004659 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004660 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4661 "reshape" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
NeilBrownccfcc3c2006-03-27 01:18:09 -08004663 "resync" : "recovery")),
NeilBrown4588b422006-03-27 01:18:04 -08004664 per_milli/10, per_milli % 10,
4665 (unsigned long long) resync,
4666 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
4668 /*
4669 * We do not want to overflow, so the order of operands and
4670 * the * 100 / 100 trick are important. We do a +1 to be
4671 * safe against division by zero. We only estimate anyway.
4672 *
4673 * dt: time from mark until now
4674 * db: blocks written from mark until now
4675 * rt: remaining time
4676 */
4677 dt = ((jiffies - mddev->resync_mark) / HZ);
4678 if (!dt) dt++;
4679 db = resync - (mddev->resync_mark_cnt/2);
NeilBrown4588b422006-03-27 01:18:04 -08004680 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
4682 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4683
4684 seq_printf(seq, " speed=%ldK/sec", db/dt);
4685}
4686
4687static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4688{
4689 struct list_head *tmp;
4690 loff_t l = *pos;
4691 mddev_t *mddev;
4692
4693 if (l >= 0x10000)
4694 return NULL;
4695 if (!l--)
4696 /* header */
4697 return (void*)1;
4698
4699 spin_lock(&all_mddevs_lock);
4700 list_for_each(tmp,&all_mddevs)
4701 if (!l--) {
4702 mddev = list_entry(tmp, mddev_t, all_mddevs);
4703 mddev_get(mddev);
4704 spin_unlock(&all_mddevs_lock);
4705 return mddev;
4706 }
4707 spin_unlock(&all_mddevs_lock);
4708 if (!l--)
4709 return (void*)2;/* tail */
4710 return NULL;
4711}
4712
4713static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4714{
4715 struct list_head *tmp;
4716 mddev_t *next_mddev, *mddev = v;
4717
4718 ++*pos;
4719 if (v == (void*)2)
4720 return NULL;
4721
4722 spin_lock(&all_mddevs_lock);
4723 if (v == (void*)1)
4724 tmp = all_mddevs.next;
4725 else
4726 tmp = mddev->all_mddevs.next;
4727 if (tmp != &all_mddevs)
4728 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4729 else {
4730 next_mddev = (void*)2;
4731 *pos = 0x10000;
4732 }
4733 spin_unlock(&all_mddevs_lock);
4734
4735 if (v != (void*)1)
4736 mddev_put(mddev);
4737 return next_mddev;
4738
4739}
4740
4741static void md_seq_stop(struct seq_file *seq, void *v)
4742{
4743 mddev_t *mddev = v;
4744
4745 if (mddev && v != (void*)1 && v != (void*)2)
4746 mddev_put(mddev);
4747}
4748
NeilBrownd7603b72006-01-06 00:20:30 -08004749struct mdstat_info {
4750 int event;
4751};
4752
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753static int md_seq_show(struct seq_file *seq, void *v)
4754{
4755 mddev_t *mddev = v;
4756 sector_t size;
4757 struct list_head *tmp2;
4758 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004759 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004760 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
4762 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004763 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 seq_printf(seq, "Personalities : ");
4765 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004766 list_for_each_entry(pers, &pers_list, list)
4767 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
4769 spin_unlock(&pers_lock);
4770 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004771 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 return 0;
4773 }
4774 if (v == (void*)2) {
4775 status_unused(seq);
4776 return 0;
4777 }
4778
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004779 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4783 seq_printf(seq, "%s : %sactive", mdname(mddev),
4784 mddev->pers ? "" : "in");
4785 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004786 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004788 if (mddev->ro==2)
4789 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 seq_printf(seq, " %s", mddev->pers->name);
4791 }
4792
4793 size = 0;
4794 ITERATE_RDEV(mddev,rdev,tmp2) {
4795 char b[BDEVNAME_SIZE];
4796 seq_printf(seq, " %s[%d]",
4797 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004798 if (test_bit(WriteMostly, &rdev->flags))
4799 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004800 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 seq_printf(seq, "(F)");
4802 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004803 } else if (rdev->raid_disk < 0)
4804 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 size += rdev->size;
4806 }
4807
4808 if (!list_empty(&mddev->disks)) {
4809 if (mddev->pers)
4810 seq_printf(seq, "\n %llu blocks",
4811 (unsigned long long)mddev->array_size);
4812 else
4813 seq_printf(seq, "\n %llu blocks",
4814 (unsigned long long)size);
4815 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004816 if (mddev->persistent) {
4817 if (mddev->major_version != 0 ||
4818 mddev->minor_version != 90) {
4819 seq_printf(seq," super %d.%d",
4820 mddev->major_version,
4821 mddev->minor_version);
4822 }
4823 } else
4824 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
4826 if (mddev->pers) {
4827 mddev->pers->status (seq, mddev);
4828 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004829 if (mddev->pers->sync_request) {
4830 if (mddev->curr_resync > 2) {
4831 status_resync (seq, mddev);
4832 seq_printf(seq, "\n ");
4833 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4834 seq_printf(seq, "\tresync=DELAYED\n ");
4835 else if (mddev->recovery_cp < MaxSector)
4836 seq_printf(seq, "\tresync=PENDING\n ");
4837 }
NeilBrown32a76272005-06-21 17:17:14 -07004838 } else
4839 seq_printf(seq, "\n ");
4840
4841 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07004842 unsigned long chunk_kb;
4843 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07004844 spin_lock_irqsave(&bitmap->lock, flags);
4845 chunk_kb = bitmap->chunksize >> 10;
4846 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4847 "%lu%s chunk",
4848 bitmap->pages - bitmap->missing_pages,
4849 bitmap->pages,
4850 (bitmap->pages - bitmap->missing_pages)
4851 << (PAGE_SHIFT - 10),
4852 chunk_kb ? chunk_kb : bitmap->chunksize,
4853 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07004854 if (bitmap->file) {
4855 seq_printf(seq, ", file: ");
4856 seq_path(seq, bitmap->file->f_vfsmnt,
4857 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07004858 }
NeilBrown78d742d2005-06-21 17:17:15 -07004859
NeilBrown32a76272005-06-21 17:17:14 -07004860 seq_printf(seq, "\n");
4861 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 }
4863
4864 seq_printf(seq, "\n");
4865 }
4866 mddev_unlock(mddev);
4867
4868 return 0;
4869}
4870
4871static struct seq_operations md_seq_ops = {
4872 .start = md_seq_start,
4873 .next = md_seq_next,
4874 .stop = md_seq_stop,
4875 .show = md_seq_show,
4876};
4877
4878static int md_seq_open(struct inode *inode, struct file *file)
4879{
4880 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08004881 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4882 if (mi == NULL)
4883 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
4885 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08004886 if (error)
4887 kfree(mi);
4888 else {
4889 struct seq_file *p = file->private_data;
4890 p->private = mi;
4891 mi->event = atomic_read(&md_event_count);
4892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 return error;
4894}
4895
NeilBrownd7603b72006-01-06 00:20:30 -08004896static int md_seq_release(struct inode *inode, struct file *file)
4897{
4898 struct seq_file *m = file->private_data;
4899 struct mdstat_info *mi = m->private;
4900 m->private = NULL;
4901 kfree(mi);
4902 return seq_release(inode, file);
4903}
4904
4905static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4906{
4907 struct seq_file *m = filp->private_data;
4908 struct mdstat_info *mi = m->private;
4909 int mask;
4910
4911 poll_wait(filp, &md_event_waiters, wait);
4912
4913 /* always allow read */
4914 mask = POLLIN | POLLRDNORM;
4915
4916 if (mi->event != atomic_read(&md_event_count))
4917 mask |= POLLERR | POLLPRI;
4918 return mask;
4919}
4920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921static struct file_operations md_seq_fops = {
4922 .open = md_seq_open,
4923 .read = seq_read,
4924 .llseek = seq_lseek,
NeilBrownd7603b72006-01-06 00:20:30 -08004925 .release = md_seq_release,
4926 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927};
4928
NeilBrown2604b702006-01-06 00:20:36 -08004929int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004932 list_add_tail(&p->list, &pers_list);
4933 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 spin_unlock(&pers_lock);
4935 return 0;
4936}
4937
NeilBrown2604b702006-01-06 00:20:36 -08004938int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939{
NeilBrown2604b702006-01-06 00:20:36 -08004940 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004942 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 spin_unlock(&pers_lock);
4944 return 0;
4945}
4946
4947static int is_mddev_idle(mddev_t *mddev)
4948{
4949 mdk_rdev_t * rdev;
4950 struct list_head *tmp;
4951 int idle;
4952 unsigned long curr_events;
4953
4954 idle = 1;
4955 ITERATE_RDEV(mddev,rdev,tmp) {
4956 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01004957 curr_events = disk_stat_read(disk, sectors[0]) +
4958 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08004960 /* The difference between curr_events and last_events
4961 * will be affected by any new non-sync IO (making
4962 * curr_events bigger) and any difference in the amount of
4963 * in-flight syncio (making current_events bigger or smaller)
4964 * The amount in-flight is currently limited to
4965 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4966 * which is at most 4096 sectors.
4967 * These numbers are fairly fragile and should be made
4968 * more robust, probably by enforcing the
4969 * 'window size' that md_do_sync sort-of uses.
4970 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 * Note: the following is an unsigned comparison.
4972 */
NeilBrownc0e48522005-11-18 01:11:01 -08004973 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 rdev->last_events = curr_events;
4975 idle = 0;
4976 }
4977 }
4978 return idle;
4979}
4980
4981void md_done_sync(mddev_t *mddev, int blocks, int ok)
4982{
4983 /* another "blocks" (512byte) blocks have been synced */
4984 atomic_sub(blocks, &mddev->recovery_active);
4985 wake_up(&mddev->recovery_wait);
4986 if (!ok) {
4987 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4988 md_wakeup_thread(mddev->thread);
4989 // stop recovery, signal do_sync ....
4990 }
4991}
4992
4993
NeilBrown06d91a52005-06-21 17:17:12 -07004994/* md_write_start(mddev, bi)
4995 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07004996 * in superblock) before writing, schedule a superblock update
4997 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07004998 */
NeilBrown3d310eb2005-06-21 17:17:26 -07004999void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000{
NeilBrown06d91a52005-06-21 17:17:12 -07005001 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005002 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005003
NeilBrownf91de922005-11-08 21:39:36 -08005004 BUG_ON(mddev->ro == 1);
5005 if (mddev->ro == 2) {
5006 /* need to switch to read/write */
5007 mddev->ro = 0;
5008 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5009 md_wakeup_thread(mddev->thread);
5010 }
NeilBrown06d91a52005-06-21 17:17:12 -07005011 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005012 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005013 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005014 if (mddev->in_sync) {
5015 mddev->in_sync = 0;
NeilBrown42543762006-06-26 00:27:57 -07005016 mddev->sb_dirty = 3;
NeilBrown3d310eb2005-06-21 17:17:26 -07005017 md_wakeup_thread(mddev->thread);
5018 }
NeilBrowna9701a32005-11-08 21:39:34 -08005019 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005020 }
NeilBrown3d310eb2005-06-21 17:17:26 -07005021 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022}
5023
5024void md_write_end(mddev_t *mddev)
5025{
5026 if (atomic_dec_and_test(&mddev->writes_pending)) {
5027 if (mddev->safemode == 2)
5028 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005029 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5031 }
5032}
5033
Adrian Bunk75c96f82005-05-05 16:16:09 -07005034static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
5036#define SYNC_MARKS 10
5037#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005038void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039{
5040 mddev_t *mddev2;
5041 unsigned int currspeed = 0,
5042 window;
NeilBrown57afd892005-06-21 17:17:13 -07005043 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 unsigned long mark[SYNC_MARKS];
5045 sector_t mark_cnt[SYNC_MARKS];
5046 int last_mark,m;
5047 struct list_head *tmp;
5048 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005049 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005050 struct list_head *rtmp;
5051 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
5053 /* just incase thread restarts... */
5054 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5055 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005056 if (mddev->ro) /* never try to sync a read-only array */
5057 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058
5059 /* we overload curr_resync somewhat here.
5060 * 0 == not engaged in resync at all
5061 * 2 == checking that there is no conflict with another sync
5062 * 1 == like 2, but have yielded to allow conflicting resync to
5063 * commense
5064 * other == active in resync - this many blocks
5065 *
5066 * Before starting a resync we must have set curr_resync to
5067 * 2, and then checked that every "conflicting" array has curr_resync
5068 * less than ours. When we find one that is the same or higher
5069 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5070 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5071 * This will mean we have to start checking from the beginning again.
5072 *
5073 */
5074
5075 do {
5076 mddev->curr_resync = 2;
5077
5078 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005079 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005080 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 goto skip;
5082 }
5083 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 if (mddev2 == mddev)
5085 continue;
5086 if (mddev2->curr_resync &&
5087 match_mddev_units(mddev,mddev2)) {
5088 DEFINE_WAIT(wq);
5089 if (mddev < mddev2 && mddev->curr_resync == 2) {
5090 /* arbitrarily yield */
5091 mddev->curr_resync = 1;
5092 wake_up(&resync_wait);
5093 }
5094 if (mddev > mddev2 && mddev->curr_resync == 1)
5095 /* no need to wait here, we can wait the next
5096 * time 'round when curr_resync == 2
5097 */
5098 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005099 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5100 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005101 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 printk(KERN_INFO "md: delaying resync of %s"
5103 " until %s has finished resync (they"
5104 " share one or more physical units)\n",
5105 mdname(mddev), mdname(mddev2));
5106 mddev_put(mddev2);
5107 schedule();
5108 finish_wait(&resync_wait, &wq);
5109 goto try_again;
5110 }
5111 finish_wait(&resync_wait, &wq);
5112 }
5113 }
5114 } while (mddev->curr_resync < 2);
5115
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005116 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005117 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005119 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 */
5121 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005122 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005123 /* we don't use the checkpoint if there's a bitmap */
5124 if (!mddev->bitmap &&
5125 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5126 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005127 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5128 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005129 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 /* recovery follows the physical size of devices */
5131 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005132 j = MaxSector;
5133 ITERATE_RDEV(mddev,rdev,rtmp)
5134 if (rdev->raid_disk >= 0 &&
5135 !test_bit(Faulty, &rdev->flags) &&
5136 !test_bit(In_sync, &rdev->flags) &&
5137 rdev->recovery_offset < j)
5138 j = rdev->recovery_offset;
5139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
5141 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
5142 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
NeilBrown88202a02006-01-06 00:21:36 -08005143 " %d KB/sec/disc.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005144 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 "(but not more than %d KB/sec) for reconstruction.\n",
NeilBrown88202a02006-01-06 00:21:36 -08005146 speed_max(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
5148 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005149
NeilBrown57afd892005-06-21 17:17:13 -07005150 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 for (m = 0; m < SYNC_MARKS; m++) {
5152 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005153 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 }
5155 last_mark = 0;
5156 mddev->resync_mark = mark[last_mark];
5157 mddev->resync_mark_cnt = mark_cnt[last_mark];
5158
5159 /*
5160 * Tune reconstruction:
5161 */
5162 window = 32*(PAGE_SIZE/512);
5163 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5164 window/2,(unsigned long long) max_sectors/2);
5165
5166 atomic_set(&mddev->recovery_active, 0);
5167 init_waitqueue_head(&mddev->recovery_wait);
5168 last_check = 0;
5169
5170 if (j>2) {
5171 printk(KERN_INFO
5172 "md: resuming recovery of %s from checkpoint.\n",
5173 mdname(mddev));
5174 mddev->curr_resync = j;
5175 }
5176
5177 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005178 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
NeilBrown57afd892005-06-21 17:17:13 -07005180 skipped = 0;
5181 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08005182 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005183 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5185 goto out;
5186 }
NeilBrown57afd892005-06-21 17:17:13 -07005187
5188 if (!skipped) { /* actual IO requested */
5189 io_sectors += sectors;
5190 atomic_add(sectors, &mddev->recovery_active);
5191 }
5192
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 j += sectors;
5194 if (j>1) mddev->curr_resync = j;
NeilBrownd7603b72006-01-06 00:20:30 -08005195 if (last_check == 0)
5196 /* this is the earliers that rebuilt will be
5197 * visible in /proc/mdstat
5198 */
5199 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005200
5201 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 continue;
5203
NeilBrown57afd892005-06-21 17:17:13 -07005204 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
5206 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5207 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5208 break;
5209
5210 repeat:
5211 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5212 /* step marks */
5213 int next = (last_mark+1) % SYNC_MARKS;
5214
5215 mddev->resync_mark = mark[next];
5216 mddev->resync_mark_cnt = mark_cnt[next];
5217 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005218 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 last_mark = next;
5220 }
5221
5222
NeilBrown787453c2005-11-08 21:39:43 -08005223 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 /*
5225 * got a signal, exit.
5226 */
5227 printk(KERN_INFO
5228 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5230 goto out;
5231 }
5232
5233 /*
5234 * this loop exits only if either when we are slower than
5235 * the 'hard' speed limit, or the system was IO-idle for
5236 * a jiffy.
5237 * the system might be non-idle CPU-wise, but we only care
5238 * about not overloading the IO subsystem. (things like an
5239 * e2fsck being done on the RAID array should execute fast)
5240 */
5241 mddev->queue->unplug_fn(mddev->queue);
5242 cond_resched();
5243
NeilBrown57afd892005-06-21 17:17:13 -07005244 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5245 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
NeilBrown88202a02006-01-06 00:21:36 -08005247 if (currspeed > speed_min(mddev)) {
5248 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005250 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 goto repeat;
5252 }
5253 }
5254 }
5255 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
5256 /*
5257 * this also signals 'finished resyncing' to md_stop
5258 */
5259 out:
5260 mddev->queue->unplug_fn(mddev->queue);
5261
5262 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5263
5264 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005265 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
5267 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005268 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
5269 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005270 mddev->curr_resync > 2) {
5271 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5272 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5273 if (mddev->curr_resync >= mddev->recovery_cp) {
5274 printk(KERN_INFO
5275 "md: checkpointing recovery of %s.\n",
5276 mdname(mddev));
5277 mddev->recovery_cp = mddev->curr_resync;
5278 }
5279 } else
5280 mddev->recovery_cp = MaxSector;
5281 } else {
5282 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5283 mddev->curr_resync = MaxSector;
5284 ITERATE_RDEV(mddev,rdev,rtmp)
5285 if (rdev->raid_disk >= 0 &&
5286 !test_bit(Faulty, &rdev->flags) &&
5287 !test_bit(In_sync, &rdev->flags) &&
5288 rdev->recovery_offset < mddev->curr_resync)
5289 rdev->recovery_offset = mddev->curr_resync;
5290 mddev->sb_dirty = 1;
5291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 }
5293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 skip:
5295 mddev->curr_resync = 0;
5296 wake_up(&resync_wait);
5297 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5298 md_wakeup_thread(mddev->thread);
5299}
NeilBrown29269552006-03-27 01:18:10 -08005300EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
5302
5303/*
5304 * This routine is regularly called by all per-raid-array threads to
5305 * deal with generic issues like resync and super-block update.
5306 * Raid personalities that don't have a thread (linear/raid0) do not
5307 * need this as they never do any recovery or update the superblock.
5308 *
5309 * It does not do any resync itself, but rather "forks" off other threads
5310 * to do that as needed.
5311 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5312 * "->recovery" and create a thread at ->sync_thread.
5313 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5314 * and wakeups up this thread which will reap the thread and finish up.
5315 * This thread also removes any faulty devices (with nr_pending == 0).
5316 *
5317 * The overall approach is:
5318 * 1/ if the superblock needs updating, update it.
5319 * 2/ If a recovery thread is running, don't do anything else.
5320 * 3/ If recovery has finished, clean up, possibly marking spares active.
5321 * 4/ If there are any faulty devices, remove them.
5322 * 5/ If array is degraded, try to add spares devices
5323 * 6/ If array has spares or is not in-sync, start a resync thread.
5324 */
5325void md_check_recovery(mddev_t *mddev)
5326{
5327 mdk_rdev_t *rdev;
5328 struct list_head *rtmp;
5329
5330
NeilBrown5f404022005-06-21 17:17:16 -07005331 if (mddev->bitmap)
5332 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
5334 if (mddev->ro)
5335 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005336
5337 if (signal_pending(current)) {
5338 if (mddev->pers->sync_request) {
5339 printk(KERN_INFO "md: %s in immediate safe mode\n",
5340 mdname(mddev));
5341 mddev->safemode = 2;
5342 }
5343 flush_signals(current);
5344 }
5345
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 if ( ! (
5347 mddev->sb_dirty ||
5348 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005349 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5350 (mddev->safemode == 1) ||
5351 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5352 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 ))
5354 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005355
NeilBrowndf5b89b2006-03-27 01:18:20 -08005356 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07005358
NeilBrowna9701a32005-11-08 21:39:34 -08005359 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005360 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5361 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5362 mddev->in_sync = 1;
NeilBrown42543762006-06-26 00:27:57 -07005363 mddev->sb_dirty = 3;
NeilBrownfca4d842005-06-21 17:17:11 -07005364 }
5365 if (mddev->safemode == 1)
5366 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005367 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005368
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 if (mddev->sb_dirty)
5370 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07005371
NeilBrown06d91a52005-06-21 17:17:12 -07005372
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5374 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5375 /* resync/recovery still happening */
5376 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5377 goto unlock;
5378 }
5379 if (mddev->sync_thread) {
5380 /* resync has finished, collect result */
5381 md_unregister_thread(mddev->sync_thread);
5382 mddev->sync_thread = NULL;
5383 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5384 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5385 /* success...*/
5386 /* activate any spares */
5387 mddev->pers->spare_active(mddev);
5388 }
5389 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07005390
5391 /* if array is no-longer degraded, then any saved_raid_disk
5392 * information must be scrapped
5393 */
5394 if (!mddev->degraded)
5395 ITERATE_RDEV(mddev,rdev,rtmp)
5396 rdev->saved_raid_disk = -1;
5397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 mddev->recovery = 0;
5399 /* flag recovery needed just to double check */
5400 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005401 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 goto unlock;
5403 }
NeilBrown24dd4692005-11-08 21:39:26 -08005404 /* Clear some bits that don't mean anything, but
5405 * might be left set
5406 */
5407 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5408 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5409 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5410 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005412 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5413 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 /* no recovery is running.
5415 * remove any failed drives, then
5416 * add spares if possible.
5417 * Spare are also removed and re-added, to allow
5418 * the personality to fail the re-add.
5419 */
5420 ITERATE_RDEV(mddev,rdev,rtmp)
5421 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005422 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08005424 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
5425 char nm[20];
5426 sprintf(nm,"rd%d", rdev->raid_disk);
5427 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08005429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 }
5431
5432 if (mddev->degraded) {
5433 ITERATE_RDEV(mddev,rdev,rtmp)
5434 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08005435 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005436 rdev->recovery_offset = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08005437 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5438 char nm[20];
5439 sprintf(nm, "rd%d", rdev->raid_disk);
5440 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 spares++;
NeilBrownd7603b72006-01-06 00:20:30 -08005442 md_new_event(mddev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08005443 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 break;
5445 }
5446 }
5447
NeilBrown24dd4692005-11-08 21:39:26 -08005448 if (spares) {
5449 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5450 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5451 } else if (mddev->recovery_cp < MaxSector) {
5452 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5453 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5454 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005456
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 if (mddev->pers->sync_request) {
5458 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005459 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5460 /* We are adding a device or devices to an array
5461 * which has the bitmap stored on all devices.
5462 * So make sure all bitmap pages get written
5463 */
5464 bitmap_write_all(mddev->bitmap);
5465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 mddev->sync_thread = md_register_thread(md_do_sync,
5467 mddev,
5468 "%s_resync");
5469 if (!mddev->sync_thread) {
5470 printk(KERN_ERR "%s: could not start resync"
5471 " thread...\n",
5472 mdname(mddev));
5473 /* leave the spares where they are, it shouldn't hurt */
5474 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005475 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005477 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 }
5479 unlock:
5480 mddev_unlock(mddev);
5481 }
5482}
5483
Adrian Bunk75c96f82005-05-05 16:16:09 -07005484static int md_notify_reboot(struct notifier_block *this,
5485 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486{
5487 struct list_head *tmp;
5488 mddev_t *mddev;
5489
5490 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5491
5492 printk(KERN_INFO "md: stopping all md devices.\n");
5493
5494 ITERATE_MDDEV(mddev,tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005495 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005497 mddev_unlock(mddev);
5498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 /*
5500 * certain more exotic SCSI devices are known to be
5501 * volatile wrt too early system reboots. While the
5502 * right place to handle this issue is the given
5503 * driver, we do want to have a safe RAID driver ...
5504 */
5505 mdelay(1000*1);
5506 }
5507 return NOTIFY_DONE;
5508}
5509
Adrian Bunk75c96f82005-05-05 16:16:09 -07005510static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 .notifier_call = md_notify_reboot,
5512 .next = NULL,
5513 .priority = INT_MAX, /* before any real devices */
5514};
5515
5516static void md_geninit(void)
5517{
5518 struct proc_dir_entry *p;
5519
5520 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5521
5522 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5523 if (p)
5524 p->proc_fops = &md_seq_fops;
5525}
5526
Adrian Bunk75c96f82005-05-05 16:16:09 -07005527static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528{
5529 int minor;
5530
5531 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5532 " MD_SB_DISKS=%d\n",
5533 MD_MAJOR_VERSION, MD_MINOR_VERSION,
5534 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08005535 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07005536 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
5538 if (register_blkdev(MAJOR_NR, "md"))
5539 return -1;
5540 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5541 unregister_blkdev(MAJOR_NR, "md");
5542 return -1;
5543 }
5544 devfs_mk_dir("md");
5545 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
5546 md_probe, NULL, NULL);
5547 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
5548 md_probe, NULL, NULL);
5549
5550 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5551 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
5552 S_IFBLK|S_IRUSR|S_IWUSR,
5553 "md/%d", minor);
5554
5555 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5556 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
5557 S_IFBLK|S_IRUSR|S_IWUSR,
5558 "md/mdp%d", minor);
5559
5560
5561 register_reboot_notifier(&md_notifier);
5562 raid_table_header = register_sysctl_table(raid_root_table, 1);
5563
5564 md_geninit();
5565 return (0);
5566}
5567
5568
5569#ifndef MODULE
5570
5571/*
5572 * Searches all registered partitions for autorun RAID arrays
5573 * at boot time.
5574 */
5575static dev_t detected_devices[128];
5576static int dev_cnt;
5577
5578void md_autodetect_dev(dev_t dev)
5579{
5580 if (dev_cnt >= 0 && dev_cnt < 127)
5581 detected_devices[dev_cnt++] = dev;
5582}
5583
5584
5585static void autostart_arrays(int part)
5586{
5587 mdk_rdev_t *rdev;
5588 int i;
5589
5590 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5591
5592 for (i = 0; i < dev_cnt; i++) {
5593 dev_t dev = detected_devices[i];
5594
5595 rdev = md_import_device(dev,0, 0);
5596 if (IS_ERR(rdev))
5597 continue;
5598
NeilBrownb2d444d2005-11-08 21:39:31 -08005599 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 MD_BUG();
5601 continue;
5602 }
5603 list_add(&rdev->same_set, &pending_raid_disks);
5604 }
5605 dev_cnt = 0;
5606
5607 autorun_devices(part);
5608}
5609
5610#endif
5611
5612static __exit void md_exit(void)
5613{
5614 mddev_t *mddev;
5615 struct list_head *tmp;
5616 int i;
5617 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
5618 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
5619 for (i=0; i < MAX_MD_DEVS; i++)
5620 devfs_remove("md/%d", i);
5621 for (i=0; i < MAX_MD_DEVS; i++)
5622 devfs_remove("md/d%d", i);
5623
5624 devfs_remove("md");
5625
5626 unregister_blkdev(MAJOR_NR,"md");
5627 unregister_blkdev(mdp_major, "mdp");
5628 unregister_reboot_notifier(&md_notifier);
5629 unregister_sysctl_table(raid_table_header);
5630 remove_proc_entry("mdstat", NULL);
5631 ITERATE_MDDEV(mddev,tmp) {
5632 struct gendisk *disk = mddev->gendisk;
5633 if (!disk)
5634 continue;
5635 export_array(mddev);
5636 del_gendisk(disk);
5637 put_disk(disk);
5638 mddev->gendisk = NULL;
5639 mddev_put(mddev);
5640 }
5641}
5642
5643module_init(md_init)
5644module_exit(md_exit)
5645
NeilBrownf91de922005-11-08 21:39:36 -08005646static int get_ro(char *buffer, struct kernel_param *kp)
5647{
5648 return sprintf(buffer, "%d", start_readonly);
5649}
5650static int set_ro(const char *val, struct kernel_param *kp)
5651{
5652 char *e;
5653 int num = simple_strtoul(val, &e, 10);
5654 if (*val && (*e == '\0' || *e == '\n')) {
5655 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005656 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005657 }
5658 return -EINVAL;
5659}
5660
5661module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005662module_param(start_dirty_degraded, int, 0644);
5663
NeilBrownf91de922005-11-08 21:39:36 -08005664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665EXPORT_SYMBOL(register_md_personality);
5666EXPORT_SYMBOL(unregister_md_personality);
5667EXPORT_SYMBOL(md_error);
5668EXPORT_SYMBOL(md_done_sync);
5669EXPORT_SYMBOL(md_write_start);
5670EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671EXPORT_SYMBOL(md_register_thread);
5672EXPORT_SYMBOL(md_unregister_thread);
5673EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674EXPORT_SYMBOL(md_check_recovery);
5675MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005676MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005677MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);