blob: c28a120b4161ea7324eae5160ed2a4c2fc7e0ab6 [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>
Ahmed S. Darwish50511da2007-05-09 02:35:34 -070036#include <linux/kernel.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080043#include <linux/poll.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080044#include <linux/mutex.h>
NeilBrown16f17b32006-06-26 00:27:37 -070045#include <linux/ctype.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080046#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <linux/init.h>
49
NeilBrown32a76272005-06-21 17:17:14 -070050#include <linux/file.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#ifdef CONFIG_KMOD
53#include <linux/kmod.h>
54#endif
55
56#include <asm/unaligned.h>
57
58#define MAJOR_NR MD_MAJOR
59#define MD_DRIVER
60
61/* 63 partitions with the alternate major number (mdp) */
62#define MdpMinorShift 6
63
64#define DEBUG 0
65#define dprintk(x...) ((void)(DEBUG && printk(x)))
66
67
68#ifndef MODULE
69static void autostart_arrays (int part);
70#endif
71
NeilBrown2604b702006-01-06 00:20:36 -080072static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static DEFINE_SPINLOCK(pers_lock);
74
Adrian Bunk5e563412006-06-26 00:27:42 -070075static void md_print_devices(void);
76
77#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
81 * is 1000 KB/sec, so the extra system load does not show up that much.
82 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070083 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * subsystem is idle. There is also an 'absolute maximum' reconstruction
85 * speed limit - in case reconstruction slows down your system despite
86 * idle IO detection.
87 *
88 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080089 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
91
92static int sysctl_speed_limit_min = 1000;
93static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080094static inline int speed_min(mddev_t *mddev)
95{
96 return mddev->sync_speed_min ?
97 mddev->sync_speed_min : sysctl_speed_limit_min;
98}
99
100static inline int speed_max(mddev_t *mddev)
101{
102 return mddev->sync_speed_max ?
103 mddev->sync_speed_max : sysctl_speed_limit_max;
104}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106static struct ctl_table_header *raid_table_header;
107
108static ctl_table raid_table[] = {
109 {
110 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
111 .procname = "speed_limit_min",
112 .data = &sysctl_speed_limit_min,
113 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700114 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .proc_handler = &proc_dointvec,
116 },
117 {
118 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
119 .procname = "speed_limit_max",
120 .data = &sysctl_speed_limit_max,
121 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700122 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .proc_handler = &proc_dointvec,
124 },
125 { .ctl_name = 0 }
126};
127
128static ctl_table raid_dir_table[] = {
129 {
130 .ctl_name = DEV_RAID,
131 .procname = "raid",
132 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700133 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .child = raid_table,
135 },
136 { .ctl_name = 0 }
137};
138
139static ctl_table raid_root_table[] = {
140 {
141 .ctl_name = CTL_DEV,
142 .procname = "dev",
143 .maxlen = 0,
144 .mode = 0555,
145 .child = raid_dir_table,
146 },
147 { .ctl_name = 0 }
148};
149
150static struct block_device_operations md_fops;
151
NeilBrownf91de922005-11-08 21:39:36 -0800152static int start_readonly;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
NeilBrownd7603b72006-01-06 00:20:30 -0800155 * We have a system wide 'event count' that is incremented
156 * on any 'interesting' event, and readers of /proc/mdstat
157 * can use 'poll' or 'select' to find out when the event
158 * count increases.
159 *
160 * Events are:
161 * start array, stop array, error, add device, remove device,
162 * start build, activate spare
163 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800164static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800165static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800166void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800167{
168 atomic_inc(&md_event_count);
169 wake_up(&md_event_waiters);
NeilBrown4508a7a2006-03-20 17:53:53 +1100170 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -0800171}
NeilBrown29269552006-03-27 01:18:10 -0800172EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800173
NeilBrownc331eb02006-05-30 21:27:13 -0700174/* Alternate version that can be called from interrupts
175 * when calling sysfs_notify isn't needed.
176 */
Adrian Bunk05381952006-06-26 00:28:01 -0700177static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700178{
179 atomic_inc(&md_event_count);
180 wake_up(&md_event_waiters);
181}
182
NeilBrownd7603b72006-01-06 00:20:30 -0800183/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * Enables to iterate over all existing md arrays
185 * all_mddevs_lock protects this list.
186 */
187static LIST_HEAD(all_mddevs);
188static DEFINE_SPINLOCK(all_mddevs_lock);
189
190
191/*
192 * iterates through all used mddevs in the system.
193 * We take care to grab the all_mddevs_lock whenever navigating
194 * the list, and to always hold a refcount when unlocked.
195 * Any code which breaks out of this loop while own
196 * a reference to the current mddev and must mddev_put it.
197 */
198#define ITERATE_MDDEV(mddev,tmp) \
199 \
200 for (({ spin_lock(&all_mddevs_lock); \
201 tmp = all_mddevs.next; \
202 mddev = NULL;}); \
203 ({ if (tmp != &all_mddevs) \
204 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
205 spin_unlock(&all_mddevs_lock); \
206 if (mddev) mddev_put(mddev); \
207 mddev = list_entry(tmp, mddev_t, all_mddevs); \
208 tmp != &all_mddevs;}); \
209 ({ spin_lock(&all_mddevs_lock); \
210 tmp = tmp->next;}) \
211 )
212
213
Jens Axboe165125e2007-07-24 09:28:11 +0200214static int md_fail_request (struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
NeilBrown6712ecf2007-09-27 12:47:43 +0200216 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218}
219
220static inline mddev_t *mddev_get(mddev_t *mddev)
221{
222 atomic_inc(&mddev->active);
223 return mddev;
224}
225
226static void mddev_put(mddev_t *mddev)
227{
228 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
229 return;
230 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
231 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800232 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500233 blk_cleanup_queue(mddev->queue);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800234 kobject_put(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800235 } else
236 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239static mddev_t * mddev_find(dev_t unit)
240{
241 mddev_t *mddev, *new = NULL;
242
243 retry:
244 spin_lock(&all_mddevs_lock);
245 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
246 if (mddev->unit == unit) {
247 mddev_get(mddev);
248 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700249 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return mddev;
251 }
252
253 if (new) {
254 list_add(&new->all_mddevs, &all_mddevs);
255 spin_unlock(&all_mddevs_lock);
256 return new;
257 }
258 spin_unlock(&all_mddevs_lock);
259
NeilBrown9ffae0c2006-01-06 00:20:32 -0800260 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!new)
262 return NULL;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 new->unit = unit;
265 if (MAJOR(unit) == MD_MAJOR)
266 new->md_minor = MINOR(unit);
267 else
268 new->md_minor = MINOR(unit) >> MdpMinorShift;
269
NeilBrowndf5b89b2006-03-27 01:18:20 -0800270 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 INIT_LIST_HEAD(&new->disks);
272 INIT_LIST_HEAD(&new->all_mddevs);
273 init_timer(&new->safemode_timer);
274 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700275 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700276 init_waitqueue_head(&new->sb_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700277 new->reshape_position = MaxSector;
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
NeilBrown6712ecf2007-09-27 12:47:43 +0200387static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700388{
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
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700392 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
393 printk("md: super_written gets error=%d, uptodate=%d\n",
394 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
395 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800396 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700397 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700398
NeilBrowna9701a32005-11-08 21:39:34 -0800399 if (atomic_dec_and_test(&mddev->pending_writes))
400 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700401 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700402}
403
NeilBrown6712ecf2007-09-27 12:47:43 +0200404static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800405{
406 struct bio *bio2 = bio->bi_private;
407 mdk_rdev_t *rdev = bio2->bi_private;
408 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800409
410 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
411 error == -EOPNOTSUPP) {
412 unsigned long flags;
413 /* barriers don't appear to be supported :-( */
414 set_bit(BarriersNotsupp, &rdev->flags);
415 mddev->barriers_work = 0;
416 spin_lock_irqsave(&mddev->write_lock, flags);
417 bio2->bi_next = mddev->biolist;
418 mddev->biolist = bio2;
419 spin_unlock_irqrestore(&mddev->write_lock, flags);
420 wake_up(&mddev->sb_wait);
421 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200422 } else {
423 bio_put(bio2);
424 bio->bi_private = rdev;
425 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800426 }
NeilBrowna9701a32005-11-08 21:39:34 -0800427}
428
NeilBrown7bfa19f2005-06-21 17:17:28 -0700429void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
430 sector_t sector, int size, struct page *page)
431{
432 /* write first size bytes of page to sector of rdev
433 * Increment mddev->pending_writes before returning
434 * and decrement it on completion, waking up sb_wait
435 * if zero is reached.
436 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800437 *
438 * As we might need to resubmit the request if BIO_RW_BARRIER
439 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700440 */
441 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800442 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700443
444 bio->bi_bdev = rdev->bdev;
445 bio->bi_sector = sector;
446 bio_add_page(bio, page, size, 0);
447 bio->bi_private = rdev;
448 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800449 bio->bi_rw = rw;
450
NeilBrown7bfa19f2005-06-21 17:17:28 -0700451 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800452 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
453 struct bio *rbio;
454 rw |= (1<<BIO_RW_BARRIER);
455 rbio = bio_clone(bio, GFP_NOIO);
456 rbio->bi_private = bio;
457 rbio->bi_end_io = super_written_barrier;
458 submit_bio(rw, rbio);
459 } else
460 submit_bio(rw, bio);
461}
462
463void md_super_wait(mddev_t *mddev)
464{
465 /* wait for all superblock writes that were scheduled to complete.
466 * if any had to be retried (due to BARRIER problems), retry them
467 */
468 DEFINE_WAIT(wq);
469 for(;;) {
470 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
471 if (atomic_read(&mddev->pending_writes)==0)
472 break;
473 while (mddev->biolist) {
474 struct bio *bio;
475 spin_lock_irq(&mddev->write_lock);
476 bio = mddev->biolist;
477 mddev->biolist = bio->bi_next ;
478 bio->bi_next = NULL;
479 spin_unlock_irq(&mddev->write_lock);
480 submit_bio(bio->bi_rw, bio);
481 }
482 schedule();
483 }
484 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700485}
486
NeilBrown6712ecf2007-09-27 12:47:43 +0200487static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
NeilBrowna654b9d82005-06-21 17:17:27 -0700492int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct page *page, int rw)
494{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700495 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct completion event;
497 int ret;
498
499 rw |= (1 << BIO_RW_SYNC);
500
501 bio->bi_bdev = bdev;
502 bio->bi_sector = sector;
503 bio_add_page(bio, page, size, 0);
504 init_completion(&event);
505 bio->bi_private = &event;
506 bio->bi_end_io = bi_complete;
507 submit_bio(rw, bio);
508 wait_for_completion(&event);
509
510 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
511 bio_put(bio);
512 return ret;
513}
NeilBrowna8745db2006-01-06 00:20:34 -0800514EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
NeilBrown0002b272005-09-09 16:23:53 -0700516static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 char b[BDEVNAME_SIZE];
519 if (!rdev->sb_page) {
520 MD_BUG();
521 return -EINVAL;
522 }
523 if (rdev->sb_loaded)
524 return 0;
525
526
NeilBrown0002b272005-09-09 16:23:53 -0700527 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 goto fail;
529 rdev->sb_loaded = 1;
530 return 0;
531
532fail:
533 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
534 bdevname(rdev->bdev,b));
535 return -EINVAL;
536}
537
538static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
539{
540 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
541 (sb1->set_uuid1 == sb2->set_uuid1) &&
542 (sb1->set_uuid2 == sb2->set_uuid2) &&
543 (sb1->set_uuid3 == sb2->set_uuid3))
544
545 return 1;
546
547 return 0;
548}
549
550
551static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
552{
553 int ret;
554 mdp_super_t *tmp1, *tmp2;
555
556 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
557 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
558
559 if (!tmp1 || !tmp2) {
560 ret = 0;
561 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
562 goto abort;
563 }
564
565 *tmp1 = *sb1;
566 *tmp2 = *sb2;
567
568 /*
569 * nr_disks is not constant
570 */
571 tmp1->nr_disks = 0;
572 tmp2->nr_disks = 0;
573
574 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
575 ret = 0;
576 else
577 ret = 1;
578
579abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700580 kfree(tmp1);
581 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return ret;
583}
584
NeilBrown4d167f02007-05-09 02:35:37 -0700585
586static u32 md_csum_fold(u32 csum)
587{
588 csum = (csum & 0xffff) + (csum >> 16);
589 return (csum & 0xffff) + (csum >> 16);
590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592static unsigned int calc_sb_csum(mdp_super_t * sb)
593{
NeilBrown4d167f02007-05-09 02:35:37 -0700594 u64 newcsum = 0;
595 u32 *sb32 = (u32*)sb;
596 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 unsigned int disk_csum, csum;
598
599 disk_csum = sb->sb_csum;
600 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700601
602 for (i = 0; i < MD_SB_BYTES/4 ; i++)
603 newcsum += sb32[i];
604 csum = (newcsum & 0xffffffff) + (newcsum>>32);
605
606
607#ifdef CONFIG_ALPHA
608 /* This used to use csum_partial, which was wrong for several
609 * reasons including that different results are returned on
610 * different architectures. It isn't critical that we get exactly
611 * the same return value as before (we always csum_fold before
612 * testing, and that removes any differences). However as we
613 * know that csum_partial always returned a 16bit value on
614 * alphas, do a fold to maximise conformity to previous behaviour.
615 */
616 sb->sb_csum = md_csum_fold(disk_csum);
617#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700619#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return csum;
621}
622
623
624/*
625 * Handle superblock details.
626 * We want to be able to handle multiple superblock formats
627 * so we have a common interface to them all, and an array of
628 * different handlers.
629 * We rely on user-space to write the initial superblock, and support
630 * reading and updating of superblocks.
631 * Interface methods are:
632 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
633 * loads and validates a superblock on dev.
634 * if refdev != NULL, compare superblocks on both devices
635 * Return:
636 * 0 - dev has a superblock that is compatible with refdev
637 * 1 - dev has a superblock that is compatible and newer than refdev
638 * so dev should be used as the refdev in future
639 * -EINVAL superblock incompatible or invalid
640 * -othererror e.g. -EIO
641 *
642 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
643 * Verify that dev is acceptable into mddev.
644 * The first time, mddev->raid_disks will be 0, and data from
645 * dev should be merged in. Subsequent calls check that dev
646 * is new enough. Return 0 or -EINVAL
647 *
648 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
649 * Update the superblock for rdev with data in mddev
650 * This does not write to disc.
651 *
652 */
653
654struct super_type {
655 char *name;
656 struct module *owner;
657 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
658 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
659 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
660};
661
662/*
663 * load_super for 0.90.0
664 */
665static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
666{
667 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
668 mdp_super_t *sb;
669 int ret;
670 sector_t sb_offset;
671
672 /*
673 * Calculate the position of the superblock,
674 * it's at the end of the disk.
675 *
676 * It also happens to be a multiple of 4Kb.
677 */
678 sb_offset = calc_dev_sboffset(rdev->bdev);
679 rdev->sb_offset = sb_offset;
680
NeilBrown0002b272005-09-09 16:23:53 -0700681 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (ret) return ret;
683
684 ret = -EINVAL;
685
686 bdevname(rdev->bdev, b);
687 sb = (mdp_super_t*)page_address(rdev->sb_page);
688
689 if (sb->md_magic != MD_SB_MAGIC) {
690 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
691 b);
692 goto abort;
693 }
694
695 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800696 sb->minor_version < 90 ||
697 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
699 sb->major_version, sb->minor_version,
700 b);
701 goto abort;
702 }
703
704 if (sb->raid_disks <= 0)
705 goto abort;
706
NeilBrown4d167f02007-05-09 02:35:37 -0700707 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
709 b);
710 goto abort;
711 }
712
713 rdev->preferred_minor = sb->md_minor;
714 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700715 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
NeilBrowne11e93f2007-05-09 02:35:36 -0700717 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
718 if (sb->level != 1 && sb->level != 4
719 && sb->level != 5 && sb->level != 6
720 && sb->level != 10) {
721 /* FIXME use a better test */
722 printk(KERN_WARNING
723 "md: bitmaps not supported for this level.\n");
724 goto abort;
725 }
726 }
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (sb->level == LEVEL_MULTIPATH)
729 rdev->desc_nr = -1;
730 else
731 rdev->desc_nr = sb->this_disk.number;
732
733 if (refdev == 0)
734 ret = 1;
735 else {
736 __u64 ev1, ev2;
737 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
738 if (!uuid_equal(refsb, sb)) {
739 printk(KERN_WARNING "md: %s has different UUID to %s\n",
740 b, bdevname(refdev->bdev,b2));
741 goto abort;
742 }
743 if (!sb_equal(refsb, sb)) {
744 printk(KERN_WARNING "md: %s has same UUID"
745 " but different superblock to %s\n",
746 b, bdevname(refdev->bdev, b2));
747 goto abort;
748 }
749 ev1 = md_event(sb);
750 ev2 = md_event(refsb);
751 if (ev1 > ev2)
752 ret = 1;
753 else
754 ret = 0;
755 }
756 rdev->size = calc_dev_size(rdev, sb->chunk_size);
757
NeilBrown2bf071b2006-01-06 00:20:55 -0800758 if (rdev->size < sb->size && sb->level > 1)
759 /* "this cannot possibly happen" ... */
760 ret = -EINVAL;
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 abort:
763 return ret;
764}
765
766/*
767 * validate_super for 0.90.0
768 */
769static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
770{
771 mdp_disk_t *desc;
772 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700773 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
NeilBrown41158c72005-06-21 17:17:25 -0700775 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800776 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (mddev->raid_disks == 0) {
778 mddev->major_version = 0;
779 mddev->minor_version = sb->minor_version;
780 mddev->patch_version = sb->patch_version;
781 mddev->persistent = ! sb->not_persistent;
782 mddev->chunk_size = sb->chunk_size;
783 mddev->ctime = sb->ctime;
784 mddev->utime = sb->utime;
785 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800786 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 mddev->layout = sb->layout;
788 mddev->raid_disks = sb->raid_disks;
789 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700790 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700791 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700792 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
NeilBrownf6705572006-03-27 01:18:11 -0800794 if (mddev->minor_version >= 91) {
795 mddev->reshape_position = sb->reshape_position;
796 mddev->delta_disks = sb->delta_disks;
797 mddev->new_level = sb->new_level;
798 mddev->new_layout = sb->new_layout;
799 mddev->new_chunk = sb->new_chunk;
800 } else {
801 mddev->reshape_position = MaxSector;
802 mddev->delta_disks = 0;
803 mddev->new_level = mddev->level;
804 mddev->new_layout = mddev->layout;
805 mddev->new_chunk = mddev->chunk_size;
806 }
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (sb->state & (1<<MD_SB_CLEAN))
809 mddev->recovery_cp = MaxSector;
810 else {
811 if (sb->events_hi == sb->cp_events_hi &&
812 sb->events_lo == sb->cp_events_lo) {
813 mddev->recovery_cp = sb->recovery_cp;
814 } else
815 mddev->recovery_cp = 0;
816 }
817
818 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
819 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
820 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
821 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
822
823 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700824
825 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700826 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700827 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700828
NeilBrown41158c72005-06-21 17:17:25 -0700829 } else if (mddev->pers == NULL) {
830 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 ++ev1;
832 if (ev1 < mddev->events)
833 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700834 } else if (mddev->bitmap) {
835 /* if adding to array with a bitmap, then we can accept an
836 * older device ... but not too old.
837 */
NeilBrown41158c72005-06-21 17:17:25 -0700838 if (ev1 < mddev->bitmap->events_cleared)
839 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700840 } else {
841 if (ev1 < mddev->events)
842 /* just a hot-add of a new device, leave raid_disk at -1 */
843 return 0;
844 }
NeilBrown41158c72005-06-21 17:17:25 -0700845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 desc = sb->disks + rdev->desc_nr;
848
849 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800850 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700851 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
852 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800853 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 rdev->raid_disk = desc->raid_disk;
855 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700856 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
857 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700858 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800859 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 0;
861}
862
863/*
864 * sync_super for 0.90.0
865 */
866static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
867{
868 mdp_super_t *sb;
869 struct list_head *tmp;
870 mdk_rdev_t *rdev2;
871 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /* make rdev->sb match mddev data..
875 *
876 * 1/ zero out disks
877 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
878 * 3/ any empty disks < next_spare become removed
879 *
880 * disks[0] gets initialised to REMOVED because
881 * we cannot be sure from other fields if it has
882 * been initialised or not.
883 */
884 int i;
885 int active=0, working=0,failed=0,spare=0,nr_disks=0;
886
NeilBrown61181562005-09-09 16:24:02 -0700887 rdev->sb_size = MD_SB_BYTES;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 sb = (mdp_super_t*)page_address(rdev->sb_page);
890
891 memset(sb, 0, sizeof(*sb));
892
893 sb->md_magic = MD_SB_MAGIC;
894 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 sb->patch_version = mddev->patch_version;
896 sb->gvalid_words = 0; /* ignored */
897 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
898 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
899 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
900 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
901
902 sb->ctime = mddev->ctime;
903 sb->level = mddev->level;
904 sb->size = mddev->size;
905 sb->raid_disks = mddev->raid_disks;
906 sb->md_minor = mddev->md_minor;
907 sb->not_persistent = !mddev->persistent;
908 sb->utime = mddev->utime;
909 sb->state = 0;
910 sb->events_hi = (mddev->events>>32);
911 sb->events_lo = (u32)mddev->events;
912
NeilBrownf6705572006-03-27 01:18:11 -0800913 if (mddev->reshape_position == MaxSector)
914 sb->minor_version = 90;
915 else {
916 sb->minor_version = 91;
917 sb->reshape_position = mddev->reshape_position;
918 sb->new_level = mddev->new_level;
919 sb->delta_disks = mddev->delta_disks;
920 sb->new_layout = mddev->new_layout;
921 sb->new_chunk = mddev->new_chunk;
922 }
923 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (mddev->in_sync)
925 {
926 sb->recovery_cp = mddev->recovery_cp;
927 sb->cp_events_hi = (mddev->events>>32);
928 sb->cp_events_lo = (u32)mddev->events;
929 if (mddev->recovery_cp == MaxSector)
930 sb->state = (1<< MD_SB_CLEAN);
931 } else
932 sb->recovery_cp = 0;
933
934 sb->layout = mddev->layout;
935 sb->chunk_size = mddev->chunk_size;
936
NeilBrowna654b9d82005-06-21 17:17:27 -0700937 if (mddev->bitmap && mddev->bitmap_file == NULL)
938 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 sb->disks[0].state = (1<<MD_DISK_REMOVED);
941 ITERATE_RDEV(mddev,rdev2,tmp) {
942 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800943 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800944 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
945 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800946 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800948 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800949 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 d = &sb->disks[rdev2->desc_nr];
951 nr_disks++;
952 d->number = rdev2->desc_nr;
953 d->major = MAJOR(rdev2->bdev->bd_dev);
954 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800955 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
956 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 d->raid_disk = rdev2->raid_disk;
958 else
959 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800960 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800962 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 d->state = (1<<MD_DISK_ACTIVE);
964 d->state |= (1<<MD_DISK_SYNC);
965 active++;
966 working++;
967 } else {
968 d->state = 0;
969 spare++;
970 working++;
971 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700972 if (test_bit(WriteMostly, &rdev2->flags))
973 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* now set the "removed" and "faulty" bits on any missing devices */
976 for (i=0 ; i < mddev->raid_disks ; i++) {
977 mdp_disk_t *d = &sb->disks[i];
978 if (d->state == 0 && d->number == 0) {
979 d->number = i;
980 d->raid_disk = i;
981 d->state = (1<<MD_DISK_REMOVED);
982 d->state |= (1<<MD_DISK_FAULTY);
983 failed++;
984 }
985 }
986 sb->nr_disks = nr_disks;
987 sb->active_disks = active;
988 sb->working_disks = working;
989 sb->failed_disks = failed;
990 sb->spare_disks = spare;
991
992 sb->this_disk = sb->disks[rdev->desc_nr];
993 sb->sb_csum = calc_sb_csum(sb);
994}
995
996/*
997 * version 1 superblock
998 */
999
NeilBrown1c05b4b2006-10-21 10:24:08 -07001000static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001002 __le32 disk_csum;
1003 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 unsigned long long newcsum;
1005 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001006 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 int i;
1008
1009 disk_csum = sb->sb_csum;
1010 sb->sb_csum = 0;
1011 newcsum = 0;
1012 for (i=0; size>=4; size -= 4 )
1013 newcsum += le32_to_cpu(*isuper++);
1014
1015 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001016 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1019 sb->sb_csum = disk_csum;
1020 return cpu_to_le32(csum);
1021}
1022
1023static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1024{
1025 struct mdp_superblock_1 *sb;
1026 int ret;
1027 sector_t sb_offset;
1028 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001029 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 /*
1032 * Calculate the position of the superblock.
1033 * It is always aligned to a 4K boundary and
1034 * depeding on minor_version, it can be:
1035 * 0: At least 8K, but less than 12K, from end of device
1036 * 1: At start of device
1037 * 2: 4K from start of device.
1038 */
1039 switch(minor_version) {
1040 case 0:
1041 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1042 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001043 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* convert from sectors to K */
1045 sb_offset /= 2;
1046 break;
1047 case 1:
1048 sb_offset = 0;
1049 break;
1050 case 2:
1051 sb_offset = 4;
1052 break;
1053 default:
1054 return -EINVAL;
1055 }
1056 rdev->sb_offset = sb_offset;
1057
NeilBrown0002b272005-09-09 16:23:53 -07001058 /* superblock is rarely larger than 1K, but it can be larger,
1059 * and it is safe to read 4k, so we do that
1060 */
1061 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (ret) return ret;
1063
1064
1065 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1066
1067 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1068 sb->major_version != cpu_to_le32(1) ||
1069 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1070 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001071 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return -EINVAL;
1073
1074 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1075 printk("md: invalid superblock checksum on %s\n",
1076 bdevname(rdev->bdev,b));
1077 return -EINVAL;
1078 }
1079 if (le64_to_cpu(sb->data_size) < 10) {
1080 printk("md: data_size too small on %s\n",
1081 bdevname(rdev->bdev,b));
1082 return -EINVAL;
1083 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001084 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1085 if (sb->level != cpu_to_le32(1) &&
1086 sb->level != cpu_to_le32(4) &&
1087 sb->level != cpu_to_le32(5) &&
1088 sb->level != cpu_to_le32(6) &&
1089 sb->level != cpu_to_le32(10)) {
1090 printk(KERN_WARNING
1091 "md: bitmaps not supported for this level.\n");
1092 return -EINVAL;
1093 }
1094 }
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 rdev->preferred_minor = 0xffff;
1097 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001098 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
NeilBrown0002b272005-09-09 16:23:53 -07001100 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001101 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001102 if (rdev->sb_size & bmask)
1103 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1104
NeilBrown31b65a02006-07-10 04:44:14 -07001105 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1106 rdev->desc_nr = -1;
1107 else
1108 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (refdev == 0)
NeilBrown8ed75462006-02-03 03:03:41 -08001111 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 else {
1113 __u64 ev1, ev2;
1114 struct mdp_superblock_1 *refsb =
1115 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1116
1117 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1118 sb->level != refsb->level ||
1119 sb->layout != refsb->layout ||
1120 sb->chunksize != refsb->chunksize) {
1121 printk(KERN_WARNING "md: %s has strangely different"
1122 " superblock to %s\n",
1123 bdevname(rdev->bdev,b),
1124 bdevname(refdev->bdev,b2));
1125 return -EINVAL;
1126 }
1127 ev1 = le64_to_cpu(sb->events);
1128 ev2 = le64_to_cpu(refsb->events);
1129
1130 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001131 ret = 1;
1132 else
1133 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135 if (minor_version)
1136 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1137 else
1138 rdev->size = rdev->sb_offset;
1139 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1140 return -EINVAL;
1141 rdev->size = le64_to_cpu(sb->data_size)/2;
1142 if (le32_to_cpu(sb->chunksize))
1143 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001144
NeilBrown1c05b4b2006-10-21 10:24:08 -07001145 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001146 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001147 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
1150static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1151{
1152 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001153 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
NeilBrown41158c72005-06-21 17:17:25 -07001155 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001156 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (mddev->raid_disks == 0) {
1158 mddev->major_version = 1;
1159 mddev->patch_version = 0;
1160 mddev->persistent = 1;
1161 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1162 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1163 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1164 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001165 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 mddev->layout = le32_to_cpu(sb->layout);
1167 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1168 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001169 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001170 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001171 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1174 memcpy(mddev->uuid, sb->set_uuid, 16);
1175
1176 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001177
NeilBrown71c08052005-09-09 16:23:51 -07001178 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001179 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001180 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001181
NeilBrownf6705572006-03-27 01:18:11 -08001182 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1183 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1184 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1185 mddev->new_level = le32_to_cpu(sb->new_level);
1186 mddev->new_layout = le32_to_cpu(sb->new_layout);
1187 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1188 } else {
1189 mddev->reshape_position = MaxSector;
1190 mddev->delta_disks = 0;
1191 mddev->new_level = mddev->level;
1192 mddev->new_layout = mddev->layout;
1193 mddev->new_chunk = mddev->chunk_size;
1194 }
1195
NeilBrown41158c72005-06-21 17:17:25 -07001196 } else if (mddev->pers == NULL) {
1197 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 ++ev1;
1199 if (ev1 < mddev->events)
1200 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001201 } else if (mddev->bitmap) {
1202 /* If adding to array with a bitmap, then we can accept an
1203 * older device, but not too old.
1204 */
NeilBrown41158c72005-06-21 17:17:25 -07001205 if (ev1 < mddev->bitmap->events_cleared)
1206 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001207 } else {
1208 if (ev1 < mddev->events)
1209 /* just a hot-add of a new device, leave raid_disk at -1 */
1210 return 0;
1211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (mddev->level != LEVEL_MULTIPATH) {
1213 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1215 switch(role) {
1216 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 break;
1218 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001219 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 break;
1221 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001222 if ((le32_to_cpu(sb->feature_map) &
1223 MD_FEATURE_RECOVERY_OFFSET))
1224 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1225 else
1226 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 rdev->raid_disk = role;
1228 break;
1229 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001230 if (sb->devflags & WriteMostly1)
1231 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001232 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001233 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return 0;
1236}
1237
1238static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1239{
1240 struct mdp_superblock_1 *sb;
1241 struct list_head *tmp;
1242 mdk_rdev_t *rdev2;
1243 int max_dev, i;
1244 /* make rdev->sb match mddev and rdev data. */
1245
1246 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1247
1248 sb->feature_map = 0;
1249 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001250 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 memset(sb->pad1, 0, sizeof(sb->pad1));
1252 memset(sb->pad2, 0, sizeof(sb->pad2));
1253 memset(sb->pad3, 0, sizeof(sb->pad3));
1254
1255 sb->utime = cpu_to_le64((__u64)mddev->utime);
1256 sb->events = cpu_to_le64(mddev->events);
1257 if (mddev->in_sync)
1258 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1259 else
1260 sb->resync_offset = cpu_to_le64(0);
1261
NeilBrown1c05b4b2006-10-21 10:24:08 -07001262 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001263
NeilBrownf0ca3402006-02-02 14:28:04 -08001264 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001265 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001266
NeilBrowna654b9d82005-06-21 17:17:27 -07001267 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1268 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001269 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001270 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001271
1272 if (rdev->raid_disk >= 0 &&
1273 !test_bit(In_sync, &rdev->flags) &&
1274 rdev->recovery_offset > 0) {
1275 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1276 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1277 }
1278
NeilBrownf6705572006-03-27 01:18:11 -08001279 if (mddev->reshape_position != MaxSector) {
1280 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1281 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1282 sb->new_layout = cpu_to_le32(mddev->new_layout);
1283 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1284 sb->new_level = cpu_to_le32(mddev->new_level);
1285 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1286 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 max_dev = 0;
1289 ITERATE_RDEV(mddev,rdev2,tmp)
1290 if (rdev2->desc_nr+1 > max_dev)
1291 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001292
1293 if (max_dev > le32_to_cpu(sb->max_dev))
1294 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 for (i=0; i<max_dev;i++)
1296 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1297
1298 ITERATE_RDEV(mddev,rdev2,tmp) {
1299 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001300 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001302 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001304 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1305 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 else
1307 sb->dev_roles[i] = cpu_to_le16(0xffff);
1308 }
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 sb->sb_csum = calc_sb_1_csum(sb);
1311}
1312
1313
Adrian Bunk75c96f82005-05-05 16:16:09 -07001314static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 [0] = {
1316 .name = "0.90.0",
1317 .owner = THIS_MODULE,
1318 .load_super = super_90_load,
1319 .validate_super = super_90_validate,
1320 .sync_super = super_90_sync,
1321 },
1322 [1] = {
1323 .name = "md-1",
1324 .owner = THIS_MODULE,
1325 .load_super = super_1_load,
1326 .validate_super = super_1_validate,
1327 .sync_super = super_1_sync,
1328 },
1329};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1332{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001333 struct list_head *tmp, *tmp2;
1334 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 ITERATE_RDEV(mddev1,rdev,tmp)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001337 ITERATE_RDEV(mddev2, rdev2, tmp2)
1338 if (rdev->bdev->bd_contains ==
1339 rdev2->bdev->bd_contains)
1340 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 return 0;
1343}
1344
1345static LIST_HEAD(pending_raid_disks);
1346
1347static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1348{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001349 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001350 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001351 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001352 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 if (rdev->mddev) {
1355 MD_BUG();
1356 return -EINVAL;
1357 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001358 /* make sure rdev->size exceeds mddev->size */
1359 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001360 if (mddev->pers) {
1361 /* Cannot change size, so fail
1362 * If mddev->level <= 0, then we don't care
1363 * about aligning sizes (e.g. linear)
1364 */
1365 if (mddev->level > 0)
1366 return -ENOSPC;
1367 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001368 mddev->size = rdev->size;
1369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /* Verify rdev->desc_nr is unique.
1372 * If it is -1, assign a free number, else
1373 * check number is not in use
1374 */
1375 if (rdev->desc_nr < 0) {
1376 int choice = 0;
1377 if (mddev->pers) choice = mddev->raid_disks;
1378 while (find_rdev_nr(mddev, choice))
1379 choice++;
1380 rdev->desc_nr = choice;
1381 } else {
1382 if (find_rdev_nr(mddev, rdev->desc_nr))
1383 return -EBUSY;
1384 }
NeilBrown19133a42005-11-08 21:39:35 -08001385 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001386 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001387 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001390 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001391
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001392 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001393 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001394
NeilBrownf637b9f2005-11-08 21:39:37 -08001395 if (rdev->bdev->bd_part)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001396 ko = &rdev->bdev->bd_part->dev.kobj;
NeilBrownf637b9f2005-11-08 21:39:37 -08001397 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001398 ko = &rdev->bdev->bd_disk->dev.kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001399 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1400 kobject_del(&rdev->kobj);
1401 goto fail;
1402 }
1403 list_add(&rdev->same_set, &mddev->disks);
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001404 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001406
1407 fail:
1408 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1409 b, mdname(mddev));
1410 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
NeilBrown5792a282007-04-04 19:08:18 -07001413static void delayed_delete(struct work_struct *ws)
1414{
1415 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1416 kobject_del(&rdev->kobj);
1417}
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1420{
1421 char b[BDEVNAME_SIZE];
1422 if (!rdev->mddev) {
1423 MD_BUG();
1424 return;
1425 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001426 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 list_del_init(&rdev->same_set);
1428 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1429 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001430 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001431
1432 /* We need to delay this, otherwise we can deadlock when
1433 * writing to 'remove' to "dev/state"
1434 */
1435 INIT_WORK(&rdev->del_work, delayed_delete);
1436 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
1439/*
1440 * prevent the device from being mounted, repartitioned or
1441 * otherwise reused by a RAID array (or any other kernel
1442 * subsystem), by bd_claiming the device.
1443 */
1444static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1445{
1446 int err = 0;
1447 struct block_device *bdev;
1448 char b[BDEVNAME_SIZE];
1449
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001450 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (IS_ERR(bdev)) {
1452 printk(KERN_ERR "md: could not open %s.\n",
1453 __bdevname(dev, b));
1454 return PTR_ERR(bdev);
1455 }
1456 err = bd_claim(bdev, rdev);
1457 if (err) {
1458 printk(KERN_ERR "md: could not bd_claim %s.\n",
1459 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001460 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return err;
1462 }
1463 rdev->bdev = bdev;
1464 return err;
1465}
1466
1467static void unlock_rdev(mdk_rdev_t *rdev)
1468{
1469 struct block_device *bdev = rdev->bdev;
1470 rdev->bdev = NULL;
1471 if (!bdev)
1472 MD_BUG();
1473 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001474 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477void md_autodetect_dev(dev_t dev);
1478
1479static void export_rdev(mdk_rdev_t * rdev)
1480{
1481 char b[BDEVNAME_SIZE];
1482 printk(KERN_INFO "md: export_rdev(%s)\n",
1483 bdevname(rdev->bdev,b));
1484 if (rdev->mddev)
1485 MD_BUG();
1486 free_disk_sb(rdev);
1487 list_del_init(&rdev->same_set);
1488#ifndef MODULE
1489 md_autodetect_dev(rdev->bdev->bd_dev);
1490#endif
1491 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001492 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
1495static void kick_rdev_from_array(mdk_rdev_t * rdev)
1496{
1497 unbind_rdev_from_array(rdev);
1498 export_rdev(rdev);
1499}
1500
1501static void export_array(mddev_t *mddev)
1502{
1503 struct list_head *tmp;
1504 mdk_rdev_t *rdev;
1505
1506 ITERATE_RDEV(mddev,rdev,tmp) {
1507 if (!rdev->mddev) {
1508 MD_BUG();
1509 continue;
1510 }
1511 kick_rdev_from_array(rdev);
1512 }
1513 if (!list_empty(&mddev->disks))
1514 MD_BUG();
1515 mddev->raid_disks = 0;
1516 mddev->major_version = 0;
1517}
1518
1519static void print_desc(mdp_disk_t *desc)
1520{
1521 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1522 desc->major,desc->minor,desc->raid_disk,desc->state);
1523}
1524
1525static void print_sb(mdp_super_t *sb)
1526{
1527 int i;
1528
1529 printk(KERN_INFO
1530 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1531 sb->major_version, sb->minor_version, sb->patch_version,
1532 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1533 sb->ctime);
1534 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1535 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1536 sb->md_minor, sb->layout, sb->chunk_size);
1537 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1538 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1539 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1540 sb->failed_disks, sb->spare_disks,
1541 sb->sb_csum, (unsigned long)sb->events_lo);
1542
1543 printk(KERN_INFO);
1544 for (i = 0; i < MD_SB_DISKS; i++) {
1545 mdp_disk_t *desc;
1546
1547 desc = sb->disks + i;
1548 if (desc->number || desc->major || desc->minor ||
1549 desc->raid_disk || (desc->state && (desc->state != 4))) {
1550 printk(" D %2d: ", i);
1551 print_desc(desc);
1552 }
1553 }
1554 printk(KERN_INFO "md: THIS: ");
1555 print_desc(&sb->this_disk);
1556
1557}
1558
1559static void print_rdev(mdk_rdev_t *rdev)
1560{
1561 char b[BDEVNAME_SIZE];
1562 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1563 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001564 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1565 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (rdev->sb_loaded) {
1567 printk(KERN_INFO "md: rdev superblock:\n");
1568 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1569 } else
1570 printk(KERN_INFO "md: no rdev superblock!\n");
1571}
1572
Adrian Bunk5e563412006-06-26 00:27:42 -07001573static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 struct list_head *tmp, *tmp2;
1576 mdk_rdev_t *rdev;
1577 mddev_t *mddev;
1578 char b[BDEVNAME_SIZE];
1579
1580 printk("\n");
1581 printk("md: **********************************\n");
1582 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1583 printk("md: **********************************\n");
1584 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
NeilBrown32a76272005-06-21 17:17:14 -07001586 if (mddev->bitmap)
1587 bitmap_print_sb(mddev->bitmap);
1588 else
1589 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 ITERATE_RDEV(mddev,rdev,tmp2)
1591 printk("<%s>", bdevname(rdev->bdev,b));
1592 printk("\n");
1593
1594 ITERATE_RDEV(mddev,rdev,tmp2)
1595 print_rdev(rdev);
1596 }
1597 printk("md: **********************************\n");
1598 printk("\n");
1599}
1600
1601
NeilBrown42543762006-06-26 00:27:57 -07001602static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
NeilBrown42543762006-06-26 00:27:57 -07001604 /* Update each superblock (in-memory image), but
1605 * if we are allowed to, skip spares which already
1606 * have the right event counter, or have one earlier
1607 * (which would mean they aren't being marked as dirty
1608 * with the rest of the array)
1609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 mdk_rdev_t *rdev;
1611 struct list_head *tmp;
1612
1613 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrown42543762006-06-26 00:27:57 -07001614 if (rdev->sb_events == mddev->events ||
1615 (nospares &&
1616 rdev->raid_disk < 0 &&
1617 (rdev->sb_events&1)==0 &&
1618 rdev->sb_events+1 == mddev->events)) {
1619 /* Don't update this superblock */
1620 rdev->sb_loaded = 2;
1621 } else {
1622 super_types[mddev->major_version].
1623 sync_super(mddev, rdev);
1624 rdev->sb_loaded = 1;
1625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627}
1628
NeilBrown850b2b42006-10-03 01:15:46 -07001629static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 struct list_head *tmp;
1632 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001633 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001634 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001637 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001638
NeilBrown850b2b42006-10-03 01:15:46 -07001639 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1640 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1641 force_change = 1;
1642 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1643 /* just a clean<-> dirty transition, possibly leave spares alone,
1644 * though if events isn't the right even/odd, we will have to do
1645 * spares after all
1646 */
1647 nospares = 1;
1648 if (force_change)
1649 nospares = 0;
1650 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001651 /* If the array is degraded, then skipping spares is both
1652 * dangerous and fairly pointless.
1653 * Dangerous because a device that was removed from the array
1654 * might have a event_count that still looks up-to-date,
1655 * so it can be re-added without a resync.
1656 * Pointless because if there are any spares to skip,
1657 * then a recovery will happen and soon that array won't
1658 * be degraded any more and the spare can go back to sleep then.
1659 */
NeilBrown850b2b42006-10-03 01:15:46 -07001660 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001661
NeilBrown06d91a52005-06-21 17:17:12 -07001662 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001664
1665 /* If this is just a dirty<->clean transition, and the array is clean
1666 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001667 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001668 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001669 && (mddev->events & 1)
1670 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001671 mddev->events--;
1672 else {
1673 /* otherwise we have to go forward and ... */
1674 mddev->events ++;
1675 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1676 /* .. if the array isn't clean, insist on an odd 'events' */
1677 if ((mddev->events&1)==0) {
1678 mddev->events++;
1679 nospares = 0;
1680 }
1681 } else {
1682 /* otherwise insist on an even 'events' (for clean states) */
1683 if ((mddev->events&1)) {
1684 mddev->events++;
1685 nospares = 0;
1686 }
1687 }
1688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 if (!mddev->events) {
1691 /*
1692 * oops, this 64-bit counter should never wrap.
1693 * Either we are in around ~1 trillion A.C., assuming
1694 * 1 reboot per second, or we have a bug:
1695 */
1696 MD_BUG();
1697 mddev->events --;
1698 }
NeilBrown42543762006-06-26 00:27:57 -07001699 sync_sbs(mddev, nospares);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 /*
1702 * do not write anything to disk if using
1703 * nonpersistent superblocks
1704 */
NeilBrown06d91a52005-06-21 17:17:12 -07001705 if (!mddev->persistent) {
NeilBrown850b2b42006-10-03 01:15:46 -07001706 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001707 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001708 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001710 }
NeilBrowna9701a32005-11-08 21:39:34 -08001711 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 dprintk(KERN_INFO
1714 "md: updating %s RAID superblock on device (in sync %d)\n",
1715 mdname(mddev),mddev->in_sync);
1716
NeilBrown4ad13662007-07-17 04:06:13 -07001717 bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 ITERATE_RDEV(mddev,rdev,tmp) {
1719 char b[BDEVNAME_SIZE];
1720 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001721 if (rdev->sb_loaded != 1)
1722 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001723 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 dprintk("(skipping faulty ");
1725
1726 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001727 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001728 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001729 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001730 rdev->sb_page);
1731 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1732 bdevname(rdev->bdev,b),
1733 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001734 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 } else
1737 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001738 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /* only need to write one superblock... */
1740 break;
1741 }
NeilBrowna9701a32005-11-08 21:39:34 -08001742 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001743 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001744
NeilBrowna9701a32005-11-08 21:39:34 -08001745 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001746 if (mddev->in_sync != sync_req ||
1747 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001748 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001749 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001750 goto repeat;
1751 }
NeilBrown850b2b42006-10-03 01:15:46 -07001752 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001753 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001754 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
NeilBrownbce74da2006-01-06 00:20:41 -08001758/* words written to sysfs files may, or my not, be \n terminated.
1759 * We want to accept with case. For this we use cmd_match.
1760 */
1761static int cmd_match(const char *cmd, const char *str)
1762{
1763 /* See if cmd, written into a sysfs file, matches
1764 * str. They must either be the same, or cmd can
1765 * have a trailing newline
1766 */
1767 while (*cmd && *str && *cmd == *str) {
1768 cmd++;
1769 str++;
1770 }
1771 if (*cmd == '\n')
1772 cmd++;
1773 if (*str || *cmd)
1774 return 0;
1775 return 1;
1776}
1777
NeilBrown86e6ffd2005-11-08 21:39:24 -08001778struct rdev_sysfs_entry {
1779 struct attribute attr;
1780 ssize_t (*show)(mdk_rdev_t *, char *);
1781 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1782};
1783
1784static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001785state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001786{
1787 char *sep = "";
1788 int len=0;
1789
NeilBrownb2d444d2005-11-08 21:39:31 -08001790 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001791 len+= sprintf(page+len, "%sfaulty",sep);
1792 sep = ",";
1793 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001794 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001795 len += sprintf(page+len, "%sin_sync",sep);
1796 sep = ",";
1797 }
NeilBrownf6556752006-06-26 00:28:01 -07001798 if (test_bit(WriteMostly, &rdev->flags)) {
1799 len += sprintf(page+len, "%swrite_mostly",sep);
1800 sep = ",";
1801 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001802 if (!test_bit(Faulty, &rdev->flags) &&
1803 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001804 len += sprintf(page+len, "%sspare", sep);
1805 sep = ",";
1806 }
1807 return len+sprintf(page+len, "\n");
1808}
1809
NeilBrown45dc2de2006-06-26 00:27:58 -07001810static ssize_t
1811state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1812{
1813 /* can write
1814 * faulty - simulates and error
1815 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001816 * writemostly - sets write_mostly
1817 * -writemostly - clears write_mostly
NeilBrown45dc2de2006-06-26 00:27:58 -07001818 */
1819 int err = -EINVAL;
1820 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1821 md_error(rdev->mddev, rdev);
1822 err = 0;
1823 } else if (cmd_match(buf, "remove")) {
1824 if (rdev->raid_disk >= 0)
1825 err = -EBUSY;
1826 else {
1827 mddev_t *mddev = rdev->mddev;
1828 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001829 if (mddev->pers)
1830 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001831 md_new_event(mddev);
1832 err = 0;
1833 }
NeilBrownf6556752006-06-26 00:28:01 -07001834 } else if (cmd_match(buf, "writemostly")) {
1835 set_bit(WriteMostly, &rdev->flags);
1836 err = 0;
1837 } else if (cmd_match(buf, "-writemostly")) {
1838 clear_bit(WriteMostly, &rdev->flags);
1839 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001840 }
1841 return err ? err : len;
1842}
NeilBrown80ca3a42006-07-10 04:44:18 -07001843static struct rdev_sysfs_entry rdev_state =
1844__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001845
1846static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001847super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001848{
1849 if (rdev->sb_loaded && rdev->sb_size) {
1850 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1851 return rdev->sb_size;
1852 } else
1853 return 0;
1854}
NeilBrown96de1e62005-11-08 21:39:39 -08001855static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1856
NeilBrown4dbcdc72006-01-06 00:20:52 -08001857static ssize_t
1858errors_show(mdk_rdev_t *rdev, char *page)
1859{
1860 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1861}
1862
1863static ssize_t
1864errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1865{
1866 char *e;
1867 unsigned long n = simple_strtoul(buf, &e, 10);
1868 if (*buf && (*e == 0 || *e == '\n')) {
1869 atomic_set(&rdev->corrected_errors, n);
1870 return len;
1871 }
1872 return -EINVAL;
1873}
1874static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001875__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001876
NeilBrown014236d2006-01-06 00:20:55 -08001877static ssize_t
1878slot_show(mdk_rdev_t *rdev, char *page)
1879{
1880 if (rdev->raid_disk < 0)
1881 return sprintf(page, "none\n");
1882 else
1883 return sprintf(page, "%d\n", rdev->raid_disk);
1884}
1885
1886static ssize_t
1887slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1888{
1889 char *e;
1890 int slot = simple_strtoul(buf, &e, 10);
1891 if (strncmp(buf, "none", 4)==0)
1892 slot = -1;
1893 else if (e==buf || (*e && *e!= '\n'))
1894 return -EINVAL;
1895 if (rdev->mddev->pers)
1896 /* Cannot set slot in active array (yet) */
1897 return -EBUSY;
1898 if (slot >= rdev->mddev->raid_disks)
1899 return -ENOSPC;
1900 rdev->raid_disk = slot;
1901 /* assume it is working */
1902 rdev->flags = 0;
1903 set_bit(In_sync, &rdev->flags);
1904 return len;
1905}
1906
1907
1908static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001909__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001910
NeilBrown93c8cad2006-01-06 00:20:56 -08001911static ssize_t
1912offset_show(mdk_rdev_t *rdev, char *page)
1913{
Andrew Morton6961ece2006-01-06 00:20:59 -08001914 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001915}
1916
1917static ssize_t
1918offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1919{
1920 char *e;
1921 unsigned long long offset = simple_strtoull(buf, &e, 10);
1922 if (e==buf || (*e && *e != '\n'))
1923 return -EINVAL;
1924 if (rdev->mddev->pers)
1925 return -EBUSY;
1926 rdev->data_offset = offset;
1927 return len;
1928}
1929
1930static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001931__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001932
NeilBrown83303b62006-01-06 00:21:06 -08001933static ssize_t
1934rdev_size_show(mdk_rdev_t *rdev, char *page)
1935{
1936 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1937}
1938
1939static ssize_t
1940rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1941{
1942 char *e;
1943 unsigned long long size = simple_strtoull(buf, &e, 10);
1944 if (e==buf || (*e && *e != '\n'))
1945 return -EINVAL;
1946 if (rdev->mddev->pers)
1947 return -EBUSY;
1948 rdev->size = size;
1949 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1950 rdev->mddev->size = size;
1951 return len;
1952}
1953
1954static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07001955__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08001956
NeilBrown86e6ffd2005-11-08 21:39:24 -08001957static struct attribute *rdev_default_attrs[] = {
1958 &rdev_state.attr,
1959 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001960 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001961 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001962 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001963 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001964 NULL,
1965};
1966static ssize_t
1967rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1968{
1969 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1970 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1971
1972 if (!entry->show)
1973 return -EIO;
1974 return entry->show(rdev, page);
1975}
1976
1977static ssize_t
1978rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1979 const char *page, size_t length)
1980{
1981 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1982 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1983
1984 if (!entry->store)
1985 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07001986 if (!capable(CAP_SYS_ADMIN))
1987 return -EACCES;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001988 return entry->store(rdev, page, length);
1989}
1990
1991static void rdev_free(struct kobject *ko)
1992{
1993 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1994 kfree(rdev);
1995}
1996static struct sysfs_ops rdev_sysfs_ops = {
1997 .show = rdev_attr_show,
1998 .store = rdev_attr_store,
1999};
2000static struct kobj_type rdev_ktype = {
2001 .release = rdev_free,
2002 .sysfs_ops = &rdev_sysfs_ops,
2003 .default_attrs = rdev_default_attrs,
2004};
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006/*
2007 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2008 *
2009 * mark the device faulty if:
2010 *
2011 * - the device is nonexistent (zero size)
2012 * - the device has no valid superblock
2013 *
2014 * a faulty rdev _never_ has rdev->sb set.
2015 */
2016static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2017{
2018 char b[BDEVNAME_SIZE];
2019 int err;
2020 mdk_rdev_t *rdev;
2021 sector_t size;
2022
NeilBrown9ffae0c2006-01-06 00:20:32 -08002023 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (!rdev) {
2025 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2026 return ERR_PTR(-ENOMEM);
2027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 if ((err = alloc_disk_sb(rdev)))
2030 goto abort_free;
2031
2032 err = lock_rdev(rdev, newdev);
2033 if (err)
2034 goto abort_free;
2035
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002036 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002039 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002040 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002041 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002043 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002045 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002046 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2049 if (!size) {
2050 printk(KERN_WARNING
2051 "md: %s has zero or unknown size, marking faulty!\n",
2052 bdevname(rdev->bdev,b));
2053 err = -EINVAL;
2054 goto abort_free;
2055 }
2056
2057 if (super_format >= 0) {
2058 err = super_types[super_format].
2059 load_super(rdev, NULL, super_minor);
2060 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002061 printk(KERN_WARNING
2062 "md: %s does not have a valid v%d.%d "
2063 "superblock, not importing!\n",
2064 bdevname(rdev->bdev,b),
2065 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 goto abort_free;
2067 }
2068 if (err < 0) {
2069 printk(KERN_WARNING
2070 "md: could not read %s's sb, not importing!\n",
2071 bdevname(rdev->bdev,b));
2072 goto abort_free;
2073 }
2074 }
2075 INIT_LIST_HEAD(&rdev->same_set);
2076
2077 return rdev;
2078
2079abort_free:
2080 if (rdev->sb_page) {
2081 if (rdev->bdev)
2082 unlock_rdev(rdev);
2083 free_disk_sb(rdev);
2084 }
2085 kfree(rdev);
2086 return ERR_PTR(err);
2087}
2088
2089/*
2090 * Check a full RAID array for plausibility
2091 */
2092
2093
NeilBrowna757e642005-04-16 15:26:42 -07002094static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
2096 int i;
2097 struct list_head *tmp;
2098 mdk_rdev_t *rdev, *freshest;
2099 char b[BDEVNAME_SIZE];
2100
2101 freshest = NULL;
2102 ITERATE_RDEV(mddev,rdev,tmp)
2103 switch (super_types[mddev->major_version].
2104 load_super(rdev, freshest, mddev->minor_version)) {
2105 case 1:
2106 freshest = rdev;
2107 break;
2108 case 0:
2109 break;
2110 default:
2111 printk( KERN_ERR \
2112 "md: fatal superblock inconsistency in %s"
2113 " -- removing from array\n",
2114 bdevname(rdev->bdev,b));
2115 kick_rdev_from_array(rdev);
2116 }
2117
2118
2119 super_types[mddev->major_version].
2120 validate_super(mddev, freshest);
2121
2122 i = 0;
2123 ITERATE_RDEV(mddev,rdev,tmp) {
2124 if (rdev != freshest)
2125 if (super_types[mddev->major_version].
2126 validate_super(mddev, rdev)) {
2127 printk(KERN_WARNING "md: kicking non-fresh %s"
2128 " from array!\n",
2129 bdevname(rdev->bdev,b));
2130 kick_rdev_from_array(rdev);
2131 continue;
2132 }
2133 if (mddev->level == LEVEL_MULTIPATH) {
2134 rdev->desc_nr = i++;
2135 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002136 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002137 } else if (rdev->raid_disk >= mddev->raid_disks) {
2138 rdev->raid_disk = -1;
2139 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 }
2142
2143
2144
2145 if (mddev->recovery_cp != MaxSector &&
2146 mddev->level >= 1)
2147 printk(KERN_ERR "md: %s: raid array is not clean"
2148 " -- starting background reconstruction\n",
2149 mdname(mddev));
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
NeilBrowneae17012005-11-08 21:39:23 -08002153static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002154safe_delay_show(mddev_t *mddev, char *page)
2155{
2156 int msec = (mddev->safemode_delay*1000)/HZ;
2157 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2158}
2159static ssize_t
2160safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2161{
2162 int scale=1;
2163 int dot=0;
2164 int i;
2165 unsigned long msec;
2166 char buf[30];
2167 char *e;
2168 /* remove a period, and count digits after it */
2169 if (len >= sizeof(buf))
2170 return -EINVAL;
2171 strlcpy(buf, cbuf, len);
2172 buf[len] = 0;
2173 for (i=0; i<len; i++) {
2174 if (dot) {
2175 if (isdigit(buf[i])) {
2176 buf[i-1] = buf[i];
2177 scale *= 10;
2178 }
2179 buf[i] = 0;
2180 } else if (buf[i] == '.') {
2181 dot=1;
2182 buf[i] = 0;
2183 }
2184 }
2185 msec = simple_strtoul(buf, &e, 10);
2186 if (e == buf || (*e && *e != '\n'))
2187 return -EINVAL;
2188 msec = (msec * 1000) / scale;
2189 if (msec == 0)
2190 mddev->safemode_delay = 0;
2191 else {
2192 mddev->safemode_delay = (msec*HZ)/1000;
2193 if (mddev->safemode_delay == 0)
2194 mddev->safemode_delay = 1;
2195 }
2196 return len;
2197}
2198static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002199__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002200
2201static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002202level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002203{
NeilBrown2604b702006-01-06 00:20:36 -08002204 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002205 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002206 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002207 else if (mddev->clevel[0])
2208 return sprintf(page, "%s\n", mddev->clevel);
2209 else if (mddev->level != LEVEL_NONE)
2210 return sprintf(page, "%d\n", mddev->level);
2211 else
2212 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002213}
2214
NeilBrownd9d166c2006-01-06 00:20:51 -08002215static ssize_t
2216level_store(mddev_t *mddev, const char *buf, size_t len)
2217{
2218 int rv = len;
2219 if (mddev->pers)
2220 return -EBUSY;
2221 if (len == 0)
2222 return 0;
2223 if (len >= sizeof(mddev->clevel))
2224 return -ENOSPC;
2225 strncpy(mddev->clevel, buf, len);
2226 if (mddev->clevel[len-1] == '\n')
2227 len--;
2228 mddev->clevel[len] = 0;
2229 mddev->level = LEVEL_NONE;
2230 return rv;
2231}
2232
2233static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002234__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002235
NeilBrownd4dbd022006-06-26 00:27:59 -07002236
2237static ssize_t
2238layout_show(mddev_t *mddev, char *page)
2239{
2240 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002241 if (mddev->reshape_position != MaxSector &&
2242 mddev->layout != mddev->new_layout)
2243 return sprintf(page, "%d (%d)\n",
2244 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002245 return sprintf(page, "%d\n", mddev->layout);
2246}
2247
2248static ssize_t
2249layout_store(mddev_t *mddev, const char *buf, size_t len)
2250{
2251 char *e;
2252 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002253
2254 if (!*buf || (*e && *e != '\n'))
2255 return -EINVAL;
2256
NeilBrown08a02ec2007-05-09 02:35:38 -07002257 if (mddev->pers)
2258 return -EBUSY;
2259 if (mddev->reshape_position != MaxSector)
2260 mddev->new_layout = n;
2261 else
2262 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002263 return len;
2264}
2265static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002266__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002267
2268
NeilBrowneae17012005-11-08 21:39:23 -08002269static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002270raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002271{
NeilBrownbb636542005-11-08 21:39:45 -08002272 if (mddev->raid_disks == 0)
2273 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002274 if (mddev->reshape_position != MaxSector &&
2275 mddev->delta_disks != 0)
2276 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2277 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002278 return sprintf(page, "%d\n", mddev->raid_disks);
2279}
2280
NeilBrownda943b992006-01-06 00:20:54 -08002281static int update_raid_disks(mddev_t *mddev, int raid_disks);
2282
2283static ssize_t
2284raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2285{
NeilBrownda943b992006-01-06 00:20:54 -08002286 char *e;
2287 int rv = 0;
2288 unsigned long n = simple_strtoul(buf, &e, 10);
2289
2290 if (!*buf || (*e && *e != '\n'))
2291 return -EINVAL;
2292
2293 if (mddev->pers)
2294 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002295 else if (mddev->reshape_position != MaxSector) {
2296 int olddisks = mddev->raid_disks - mddev->delta_disks;
2297 mddev->delta_disks = n - olddisks;
2298 mddev->raid_disks = n;
2299 } else
NeilBrownda943b992006-01-06 00:20:54 -08002300 mddev->raid_disks = n;
2301 return rv ? rv : len;
2302}
2303static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002304__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002305
NeilBrown24dd4692005-11-08 21:39:26 -08002306static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002307chunk_size_show(mddev_t *mddev, char *page)
2308{
NeilBrown08a02ec2007-05-09 02:35:38 -07002309 if (mddev->reshape_position != MaxSector &&
2310 mddev->chunk_size != mddev->new_chunk)
2311 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2312 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002313 return sprintf(page, "%d\n", mddev->chunk_size);
2314}
2315
2316static ssize_t
2317chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2318{
2319 /* can only set chunk_size if array is not yet active */
2320 char *e;
2321 unsigned long n = simple_strtoul(buf, &e, 10);
2322
NeilBrown3b343802006-01-06 00:20:47 -08002323 if (!*buf || (*e && *e != '\n'))
2324 return -EINVAL;
2325
NeilBrown08a02ec2007-05-09 02:35:38 -07002326 if (mddev->pers)
2327 return -EBUSY;
2328 else if (mddev->reshape_position != MaxSector)
2329 mddev->new_chunk = n;
2330 else
2331 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002332 return len;
2333}
2334static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002335__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002336
NeilBrowna94213b2006-06-26 00:28:00 -07002337static ssize_t
2338resync_start_show(mddev_t *mddev, char *page)
2339{
2340 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2341}
2342
2343static ssize_t
2344resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2345{
2346 /* can only set chunk_size if array is not yet active */
2347 char *e;
2348 unsigned long long n = simple_strtoull(buf, &e, 10);
2349
2350 if (mddev->pers)
2351 return -EBUSY;
2352 if (!*buf || (*e && *e != '\n'))
2353 return -EINVAL;
2354
2355 mddev->recovery_cp = n;
2356 return len;
2357}
2358static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002359__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002360
NeilBrown9e653b62006-06-26 00:27:58 -07002361/*
2362 * The array state can be:
2363 *
2364 * clear
2365 * No devices, no size, no level
2366 * Equivalent to STOP_ARRAY ioctl
2367 * inactive
2368 * May have some settings, but array is not active
2369 * all IO results in error
2370 * When written, doesn't tear down array, but just stops it
2371 * suspended (not supported yet)
2372 * All IO requests will block. The array can be reconfigured.
2373 * Writing this, if accepted, will block until array is quiessent
2374 * readonly
2375 * no resync can happen. no superblocks get written.
2376 * write requests fail
2377 * read-auto
2378 * like readonly, but behaves like 'clean' on a write request.
2379 *
2380 * clean - no pending writes, but otherwise active.
2381 * When written to inactive array, starts without resync
2382 * If a write request arrives then
2383 * if metadata is known, mark 'dirty' and switch to 'active'.
2384 * if not known, block and switch to write-pending
2385 * If written to an active array that has pending writes, then fails.
2386 * active
2387 * fully active: IO and resync can be happening.
2388 * When written to inactive array, starts with resync
2389 *
2390 * write-pending
2391 * clean, but writes are blocked waiting for 'active' to be written.
2392 *
2393 * active-idle
2394 * like active, but no writes have been seen for a while (100msec).
2395 *
2396 */
2397enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2398 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002399static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002400 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2401 "write-pending", "active-idle", NULL };
2402
2403static int match_word(const char *word, char **list)
2404{
2405 int n;
2406 for (n=0; list[n]; n++)
2407 if (cmd_match(word, list[n]))
2408 break;
2409 return n;
2410}
2411
2412static ssize_t
2413array_state_show(mddev_t *mddev, char *page)
2414{
2415 enum array_state st = inactive;
2416
2417 if (mddev->pers)
2418 switch(mddev->ro) {
2419 case 1:
2420 st = readonly;
2421 break;
2422 case 2:
2423 st = read_auto;
2424 break;
2425 case 0:
2426 if (mddev->in_sync)
2427 st = clean;
2428 else if (mddev->safemode)
2429 st = active_idle;
2430 else
2431 st = active;
2432 }
2433 else {
2434 if (list_empty(&mddev->disks) &&
2435 mddev->raid_disks == 0 &&
2436 mddev->size == 0)
2437 st = clear;
2438 else
2439 st = inactive;
2440 }
2441 return sprintf(page, "%s\n", array_states[st]);
2442}
2443
2444static int do_md_stop(mddev_t * mddev, int ro);
2445static int do_md_run(mddev_t * mddev);
2446static int restart_array(mddev_t *mddev);
2447
2448static ssize_t
2449array_state_store(mddev_t *mddev, const char *buf, size_t len)
2450{
2451 int err = -EINVAL;
2452 enum array_state st = match_word(buf, array_states);
2453 switch(st) {
2454 case bad_word:
2455 break;
2456 case clear:
2457 /* stopping an active array */
2458 if (mddev->pers) {
2459 if (atomic_read(&mddev->active) > 1)
2460 return -EBUSY;
2461 err = do_md_stop(mddev, 0);
2462 }
2463 break;
2464 case inactive:
2465 /* stopping an active array */
2466 if (mddev->pers) {
2467 if (atomic_read(&mddev->active) > 1)
2468 return -EBUSY;
2469 err = do_md_stop(mddev, 2);
2470 }
2471 break;
2472 case suspended:
2473 break; /* not supported yet */
2474 case readonly:
2475 if (mddev->pers)
2476 err = do_md_stop(mddev, 1);
2477 else {
2478 mddev->ro = 1;
2479 err = do_md_run(mddev);
2480 }
2481 break;
2482 case read_auto:
2483 /* stopping an active array */
2484 if (mddev->pers) {
2485 err = do_md_stop(mddev, 1);
2486 if (err == 0)
2487 mddev->ro = 2; /* FIXME mark devices writable */
2488 } else {
2489 mddev->ro = 2;
2490 err = do_md_run(mddev);
2491 }
2492 break;
2493 case clean:
2494 if (mddev->pers) {
2495 restart_array(mddev);
2496 spin_lock_irq(&mddev->write_lock);
2497 if (atomic_read(&mddev->writes_pending) == 0) {
2498 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07002499 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002500 }
2501 spin_unlock_irq(&mddev->write_lock);
2502 } else {
2503 mddev->ro = 0;
2504 mddev->recovery_cp = MaxSector;
2505 err = do_md_run(mddev);
2506 }
2507 break;
2508 case active:
2509 if (mddev->pers) {
2510 restart_array(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002511 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002512 wake_up(&mddev->sb_wait);
2513 err = 0;
2514 } else {
2515 mddev->ro = 0;
2516 err = do_md_run(mddev);
2517 }
2518 break;
2519 case write_pending:
2520 case active_idle:
2521 /* these cannot be set */
2522 break;
2523 }
2524 if (err)
2525 return err;
2526 else
2527 return len;
2528}
NeilBrown80ca3a42006-07-10 04:44:18 -07002529static struct md_sysfs_entry md_array_state =
2530__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002531
NeilBrown6d7ff732006-01-06 00:21:16 -08002532static ssize_t
2533null_show(mddev_t *mddev, char *page)
2534{
2535 return -EINVAL;
2536}
2537
2538static ssize_t
2539new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2540{
2541 /* buf must be %d:%d\n? giving major and minor numbers */
2542 /* The new device is added to the array.
2543 * If the array has a persistent superblock, we read the
2544 * superblock to initialise info and check validity.
2545 * Otherwise, only checking done is that in bind_rdev_to_array,
2546 * which mainly checks size.
2547 */
2548 char *e;
2549 int major = simple_strtoul(buf, &e, 10);
2550 int minor;
2551 dev_t dev;
2552 mdk_rdev_t *rdev;
2553 int err;
2554
2555 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2556 return -EINVAL;
2557 minor = simple_strtoul(e+1, &e, 10);
2558 if (*e && *e != '\n')
2559 return -EINVAL;
2560 dev = MKDEV(major, minor);
2561 if (major != MAJOR(dev) ||
2562 minor != MINOR(dev))
2563 return -EOVERFLOW;
2564
2565
2566 if (mddev->persistent) {
2567 rdev = md_import_device(dev, mddev->major_version,
2568 mddev->minor_version);
2569 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2570 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2571 mdk_rdev_t, same_set);
2572 err = super_types[mddev->major_version]
2573 .load_super(rdev, rdev0, mddev->minor_version);
2574 if (err < 0)
2575 goto out;
2576 }
2577 } else
2578 rdev = md_import_device(dev, -1, -1);
2579
2580 if (IS_ERR(rdev))
2581 return PTR_ERR(rdev);
2582 err = bind_rdev_to_array(rdev, mddev);
2583 out:
2584 if (err)
2585 export_rdev(rdev);
2586 return err ? err : len;
2587}
2588
2589static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002590__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002591
2592static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002593bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2594{
2595 char *end;
2596 unsigned long chunk, end_chunk;
2597
2598 if (!mddev->bitmap)
2599 goto out;
2600 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2601 while (*buf) {
2602 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2603 if (buf == end) break;
2604 if (*end == '-') { /* range */
2605 buf = end + 1;
2606 end_chunk = simple_strtoul(buf, &end, 0);
2607 if (buf == end) break;
2608 }
2609 if (*end && !isspace(*end)) break;
2610 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2611 buf = end;
2612 while (isspace(*buf)) buf++;
2613 }
2614 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2615out:
2616 return len;
2617}
2618
2619static struct md_sysfs_entry md_bitmap =
2620__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2621
2622static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002623size_show(mddev_t *mddev, char *page)
2624{
2625 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2626}
2627
2628static int update_size(mddev_t *mddev, unsigned long size);
2629
2630static ssize_t
2631size_store(mddev_t *mddev, const char *buf, size_t len)
2632{
2633 /* If array is inactive, we can reduce the component size, but
2634 * not increase it (except from 0).
2635 * If array is active, we can try an on-line resize
2636 */
2637 char *e;
2638 int err = 0;
2639 unsigned long long size = simple_strtoull(buf, &e, 10);
2640 if (!*buf || *buf == '\n' ||
2641 (*e && *e != '\n'))
2642 return -EINVAL;
2643
2644 if (mddev->pers) {
2645 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002646 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002647 } else {
2648 if (mddev->size == 0 ||
2649 mddev->size > size)
2650 mddev->size = size;
2651 else
2652 err = -ENOSPC;
2653 }
2654 return err ? err : len;
2655}
2656
2657static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002658__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002659
NeilBrown8bb93aa2006-01-06 00:20:50 -08002660
2661/* Metdata version.
2662 * This is either 'none' for arrays with externally managed metadata,
2663 * or N.M for internally known formats
2664 */
2665static ssize_t
2666metadata_show(mddev_t *mddev, char *page)
2667{
2668 if (mddev->persistent)
2669 return sprintf(page, "%d.%d\n",
2670 mddev->major_version, mddev->minor_version);
2671 else
2672 return sprintf(page, "none\n");
2673}
2674
2675static ssize_t
2676metadata_store(mddev_t *mddev, const char *buf, size_t len)
2677{
2678 int major, minor;
2679 char *e;
2680 if (!list_empty(&mddev->disks))
2681 return -EBUSY;
2682
2683 if (cmd_match(buf, "none")) {
2684 mddev->persistent = 0;
2685 mddev->major_version = 0;
2686 mddev->minor_version = 90;
2687 return len;
2688 }
2689 major = simple_strtoul(buf, &e, 10);
2690 if (e==buf || *e != '.')
2691 return -EINVAL;
2692 buf = e+1;
2693 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002694 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002695 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002696 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002697 return -ENOENT;
2698 mddev->major_version = major;
2699 mddev->minor_version = minor;
2700 mddev->persistent = 1;
2701 return len;
2702}
2703
2704static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002705__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002706
NeilBrowna35b0d62006-01-06 00:20:49 -08002707static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002708action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002709{
NeilBrown7eec3142005-11-08 21:39:44 -08002710 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002711 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002712 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002713 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2714 type = "reshape";
2715 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002716 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2717 type = "resync";
2718 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2719 type = "check";
2720 else
2721 type = "repair";
2722 } else
2723 type = "recover";
2724 }
2725 return sprintf(page, "%s\n", type);
2726}
2727
2728static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002729action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002730{
NeilBrown7eec3142005-11-08 21:39:44 -08002731 if (!mddev->pers || !mddev->pers->sync_request)
2732 return -EINVAL;
2733
NeilBrownbce74da2006-01-06 00:20:41 -08002734 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002735 if (mddev->sync_thread) {
2736 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2737 md_unregister_thread(mddev->sync_thread);
2738 mddev->sync_thread = NULL;
2739 mddev->recovery = 0;
2740 }
NeilBrown03c902e2006-01-06 00:20:46 -08002741 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2742 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002743 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002744 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002745 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002746 else if (cmd_match(page, "reshape")) {
2747 int err;
2748 if (mddev->pers->start_reshape == NULL)
2749 return -EINVAL;
2750 err = mddev->pers->start_reshape(mddev);
2751 if (err)
2752 return err;
2753 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002754 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002755 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002756 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002757 return -EINVAL;
2758 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2759 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002760 }
NeilBrown03c902e2006-01-06 00:20:46 -08002761 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002762 md_wakeup_thread(mddev->thread);
2763 return len;
2764}
2765
NeilBrown9d888832005-11-08 21:39:26 -08002766static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002767mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002768{
2769 return sprintf(page, "%llu\n",
2770 (unsigned long long) mddev->resync_mismatches);
2771}
2772
NeilBrown80ca3a42006-07-10 04:44:18 -07002773static struct md_sysfs_entry md_scan_mode =
2774__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002775
NeilBrown96de1e62005-11-08 21:39:39 -08002776
NeilBrown80ca3a42006-07-10 04:44:18 -07002777static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002778
NeilBrown88202a02006-01-06 00:21:36 -08002779static ssize_t
2780sync_min_show(mddev_t *mddev, char *page)
2781{
2782 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2783 mddev->sync_speed_min ? "local": "system");
2784}
2785
2786static ssize_t
2787sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2788{
2789 int min;
2790 char *e;
2791 if (strncmp(buf, "system", 6)==0) {
2792 mddev->sync_speed_min = 0;
2793 return len;
2794 }
2795 min = simple_strtoul(buf, &e, 10);
2796 if (buf == e || (*e && *e != '\n') || min <= 0)
2797 return -EINVAL;
2798 mddev->sync_speed_min = min;
2799 return len;
2800}
2801
2802static struct md_sysfs_entry md_sync_min =
2803__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2804
2805static ssize_t
2806sync_max_show(mddev_t *mddev, char *page)
2807{
2808 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2809 mddev->sync_speed_max ? "local": "system");
2810}
2811
2812static ssize_t
2813sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2814{
2815 int max;
2816 char *e;
2817 if (strncmp(buf, "system", 6)==0) {
2818 mddev->sync_speed_max = 0;
2819 return len;
2820 }
2821 max = simple_strtoul(buf, &e, 10);
2822 if (buf == e || (*e && *e != '\n') || max <= 0)
2823 return -EINVAL;
2824 mddev->sync_speed_max = max;
2825 return len;
2826}
2827
2828static struct md_sysfs_entry md_sync_max =
2829__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2830
Iustin Popd7f3d292007-10-16 23:30:54 -07002831static ssize_t
2832degraded_show(mddev_t *mddev, char *page)
2833{
2834 return sprintf(page, "%d\n", mddev->degraded);
2835}
2836static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08002837
2838static ssize_t
2839sync_speed_show(mddev_t *mddev, char *page)
2840{
2841 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07002842 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08002843 dt = ((jiffies - mddev->resync_mark) / HZ);
2844 if (!dt) dt++;
2845 db = resync - (mddev->resync_mark_cnt);
2846 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2847}
2848
NeilBrown80ca3a42006-07-10 04:44:18 -07002849static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08002850
2851static ssize_t
2852sync_completed_show(mddev_t *mddev, char *page)
2853{
2854 unsigned long max_blocks, resync;
2855
2856 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2857 max_blocks = mddev->resync_max_sectors;
2858 else
2859 max_blocks = mddev->size << 1;
2860
2861 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2862 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2863}
2864
NeilBrown80ca3a42006-07-10 04:44:18 -07002865static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08002866
NeilBrowne464eaf2006-03-27 01:18:14 -08002867static ssize_t
2868suspend_lo_show(mddev_t *mddev, char *page)
2869{
2870 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2871}
2872
2873static ssize_t
2874suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2875{
2876 char *e;
2877 unsigned long long new = simple_strtoull(buf, &e, 10);
2878
2879 if (mddev->pers->quiesce == NULL)
2880 return -EINVAL;
2881 if (buf == e || (*e && *e != '\n'))
2882 return -EINVAL;
2883 if (new >= mddev->suspend_hi ||
2884 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2885 mddev->suspend_lo = new;
2886 mddev->pers->quiesce(mddev, 2);
2887 return len;
2888 } else
2889 return -EINVAL;
2890}
2891static struct md_sysfs_entry md_suspend_lo =
2892__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2893
2894
2895static ssize_t
2896suspend_hi_show(mddev_t *mddev, char *page)
2897{
2898 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2899}
2900
2901static ssize_t
2902suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2903{
2904 char *e;
2905 unsigned long long new = simple_strtoull(buf, &e, 10);
2906
2907 if (mddev->pers->quiesce == NULL)
2908 return -EINVAL;
2909 if (buf == e || (*e && *e != '\n'))
2910 return -EINVAL;
2911 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2912 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2913 mddev->suspend_hi = new;
2914 mddev->pers->quiesce(mddev, 1);
2915 mddev->pers->quiesce(mddev, 0);
2916 return len;
2917 } else
2918 return -EINVAL;
2919}
2920static struct md_sysfs_entry md_suspend_hi =
2921__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2922
NeilBrown08a02ec2007-05-09 02:35:38 -07002923static ssize_t
2924reshape_position_show(mddev_t *mddev, char *page)
2925{
2926 if (mddev->reshape_position != MaxSector)
2927 return sprintf(page, "%llu\n",
2928 (unsigned long long)mddev->reshape_position);
2929 strcpy(page, "none\n");
2930 return 5;
2931}
2932
2933static ssize_t
2934reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
2935{
2936 char *e;
2937 unsigned long long new = simple_strtoull(buf, &e, 10);
2938 if (mddev->pers)
2939 return -EBUSY;
2940 if (buf == e || (*e && *e != '\n'))
2941 return -EINVAL;
2942 mddev->reshape_position = new;
2943 mddev->delta_disks = 0;
2944 mddev->new_level = mddev->level;
2945 mddev->new_layout = mddev->layout;
2946 mddev->new_chunk = mddev->chunk_size;
2947 return len;
2948}
2949
2950static struct md_sysfs_entry md_reshape_position =
2951__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
2952 reshape_position_store);
2953
NeilBrowne464eaf2006-03-27 01:18:14 -08002954
NeilBrowneae17012005-11-08 21:39:23 -08002955static struct attribute *md_default_attrs[] = {
2956 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07002957 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002958 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002959 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002960 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07002961 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002962 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08002963 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07002964 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07002965 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07002966 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002967 NULL,
2968};
2969
2970static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002971 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002972 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002973 &md_sync_min.attr,
2974 &md_sync_max.attr,
2975 &md_sync_speed.attr,
2976 &md_sync_completed.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08002977 &md_suspend_lo.attr,
2978 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07002979 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07002980 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002981 NULL,
2982};
NeilBrown411036f2005-11-08 21:39:40 -08002983static struct attribute_group md_redundancy_group = {
2984 .name = NULL,
2985 .attrs = md_redundancy_attrs,
2986};
2987
NeilBrowneae17012005-11-08 21:39:23 -08002988
2989static ssize_t
2990md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2991{
2992 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2993 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002994 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002995
2996 if (!entry->show)
2997 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002998 rv = mddev_lock(mddev);
2999 if (!rv) {
3000 rv = entry->show(mddev, page);
3001 mddev_unlock(mddev);
3002 }
NeilBrown96de1e62005-11-08 21:39:39 -08003003 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003004}
3005
3006static ssize_t
3007md_attr_store(struct kobject *kobj, struct attribute *attr,
3008 const char *page, size_t length)
3009{
3010 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3011 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003012 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003013
3014 if (!entry->store)
3015 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003016 if (!capable(CAP_SYS_ADMIN))
3017 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003018 rv = mddev_lock(mddev);
3019 if (!rv) {
3020 rv = entry->store(mddev, page, length);
3021 mddev_unlock(mddev);
3022 }
NeilBrown96de1e62005-11-08 21:39:39 -08003023 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003024}
3025
3026static void md_free(struct kobject *ko)
3027{
3028 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3029 kfree(mddev);
3030}
3031
3032static struct sysfs_ops md_sysfs_ops = {
3033 .show = md_attr_show,
3034 .store = md_attr_store,
3035};
3036static struct kobj_type md_ktype = {
3037 .release = md_free,
3038 .sysfs_ops = &md_sysfs_ops,
3039 .default_attrs = md_default_attrs,
3040};
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042int mdp_major = 0;
3043
3044static struct kobject *md_probe(dev_t dev, int *part, void *data)
3045{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003046 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 mddev_t *mddev = mddev_find(dev);
3048 struct gendisk *disk;
3049 int partitioned = (MAJOR(dev) != MD_MAJOR);
3050 int shift = partitioned ? MdpMinorShift : 0;
3051 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003052 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
3054 if (!mddev)
3055 return NULL;
3056
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003057 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003059 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 mddev_put(mddev);
3061 return NULL;
3062 }
3063 disk = alloc_disk(1 << shift);
3064 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003065 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 mddev_put(mddev);
3067 return NULL;
3068 }
3069 disk->major = MAJOR(dev);
3070 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003071 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003073 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 disk->fops = &md_fops;
3076 disk->private_data = mddev;
3077 disk->queue = mddev->queue;
3078 add_disk(disk);
3079 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003080 mutex_unlock(&disks_mutex);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003081 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003082 "%s", "md");
3083 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003084 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3085 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003086 else
3087 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 return NULL;
3089}
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091static void md_safemode_timeout(unsigned long data)
3092{
3093 mddev_t *mddev = (mddev_t *) data;
3094
3095 mddev->safemode = 1;
3096 md_wakeup_thread(mddev->thread);
3097}
3098
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003099static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
3101static int do_md_run(mddev_t * mddev)
3102{
NeilBrown2604b702006-01-06 00:20:36 -08003103 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 int chunk_size;
3105 struct list_head *tmp;
3106 mdk_rdev_t *rdev;
3107 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003108 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 char b[BDEVNAME_SIZE];
3110
NeilBrowna757e642005-04-16 15:26:42 -07003111 if (list_empty(&mddev->disks))
3112 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 if (mddev->pers)
3116 return -EBUSY;
3117
3118 /*
3119 * Analyze all RAID superblock(s)
3120 */
NeilBrowna757e642005-04-16 15:26:42 -07003121 if (!mddev->raid_disks)
3122 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
3124 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
NeilBrown2604b702006-01-06 00:20:36 -08003126 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (chunk_size > MAX_CHUNK_SIZE) {
3128 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3129 chunk_size, MAX_CHUNK_SIZE);
3130 return -EINVAL;
3131 }
3132 /*
3133 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3134 */
3135 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003136 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 return -EINVAL;
3138 }
3139 if (chunk_size < PAGE_SIZE) {
3140 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3141 chunk_size, PAGE_SIZE);
3142 return -EINVAL;
3143 }
3144
3145 /* devices must have minimum size of one chunk */
3146 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003147 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 continue;
3149 if (rdev->size < chunk_size / 1024) {
3150 printk(KERN_WARNING
3151 "md: Dev %s smaller than chunk_size:"
3152 " %lluk < %dk\n",
3153 bdevname(rdev->bdev,b),
3154 (unsigned long long)rdev->size,
3155 chunk_size / 1024);
3156 return -EINVAL;
3157 }
3158 }
3159 }
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003162 if (mddev->level != LEVEL_NONE)
3163 request_module("md-level-%d", mddev->level);
3164 else if (mddev->clevel[0])
3165 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166#endif
3167
3168 /*
3169 * Drop all container device buffers, from now on
3170 * the only valid external interface is through the md
3171 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 */
3173 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003174 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 continue;
3176 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003177 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003178
3179 /* perform some consistency tests on the device.
3180 * We don't want the data to overlap the metadata,
3181 * Internal Bitmap issues has handled elsewhere.
3182 */
3183 if (rdev->data_offset < rdev->sb_offset) {
3184 if (mddev->size &&
3185 rdev->data_offset + mddev->size*2
3186 > rdev->sb_offset*2) {
3187 printk("md: %s: data overlaps metadata\n",
3188 mdname(mddev));
3189 return -EINVAL;
3190 }
3191 } else {
3192 if (rdev->sb_offset*2 + rdev->sb_size/512
3193 > rdev->data_offset) {
3194 printk("md: %s: metadata overlaps data\n",
3195 mdname(mddev));
3196 return -EINVAL;
3197 }
3198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
3200
3201 md_probe(mddev->unit, NULL, NULL);
3202 disk = mddev->gendisk;
3203 if (!disk)
3204 return -ENOMEM;
3205
3206 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003207 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003208 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003210 if (mddev->level != LEVEL_NONE)
3211 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3212 mddev->level);
3213 else
3214 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3215 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 return -EINVAL;
3217 }
NeilBrown2604b702006-01-06 00:20:36 -08003218 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003220 mddev->level = pers->level;
3221 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
NeilBrownf6705572006-03-27 01:18:11 -08003223 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003224 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003225 /* This personality cannot handle reshaping... */
3226 mddev->pers = NULL;
3227 module_put(pers->owner);
3228 return -EINVAL;
3229 }
3230
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003231 if (pers->sync_request) {
3232 /* Warn if this is a potentially silly
3233 * configuration.
3234 */
3235 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3236 mdk_rdev_t *rdev2;
3237 struct list_head *tmp2;
3238 int warned = 0;
3239 ITERATE_RDEV(mddev, rdev, tmp) {
3240 ITERATE_RDEV(mddev, rdev2, tmp2) {
3241 if (rdev < rdev2 &&
3242 rdev->bdev->bd_contains ==
3243 rdev2->bdev->bd_contains) {
3244 printk(KERN_WARNING
3245 "%s: WARNING: %s appears to be"
3246 " on the same physical disk as"
3247 " %s.\n",
3248 mdname(mddev),
3249 bdevname(rdev->bdev,b),
3250 bdevname(rdev2->bdev,b2));
3251 warned = 1;
3252 }
3253 }
3254 }
3255 if (warned)
3256 printk(KERN_WARNING
3257 "True protection against single-disk"
3258 " failure might be compromised.\n");
3259 }
3260
NeilBrown657390d2005-08-26 18:34:16 -07003261 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003263 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003264 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
NeilBrownf91de922005-11-08 21:39:36 -08003266 if (start_readonly)
3267 mddev->ro = 2; /* read-only, but switch on first write */
3268
NeilBrownb15c2e52006-01-06 00:20:16 -08003269 err = mddev->pers->run(mddev);
3270 if (!err && mddev->pers->sync_request) {
3271 err = bitmap_create(mddev);
3272 if (err) {
3273 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3274 mdname(mddev), err);
3275 mddev->pers->stop(mddev);
3276 }
3277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (err) {
3279 printk(KERN_ERR "md: pers->run() failed ...\n");
3280 module_put(mddev->pers->owner);
3281 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003282 bitmap_destroy(mddev);
3283 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003285 if (mddev->pers->sync_request) {
3286 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3287 printk(KERN_WARNING
3288 "md: cannot register extra attributes for %s\n",
3289 mdname(mddev));
3290 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003291 mddev->ro = 0;
3292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 atomic_set(&mddev->writes_pending,0);
3294 mddev->safemode = 0;
3295 mddev->safemode_timer.function = md_safemode_timeout;
3296 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003297 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003299
3300 ITERATE_RDEV(mddev,rdev,tmp)
3301 if (rdev->raid_disk >= 0) {
3302 char nm[20];
3303 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003304 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3305 printk("md: cannot register %s for %s\n",
3306 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3310
NeilBrown850b2b42006-10-03 01:15:46 -07003311 if (mddev->flags)
3312 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
3314 set_capacity(disk, mddev->array_size<<1);
3315
3316 /* If we call blk_queue_make_request here, it will
3317 * re-initialise max_sectors etc which may have been
3318 * refined inside -> run. So just set the bits we need to set.
3319 * Most initialisation happended when we called
3320 * blk_queue_make_request(..., md_fail_request)
3321 * earlier.
3322 */
3323 mddev->queue->queuedata = mddev;
3324 mddev->queue->make_request_fn = mddev->pers->make_request;
3325
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003326 /* If there is a partially-recovered drive we need to
3327 * start recovery here. If we leave it to md_check_recovery,
3328 * it will remove the drives and not do the right thing
3329 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003330 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003331 struct list_head *rtmp;
3332 int spares = 0;
3333 ITERATE_RDEV(mddev,rdev,rtmp)
3334 if (rdev->raid_disk >= 0 &&
3335 !test_bit(In_sync, &rdev->flags) &&
3336 !test_bit(Faulty, &rdev->flags))
3337 /* complete an interrupted recovery */
3338 spares++;
3339 if (spares && mddev->pers->sync_request) {
3340 mddev->recovery = 0;
3341 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3342 mddev->sync_thread = md_register_thread(md_do_sync,
3343 mddev,
3344 "%s_resync");
3345 if (!mddev->sync_thread) {
3346 printk(KERN_ERR "%s: could not start resync"
3347 " thread...\n",
3348 mdname(mddev));
3349 /* leave the spares where they are, it shouldn't hurt */
3350 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003351 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003352 }
3353 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003354 md_wakeup_thread(mddev->thread);
3355 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003356
Linus Torvalds44ce62942007-05-09 18:51:36 -07003357 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003358 md_new_event(mddev);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003359 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return 0;
3361}
3362
3363static int restart_array(mddev_t *mddev)
3364{
3365 struct gendisk *disk = mddev->gendisk;
3366 int err;
3367
3368 /*
3369 * Complain if it has no devices
3370 */
3371 err = -ENXIO;
3372 if (list_empty(&mddev->disks))
3373 goto out;
3374
3375 if (mddev->pers) {
3376 err = -EBUSY;
3377 if (!mddev->ro)
3378 goto out;
3379
3380 mddev->safemode = 0;
3381 mddev->ro = 0;
3382 set_disk_ro(disk, 0);
3383
3384 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3385 mdname(mddev));
3386 /*
3387 * Kick recovery or resync if necessary
3388 */
3389 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3390 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003391 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003393 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
3396out:
3397 return err;
3398}
3399
NeilBrownacc55e22006-06-26 00:27:47 -07003400/* similar to deny_write_access, but accounts for our holding a reference
3401 * to the file ourselves */
3402static int deny_bitmap_write_access(struct file * file)
3403{
3404 struct inode *inode = file->f_mapping->host;
3405
3406 spin_lock(&inode->i_lock);
3407 if (atomic_read(&inode->i_writecount) > 1) {
3408 spin_unlock(&inode->i_lock);
3409 return -ETXTBSY;
3410 }
3411 atomic_set(&inode->i_writecount, -1);
3412 spin_unlock(&inode->i_lock);
3413
3414 return 0;
3415}
3416
3417static void restore_bitmap_write_access(struct file *file)
3418{
3419 struct inode *inode = file->f_mapping->host;
3420
3421 spin_lock(&inode->i_lock);
3422 atomic_set(&inode->i_writecount, 1);
3423 spin_unlock(&inode->i_lock);
3424}
3425
NeilBrown9e653b62006-06-26 00:27:58 -07003426/* mode:
3427 * 0 - completely stop and dis-assemble array
3428 * 1 - switch to readonly
3429 * 2 - stop but do not disassemble array
3430 */
3431static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
3433 int err = 0;
3434 struct gendisk *disk = mddev->gendisk;
3435
3436 if (mddev->pers) {
3437 if (atomic_read(&mddev->active)>2) {
3438 printk("md: %s still in use.\n",mdname(mddev));
3439 return -EBUSY;
3440 }
3441
3442 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003443 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3445 md_unregister_thread(mddev->sync_thread);
3446 mddev->sync_thread = NULL;
3447 }
3448
3449 del_timer_sync(&mddev->safemode_timer);
3450
3451 invalidate_partition(disk, 0);
3452
NeilBrown9e653b62006-06-26 00:27:58 -07003453 switch(mode) {
3454 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003456 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 goto out;
3458 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003459 break;
3460 case 0: /* disassemble */
3461 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003462 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003463 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (mddev->ro)
3465 set_disk_ro(disk, 0);
3466 blk_queue_make_request(mddev->queue, md_fail_request);
3467 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003468 mddev->queue->merge_bvec_fn = NULL;
3469 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003470 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003471 if (mddev->pers->sync_request)
3472 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3473
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 module_put(mddev->pers->owner);
3475 mddev->pers = NULL;
NeilBrown0d4ca602006-12-10 02:20:44 -08003476
3477 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003478 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 if (mddev->ro)
3481 mddev->ro = 0;
3482 }
NeilBrown850b2b42006-10-03 01:15:46 -07003483 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 /* mark array as shutdown cleanly */
3485 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003486 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 }
NeilBrown9e653b62006-06-26 00:27:58 -07003488 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003490 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 }
NeilBrown32a76272005-06-21 17:17:14 -07003492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 /*
3494 * Free resources if final stop
3495 */
NeilBrown9e653b62006-06-26 00:27:58 -07003496 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003497 mdk_rdev_t *rdev;
3498 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3501
NeilBrown978f9462006-02-02 14:28:05 -08003502 bitmap_destroy(mddev);
3503 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003504 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003505 fput(mddev->bitmap_file);
3506 mddev->bitmap_file = NULL;
3507 }
3508 mddev->bitmap_offset = 0;
3509
NeilBrown86e6ffd2005-11-08 21:39:24 -08003510 ITERATE_RDEV(mddev,rdev,tmp)
3511 if (rdev->raid_disk >= 0) {
3512 char nm[20];
3513 sprintf(nm, "rd%d", rdev->raid_disk);
3514 sysfs_remove_link(&mddev->kobj, nm);
3515 }
3516
NeilBrown5792a282007-04-04 19:08:18 -07003517 /* make sure all delayed_delete calls have finished */
3518 flush_scheduled_work();
3519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 export_array(mddev);
3521
3522 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003523 mddev->size = 0;
3524 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003525 mddev->recovery_cp = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003526 mddev->reshape_position = MaxSector;
NeilBrown9e653b62006-06-26 00:27:58 -07003527
NeilBrowna8a55c32006-06-26 00:27:39 -07003528 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3530 mdname(mddev));
3531 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003532 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533out:
3534 return err;
3535}
3536
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003537#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538static void autorun_array(mddev_t *mddev)
3539{
3540 mdk_rdev_t *rdev;
3541 struct list_head *tmp;
3542 int err;
3543
NeilBrowna757e642005-04-16 15:26:42 -07003544 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
3547 printk(KERN_INFO "md: running: ");
3548
3549 ITERATE_RDEV(mddev,rdev,tmp) {
3550 char b[BDEVNAME_SIZE];
3551 printk("<%s>", bdevname(rdev->bdev,b));
3552 }
3553 printk("\n");
3554
3555 err = do_md_run (mddev);
3556 if (err) {
3557 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3558 do_md_stop (mddev, 0);
3559 }
3560}
3561
3562/*
3563 * lets try to run arrays based on all disks that have arrived
3564 * until now. (those are in pending_raid_disks)
3565 *
3566 * the method: pick the first pending disk, collect all disks with
3567 * the same UUID, remove all from the pending list and put them into
3568 * the 'same_array' list. Then order this list based on superblock
3569 * update time (freshest comes first), kick out 'old' disks and
3570 * compare superblocks. If everything's fine then run it.
3571 *
3572 * If "unit" is allocated, then bump its reference count
3573 */
3574static void autorun_devices(int part)
3575{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 struct list_head *tmp;
3577 mdk_rdev_t *rdev0, *rdev;
3578 mddev_t *mddev;
3579 char b[BDEVNAME_SIZE];
3580
3581 printk(KERN_INFO "md: autorun ...\n");
3582 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003583 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003585 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 rdev0 = list_entry(pending_raid_disks.next,
3587 mdk_rdev_t, same_set);
3588
3589 printk(KERN_INFO "md: considering %s ...\n",
3590 bdevname(rdev0->bdev,b));
3591 INIT_LIST_HEAD(&candidates);
3592 ITERATE_RDEV_PENDING(rdev,tmp)
3593 if (super_90_load(rdev, rdev0, 0) >= 0) {
3594 printk(KERN_INFO "md: adding %s ...\n",
3595 bdevname(rdev->bdev,b));
3596 list_move(&rdev->same_set, &candidates);
3597 }
3598 /*
3599 * now we have a set of devices, with all of them having
3600 * mostly sane superblocks. It's time to allocate the
3601 * mddev.
3602 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003603 if (part) {
3604 dev = MKDEV(mdp_major,
3605 rdev0->preferred_minor << MdpMinorShift);
3606 unit = MINOR(dev) >> MdpMinorShift;
3607 } else {
3608 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3609 unit = MINOR(dev);
3610 }
3611 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3613 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3614 break;
3615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
3617 md_probe(dev, NULL, NULL);
3618 mddev = mddev_find(dev);
3619 if (!mddev) {
3620 printk(KERN_ERR
3621 "md: cannot allocate memory for md drive.\n");
3622 break;
3623 }
3624 if (mddev_lock(mddev))
3625 printk(KERN_WARNING "md: %s locked, cannot run\n",
3626 mdname(mddev));
3627 else if (mddev->raid_disks || mddev->major_version
3628 || !list_empty(&mddev->disks)) {
3629 printk(KERN_WARNING
3630 "md: %s already running, cannot run %s\n",
3631 mdname(mddev), bdevname(rdev0->bdev,b));
3632 mddev_unlock(mddev);
3633 } else {
3634 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3635 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3636 list_del_init(&rdev->same_set);
3637 if (bind_rdev_to_array(rdev, mddev))
3638 export_rdev(rdev);
3639 }
3640 autorun_array(mddev);
3641 mddev_unlock(mddev);
3642 }
3643 /* on success, candidates will be empty, on error
3644 * it won't...
3645 */
3646 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3647 export_rdev(rdev);
3648 mddev_put(mddev);
3649 }
3650 printk(KERN_INFO "md: ... autorun DONE.\n");
3651}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003652#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654static int get_version(void __user * arg)
3655{
3656 mdu_version_t ver;
3657
3658 ver.major = MD_MAJOR_VERSION;
3659 ver.minor = MD_MINOR_VERSION;
3660 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3661
3662 if (copy_to_user(arg, &ver, sizeof(ver)))
3663 return -EFAULT;
3664
3665 return 0;
3666}
3667
3668static int get_array_info(mddev_t * mddev, void __user * arg)
3669{
3670 mdu_array_info_t info;
3671 int nr,working,active,failed,spare;
3672 mdk_rdev_t *rdev;
3673 struct list_head *tmp;
3674
3675 nr=working=active=failed=spare=0;
3676 ITERATE_RDEV(mddev,rdev,tmp) {
3677 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003678 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 failed++;
3680 else {
3681 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003682 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 active++;
3684 else
3685 spare++;
3686 }
3687 }
3688
3689 info.major_version = mddev->major_version;
3690 info.minor_version = mddev->minor_version;
3691 info.patch_version = MD_PATCHLEVEL_VERSION;
3692 info.ctime = mddev->ctime;
3693 info.level = mddev->level;
3694 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003695 if (info.size != mddev->size) /* overflow */
3696 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 info.nr_disks = nr;
3698 info.raid_disks = mddev->raid_disks;
3699 info.md_minor = mddev->md_minor;
3700 info.not_persistent= !mddev->persistent;
3701
3702 info.utime = mddev->utime;
3703 info.state = 0;
3704 if (mddev->in_sync)
3705 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003706 if (mddev->bitmap && mddev->bitmap_offset)
3707 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 info.active_disks = active;
3709 info.working_disks = working;
3710 info.failed_disks = failed;
3711 info.spare_disks = spare;
3712
3713 info.layout = mddev->layout;
3714 info.chunk_size = mddev->chunk_size;
3715
3716 if (copy_to_user(arg, &info, sizeof(info)))
3717 return -EFAULT;
3718
3719 return 0;
3720}
3721
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003722static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003723{
3724 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3725 char *ptr, *buf = NULL;
3726 int err = -ENOMEM;
3727
NeilBrown2a2275d2007-01-26 00:57:11 -08003728 md_allow_write(mddev);
3729
NeilBrown32a76272005-06-21 17:17:14 -07003730 file = kmalloc(sizeof(*file), GFP_KERNEL);
3731 if (!file)
3732 goto out;
3733
3734 /* bitmap disabled, zero the first byte and copy out */
3735 if (!mddev->bitmap || !mddev->bitmap->file) {
3736 file->pathname[0] = '\0';
3737 goto copy_out;
3738 }
3739
3740 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3741 if (!buf)
3742 goto out;
3743
3744 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3745 if (!ptr)
3746 goto out;
3747
3748 strcpy(file->pathname, ptr);
3749
3750copy_out:
3751 err = 0;
3752 if (copy_to_user(arg, file, sizeof(*file)))
3753 err = -EFAULT;
3754out:
3755 kfree(buf);
3756 kfree(file);
3757 return err;
3758}
3759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760static int get_disk_info(mddev_t * mddev, void __user * arg)
3761{
3762 mdu_disk_info_t info;
3763 unsigned int nr;
3764 mdk_rdev_t *rdev;
3765
3766 if (copy_from_user(&info, arg, sizeof(info)))
3767 return -EFAULT;
3768
3769 nr = info.number;
3770
3771 rdev = find_rdev_nr(mddev, nr);
3772 if (rdev) {
3773 info.major = MAJOR(rdev->bdev->bd_dev);
3774 info.minor = MINOR(rdev->bdev->bd_dev);
3775 info.raid_disk = rdev->raid_disk;
3776 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003777 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003779 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 info.state |= (1<<MD_DISK_ACTIVE);
3781 info.state |= (1<<MD_DISK_SYNC);
3782 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003783 if (test_bit(WriteMostly, &rdev->flags))
3784 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 } else {
3786 info.major = info.minor = 0;
3787 info.raid_disk = -1;
3788 info.state = (1<<MD_DISK_REMOVED);
3789 }
3790
3791 if (copy_to_user(arg, &info, sizeof(info)))
3792 return -EFAULT;
3793
3794 return 0;
3795}
3796
3797static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3798{
3799 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3800 mdk_rdev_t *rdev;
3801 dev_t dev = MKDEV(info->major,info->minor);
3802
3803 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3804 return -EOVERFLOW;
3805
3806 if (!mddev->raid_disks) {
3807 int err;
3808 /* expecting a device which has a superblock */
3809 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3810 if (IS_ERR(rdev)) {
3811 printk(KERN_WARNING
3812 "md: md_import_device returned %ld\n",
3813 PTR_ERR(rdev));
3814 return PTR_ERR(rdev);
3815 }
3816 if (!list_empty(&mddev->disks)) {
3817 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3818 mdk_rdev_t, same_set);
3819 int err = super_types[mddev->major_version]
3820 .load_super(rdev, rdev0, mddev->minor_version);
3821 if (err < 0) {
3822 printk(KERN_WARNING
3823 "md: %s has different UUID to %s\n",
3824 bdevname(rdev->bdev,b),
3825 bdevname(rdev0->bdev,b2));
3826 export_rdev(rdev);
3827 return -EINVAL;
3828 }
3829 }
3830 err = bind_rdev_to_array(rdev, mddev);
3831 if (err)
3832 export_rdev(rdev);
3833 return err;
3834 }
3835
3836 /*
3837 * add_new_disk can be used once the array is assembled
3838 * to add "hot spares". They must already have a superblock
3839 * written
3840 */
3841 if (mddev->pers) {
3842 int err;
3843 if (!mddev->pers->hot_add_disk) {
3844 printk(KERN_WARNING
3845 "%s: personality does not support diskops!\n",
3846 mdname(mddev));
3847 return -EINVAL;
3848 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003849 if (mddev->persistent)
3850 rdev = md_import_device(dev, mddev->major_version,
3851 mddev->minor_version);
3852 else
3853 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 if (IS_ERR(rdev)) {
3855 printk(KERN_WARNING
3856 "md: md_import_device returned %ld\n",
3857 PTR_ERR(rdev));
3858 return PTR_ERR(rdev);
3859 }
NeilBrown41158c72005-06-21 17:17:25 -07003860 /* set save_raid_disk if appropriate */
3861 if (!mddev->persistent) {
3862 if (info->state & (1<<MD_DISK_SYNC) &&
3863 info->raid_disk < mddev->raid_disks)
3864 rdev->raid_disk = info->raid_disk;
3865 else
3866 rdev->raid_disk = -1;
3867 } else
3868 super_types[mddev->major_version].
3869 validate_super(mddev, rdev);
3870 rdev->saved_raid_disk = rdev->raid_disk;
3871
NeilBrownb2d444d2005-11-08 21:39:31 -08003872 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003873 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3874 set_bit(WriteMostly, &rdev->flags);
3875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 rdev->raid_disk = -1;
3877 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07003878 if (!err && !mddev->pers->hot_remove_disk) {
3879 /* If there is hot_add_disk but no hot_remove_disk
3880 * then added disks for geometry changes,
3881 * and should be added immediately.
3882 */
3883 super_types[mddev->major_version].
3884 validate_super(mddev, rdev);
3885 err = mddev->pers->hot_add_disk(mddev, rdev);
3886 if (err)
3887 unbind_rdev_from_array(rdev);
3888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 if (err)
3890 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003891
NeilBrown17571282006-12-10 02:20:52 -08003892 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07003893 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003894 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 return err;
3896 }
3897
3898 /* otherwise, add_new_disk is only allowed
3899 * for major_version==0 superblocks
3900 */
3901 if (mddev->major_version != 0) {
3902 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3903 mdname(mddev));
3904 return -EINVAL;
3905 }
3906
3907 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3908 int err;
3909 rdev = md_import_device (dev, -1, 0);
3910 if (IS_ERR(rdev)) {
3911 printk(KERN_WARNING
3912 "md: error, md_import_device() returned %ld\n",
3913 PTR_ERR(rdev));
3914 return PTR_ERR(rdev);
3915 }
3916 rdev->desc_nr = info->number;
3917 if (info->raid_disk < mddev->raid_disks)
3918 rdev->raid_disk = info->raid_disk;
3919 else
3920 rdev->raid_disk = -1;
3921
NeilBrownb2d444d2005-11-08 21:39:31 -08003922 rdev->flags = 0;
3923
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003925 if (info->state & (1<<MD_DISK_SYNC))
3926 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003928 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3929 set_bit(WriteMostly, &rdev->flags);
3930
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 if (!mddev->persistent) {
3932 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3933 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3934 } else
3935 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3936 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3937
NeilBrown2bf071b2006-01-06 00:20:55 -08003938 err = bind_rdev_to_array(rdev, mddev);
3939 if (err) {
3940 export_rdev(rdev);
3941 return err;
3942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 }
3944
3945 return 0;
3946}
3947
3948static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3949{
3950 char b[BDEVNAME_SIZE];
3951 mdk_rdev_t *rdev;
3952
3953 if (!mddev->pers)
3954 return -ENODEV;
3955
3956 rdev = find_rdev(mddev, dev);
3957 if (!rdev)
3958 return -ENXIO;
3959
3960 if (rdev->raid_disk >= 0)
3961 goto busy;
3962
3963 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07003964 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08003965 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
3967 return 0;
3968busy:
3969 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3970 bdevname(rdev->bdev,b), mdname(mddev));
3971 return -EBUSY;
3972}
3973
3974static int hot_add_disk(mddev_t * mddev, dev_t dev)
3975{
3976 char b[BDEVNAME_SIZE];
3977 int err;
3978 unsigned int size;
3979 mdk_rdev_t *rdev;
3980
3981 if (!mddev->pers)
3982 return -ENODEV;
3983
3984 if (mddev->major_version != 0) {
3985 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3986 " version-0 superblocks.\n",
3987 mdname(mddev));
3988 return -EINVAL;
3989 }
3990 if (!mddev->pers->hot_add_disk) {
3991 printk(KERN_WARNING
3992 "%s: personality does not support diskops!\n",
3993 mdname(mddev));
3994 return -EINVAL;
3995 }
3996
3997 rdev = md_import_device (dev, -1, 0);
3998 if (IS_ERR(rdev)) {
3999 printk(KERN_WARNING
4000 "md: error, md_import_device() returned %ld\n",
4001 PTR_ERR(rdev));
4002 return -EINVAL;
4003 }
4004
4005 if (mddev->persistent)
4006 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4007 else
4008 rdev->sb_offset =
4009 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4010
4011 size = calc_dev_size(rdev, mddev->chunk_size);
4012 rdev->size = size;
4013
NeilBrownb2d444d2005-11-08 21:39:31 -08004014 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 printk(KERN_WARNING
4016 "md: can not hot-add faulty %s disk to %s!\n",
4017 bdevname(rdev->bdev,b), mdname(mddev));
4018 err = -EINVAL;
4019 goto abort_export;
4020 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004021 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004023 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004024 err = bind_rdev_to_array(rdev, mddev);
4025 if (err)
4026 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
4028 /*
4029 * The rest should better be atomic, we can have disk failures
4030 * noticed in interrupt contexts ...
4031 */
4032
4033 if (rdev->desc_nr == mddev->max_disks) {
4034 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4035 mdname(mddev));
4036 err = -EBUSY;
4037 goto abort_unbind_export;
4038 }
4039
4040 rdev->raid_disk = -1;
4041
NeilBrown850b2b42006-10-03 01:15:46 -07004042 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
4044 /*
4045 * Kick recovery, maybe this spare has to be added to the
4046 * array immediately.
4047 */
4048 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4049 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004050 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 return 0;
4052
4053abort_unbind_export:
4054 unbind_rdev_from_array(rdev);
4055
4056abort_export:
4057 export_rdev(rdev);
4058 return err;
4059}
4060
NeilBrown32a76272005-06-21 17:17:14 -07004061static int set_bitmap_file(mddev_t *mddev, int fd)
4062{
4063 int err;
4064
NeilBrown36fa3062005-09-09 16:23:45 -07004065 if (mddev->pers) {
4066 if (!mddev->pers->quiesce)
4067 return -EBUSY;
4068 if (mddev->recovery || mddev->sync_thread)
4069 return -EBUSY;
4070 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004071 }
4072
NeilBrown36fa3062005-09-09 16:23:45 -07004073
4074 if (fd >= 0) {
4075 if (mddev->bitmap)
4076 return -EEXIST; /* cannot add when bitmap is present */
4077 mddev->bitmap_file = fget(fd);
4078
4079 if (mddev->bitmap_file == NULL) {
4080 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4081 mdname(mddev));
4082 return -EBADF;
4083 }
4084
4085 err = deny_bitmap_write_access(mddev->bitmap_file);
4086 if (err) {
4087 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4088 mdname(mddev));
4089 fput(mddev->bitmap_file);
4090 mddev->bitmap_file = NULL;
4091 return err;
4092 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004093 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004094 } else if (mddev->bitmap == NULL)
4095 return -ENOENT; /* cannot remove what isn't there */
4096 err = 0;
4097 if (mddev->pers) {
4098 mddev->pers->quiesce(mddev, 1);
4099 if (fd >= 0)
4100 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004101 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004102 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004103 fd = -1; /* make sure to put the file */
4104 }
NeilBrown36fa3062005-09-09 16:23:45 -07004105 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004106 }
4107 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004108 if (mddev->bitmap_file) {
4109 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004110 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004111 }
NeilBrown36fa3062005-09-09 16:23:45 -07004112 mddev->bitmap_file = NULL;
4113 }
4114
NeilBrown32a76272005-06-21 17:17:14 -07004115 return err;
4116}
4117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118/*
4119 * set_array_info is used two different ways
4120 * The original usage is when creating a new array.
4121 * In this usage, raid_disks is > 0 and it together with
4122 * level, size, not_persistent,layout,chunksize determine the
4123 * shape of the array.
4124 * This will always create an array with a type-0.90.0 superblock.
4125 * The newer usage is when assembling an array.
4126 * In this case raid_disks will be 0, and the major_version field is
4127 * use to determine which style super-blocks are to be found on the devices.
4128 * The minor and patch _version numbers are also kept incase the
4129 * super_block handler wishes to interpret them.
4130 */
4131static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4132{
4133
4134 if (info->raid_disks == 0) {
4135 /* just setting version number for superblock loading */
4136 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004137 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 super_types[info->major_version].name == NULL) {
4139 /* maybe try to auto-load a module? */
4140 printk(KERN_INFO
4141 "md: superblock version %d not known\n",
4142 info->major_version);
4143 return -EINVAL;
4144 }
4145 mddev->major_version = info->major_version;
4146 mddev->minor_version = info->minor_version;
4147 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004148 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 return 0;
4150 }
4151 mddev->major_version = MD_MAJOR_VERSION;
4152 mddev->minor_version = MD_MINOR_VERSION;
4153 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4154 mddev->ctime = get_seconds();
4155
4156 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004157 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 mddev->size = info->size;
4159 mddev->raid_disks = info->raid_disks;
4160 /* don't set md_minor, it is determined by which /dev/md* was
4161 * openned
4162 */
4163 if (info->state & (1<<MD_SB_CLEAN))
4164 mddev->recovery_cp = MaxSector;
4165 else
4166 mddev->recovery_cp = 0;
4167 mddev->persistent = ! info->not_persistent;
4168
4169 mddev->layout = info->layout;
4170 mddev->chunk_size = info->chunk_size;
4171
4172 mddev->max_disks = MD_SB_DISKS;
4173
NeilBrown850b2b42006-10-03 01:15:46 -07004174 mddev->flags = 0;
4175 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
NeilBrownb2a27032005-11-28 13:44:12 -08004177 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4178 mddev->bitmap_offset = 0;
4179
NeilBrownf6705572006-03-27 01:18:11 -08004180 mddev->reshape_position = MaxSector;
4181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 /*
4183 * Generate a 128 bit UUID
4184 */
4185 get_random_bytes(mddev->uuid, 16);
4186
NeilBrownf6705572006-03-27 01:18:11 -08004187 mddev->new_level = mddev->level;
4188 mddev->new_chunk = mddev->chunk_size;
4189 mddev->new_layout = mddev->layout;
4190 mddev->delta_disks = 0;
4191
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 return 0;
4193}
4194
NeilBrowna35b0d62006-01-06 00:20:49 -08004195static int update_size(mddev_t *mddev, unsigned long size)
4196{
4197 mdk_rdev_t * rdev;
4198 int rv;
4199 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004200 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004201
4202 if (mddev->pers->resize == NULL)
4203 return -EINVAL;
4204 /* The "size" is the amount of each device that is used.
4205 * This can only make sense for arrays with redundancy.
4206 * linear and raid0 always use whatever space is available
4207 * We can only consider changing the size if no resync
4208 * or reconstruction is happening, and if the new size
4209 * is acceptable. It must fit before the sb_offset or,
4210 * if that is <data_offset, it must fit before the
4211 * size of each device.
4212 * If size is zero, we find the largest size that fits.
4213 */
4214 if (mddev->sync_thread)
4215 return -EBUSY;
4216 ITERATE_RDEV(mddev,rdev,tmp) {
4217 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004218 avail = rdev->size * 2;
4219
NeilBrowna35b0d62006-01-06 00:20:49 -08004220 if (fit && (size == 0 || size > avail/2))
4221 size = avail/2;
4222 if (avail < ((sector_t)size << 1))
4223 return -ENOSPC;
4224 }
4225 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4226 if (!rv) {
4227 struct block_device *bdev;
4228
4229 bdev = bdget_disk(mddev->gendisk, 0);
4230 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004231 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004232 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004233 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004234 bdput(bdev);
4235 }
4236 }
4237 return rv;
4238}
4239
NeilBrownda943b992006-01-06 00:20:54 -08004240static int update_raid_disks(mddev_t *mddev, int raid_disks)
4241{
4242 int rv;
4243 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004244 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004245 return -EINVAL;
4246 if (raid_disks <= 0 ||
4247 raid_disks >= mddev->max_disks)
4248 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004249 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004250 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004251 mddev->delta_disks = raid_disks - mddev->raid_disks;
4252
4253 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004254 return rv;
4255}
4256
4257
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258/*
4259 * update_array_info is used to change the configuration of an
4260 * on-line array.
4261 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4262 * fields in the info are checked against the array.
4263 * Any differences that cannot be handled will cause an error.
4264 * Normally, only one change can be managed at a time.
4265 */
4266static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4267{
4268 int rv = 0;
4269 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004270 int state = 0;
4271
4272 /* calculate expected state,ignoring low bits */
4273 if (mddev->bitmap && mddev->bitmap_offset)
4274 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
4276 if (mddev->major_version != info->major_version ||
4277 mddev->minor_version != info->minor_version ||
4278/* mddev->patch_version != info->patch_version || */
4279 mddev->ctime != info->ctime ||
4280 mddev->level != info->level ||
4281/* mddev->layout != info->layout || */
4282 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004283 mddev->chunk_size != info->chunk_size ||
4284 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4285 ((state^info->state) & 0xfffffe00)
4286 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return -EINVAL;
4288 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004289 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 if (mddev->raid_disks != info->raid_disks) cnt++;
4291 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004292 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 if (cnt == 0) return 0;
4294 if (cnt > 1) return -EINVAL;
4295
4296 if (mddev->layout != info->layout) {
4297 /* Change layout
4298 * we don't need to do anything at the md level, the
4299 * personality will take care of it all.
4300 */
4301 if (mddev->pers->reconfig == NULL)
4302 return -EINVAL;
4303 else
4304 return mddev->pers->reconfig(mddev, info->layout, -1);
4305 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004306 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004307 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
NeilBrownda943b992006-01-06 00:20:54 -08004309 if (mddev->raid_disks != info->raid_disks)
4310 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
NeilBrown36fa3062005-09-09 16:23:45 -07004312 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4313 if (mddev->pers->quiesce == NULL)
4314 return -EINVAL;
4315 if (mddev->recovery || mddev->sync_thread)
4316 return -EBUSY;
4317 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4318 /* add the bitmap */
4319 if (mddev->bitmap)
4320 return -EEXIST;
4321 if (mddev->default_bitmap_offset == 0)
4322 return -EINVAL;
4323 mddev->bitmap_offset = mddev->default_bitmap_offset;
4324 mddev->pers->quiesce(mddev, 1);
4325 rv = bitmap_create(mddev);
4326 if (rv)
4327 bitmap_destroy(mddev);
4328 mddev->pers->quiesce(mddev, 0);
4329 } else {
4330 /* remove the bitmap */
4331 if (!mddev->bitmap)
4332 return -ENOENT;
4333 if (mddev->bitmap->file)
4334 return -EINVAL;
4335 mddev->pers->quiesce(mddev, 1);
4336 bitmap_destroy(mddev);
4337 mddev->pers->quiesce(mddev, 0);
4338 mddev->bitmap_offset = 0;
4339 }
4340 }
NeilBrown850b2b42006-10-03 01:15:46 -07004341 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 return rv;
4343}
4344
4345static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4346{
4347 mdk_rdev_t *rdev;
4348
4349 if (mddev->pers == NULL)
4350 return -ENODEV;
4351
4352 rdev = find_rdev(mddev, dev);
4353 if (!rdev)
4354 return -ENODEV;
4355
4356 md_error(mddev, rdev);
4357 return 0;
4358}
4359
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004360static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4361{
4362 mddev_t *mddev = bdev->bd_disk->private_data;
4363
4364 geo->heads = 2;
4365 geo->sectors = 4;
4366 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4367 return 0;
4368}
4369
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370static int md_ioctl(struct inode *inode, struct file *file,
4371 unsigned int cmd, unsigned long arg)
4372{
4373 int err = 0;
4374 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 mddev_t *mddev = NULL;
4376
4377 if (!capable(CAP_SYS_ADMIN))
4378 return -EACCES;
4379
4380 /*
4381 * Commands dealing with the RAID driver but not any
4382 * particular array:
4383 */
4384 switch (cmd)
4385 {
4386 case RAID_VERSION:
4387 err = get_version(argp);
4388 goto done;
4389
4390 case PRINT_RAID_DEBUG:
4391 err = 0;
4392 md_print_devices();
4393 goto done;
4394
4395#ifndef MODULE
4396 case RAID_AUTORUN:
4397 err = 0;
4398 autostart_arrays(arg);
4399 goto done;
4400#endif
4401 default:;
4402 }
4403
4404 /*
4405 * Commands creating/starting a new array:
4406 */
4407
4408 mddev = inode->i_bdev->bd_disk->private_data;
4409
4410 if (!mddev) {
4411 BUG();
4412 goto abort;
4413 }
4414
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 err = mddev_lock(mddev);
4416 if (err) {
4417 printk(KERN_INFO
4418 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4419 err, cmd);
4420 goto abort;
4421 }
4422
4423 switch (cmd)
4424 {
4425 case SET_ARRAY_INFO:
4426 {
4427 mdu_array_info_t info;
4428 if (!arg)
4429 memset(&info, 0, sizeof(info));
4430 else if (copy_from_user(&info, argp, sizeof(info))) {
4431 err = -EFAULT;
4432 goto abort_unlock;
4433 }
4434 if (mddev->pers) {
4435 err = update_array_info(mddev, &info);
4436 if (err) {
4437 printk(KERN_WARNING "md: couldn't update"
4438 " array info. %d\n", err);
4439 goto abort_unlock;
4440 }
4441 goto done_unlock;
4442 }
4443 if (!list_empty(&mddev->disks)) {
4444 printk(KERN_WARNING
4445 "md: array %s already has disks!\n",
4446 mdname(mddev));
4447 err = -EBUSY;
4448 goto abort_unlock;
4449 }
4450 if (mddev->raid_disks) {
4451 printk(KERN_WARNING
4452 "md: array %s already initialised!\n",
4453 mdname(mddev));
4454 err = -EBUSY;
4455 goto abort_unlock;
4456 }
4457 err = set_array_info(mddev, &info);
4458 if (err) {
4459 printk(KERN_WARNING "md: couldn't set"
4460 " array info. %d\n", err);
4461 goto abort_unlock;
4462 }
4463 }
4464 goto done_unlock;
4465
4466 default:;
4467 }
4468
4469 /*
4470 * Commands querying/configuring an existing array:
4471 */
NeilBrown32a76272005-06-21 17:17:14 -07004472 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004473 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrown32a76272005-06-21 17:17:14 -07004474 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
NeilBrown3f9d7b02006-12-22 01:11:41 -08004475 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4476 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 err = -ENODEV;
4478 goto abort_unlock;
4479 }
4480
4481 /*
4482 * Commands even a read-only array can execute:
4483 */
4484 switch (cmd)
4485 {
4486 case GET_ARRAY_INFO:
4487 err = get_array_info(mddev, argp);
4488 goto done_unlock;
4489
NeilBrown32a76272005-06-21 17:17:14 -07004490 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004491 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004492 goto done_unlock;
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 case GET_DISK_INFO:
4495 err = get_disk_info(mddev, argp);
4496 goto done_unlock;
4497
4498 case RESTART_ARRAY_RW:
4499 err = restart_array(mddev);
4500 goto done_unlock;
4501
4502 case STOP_ARRAY:
4503 err = do_md_stop (mddev, 0);
4504 goto done_unlock;
4505
4506 case STOP_ARRAY_RO:
4507 err = do_md_stop (mddev, 1);
4508 goto done_unlock;
4509
4510 /*
4511 * We have a problem here : there is no easy way to give a CHS
4512 * virtual geometry. We currently pretend that we have a 2 heads
4513 * 4 sectors (with a BIG number of cylinders...). This drives
4514 * dosfs just mad... ;-)
4515 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 }
4517
4518 /*
4519 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004520 * superblock, so we do not allow them on read-only arrays.
4521 * However non-MD ioctls (e.g. get-size) will still come through
4522 * here and hit the 'default' below, so only disallow
4523 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 */
NeilBrownf91de922005-11-08 21:39:36 -08004525 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4526 mddev->ro && mddev->pers) {
4527 if (mddev->ro == 2) {
4528 mddev->ro = 0;
4529 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4530 md_wakeup_thread(mddev->thread);
4531
4532 } else {
4533 err = -EROFS;
4534 goto abort_unlock;
4535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 }
4537
4538 switch (cmd)
4539 {
4540 case ADD_NEW_DISK:
4541 {
4542 mdu_disk_info_t info;
4543 if (copy_from_user(&info, argp, sizeof(info)))
4544 err = -EFAULT;
4545 else
4546 err = add_new_disk(mddev, &info);
4547 goto done_unlock;
4548 }
4549
4550 case HOT_REMOVE_DISK:
4551 err = hot_remove_disk(mddev, new_decode_dev(arg));
4552 goto done_unlock;
4553
4554 case HOT_ADD_DISK:
4555 err = hot_add_disk(mddev, new_decode_dev(arg));
4556 goto done_unlock;
4557
4558 case SET_DISK_FAULTY:
4559 err = set_disk_faulty(mddev, new_decode_dev(arg));
4560 goto done_unlock;
4561
4562 case RUN_ARRAY:
4563 err = do_md_run (mddev);
4564 goto done_unlock;
4565
NeilBrown32a76272005-06-21 17:17:14 -07004566 case SET_BITMAP_FILE:
4567 err = set_bitmap_file(mddev, (int)arg);
4568 goto done_unlock;
4569
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 err = -EINVAL;
4572 goto abort_unlock;
4573 }
4574
4575done_unlock:
4576abort_unlock:
4577 mddev_unlock(mddev);
4578
4579 return err;
4580done:
4581 if (err)
4582 MD_BUG();
4583abort:
4584 return err;
4585}
4586
4587static int md_open(struct inode *inode, struct file *file)
4588{
4589 /*
4590 * Succeed if we can lock the mddev, which confirms that
4591 * it isn't being stopped right now.
4592 */
4593 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4594 int err;
4595
NeilBrownd63a5a72006-12-08 02:36:17 -08004596 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 goto out;
4598
4599 err = 0;
4600 mddev_get(mddev);
4601 mddev_unlock(mddev);
4602
4603 check_disk_change(inode->i_bdev);
4604 out:
4605 return err;
4606}
4607
4608static int md_release(struct inode *inode, struct file * file)
4609{
4610 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4611
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004612 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 mddev_put(mddev);
4614
4615 return 0;
4616}
4617
Linus Torvalds44ce62942007-05-09 18:51:36 -07004618static int md_media_changed(struct gendisk *disk)
4619{
4620 mddev_t *mddev = disk->private_data;
4621
4622 return mddev->changed;
4623}
4624
4625static int md_revalidate(struct gendisk *disk)
4626{
4627 mddev_t *mddev = disk->private_data;
4628
4629 mddev->changed = 0;
4630 return 0;
4631}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632static struct block_device_operations md_fops =
4633{
4634 .owner = THIS_MODULE,
4635 .open = md_open,
4636 .release = md_release,
4637 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004638 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07004639 .media_changed = md_media_changed,
4640 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641};
4642
Adrian Bunk75c96f82005-05-05 16:16:09 -07004643static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644{
4645 mdk_thread_t *thread = arg;
4646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 /*
4648 * md_thread is a 'system-thread', it's priority should be very
4649 * high. We avoid resource deadlocks individually in each
4650 * raid personality. (RAID5 does preallocation) We also use RR and
4651 * the very same RT priority as kswapd, thus we will never get
4652 * into a priority inversion deadlock.
4653 *
4654 * we definitely have to have equal or higher priority than
4655 * bdflush, otherwise bdflush will deadlock if there are too
4656 * many dirty RAID5 blocks.
4657 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
NeilBrown6985c432005-10-19 21:23:47 -07004659 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004660 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
NeilBrown93588e22005-11-15 00:09:12 -08004662 /* We need to wait INTERRUPTIBLE so that
4663 * we don't add to the load-average.
4664 * That means we need to be sure no signals are
4665 * pending
4666 */
4667 if (signal_pending(current))
4668 flush_signals(current);
4669
4670 wait_event_interruptible_timeout
4671 (thread->wqueue,
4672 test_bit(THREAD_WAKEUP, &thread->flags)
4673 || kthread_should_stop(),
4674 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
4676 clear_bit(THREAD_WAKEUP, &thread->flags);
4677
NeilBrown787453c2005-11-08 21:39:43 -08004678 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004680
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 return 0;
4682}
4683
4684void md_wakeup_thread(mdk_thread_t *thread)
4685{
4686 if (thread) {
4687 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4688 set_bit(THREAD_WAKEUP, &thread->flags);
4689 wake_up(&thread->wqueue);
4690 }
4691}
4692
4693mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4694 const char *name)
4695{
4696 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
NeilBrown9ffae0c2006-01-06 00:20:32 -08004698 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 if (!thread)
4700 return NULL;
4701
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 init_waitqueue_head(&thread->wqueue);
4703
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 thread->run = run;
4705 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004706 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004707 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004708 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 kfree(thread);
4710 return NULL;
4711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 return thread;
4713}
4714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715void md_unregister_thread(mdk_thread_t *thread)
4716{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004717 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07004718
4719 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 kfree(thread);
4721}
4722
4723void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4724{
4725 if (!mddev) {
4726 MD_BUG();
4727 return;
4728 }
4729
NeilBrownb2d444d2005-11-08 21:39:31 -08004730 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 return;
NeilBrown32a76272005-06-21 17:17:14 -07004732/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4734 mdname(mddev),
4735 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4736 __builtin_return_address(0),__builtin_return_address(1),
4737 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004738*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004739 if (!mddev->pers)
4740 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 if (!mddev->pers->error_handler)
4742 return;
4743 mddev->pers->error_handler(mddev,rdev);
4744 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4745 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4746 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004747 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748}
4749
4750/* seq_file implementation /proc/mdstat */
4751
4752static void status_unused(struct seq_file *seq)
4753{
4754 int i = 0;
4755 mdk_rdev_t *rdev;
4756 struct list_head *tmp;
4757
4758 seq_printf(seq, "unused devices: ");
4759
4760 ITERATE_RDEV_PENDING(rdev,tmp) {
4761 char b[BDEVNAME_SIZE];
4762 i++;
4763 seq_printf(seq, "%s ",
4764 bdevname(rdev->bdev,b));
4765 }
4766 if (!i)
4767 seq_printf(seq, "<none>");
4768
4769 seq_printf(seq, "\n");
4770}
4771
4772
4773static void status_resync(struct seq_file *seq, mddev_t * mddev)
4774{
NeilBrown4588b422006-03-27 01:18:04 -08004775 sector_t max_blocks, resync, res;
4776 unsigned long dt, db, rt;
4777 int scale;
4778 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
4780 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4781
4782 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4783 max_blocks = mddev->resync_max_sectors >> 1;
4784 else
4785 max_blocks = mddev->size;
4786
4787 /*
4788 * Should not happen.
4789 */
4790 if (!max_blocks) {
4791 MD_BUG();
4792 return;
4793 }
NeilBrown4588b422006-03-27 01:18:04 -08004794 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4795 * in a sector_t, and (max_blocks>>scale) will fit in a
4796 * u32, as those are the requirements for sector_div.
4797 * Thus 'scale' must be at least 10
4798 */
4799 scale = 10;
4800 if (sizeof(sector_t) > sizeof(unsigned long)) {
4801 while ( max_blocks/2 > (1ULL<<(scale+32)))
4802 scale++;
4803 }
4804 res = (resync>>scale)*1000;
4805 sector_div(res, (u32)((max_blocks>>scale)+1));
4806
4807 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 {
NeilBrown4588b422006-03-27 01:18:04 -08004809 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 seq_printf(seq, "[");
4811 for (i = 0; i < x; i++)
4812 seq_printf(seq, "=");
4813 seq_printf(seq, ">");
4814 for (i = 0; i < y; i++)
4815 seq_printf(seq, ".");
4816 seq_printf(seq, "] ");
4817 }
NeilBrown4588b422006-03-27 01:18:04 -08004818 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004819 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4820 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07004821 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
4822 "check" :
4823 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4824 "resync" : "recovery"))),
4825 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08004826 (unsigned long long) resync,
4827 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
4829 /*
4830 * We do not want to overflow, so the order of operands and
4831 * the * 100 / 100 trick are important. We do a +1 to be
4832 * safe against division by zero. We only estimate anyway.
4833 *
4834 * dt: time from mark until now
4835 * db: blocks written from mark until now
4836 * rt: remaining time
4837 */
4838 dt = ((jiffies - mddev->resync_mark) / HZ);
4839 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07004840 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
4841 - mddev->resync_mark_cnt;
4842 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
4844 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4845
NeilBrownff4e8d92006-07-10 04:44:16 -07004846 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847}
4848
4849static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4850{
4851 struct list_head *tmp;
4852 loff_t l = *pos;
4853 mddev_t *mddev;
4854
4855 if (l >= 0x10000)
4856 return NULL;
4857 if (!l--)
4858 /* header */
4859 return (void*)1;
4860
4861 spin_lock(&all_mddevs_lock);
4862 list_for_each(tmp,&all_mddevs)
4863 if (!l--) {
4864 mddev = list_entry(tmp, mddev_t, all_mddevs);
4865 mddev_get(mddev);
4866 spin_unlock(&all_mddevs_lock);
4867 return mddev;
4868 }
4869 spin_unlock(&all_mddevs_lock);
4870 if (!l--)
4871 return (void*)2;/* tail */
4872 return NULL;
4873}
4874
4875static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4876{
4877 struct list_head *tmp;
4878 mddev_t *next_mddev, *mddev = v;
4879
4880 ++*pos;
4881 if (v == (void*)2)
4882 return NULL;
4883
4884 spin_lock(&all_mddevs_lock);
4885 if (v == (void*)1)
4886 tmp = all_mddevs.next;
4887 else
4888 tmp = mddev->all_mddevs.next;
4889 if (tmp != &all_mddevs)
4890 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4891 else {
4892 next_mddev = (void*)2;
4893 *pos = 0x10000;
4894 }
4895 spin_unlock(&all_mddevs_lock);
4896
4897 if (v != (void*)1)
4898 mddev_put(mddev);
4899 return next_mddev;
4900
4901}
4902
4903static void md_seq_stop(struct seq_file *seq, void *v)
4904{
4905 mddev_t *mddev = v;
4906
4907 if (mddev && v != (void*)1 && v != (void*)2)
4908 mddev_put(mddev);
4909}
4910
NeilBrownd7603b72006-01-06 00:20:30 -08004911struct mdstat_info {
4912 int event;
4913};
4914
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915static int md_seq_show(struct seq_file *seq, void *v)
4916{
4917 mddev_t *mddev = v;
4918 sector_t size;
4919 struct list_head *tmp2;
4920 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004921 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004922 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
4924 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004925 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 seq_printf(seq, "Personalities : ");
4927 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004928 list_for_each_entry(pers, &pers_list, list)
4929 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
4931 spin_unlock(&pers_lock);
4932 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004933 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 return 0;
4935 }
4936 if (v == (void*)2) {
4937 status_unused(seq);
4938 return 0;
4939 }
4940
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004941 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004943
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4945 seq_printf(seq, "%s : %sactive", mdname(mddev),
4946 mddev->pers ? "" : "in");
4947 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004948 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004950 if (mddev->ro==2)
4951 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 seq_printf(seq, " %s", mddev->pers->name);
4953 }
4954
4955 size = 0;
4956 ITERATE_RDEV(mddev,rdev,tmp2) {
4957 char b[BDEVNAME_SIZE];
4958 seq_printf(seq, " %s[%d]",
4959 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004960 if (test_bit(WriteMostly, &rdev->flags))
4961 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004962 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 seq_printf(seq, "(F)");
4964 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004965 } else if (rdev->raid_disk < 0)
4966 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 size += rdev->size;
4968 }
4969
4970 if (!list_empty(&mddev->disks)) {
4971 if (mddev->pers)
4972 seq_printf(seq, "\n %llu blocks",
4973 (unsigned long long)mddev->array_size);
4974 else
4975 seq_printf(seq, "\n %llu blocks",
4976 (unsigned long long)size);
4977 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004978 if (mddev->persistent) {
4979 if (mddev->major_version != 0 ||
4980 mddev->minor_version != 90) {
4981 seq_printf(seq," super %d.%d",
4982 mddev->major_version,
4983 mddev->minor_version);
4984 }
4985 } else
4986 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
4988 if (mddev->pers) {
4989 mddev->pers->status (seq, mddev);
4990 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004991 if (mddev->pers->sync_request) {
4992 if (mddev->curr_resync > 2) {
4993 status_resync (seq, mddev);
4994 seq_printf(seq, "\n ");
4995 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4996 seq_printf(seq, "\tresync=DELAYED\n ");
4997 else if (mddev->recovery_cp < MaxSector)
4998 seq_printf(seq, "\tresync=PENDING\n ");
4999 }
NeilBrown32a76272005-06-21 17:17:14 -07005000 } else
5001 seq_printf(seq, "\n ");
5002
5003 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005004 unsigned long chunk_kb;
5005 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005006 spin_lock_irqsave(&bitmap->lock, flags);
5007 chunk_kb = bitmap->chunksize >> 10;
5008 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5009 "%lu%s chunk",
5010 bitmap->pages - bitmap->missing_pages,
5011 bitmap->pages,
5012 (bitmap->pages - bitmap->missing_pages)
5013 << (PAGE_SHIFT - 10),
5014 chunk_kb ? chunk_kb : bitmap->chunksize,
5015 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005016 if (bitmap->file) {
5017 seq_printf(seq, ", file: ");
Josef Sipekc649bb92006-12-08 02:37:19 -08005018 seq_path(seq, bitmap->file->f_path.mnt,
5019 bitmap->file->f_path.dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005020 }
NeilBrown78d742d2005-06-21 17:17:15 -07005021
NeilBrown32a76272005-06-21 17:17:14 -07005022 seq_printf(seq, "\n");
5023 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 }
5025
5026 seq_printf(seq, "\n");
5027 }
5028 mddev_unlock(mddev);
5029
5030 return 0;
5031}
5032
5033static struct seq_operations md_seq_ops = {
5034 .start = md_seq_start,
5035 .next = md_seq_next,
5036 .stop = md_seq_stop,
5037 .show = md_seq_show,
5038};
5039
5040static int md_seq_open(struct inode *inode, struct file *file)
5041{
5042 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005043 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5044 if (mi == NULL)
5045 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
5047 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005048 if (error)
5049 kfree(mi);
5050 else {
5051 struct seq_file *p = file->private_data;
5052 p->private = mi;
5053 mi->event = atomic_read(&md_event_count);
5054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 return error;
5056}
5057
NeilBrownd7603b72006-01-06 00:20:30 -08005058static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5059{
5060 struct seq_file *m = filp->private_data;
5061 struct mdstat_info *mi = m->private;
5062 int mask;
5063
5064 poll_wait(filp, &md_event_waiters, wait);
5065
5066 /* always allow read */
5067 mask = POLLIN | POLLRDNORM;
5068
5069 if (mi->event != atomic_read(&md_event_count))
5070 mask |= POLLERR | POLLPRI;
5071 return mask;
5072}
5073
Arjan van de Venfa027c22007-02-12 00:55:33 -08005074static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005075 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 .open = md_seq_open,
5077 .read = seq_read,
5078 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005079 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005080 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081};
5082
NeilBrown2604b702006-01-06 00:20:36 -08005083int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005086 list_add_tail(&p->list, &pers_list);
5087 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 spin_unlock(&pers_lock);
5089 return 0;
5090}
5091
NeilBrown2604b702006-01-06 00:20:36 -08005092int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093{
NeilBrown2604b702006-01-06 00:20:36 -08005094 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005096 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 spin_unlock(&pers_lock);
5098 return 0;
5099}
5100
5101static int is_mddev_idle(mddev_t *mddev)
5102{
5103 mdk_rdev_t * rdev;
5104 struct list_head *tmp;
5105 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005106 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
5108 idle = 1;
5109 ITERATE_RDEV(mddev,rdev,tmp) {
5110 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005111 curr_events = disk_stat_read(disk, sectors[0]) +
5112 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005114 /* sync IO will cause sync_io to increase before the disk_stats
5115 * as sync_io is counted when a request starts, and
5116 * disk_stats is counted when it completes.
5117 * So resync activity will cause curr_events to be smaller than
5118 * when there was no such activity.
5119 * non-sync IO will cause disk_stat to increase without
5120 * increasing sync_io so curr_events will (eventually)
5121 * be larger than it was before. Once it becomes
5122 * substantially larger, the test below will cause
5123 * the array to appear non-idle, and resync will slow
5124 * down.
5125 * If there is a lot of outstanding resync activity when
5126 * we set last_event to curr_events, then all that activity
5127 * completing might cause the array to appear non-idle
5128 * and resync will be slowed down even though there might
5129 * not have been non-resync activity. This will only
5130 * happen once though. 'last_events' will soon reflect
5131 * the state where there is little or no outstanding
5132 * resync requests, and further resync activity will
5133 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005134 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005136 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 rdev->last_events = curr_events;
5138 idle = 0;
5139 }
5140 }
5141 return idle;
5142}
5143
5144void md_done_sync(mddev_t *mddev, int blocks, int ok)
5145{
5146 /* another "blocks" (512byte) blocks have been synced */
5147 atomic_sub(blocks, &mddev->recovery_active);
5148 wake_up(&mddev->recovery_wait);
5149 if (!ok) {
5150 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5151 md_wakeup_thread(mddev->thread);
5152 // stop recovery, signal do_sync ....
5153 }
5154}
5155
5156
NeilBrown06d91a52005-06-21 17:17:12 -07005157/* md_write_start(mddev, bi)
5158 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005159 * in superblock) before writing, schedule a superblock update
5160 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005161 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005162void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163{
NeilBrown06d91a52005-06-21 17:17:12 -07005164 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005165 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005166
NeilBrownf91de922005-11-08 21:39:36 -08005167 BUG_ON(mddev->ro == 1);
5168 if (mddev->ro == 2) {
5169 /* need to switch to read/write */
5170 mddev->ro = 0;
5171 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5172 md_wakeup_thread(mddev->thread);
5173 }
NeilBrown06d91a52005-06-21 17:17:12 -07005174 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005175 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005176 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005177 if (mddev->in_sync) {
5178 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005179 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005180 md_wakeup_thread(mddev->thread);
5181 }
NeilBrowna9701a32005-11-08 21:39:34 -08005182 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005183 }
NeilBrown850b2b42006-10-03 01:15:46 -07005184 wait_event(mddev->sb_wait, mddev->flags==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185}
5186
5187void md_write_end(mddev_t *mddev)
5188{
5189 if (atomic_dec_and_test(&mddev->writes_pending)) {
5190 if (mddev->safemode == 2)
5191 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005192 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5194 }
5195}
5196
NeilBrown2a2275d2007-01-26 00:57:11 -08005197/* md_allow_write(mddev)
5198 * Calling this ensures that the array is marked 'active' so that writes
5199 * may proceed without blocking. It is important to call this before
5200 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5201 * Must be called with mddev_lock held.
5202 */
5203void md_allow_write(mddev_t *mddev)
5204{
5205 if (!mddev->pers)
5206 return;
5207 if (mddev->ro)
5208 return;
5209
5210 spin_lock_irq(&mddev->write_lock);
5211 if (mddev->in_sync) {
5212 mddev->in_sync = 0;
5213 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5214 if (mddev->safemode_delay &&
5215 mddev->safemode == 0)
5216 mddev->safemode = 1;
5217 spin_unlock_irq(&mddev->write_lock);
5218 md_update_sb(mddev, 0);
5219 } else
5220 spin_unlock_irq(&mddev->write_lock);
5221}
5222EXPORT_SYMBOL_GPL(md_allow_write);
5223
Adrian Bunk75c96f82005-05-05 16:16:09 -07005224static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
5226#define SYNC_MARKS 10
5227#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005228void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229{
5230 mddev_t *mddev2;
5231 unsigned int currspeed = 0,
5232 window;
NeilBrown57afd892005-06-21 17:17:13 -07005233 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 unsigned long mark[SYNC_MARKS];
5235 sector_t mark_cnt[SYNC_MARKS];
5236 int last_mark,m;
5237 struct list_head *tmp;
5238 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005239 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005240 struct list_head *rtmp;
5241 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005242 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
5244 /* just incase thread restarts... */
5245 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5246 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005247 if (mddev->ro) /* never try to sync a read-only array */
5248 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
NeilBrown61df9d92006-10-03 01:15:57 -07005250 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5251 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5252 desc = "data-check";
5253 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5254 desc = "requested-resync";
5255 else
5256 desc = "resync";
5257 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5258 desc = "reshape";
5259 else
5260 desc = "recovery";
5261
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 /* we overload curr_resync somewhat here.
5263 * 0 == not engaged in resync at all
5264 * 2 == checking that there is no conflict with another sync
5265 * 1 == like 2, but have yielded to allow conflicting resync to
5266 * commense
5267 * other == active in resync - this many blocks
5268 *
5269 * Before starting a resync we must have set curr_resync to
5270 * 2, and then checked that every "conflicting" array has curr_resync
5271 * less than ours. When we find one that is the same or higher
5272 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5273 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5274 * This will mean we have to start checking from the beginning again.
5275 *
5276 */
5277
5278 do {
5279 mddev->curr_resync = 2;
5280
5281 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005282 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005283 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 goto skip;
5285 }
5286 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 if (mddev2 == mddev)
5288 continue;
5289 if (mddev2->curr_resync &&
5290 match_mddev_units(mddev,mddev2)) {
5291 DEFINE_WAIT(wq);
5292 if (mddev < mddev2 && mddev->curr_resync == 2) {
5293 /* arbitrarily yield */
5294 mddev->curr_resync = 1;
5295 wake_up(&resync_wait);
5296 }
5297 if (mddev > mddev2 && mddev->curr_resync == 1)
5298 /* no need to wait here, we can wait the next
5299 * time 'round when curr_resync == 2
5300 */
5301 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005302 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5303 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005304 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005305 printk(KERN_INFO "md: delaying %s of %s"
5306 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005308 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 mddev_put(mddev2);
5310 schedule();
5311 finish_wait(&resync_wait, &wq);
5312 goto try_again;
5313 }
5314 finish_wait(&resync_wait, &wq);
5315 }
5316 }
5317 } while (mddev->curr_resync < 2);
5318
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005319 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005320 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005322 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 */
5324 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005325 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005326 /* we don't use the checkpoint if there's a bitmap */
5327 if (!mddev->bitmap &&
5328 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5329 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005330 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5331 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005332 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 /* recovery follows the physical size of devices */
5334 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005335 j = MaxSector;
5336 ITERATE_RDEV(mddev,rdev,rtmp)
5337 if (rdev->raid_disk >= 0 &&
5338 !test_bit(Faulty, &rdev->flags) &&
5339 !test_bit(In_sync, &rdev->flags) &&
5340 rdev->recovery_offset < j)
5341 j = rdev->recovery_offset;
5342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
NeilBrown61df9d92006-10-03 01:15:57 -07005344 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5345 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5346 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005347 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005348 "(but not more than %d KB/sec) for %s.\n",
5349 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
5351 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005352
NeilBrown57afd892005-06-21 17:17:13 -07005353 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 for (m = 0; m < SYNC_MARKS; m++) {
5355 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005356 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 }
5358 last_mark = 0;
5359 mddev->resync_mark = mark[last_mark];
5360 mddev->resync_mark_cnt = mark_cnt[last_mark];
5361
5362 /*
5363 * Tune reconstruction:
5364 */
5365 window = 32*(PAGE_SIZE/512);
5366 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5367 window/2,(unsigned long long) max_sectors/2);
5368
5369 atomic_set(&mddev->recovery_active, 0);
5370 init_waitqueue_head(&mddev->recovery_wait);
5371 last_check = 0;
5372
5373 if (j>2) {
5374 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005375 "md: resuming %s of %s from checkpoint.\n",
5376 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 mddev->curr_resync = j;
5378 }
5379
5380 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005381 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382
NeilBrown57afd892005-06-21 17:17:13 -07005383 skipped = 0;
5384 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08005385 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005386 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5388 goto out;
5389 }
NeilBrown57afd892005-06-21 17:17:13 -07005390
5391 if (!skipped) { /* actual IO requested */
5392 io_sectors += sectors;
5393 atomic_add(sectors, &mddev->recovery_active);
5394 }
5395
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 j += sectors;
5397 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005398 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005399 if (last_check == 0)
5400 /* this is the earliers that rebuilt will be
5401 * visible in /proc/mdstat
5402 */
5403 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005404
5405 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 continue;
5407
NeilBrown57afd892005-06-21 17:17:13 -07005408 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
5410 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5411 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5412 break;
5413
5414 repeat:
5415 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5416 /* step marks */
5417 int next = (last_mark+1) % SYNC_MARKS;
5418
5419 mddev->resync_mark = mark[next];
5420 mddev->resync_mark_cnt = mark_cnt[next];
5421 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005422 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 last_mark = next;
5424 }
5425
5426
NeilBrown787453c2005-11-08 21:39:43 -08005427 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 /*
5429 * got a signal, exit.
5430 */
5431 printk(KERN_INFO
5432 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5434 goto out;
5435 }
5436
5437 /*
5438 * this loop exits only if either when we are slower than
5439 * the 'hard' speed limit, or the system was IO-idle for
5440 * a jiffy.
5441 * the system might be non-idle CPU-wise, but we only care
5442 * about not overloading the IO subsystem. (things like an
5443 * e2fsck being done on the RAID array should execute fast)
5444 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005445 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 cond_resched();
5447
NeilBrown57afd892005-06-21 17:17:13 -07005448 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5449 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
NeilBrown88202a02006-01-06 00:21:36 -08005451 if (currspeed > speed_min(mddev)) {
5452 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005454 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 goto repeat;
5456 }
5457 }
5458 }
NeilBrown61df9d92006-10-03 01:15:57 -07005459 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 /*
5461 * this also signals 'finished resyncing' to md_stop
5462 */
5463 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005464 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
5466 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5467
5468 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005469 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
5471 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005472 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005473 mddev->curr_resync > 2) {
5474 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5475 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5476 if (mddev->curr_resync >= mddev->recovery_cp) {
5477 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005478 "md: checkpointing %s of %s.\n",
5479 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005480 mddev->recovery_cp = mddev->curr_resync;
5481 }
5482 } else
5483 mddev->recovery_cp = MaxSector;
5484 } else {
5485 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5486 mddev->curr_resync = MaxSector;
5487 ITERATE_RDEV(mddev,rdev,rtmp)
5488 if (rdev->raid_disk >= 0 &&
5489 !test_bit(Faulty, &rdev->flags) &&
5490 !test_bit(In_sync, &rdev->flags) &&
5491 rdev->recovery_offset < mddev->curr_resync)
5492 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 }
NeilBrown17571282006-12-10 02:20:52 -08005495 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 skip:
5498 mddev->curr_resync = 0;
5499 wake_up(&resync_wait);
5500 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5501 md_wakeup_thread(mddev->thread);
5502}
NeilBrown29269552006-03-27 01:18:10 -08005503EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
5505
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005506static int remove_and_add_spares(mddev_t *mddev)
5507{
5508 mdk_rdev_t *rdev;
5509 struct list_head *rtmp;
5510 int spares = 0;
5511
5512 ITERATE_RDEV(mddev,rdev,rtmp)
5513 if (rdev->raid_disk >= 0 &&
5514 (test_bit(Faulty, &rdev->flags) ||
5515 ! test_bit(In_sync, &rdev->flags)) &&
5516 atomic_read(&rdev->nr_pending)==0) {
5517 if (mddev->pers->hot_remove_disk(
5518 mddev, rdev->raid_disk)==0) {
5519 char nm[20];
5520 sprintf(nm,"rd%d", rdev->raid_disk);
5521 sysfs_remove_link(&mddev->kobj, nm);
5522 rdev->raid_disk = -1;
5523 }
5524 }
5525
5526 if (mddev->degraded) {
5527 ITERATE_RDEV(mddev,rdev,rtmp)
5528 if (rdev->raid_disk < 0
5529 && !test_bit(Faulty, &rdev->flags)) {
5530 rdev->recovery_offset = 0;
5531 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5532 char nm[20];
5533 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005534 if (sysfs_create_link(&mddev->kobj,
5535 &rdev->kobj, nm))
5536 printk(KERN_WARNING
5537 "md: cannot register "
5538 "%s for %s\n",
5539 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005540 spares++;
5541 md_new_event(mddev);
5542 } else
5543 break;
5544 }
5545 }
5546 return spares;
5547}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548/*
5549 * This routine is regularly called by all per-raid-array threads to
5550 * deal with generic issues like resync and super-block update.
5551 * Raid personalities that don't have a thread (linear/raid0) do not
5552 * need this as they never do any recovery or update the superblock.
5553 *
5554 * It does not do any resync itself, but rather "forks" off other threads
5555 * to do that as needed.
5556 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5557 * "->recovery" and create a thread at ->sync_thread.
5558 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5559 * and wakeups up this thread which will reap the thread and finish up.
5560 * This thread also removes any faulty devices (with nr_pending == 0).
5561 *
5562 * The overall approach is:
5563 * 1/ if the superblock needs updating, update it.
5564 * 2/ If a recovery thread is running, don't do anything else.
5565 * 3/ If recovery has finished, clean up, possibly marking spares active.
5566 * 4/ If there are any faulty devices, remove them.
5567 * 5/ If array is degraded, try to add spares devices
5568 * 6/ If array has spares or is not in-sync, start a resync thread.
5569 */
5570void md_check_recovery(mddev_t *mddev)
5571{
5572 mdk_rdev_t *rdev;
5573 struct list_head *rtmp;
5574
5575
NeilBrown5f404022005-06-21 17:17:16 -07005576 if (mddev->bitmap)
5577 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
5579 if (mddev->ro)
5580 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005581
5582 if (signal_pending(current)) {
5583 if (mddev->pers->sync_request) {
5584 printk(KERN_INFO "md: %s in immediate safe mode\n",
5585 mdname(mddev));
5586 mddev->safemode = 2;
5587 }
5588 flush_signals(current);
5589 }
5590
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 if ( ! (
NeilBrown850b2b42006-10-03 01:15:46 -07005592 mddev->flags ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005594 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5595 (mddev->safemode == 1) ||
5596 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5597 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 ))
5599 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005600
NeilBrowndf5b89b2006-03-27 01:18:20 -08005601 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005602 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07005603
NeilBrowna9701a32005-11-08 21:39:34 -08005604 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005605 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5606 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5607 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07005608 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownfca4d842005-06-21 17:17:11 -07005609 }
5610 if (mddev->safemode == 1)
5611 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005612 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005613
NeilBrown850b2b42006-10-03 01:15:46 -07005614 if (mddev->flags)
5615 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005616
NeilBrown06d91a52005-06-21 17:17:12 -07005617
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5619 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5620 /* resync/recovery still happening */
5621 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5622 goto unlock;
5623 }
5624 if (mddev->sync_thread) {
5625 /* resync has finished, collect result */
5626 md_unregister_thread(mddev->sync_thread);
5627 mddev->sync_thread = NULL;
5628 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5629 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5630 /* success...*/
5631 /* activate any spares */
5632 mddev->pers->spare_active(mddev);
5633 }
NeilBrown850b2b42006-10-03 01:15:46 -07005634 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005635
5636 /* if array is no-longer degraded, then any saved_raid_disk
5637 * information must be scrapped
5638 */
5639 if (!mddev->degraded)
5640 ITERATE_RDEV(mddev,rdev,rtmp)
5641 rdev->saved_raid_disk = -1;
5642
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 mddev->recovery = 0;
5644 /* flag recovery needed just to double check */
5645 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005646 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 goto unlock;
5648 }
NeilBrown24dd4692005-11-08 21:39:26 -08005649 /* Clear some bits that don't mean anything, but
5650 * might be left set
5651 */
5652 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5653 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5654 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5655 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005657 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5658 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 /* no recovery is running.
5660 * remove any failed drives, then
5661 * add spares if possible.
5662 * Spare are also removed and re-added, to allow
5663 * the personality to fail the re-add.
5664 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005666 if (mddev->reshape_position != MaxSector) {
5667 if (mddev->pers->check_reshape(mddev) != 0)
5668 /* Cannot proceed */
5669 goto unlock;
5670 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5671 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08005672 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5673 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5674 } else if (mddev->recovery_cp < MaxSector) {
5675 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5676 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5677 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005679
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 if (mddev->pers->sync_request) {
5681 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005682 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5683 /* We are adding a device or devices to an array
5684 * which has the bitmap stored on all devices.
5685 * So make sure all bitmap pages get written
5686 */
5687 bitmap_write_all(mddev->bitmap);
5688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 mddev->sync_thread = md_register_thread(md_do_sync,
5690 mddev,
5691 "%s_resync");
5692 if (!mddev->sync_thread) {
5693 printk(KERN_ERR "%s: could not start resync"
5694 " thread...\n",
5695 mdname(mddev));
5696 /* leave the spares where they are, it shouldn't hurt */
5697 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005698 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005700 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 }
5702 unlock:
5703 mddev_unlock(mddev);
5704 }
5705}
5706
Adrian Bunk75c96f82005-05-05 16:16:09 -07005707static int md_notify_reboot(struct notifier_block *this,
5708 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709{
5710 struct list_head *tmp;
5711 mddev_t *mddev;
5712
5713 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5714
5715 printk(KERN_INFO "md: stopping all md devices.\n");
5716
5717 ITERATE_MDDEV(mddev,tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005718 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005720 mddev_unlock(mddev);
5721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 /*
5723 * certain more exotic SCSI devices are known to be
5724 * volatile wrt too early system reboots. While the
5725 * right place to handle this issue is the given
5726 * driver, we do want to have a safe RAID driver ...
5727 */
5728 mdelay(1000*1);
5729 }
5730 return NOTIFY_DONE;
5731}
5732
Adrian Bunk75c96f82005-05-05 16:16:09 -07005733static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 .notifier_call = md_notify_reboot,
5735 .next = NULL,
5736 .priority = INT_MAX, /* before any real devices */
5737};
5738
5739static void md_geninit(void)
5740{
5741 struct proc_dir_entry *p;
5742
5743 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5744
5745 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5746 if (p)
5747 p->proc_fops = &md_seq_fops;
5748}
5749
Adrian Bunk75c96f82005-05-05 16:16:09 -07005750static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 if (register_blkdev(MAJOR_NR, "md"))
5753 return -1;
5754 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5755 unregister_blkdev(MAJOR_NR, "md");
5756 return -1;
5757 }
NeilBrowne8703fe2006-10-03 01:15:59 -07005758 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
5759 md_probe, NULL, NULL);
5760 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 md_probe, NULL, NULL);
5762
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08005764 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
5766 md_geninit();
5767 return (0);
5768}
5769
5770
5771#ifndef MODULE
5772
5773/*
5774 * Searches all registered partitions for autorun RAID arrays
5775 * at boot time.
5776 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005777
5778static LIST_HEAD(all_detected_devices);
5779struct detected_devices_node {
5780 struct list_head list;
5781 dev_t dev;
5782};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
5784void md_autodetect_dev(dev_t dev)
5785{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005786 struct detected_devices_node *node_detected_dev;
5787
5788 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
5789 if (node_detected_dev) {
5790 node_detected_dev->dev = dev;
5791 list_add_tail(&node_detected_dev->list, &all_detected_devices);
5792 } else {
5793 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
5794 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
5795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796}
5797
5798
5799static void autostart_arrays(int part)
5800{
5801 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005802 struct detected_devices_node *node_detected_dev;
5803 dev_t dev;
5804 int i_scanned, i_passed;
5805
5806 i_scanned = 0;
5807 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808
5809 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5810
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005811 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
5812 i_scanned++;
5813 node_detected_dev = list_entry(all_detected_devices.next,
5814 struct detected_devices_node, list);
5815 list_del(&node_detected_dev->list);
5816 dev = node_detected_dev->dev;
5817 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07005818 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 if (IS_ERR(rdev))
5820 continue;
5821
NeilBrownb2d444d2005-11-08 21:39:31 -08005822 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 MD_BUG();
5824 continue;
5825 }
5826 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005827 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005829
5830 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
5831 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
5833 autorun_devices(part);
5834}
5835
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005836#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837
5838static __exit void md_exit(void)
5839{
5840 mddev_t *mddev;
5841 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07005842
NeilBrowne8703fe2006-10-03 01:15:59 -07005843 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
5844 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845
5846 unregister_blkdev(MAJOR_NR,"md");
5847 unregister_blkdev(mdp_major, "mdp");
5848 unregister_reboot_notifier(&md_notifier);
5849 unregister_sysctl_table(raid_table_header);
5850 remove_proc_entry("mdstat", NULL);
5851 ITERATE_MDDEV(mddev,tmp) {
5852 struct gendisk *disk = mddev->gendisk;
5853 if (!disk)
5854 continue;
5855 export_array(mddev);
5856 del_gendisk(disk);
5857 put_disk(disk);
5858 mddev->gendisk = NULL;
5859 mddev_put(mddev);
5860 }
5861}
5862
Dan Williams685784a2007-07-09 11:56:42 -07005863subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864module_exit(md_exit)
5865
NeilBrownf91de922005-11-08 21:39:36 -08005866static int get_ro(char *buffer, struct kernel_param *kp)
5867{
5868 return sprintf(buffer, "%d", start_readonly);
5869}
5870static int set_ro(const char *val, struct kernel_param *kp)
5871{
5872 char *e;
5873 int num = simple_strtoul(val, &e, 10);
5874 if (*val && (*e == '\0' || *e == '\n')) {
5875 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005876 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005877 }
5878 return -EINVAL;
5879}
5880
NeilBrown80ca3a42006-07-10 04:44:18 -07005881module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
5882module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005883
NeilBrownf91de922005-11-08 21:39:36 -08005884
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885EXPORT_SYMBOL(register_md_personality);
5886EXPORT_SYMBOL(unregister_md_personality);
5887EXPORT_SYMBOL(md_error);
5888EXPORT_SYMBOL(md_done_sync);
5889EXPORT_SYMBOL(md_write_start);
5890EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891EXPORT_SYMBOL(md_register_thread);
5892EXPORT_SYMBOL(md_unregister_thread);
5893EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894EXPORT_SYMBOL(md_check_recovery);
5895MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005896MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005897MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);