blob: acd716b657b863c0e4e6ceae949968d0bc56448e [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 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800198#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 \
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;
NeilBrownc6207272008-02-06 01:39:52 -0800278 new->resync_max = MaxSector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 new->queue = blk_alloc_queue(GFP_KERNEL);
281 if (!new->queue) {
282 kfree(new);
283 return NULL;
284 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200285 /* Can be unlocked because the queue is new: no concurrency */
286 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 blk_queue_make_request(new->queue, md_fail_request);
289
290 goto retry;
291}
292
293static inline int mddev_lock(mddev_t * mddev)
294{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800295 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static inline int mddev_trylock(mddev_t * mddev)
299{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800300 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303static inline void mddev_unlock(mddev_t * mddev)
304{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800305 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
NeilBrown005eca52005-08-22 13:11:08 -0700307 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
NeilBrown2989ddb2006-01-06 00:20:43 -0800310static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 mdk_rdev_t * rdev;
313 struct list_head *tmp;
314
NeilBrownd089c6a2008-02-06 01:39:59 -0800315 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (rdev->desc_nr == nr)
317 return rdev;
318 }
319 return NULL;
320}
321
322static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
323{
324 struct list_head *tmp;
325 mdk_rdev_t *rdev;
326
NeilBrownd089c6a2008-02-06 01:39:59 -0800327 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (rdev->bdev->bd_dev == dev)
329 return rdev;
330 }
331 return NULL;
332}
333
NeilBrownd9d166c2006-01-06 00:20:51 -0800334static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800335{
336 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800337 list_for_each_entry(pers, &pers_list, list) {
338 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800339 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800340 if (strcmp(pers->name, clevel)==0)
341 return pers;
342 }
NeilBrown2604b702006-01-06 00:20:36 -0800343 return NULL;
344}
345
Jesper Juhl77933d72005-07-27 11:46:09 -0700346static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
349 return MD_NEW_SIZE_BLOCKS(size);
350}
351
352static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
353{
354 sector_t size;
355
356 size = rdev->sb_offset;
357
358 if (chunk_size)
359 size &= ~((sector_t)chunk_size/1024 - 1);
360 return size;
361}
362
363static int alloc_disk_sb(mdk_rdev_t * rdev)
364{
365 if (rdev->sb_page)
366 MD_BUG();
367
368 rdev->sb_page = alloc_page(GFP_KERNEL);
369 if (!rdev->sb_page) {
370 printk(KERN_ALERT "md: out of memory.\n");
371 return -EINVAL;
372 }
373
374 return 0;
375}
376
377static void free_disk_sb(mdk_rdev_t * rdev)
378{
379 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800380 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 rdev->sb_loaded = 0;
382 rdev->sb_page = NULL;
383 rdev->sb_offset = 0;
384 rdev->size = 0;
385 }
386}
387
388
NeilBrown6712ecf2007-09-27 12:47:43 +0200389static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700390{
391 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800392 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700393
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700394 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
395 printk("md: super_written gets error=%d, uptodate=%d\n",
396 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
397 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800398 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700399 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700400
NeilBrowna9701a32005-11-08 21:39:34 -0800401 if (atomic_dec_and_test(&mddev->pending_writes))
402 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700403 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700404}
405
NeilBrown6712ecf2007-09-27 12:47:43 +0200406static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800407{
408 struct bio *bio2 = bio->bi_private;
409 mdk_rdev_t *rdev = bio2->bi_private;
410 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800411
412 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
413 error == -EOPNOTSUPP) {
414 unsigned long flags;
415 /* barriers don't appear to be supported :-( */
416 set_bit(BarriersNotsupp, &rdev->flags);
417 mddev->barriers_work = 0;
418 spin_lock_irqsave(&mddev->write_lock, flags);
419 bio2->bi_next = mddev->biolist;
420 mddev->biolist = bio2;
421 spin_unlock_irqrestore(&mddev->write_lock, flags);
422 wake_up(&mddev->sb_wait);
423 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200424 } else {
425 bio_put(bio2);
426 bio->bi_private = rdev;
427 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800428 }
NeilBrowna9701a32005-11-08 21:39:34 -0800429}
430
NeilBrown7bfa19f2005-06-21 17:17:28 -0700431void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
432 sector_t sector, int size, struct page *page)
433{
434 /* write first size bytes of page to sector of rdev
435 * Increment mddev->pending_writes before returning
436 * and decrement it on completion, waking up sb_wait
437 * if zero is reached.
438 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800439 *
440 * As we might need to resubmit the request if BIO_RW_BARRIER
441 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700442 */
443 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800444 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700445
446 bio->bi_bdev = rdev->bdev;
447 bio->bi_sector = sector;
448 bio_add_page(bio, page, size, 0);
449 bio->bi_private = rdev;
450 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800451 bio->bi_rw = rw;
452
NeilBrown7bfa19f2005-06-21 17:17:28 -0700453 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800454 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
455 struct bio *rbio;
456 rw |= (1<<BIO_RW_BARRIER);
457 rbio = bio_clone(bio, GFP_NOIO);
458 rbio->bi_private = bio;
459 rbio->bi_end_io = super_written_barrier;
460 submit_bio(rw, rbio);
461 } else
462 submit_bio(rw, bio);
463}
464
465void md_super_wait(mddev_t *mddev)
466{
467 /* wait for all superblock writes that were scheduled to complete.
468 * if any had to be retried (due to BARRIER problems), retry them
469 */
470 DEFINE_WAIT(wq);
471 for(;;) {
472 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
473 if (atomic_read(&mddev->pending_writes)==0)
474 break;
475 while (mddev->biolist) {
476 struct bio *bio;
477 spin_lock_irq(&mddev->write_lock);
478 bio = mddev->biolist;
479 mddev->biolist = bio->bi_next ;
480 bio->bi_next = NULL;
481 spin_unlock_irq(&mddev->write_lock);
482 submit_bio(bio->bi_rw, bio);
483 }
484 schedule();
485 }
486 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700487}
488
NeilBrown6712ecf2007-09-27 12:47:43 +0200489static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
NeilBrowna654b9d82005-06-21 17:17:27 -0700494int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct page *page, int rw)
496{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700497 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct completion event;
499 int ret;
500
501 rw |= (1 << BIO_RW_SYNC);
502
503 bio->bi_bdev = bdev;
504 bio->bi_sector = sector;
505 bio_add_page(bio, page, size, 0);
506 init_completion(&event);
507 bio->bi_private = &event;
508 bio->bi_end_io = bi_complete;
509 submit_bio(rw, bio);
510 wait_for_completion(&event);
511
512 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
513 bio_put(bio);
514 return ret;
515}
NeilBrowna8745db2006-01-06 00:20:34 -0800516EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
NeilBrown0002b272005-09-09 16:23:53 -0700518static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 char b[BDEVNAME_SIZE];
521 if (!rdev->sb_page) {
522 MD_BUG();
523 return -EINVAL;
524 }
525 if (rdev->sb_loaded)
526 return 0;
527
528
NeilBrown0002b272005-09-09 16:23:53 -0700529 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 goto fail;
531 rdev->sb_loaded = 1;
532 return 0;
533
534fail:
535 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
536 bdevname(rdev->bdev,b));
537 return -EINVAL;
538}
539
540static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
541{
542 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
543 (sb1->set_uuid1 == sb2->set_uuid1) &&
544 (sb1->set_uuid2 == sb2->set_uuid2) &&
545 (sb1->set_uuid3 == sb2->set_uuid3))
546
547 return 1;
548
549 return 0;
550}
551
552
553static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
554{
555 int ret;
556 mdp_super_t *tmp1, *tmp2;
557
558 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
559 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
560
561 if (!tmp1 || !tmp2) {
562 ret = 0;
563 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
564 goto abort;
565 }
566
567 *tmp1 = *sb1;
568 *tmp2 = *sb2;
569
570 /*
571 * nr_disks is not constant
572 */
573 tmp1->nr_disks = 0;
574 tmp2->nr_disks = 0;
575
576 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
577 ret = 0;
578 else
579 ret = 1;
580
581abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700582 kfree(tmp1);
583 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return ret;
585}
586
NeilBrown4d167f02007-05-09 02:35:37 -0700587
588static u32 md_csum_fold(u32 csum)
589{
590 csum = (csum & 0xffff) + (csum >> 16);
591 return (csum & 0xffff) + (csum >> 16);
592}
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594static unsigned int calc_sb_csum(mdp_super_t * sb)
595{
NeilBrown4d167f02007-05-09 02:35:37 -0700596 u64 newcsum = 0;
597 u32 *sb32 = (u32*)sb;
598 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 unsigned int disk_csum, csum;
600
601 disk_csum = sb->sb_csum;
602 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700603
604 for (i = 0; i < MD_SB_BYTES/4 ; i++)
605 newcsum += sb32[i];
606 csum = (newcsum & 0xffffffff) + (newcsum>>32);
607
608
609#ifdef CONFIG_ALPHA
610 /* This used to use csum_partial, which was wrong for several
611 * reasons including that different results are returned on
612 * different architectures. It isn't critical that we get exactly
613 * the same return value as before (we always csum_fold before
614 * testing, and that removes any differences). However as we
615 * know that csum_partial always returned a 16bit value on
616 * alphas, do a fold to maximise conformity to previous behaviour.
617 */
618 sb->sb_csum = md_csum_fold(disk_csum);
619#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700621#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return csum;
623}
624
625
626/*
627 * Handle superblock details.
628 * We want to be able to handle multiple superblock formats
629 * so we have a common interface to them all, and an array of
630 * different handlers.
631 * We rely on user-space to write the initial superblock, and support
632 * reading and updating of superblocks.
633 * Interface methods are:
634 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
635 * loads and validates a superblock on dev.
636 * if refdev != NULL, compare superblocks on both devices
637 * Return:
638 * 0 - dev has a superblock that is compatible with refdev
639 * 1 - dev has a superblock that is compatible and newer than refdev
640 * so dev should be used as the refdev in future
641 * -EINVAL superblock incompatible or invalid
642 * -othererror e.g. -EIO
643 *
644 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
645 * Verify that dev is acceptable into mddev.
646 * The first time, mddev->raid_disks will be 0, and data from
647 * dev should be merged in. Subsequent calls check that dev
648 * is new enough. Return 0 or -EINVAL
649 *
650 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
651 * Update the superblock for rdev with data in mddev
652 * This does not write to disc.
653 *
654 */
655
656struct super_type {
657 char *name;
658 struct module *owner;
659 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
660 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
661 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
662};
663
664/*
665 * load_super for 0.90.0
666 */
667static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
668{
669 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
670 mdp_super_t *sb;
671 int ret;
672 sector_t sb_offset;
673
674 /*
675 * Calculate the position of the superblock,
676 * it's at the end of the disk.
677 *
678 * It also happens to be a multiple of 4Kb.
679 */
680 sb_offset = calc_dev_sboffset(rdev->bdev);
681 rdev->sb_offset = sb_offset;
682
NeilBrown0002b272005-09-09 16:23:53 -0700683 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (ret) return ret;
685
686 ret = -EINVAL;
687
688 bdevname(rdev->bdev, b);
689 sb = (mdp_super_t*)page_address(rdev->sb_page);
690
691 if (sb->md_magic != MD_SB_MAGIC) {
692 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
693 b);
694 goto abort;
695 }
696
697 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800698 sb->minor_version < 90 ||
699 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
701 sb->major_version, sb->minor_version,
702 b);
703 goto abort;
704 }
705
706 if (sb->raid_disks <= 0)
707 goto abort;
708
NeilBrown4d167f02007-05-09 02:35:37 -0700709 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
711 b);
712 goto abort;
713 }
714
715 rdev->preferred_minor = sb->md_minor;
716 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700717 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
NeilBrowne11e93f2007-05-09 02:35:36 -0700719 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
720 if (sb->level != 1 && sb->level != 4
721 && sb->level != 5 && sb->level != 6
722 && sb->level != 10) {
723 /* FIXME use a better test */
724 printk(KERN_WARNING
725 "md: bitmaps not supported for this level.\n");
726 goto abort;
727 }
728 }
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (sb->level == LEVEL_MULTIPATH)
731 rdev->desc_nr = -1;
732 else
733 rdev->desc_nr = sb->this_disk.number;
734
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700735 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700737 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 __u64 ev1, ev2;
739 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
740 if (!uuid_equal(refsb, sb)) {
741 printk(KERN_WARNING "md: %s has different UUID to %s\n",
742 b, bdevname(refdev->bdev,b2));
743 goto abort;
744 }
745 if (!sb_equal(refsb, sb)) {
746 printk(KERN_WARNING "md: %s has same UUID"
747 " but different superblock to %s\n",
748 b, bdevname(refdev->bdev, b2));
749 goto abort;
750 }
751 ev1 = md_event(sb);
752 ev2 = md_event(refsb);
753 if (ev1 > ev2)
754 ret = 1;
755 else
756 ret = 0;
757 }
758 rdev->size = calc_dev_size(rdev, sb->chunk_size);
759
NeilBrown2bf071b2006-01-06 00:20:55 -0800760 if (rdev->size < sb->size && sb->level > 1)
761 /* "this cannot possibly happen" ... */
762 ret = -EINVAL;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 abort:
765 return ret;
766}
767
768/*
769 * validate_super for 0.90.0
770 */
771static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
772{
773 mdp_disk_t *desc;
774 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700775 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
NeilBrown41158c72005-06-21 17:17:25 -0700777 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800778 clear_bit(Faulty, &rdev->flags);
779 clear_bit(In_sync, &rdev->flags);
780 clear_bit(WriteMostly, &rdev->flags);
781 clear_bit(BarriersNotsupp, &rdev->flags);
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (mddev->raid_disks == 0) {
784 mddev->major_version = 0;
785 mddev->minor_version = sb->minor_version;
786 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800787 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 mddev->chunk_size = sb->chunk_size;
789 mddev->ctime = sb->ctime;
790 mddev->utime = sb->utime;
791 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800792 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 mddev->layout = sb->layout;
794 mddev->raid_disks = sb->raid_disks;
795 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700796 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700797 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700798 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
NeilBrownf6705572006-03-27 01:18:11 -0800800 if (mddev->minor_version >= 91) {
801 mddev->reshape_position = sb->reshape_position;
802 mddev->delta_disks = sb->delta_disks;
803 mddev->new_level = sb->new_level;
804 mddev->new_layout = sb->new_layout;
805 mddev->new_chunk = sb->new_chunk;
806 } else {
807 mddev->reshape_position = MaxSector;
808 mddev->delta_disks = 0;
809 mddev->new_level = mddev->level;
810 mddev->new_layout = mddev->layout;
811 mddev->new_chunk = mddev->chunk_size;
812 }
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (sb->state & (1<<MD_SB_CLEAN))
815 mddev->recovery_cp = MaxSector;
816 else {
817 if (sb->events_hi == sb->cp_events_hi &&
818 sb->events_lo == sb->cp_events_lo) {
819 mddev->recovery_cp = sb->recovery_cp;
820 } else
821 mddev->recovery_cp = 0;
822 }
823
824 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
825 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
826 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
827 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
828
829 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700830
831 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700832 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700833 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700834
NeilBrown41158c72005-06-21 17:17:25 -0700835 } else if (mddev->pers == NULL) {
836 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 ++ev1;
838 if (ev1 < mddev->events)
839 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700840 } else if (mddev->bitmap) {
841 /* if adding to array with a bitmap, then we can accept an
842 * older device ... but not too old.
843 */
NeilBrown41158c72005-06-21 17:17:25 -0700844 if (ev1 < mddev->bitmap->events_cleared)
845 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700846 } else {
847 if (ev1 < mddev->events)
848 /* just a hot-add of a new device, leave raid_disk at -1 */
849 return 0;
850 }
NeilBrown41158c72005-06-21 17:17:25 -0700851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 desc = sb->disks + rdev->desc_nr;
854
855 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800856 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700857 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
858 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800859 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 rdev->raid_disk = desc->raid_disk;
861 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700862 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
863 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700864 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800865 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return 0;
867}
868
869/*
870 * sync_super for 0.90.0
871 */
872static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
873{
874 mdp_super_t *sb;
875 struct list_head *tmp;
876 mdk_rdev_t *rdev2;
877 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 /* make rdev->sb match mddev data..
881 *
882 * 1/ zero out disks
883 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
884 * 3/ any empty disks < next_spare become removed
885 *
886 * disks[0] gets initialised to REMOVED because
887 * we cannot be sure from other fields if it has
888 * been initialised or not.
889 */
890 int i;
891 int active=0, working=0,failed=0,spare=0,nr_disks=0;
892
NeilBrown61181562005-09-09 16:24:02 -0700893 rdev->sb_size = MD_SB_BYTES;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 sb = (mdp_super_t*)page_address(rdev->sb_page);
896
897 memset(sb, 0, sizeof(*sb));
898
899 sb->md_magic = MD_SB_MAGIC;
900 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 sb->patch_version = mddev->patch_version;
902 sb->gvalid_words = 0; /* ignored */
903 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
904 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
905 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
906 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
907
908 sb->ctime = mddev->ctime;
909 sb->level = mddev->level;
910 sb->size = mddev->size;
911 sb->raid_disks = mddev->raid_disks;
912 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800913 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 sb->utime = mddev->utime;
915 sb->state = 0;
916 sb->events_hi = (mddev->events>>32);
917 sb->events_lo = (u32)mddev->events;
918
NeilBrownf6705572006-03-27 01:18:11 -0800919 if (mddev->reshape_position == MaxSector)
920 sb->minor_version = 90;
921 else {
922 sb->minor_version = 91;
923 sb->reshape_position = mddev->reshape_position;
924 sb->new_level = mddev->new_level;
925 sb->delta_disks = mddev->delta_disks;
926 sb->new_layout = mddev->new_layout;
927 sb->new_chunk = mddev->new_chunk;
928 }
929 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (mddev->in_sync)
931 {
932 sb->recovery_cp = mddev->recovery_cp;
933 sb->cp_events_hi = (mddev->events>>32);
934 sb->cp_events_lo = (u32)mddev->events;
935 if (mddev->recovery_cp == MaxSector)
936 sb->state = (1<< MD_SB_CLEAN);
937 } else
938 sb->recovery_cp = 0;
939
940 sb->layout = mddev->layout;
941 sb->chunk_size = mddev->chunk_size;
942
NeilBrowna654b9d82005-06-21 17:17:27 -0700943 if (mddev->bitmap && mddev->bitmap_file == NULL)
944 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrownd089c6a2008-02-06 01:39:59 -0800947 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800949 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800950 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
951 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800952 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800954 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800955 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 d = &sb->disks[rdev2->desc_nr];
957 nr_disks++;
958 d->number = rdev2->desc_nr;
959 d->major = MAJOR(rdev2->bdev->bd_dev);
960 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800961 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
962 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 d->raid_disk = rdev2->raid_disk;
964 else
965 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800966 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800968 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 d->state = (1<<MD_DISK_ACTIVE);
970 d->state |= (1<<MD_DISK_SYNC);
971 active++;
972 working++;
973 } else {
974 d->state = 0;
975 spare++;
976 working++;
977 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700978 if (test_bit(WriteMostly, &rdev2->flags))
979 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /* now set the "removed" and "faulty" bits on any missing devices */
982 for (i=0 ; i < mddev->raid_disks ; i++) {
983 mdp_disk_t *d = &sb->disks[i];
984 if (d->state == 0 && d->number == 0) {
985 d->number = i;
986 d->raid_disk = i;
987 d->state = (1<<MD_DISK_REMOVED);
988 d->state |= (1<<MD_DISK_FAULTY);
989 failed++;
990 }
991 }
992 sb->nr_disks = nr_disks;
993 sb->active_disks = active;
994 sb->working_disks = working;
995 sb->failed_disks = failed;
996 sb->spare_disks = spare;
997
998 sb->this_disk = sb->disks[rdev->desc_nr];
999 sb->sb_csum = calc_sb_csum(sb);
1000}
1001
1002/*
1003 * version 1 superblock
1004 */
1005
NeilBrown1c05b4b2006-10-21 10:24:08 -07001006static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001008 __le32 disk_csum;
1009 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 unsigned long long newcsum;
1011 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001012 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int i;
1014
1015 disk_csum = sb->sb_csum;
1016 sb->sb_csum = 0;
1017 newcsum = 0;
1018 for (i=0; size>=4; size -= 4 )
1019 newcsum += le32_to_cpu(*isuper++);
1020
1021 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001022 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1025 sb->sb_csum = disk_csum;
1026 return cpu_to_le32(csum);
1027}
1028
1029static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1030{
1031 struct mdp_superblock_1 *sb;
1032 int ret;
1033 sector_t sb_offset;
1034 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001035 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 /*
1038 * Calculate the position of the superblock.
1039 * It is always aligned to a 4K boundary and
1040 * depeding on minor_version, it can be:
1041 * 0: At least 8K, but less than 12K, from end of device
1042 * 1: At start of device
1043 * 2: 4K from start of device.
1044 */
1045 switch(minor_version) {
1046 case 0:
1047 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1048 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001049 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /* convert from sectors to K */
1051 sb_offset /= 2;
1052 break;
1053 case 1:
1054 sb_offset = 0;
1055 break;
1056 case 2:
1057 sb_offset = 4;
1058 break;
1059 default:
1060 return -EINVAL;
1061 }
1062 rdev->sb_offset = sb_offset;
1063
NeilBrown0002b272005-09-09 16:23:53 -07001064 /* superblock is rarely larger than 1K, but it can be larger,
1065 * and it is safe to read 4k, so we do that
1066 */
1067 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (ret) return ret;
1069
1070
1071 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1072
1073 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1074 sb->major_version != cpu_to_le32(1) ||
1075 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1076 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001077 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return -EINVAL;
1079
1080 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1081 printk("md: invalid superblock checksum on %s\n",
1082 bdevname(rdev->bdev,b));
1083 return -EINVAL;
1084 }
1085 if (le64_to_cpu(sb->data_size) < 10) {
1086 printk("md: data_size too small on %s\n",
1087 bdevname(rdev->bdev,b));
1088 return -EINVAL;
1089 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001090 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1091 if (sb->level != cpu_to_le32(1) &&
1092 sb->level != cpu_to_le32(4) &&
1093 sb->level != cpu_to_le32(5) &&
1094 sb->level != cpu_to_le32(6) &&
1095 sb->level != cpu_to_le32(10)) {
1096 printk(KERN_WARNING
1097 "md: bitmaps not supported for this level.\n");
1098 return -EINVAL;
1099 }
1100 }
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 rdev->preferred_minor = 0xffff;
1103 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001104 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
NeilBrown0002b272005-09-09 16:23:53 -07001106 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001107 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001108 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001109 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1110
1111 if (minor_version
1112 && rdev->data_offset < sb_offset + (rdev->sb_size/512))
1113 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001114
NeilBrown31b65a02006-07-10 04:44:14 -07001115 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1116 rdev->desc_nr = -1;
1117 else
1118 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1119
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001120 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001121 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001122 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 __u64 ev1, ev2;
1124 struct mdp_superblock_1 *refsb =
1125 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1126
1127 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1128 sb->level != refsb->level ||
1129 sb->layout != refsb->layout ||
1130 sb->chunksize != refsb->chunksize) {
1131 printk(KERN_WARNING "md: %s has strangely different"
1132 " superblock to %s\n",
1133 bdevname(rdev->bdev,b),
1134 bdevname(refdev->bdev,b2));
1135 return -EINVAL;
1136 }
1137 ev1 = le64_to_cpu(sb->events);
1138 ev2 = le64_to_cpu(refsb->events);
1139
1140 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001141 ret = 1;
1142 else
1143 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
NeilBrowna1801f82008-03-04 14:29:31 -08001145 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1147 else
1148 rdev->size = rdev->sb_offset;
1149 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1150 return -EINVAL;
1151 rdev->size = le64_to_cpu(sb->data_size)/2;
1152 if (le32_to_cpu(sb->chunksize))
1153 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001154
NeilBrown1c05b4b2006-10-21 10:24:08 -07001155 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001156 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001157 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
1160static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1161{
1162 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001163 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
NeilBrown41158c72005-06-21 17:17:25 -07001165 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001166 clear_bit(Faulty, &rdev->flags);
1167 clear_bit(In_sync, &rdev->flags);
1168 clear_bit(WriteMostly, &rdev->flags);
1169 clear_bit(BarriersNotsupp, &rdev->flags);
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (mddev->raid_disks == 0) {
1172 mddev->major_version = 1;
1173 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001174 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1176 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1177 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1178 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001179 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 mddev->layout = le32_to_cpu(sb->layout);
1181 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1182 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001183 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001184 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001185 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1188 memcpy(mddev->uuid, sb->set_uuid, 16);
1189
1190 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001191
NeilBrown71c08052005-09-09 16:23:51 -07001192 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001193 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001194 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001195
NeilBrownf6705572006-03-27 01:18:11 -08001196 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1197 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1198 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1199 mddev->new_level = le32_to_cpu(sb->new_level);
1200 mddev->new_layout = le32_to_cpu(sb->new_layout);
1201 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1202 } else {
1203 mddev->reshape_position = MaxSector;
1204 mddev->delta_disks = 0;
1205 mddev->new_level = mddev->level;
1206 mddev->new_layout = mddev->layout;
1207 mddev->new_chunk = mddev->chunk_size;
1208 }
1209
NeilBrown41158c72005-06-21 17:17:25 -07001210 } else if (mddev->pers == NULL) {
1211 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 ++ev1;
1213 if (ev1 < mddev->events)
1214 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001215 } else if (mddev->bitmap) {
1216 /* If adding to array with a bitmap, then we can accept an
1217 * older device, but not too old.
1218 */
NeilBrown41158c72005-06-21 17:17:25 -07001219 if (ev1 < mddev->bitmap->events_cleared)
1220 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001221 } else {
1222 if (ev1 < mddev->events)
1223 /* just a hot-add of a new device, leave raid_disk at -1 */
1224 return 0;
1225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (mddev->level != LEVEL_MULTIPATH) {
1227 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1229 switch(role) {
1230 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 break;
1232 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001233 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 break;
1235 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001236 if ((le32_to_cpu(sb->feature_map) &
1237 MD_FEATURE_RECOVERY_OFFSET))
1238 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1239 else
1240 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 rdev->raid_disk = role;
1242 break;
1243 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001244 if (sb->devflags & WriteMostly1)
1245 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001246 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001247 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return 0;
1250}
1251
1252static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1253{
1254 struct mdp_superblock_1 *sb;
1255 struct list_head *tmp;
1256 mdk_rdev_t *rdev2;
1257 int max_dev, i;
1258 /* make rdev->sb match mddev and rdev data. */
1259
1260 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1261
1262 sb->feature_map = 0;
1263 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001264 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 memset(sb->pad1, 0, sizeof(sb->pad1));
1266 memset(sb->pad2, 0, sizeof(sb->pad2));
1267 memset(sb->pad3, 0, sizeof(sb->pad3));
1268
1269 sb->utime = cpu_to_le64((__u64)mddev->utime);
1270 sb->events = cpu_to_le64(mddev->events);
1271 if (mddev->in_sync)
1272 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1273 else
1274 sb->resync_offset = cpu_to_le64(0);
1275
NeilBrown1c05b4b2006-10-21 10:24:08 -07001276 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001277
NeilBrownf0ca3402006-02-02 14:28:04 -08001278 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001279 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001280
NeilBrowna654b9d82005-06-21 17:17:27 -07001281 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1282 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001283 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001284 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001285
1286 if (rdev->raid_disk >= 0 &&
1287 !test_bit(In_sync, &rdev->flags) &&
1288 rdev->recovery_offset > 0) {
1289 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1290 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1291 }
1292
NeilBrownf6705572006-03-27 01:18:11 -08001293 if (mddev->reshape_position != MaxSector) {
1294 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1295 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1296 sb->new_layout = cpu_to_le32(mddev->new_layout);
1297 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1298 sb->new_level = cpu_to_le32(mddev->new_level);
1299 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1300 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 max_dev = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08001303 rdev_for_each(rdev2, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (rdev2->desc_nr+1 > max_dev)
1305 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001306
1307 if (max_dev > le32_to_cpu(sb->max_dev))
1308 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 for (i=0; i<max_dev;i++)
1310 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1311
NeilBrownd089c6a2008-02-06 01:39:59 -08001312 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001314 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001316 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001318 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1319 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 else
1321 sb->dev_roles[i] = cpu_to_le16(0xffff);
1322 }
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 sb->sb_csum = calc_sb_1_csum(sb);
1325}
1326
1327
Adrian Bunk75c96f82005-05-05 16:16:09 -07001328static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 [0] = {
1330 .name = "0.90.0",
1331 .owner = THIS_MODULE,
1332 .load_super = super_90_load,
1333 .validate_super = super_90_validate,
1334 .sync_super = super_90_sync,
1335 },
1336 [1] = {
1337 .name = "md-1",
1338 .owner = THIS_MODULE,
1339 .load_super = super_1_load,
1340 .validate_super = super_1_validate,
1341 .sync_super = super_1_sync,
1342 },
1343};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1346{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001347 struct list_head *tmp, *tmp2;
1348 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
NeilBrownd089c6a2008-02-06 01:39:59 -08001350 rdev_for_each(rdev, tmp, mddev1)
1351 rdev_for_each(rdev2, tmp2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001352 if (rdev->bdev->bd_contains ==
1353 rdev2->bdev->bd_contains)
1354 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 return 0;
1357}
1358
1359static LIST_HEAD(pending_raid_disks);
1360
1361static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1362{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001363 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001364 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001365 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001366 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 if (rdev->mddev) {
1369 MD_BUG();
1370 return -EINVAL;
1371 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001372 /* make sure rdev->size exceeds mddev->size */
1373 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001374 if (mddev->pers) {
1375 /* Cannot change size, so fail
1376 * If mddev->level <= 0, then we don't care
1377 * about aligning sizes (e.g. linear)
1378 */
1379 if (mddev->level > 0)
1380 return -ENOSPC;
1381 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001382 mddev->size = rdev->size;
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /* Verify rdev->desc_nr is unique.
1386 * If it is -1, assign a free number, else
1387 * check number is not in use
1388 */
1389 if (rdev->desc_nr < 0) {
1390 int choice = 0;
1391 if (mddev->pers) choice = mddev->raid_disks;
1392 while (find_rdev_nr(mddev, choice))
1393 choice++;
1394 rdev->desc_nr = choice;
1395 } else {
1396 if (find_rdev_nr(mddev, rdev->desc_nr))
1397 return -EBUSY;
1398 }
NeilBrown19133a42005-11-08 21:39:35 -08001399 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001400 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001401 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001404 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001405
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001406 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001407 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001408
NeilBrownf637b9f2005-11-08 21:39:37 -08001409 if (rdev->bdev->bd_part)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001410 ko = &rdev->bdev->bd_part->dev.kobj;
NeilBrownf637b9f2005-11-08 21:39:37 -08001411 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001412 ko = &rdev->bdev->bd_disk->dev.kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001413 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1414 kobject_del(&rdev->kobj);
1415 goto fail;
1416 }
1417 list_add(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001418 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001420
1421 fail:
1422 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1423 b, mdname(mddev));
1424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
NeilBrown177a99b2008-02-06 01:39:56 -08001427static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001428{
1429 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1430 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001431 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001432}
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1435{
1436 char b[BDEVNAME_SIZE];
1437 if (!rdev->mddev) {
1438 MD_BUG();
1439 return;
1440 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001441 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 list_del_init(&rdev->same_set);
1443 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1444 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001445 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001446
1447 /* We need to delay this, otherwise we can deadlock when
1448 * writing to 'remove' to "dev/state"
1449 */
NeilBrown177a99b2008-02-06 01:39:56 -08001450 INIT_WORK(&rdev->del_work, md_delayed_delete);
1451 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001452 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455/*
1456 * prevent the device from being mounted, repartitioned or
1457 * otherwise reused by a RAID array (or any other kernel
1458 * subsystem), by bd_claiming the device.
1459 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001460static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 int err = 0;
1463 struct block_device *bdev;
1464 char b[BDEVNAME_SIZE];
1465
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001466 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (IS_ERR(bdev)) {
1468 printk(KERN_ERR "md: could not open %s.\n",
1469 __bdevname(dev, b));
1470 return PTR_ERR(bdev);
1471 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001472 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (err) {
1474 printk(KERN_ERR "md: could not bd_claim %s.\n",
1475 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001476 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return err;
1478 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001479 if (!shared)
1480 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 rdev->bdev = bdev;
1482 return err;
1483}
1484
1485static void unlock_rdev(mdk_rdev_t *rdev)
1486{
1487 struct block_device *bdev = rdev->bdev;
1488 rdev->bdev = NULL;
1489 if (!bdev)
1490 MD_BUG();
1491 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001492 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
1495void md_autodetect_dev(dev_t dev);
1496
1497static void export_rdev(mdk_rdev_t * rdev)
1498{
1499 char b[BDEVNAME_SIZE];
1500 printk(KERN_INFO "md: export_rdev(%s)\n",
1501 bdevname(rdev->bdev,b));
1502 if (rdev->mddev)
1503 MD_BUG();
1504 free_disk_sb(rdev);
1505 list_del_init(&rdev->same_set);
1506#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001507 if (test_bit(AutoDetected, &rdev->flags))
1508 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509#endif
1510 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001511 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
1514static void kick_rdev_from_array(mdk_rdev_t * rdev)
1515{
1516 unbind_rdev_from_array(rdev);
1517 export_rdev(rdev);
1518}
1519
1520static void export_array(mddev_t *mddev)
1521{
1522 struct list_head *tmp;
1523 mdk_rdev_t *rdev;
1524
NeilBrownd089c6a2008-02-06 01:39:59 -08001525 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (!rdev->mddev) {
1527 MD_BUG();
1528 continue;
1529 }
1530 kick_rdev_from_array(rdev);
1531 }
1532 if (!list_empty(&mddev->disks))
1533 MD_BUG();
1534 mddev->raid_disks = 0;
1535 mddev->major_version = 0;
1536}
1537
1538static void print_desc(mdp_disk_t *desc)
1539{
1540 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1541 desc->major,desc->minor,desc->raid_disk,desc->state);
1542}
1543
1544static void print_sb(mdp_super_t *sb)
1545{
1546 int i;
1547
1548 printk(KERN_INFO
1549 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1550 sb->major_version, sb->minor_version, sb->patch_version,
1551 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1552 sb->ctime);
1553 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1554 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1555 sb->md_minor, sb->layout, sb->chunk_size);
1556 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1557 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1558 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1559 sb->failed_disks, sb->spare_disks,
1560 sb->sb_csum, (unsigned long)sb->events_lo);
1561
1562 printk(KERN_INFO);
1563 for (i = 0; i < MD_SB_DISKS; i++) {
1564 mdp_disk_t *desc;
1565
1566 desc = sb->disks + i;
1567 if (desc->number || desc->major || desc->minor ||
1568 desc->raid_disk || (desc->state && (desc->state != 4))) {
1569 printk(" D %2d: ", i);
1570 print_desc(desc);
1571 }
1572 }
1573 printk(KERN_INFO "md: THIS: ");
1574 print_desc(&sb->this_disk);
1575
1576}
1577
1578static void print_rdev(mdk_rdev_t *rdev)
1579{
1580 char b[BDEVNAME_SIZE];
1581 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1582 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001583 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1584 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (rdev->sb_loaded) {
1586 printk(KERN_INFO "md: rdev superblock:\n");
1587 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1588 } else
1589 printk(KERN_INFO "md: no rdev superblock!\n");
1590}
1591
Adrian Bunk5e563412006-06-26 00:27:42 -07001592static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 struct list_head *tmp, *tmp2;
1595 mdk_rdev_t *rdev;
1596 mddev_t *mddev;
1597 char b[BDEVNAME_SIZE];
1598
1599 printk("\n");
1600 printk("md: **********************************\n");
1601 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1602 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001603 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
NeilBrown32a76272005-06-21 17:17:14 -07001605 if (mddev->bitmap)
1606 bitmap_print_sb(mddev->bitmap);
1607 else
1608 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001609 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 printk("<%s>", bdevname(rdev->bdev,b));
1611 printk("\n");
1612
NeilBrownd089c6a2008-02-06 01:39:59 -08001613 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 print_rdev(rdev);
1615 }
1616 printk("md: **********************************\n");
1617 printk("\n");
1618}
1619
1620
NeilBrown42543762006-06-26 00:27:57 -07001621static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
NeilBrown42543762006-06-26 00:27:57 -07001623 /* Update each superblock (in-memory image), but
1624 * if we are allowed to, skip spares which already
1625 * have the right event counter, or have one earlier
1626 * (which would mean they aren't being marked as dirty
1627 * with the rest of the array)
1628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 mdk_rdev_t *rdev;
1630 struct list_head *tmp;
1631
NeilBrownd089c6a2008-02-06 01:39:59 -08001632 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001633 if (rdev->sb_events == mddev->events ||
1634 (nospares &&
1635 rdev->raid_disk < 0 &&
1636 (rdev->sb_events&1)==0 &&
1637 rdev->sb_events+1 == mddev->events)) {
1638 /* Don't update this superblock */
1639 rdev->sb_loaded = 2;
1640 } else {
1641 super_types[mddev->major_version].
1642 sync_super(mddev, rdev);
1643 rdev->sb_loaded = 1;
1644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
1646}
1647
NeilBrown850b2b422006-10-03 01:15:46 -07001648static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 struct list_head *tmp;
1651 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001652 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001653 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001656 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001657
NeilBrown850b2b422006-10-03 01:15:46 -07001658 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1659 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1660 force_change = 1;
1661 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1662 /* just a clean<-> dirty transition, possibly leave spares alone,
1663 * though if events isn't the right even/odd, we will have to do
1664 * spares after all
1665 */
1666 nospares = 1;
1667 if (force_change)
1668 nospares = 0;
1669 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001670 /* If the array is degraded, then skipping spares is both
1671 * dangerous and fairly pointless.
1672 * Dangerous because a device that was removed from the array
1673 * might have a event_count that still looks up-to-date,
1674 * so it can be re-added without a resync.
1675 * Pointless because if there are any spares to skip,
1676 * then a recovery will happen and soon that array won't
1677 * be degraded any more and the spare can go back to sleep then.
1678 */
NeilBrown850b2b422006-10-03 01:15:46 -07001679 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001680
NeilBrown06d91a52005-06-21 17:17:12 -07001681 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001683
1684 /* If this is just a dirty<->clean transition, and the array is clean
1685 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07001686 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001687 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001688 && (mddev->events & 1)
1689 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001690 mddev->events--;
1691 else {
1692 /* otherwise we have to go forward and ... */
1693 mddev->events ++;
1694 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1695 /* .. if the array isn't clean, insist on an odd 'events' */
1696 if ((mddev->events&1)==0) {
1697 mddev->events++;
1698 nospares = 0;
1699 }
1700 } else {
1701 /* otherwise insist on an even 'events' (for clean states) */
1702 if ((mddev->events&1)) {
1703 mddev->events++;
1704 nospares = 0;
1705 }
1706 }
1707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 if (!mddev->events) {
1710 /*
1711 * oops, this 64-bit counter should never wrap.
1712 * Either we are in around ~1 trillion A.C., assuming
1713 * 1 reboot per second, or we have a bug:
1714 */
1715 MD_BUG();
1716 mddev->events --;
1717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 /*
1720 * do not write anything to disk if using
1721 * nonpersistent superblocks
1722 */
NeilBrown06d91a52005-06-21 17:17:12 -07001723 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001724 if (!mddev->external)
1725 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1726
NeilBrowna9701a32005-11-08 21:39:34 -08001727 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001728 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001730 }
NeilBrowne6910632008-02-06 01:39:51 -08001731 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001732 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 dprintk(KERN_INFO
1735 "md: updating %s RAID superblock on device (in sync %d)\n",
1736 mdname(mddev),mddev->in_sync);
1737
NeilBrown4ad13662007-07-17 04:06:13 -07001738 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001739 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 char b[BDEVNAME_SIZE];
1741 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001742 if (rdev->sb_loaded != 1)
1743 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001744 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 dprintk("(skipping faulty ");
1746
1747 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001748 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001749 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001750 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001751 rdev->sb_page);
1752 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1753 bdevname(rdev->bdev,b),
1754 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001755 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 } else
1758 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001759 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 /* only need to write one superblock... */
1761 break;
1762 }
NeilBrowna9701a32005-11-08 21:39:34 -08001763 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07001764 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001765
NeilBrowna9701a32005-11-08 21:39:34 -08001766 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07001767 if (mddev->in_sync != sync_req ||
1768 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001769 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001770 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001771 goto repeat;
1772 }
NeilBrown850b2b422006-10-03 01:15:46 -07001773 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001774 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001775 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
NeilBrownbce74da2006-01-06 00:20:41 -08001779/* words written to sysfs files may, or my not, be \n terminated.
1780 * We want to accept with case. For this we use cmd_match.
1781 */
1782static int cmd_match(const char *cmd, const char *str)
1783{
1784 /* See if cmd, written into a sysfs file, matches
1785 * str. They must either be the same, or cmd can
1786 * have a trailing newline
1787 */
1788 while (*cmd && *str && *cmd == *str) {
1789 cmd++;
1790 str++;
1791 }
1792 if (*cmd == '\n')
1793 cmd++;
1794 if (*str || *cmd)
1795 return 0;
1796 return 1;
1797}
1798
NeilBrown86e6ffd2005-11-08 21:39:24 -08001799struct rdev_sysfs_entry {
1800 struct attribute attr;
1801 ssize_t (*show)(mdk_rdev_t *, char *);
1802 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1803};
1804
1805static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001806state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001807{
1808 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001809 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001810
NeilBrownb2d444d2005-11-08 21:39:31 -08001811 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001812 len+= sprintf(page+len, "%sfaulty",sep);
1813 sep = ",";
1814 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001815 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001816 len += sprintf(page+len, "%sin_sync",sep);
1817 sep = ",";
1818 }
NeilBrownf6556752006-06-26 00:28:01 -07001819 if (test_bit(WriteMostly, &rdev->flags)) {
1820 len += sprintf(page+len, "%swrite_mostly",sep);
1821 sep = ",";
1822 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001823 if (!test_bit(Faulty, &rdev->flags) &&
1824 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001825 len += sprintf(page+len, "%sspare", sep);
1826 sep = ",";
1827 }
1828 return len+sprintf(page+len, "\n");
1829}
1830
NeilBrown45dc2de2006-06-26 00:27:58 -07001831static ssize_t
1832state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1833{
1834 /* can write
1835 * faulty - simulates and error
1836 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001837 * writemostly - sets write_mostly
1838 * -writemostly - clears write_mostly
NeilBrown45dc2de2006-06-26 00:27:58 -07001839 */
1840 int err = -EINVAL;
1841 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1842 md_error(rdev->mddev, rdev);
1843 err = 0;
1844 } else if (cmd_match(buf, "remove")) {
1845 if (rdev->raid_disk >= 0)
1846 err = -EBUSY;
1847 else {
1848 mddev_t *mddev = rdev->mddev;
1849 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001850 if (mddev->pers)
1851 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001852 md_new_event(mddev);
1853 err = 0;
1854 }
NeilBrownf6556752006-06-26 00:28:01 -07001855 } else if (cmd_match(buf, "writemostly")) {
1856 set_bit(WriteMostly, &rdev->flags);
1857 err = 0;
1858 } else if (cmd_match(buf, "-writemostly")) {
1859 clear_bit(WriteMostly, &rdev->flags);
1860 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001861 }
1862 return err ? err : len;
1863}
NeilBrown80ca3a42006-07-10 04:44:18 -07001864static struct rdev_sysfs_entry rdev_state =
1865__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001866
1867static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08001868errors_show(mdk_rdev_t *rdev, char *page)
1869{
1870 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1871}
1872
1873static ssize_t
1874errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1875{
1876 char *e;
1877 unsigned long n = simple_strtoul(buf, &e, 10);
1878 if (*buf && (*e == 0 || *e == '\n')) {
1879 atomic_set(&rdev->corrected_errors, n);
1880 return len;
1881 }
1882 return -EINVAL;
1883}
1884static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001885__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001886
NeilBrown014236d2006-01-06 00:20:55 -08001887static ssize_t
1888slot_show(mdk_rdev_t *rdev, char *page)
1889{
1890 if (rdev->raid_disk < 0)
1891 return sprintf(page, "none\n");
1892 else
1893 return sprintf(page, "%d\n", rdev->raid_disk);
1894}
1895
1896static ssize_t
1897slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1898{
1899 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001900 int err;
1901 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001902 int slot = simple_strtoul(buf, &e, 10);
1903 if (strncmp(buf, "none", 4)==0)
1904 slot = -1;
1905 else if (e==buf || (*e && *e!= '\n'))
1906 return -EINVAL;
NeilBrownc303da62008-02-06 01:39:51 -08001907 if (rdev->mddev->pers) {
1908 /* Setting 'slot' on an active array requires also
1909 * updating the 'rd%d' link, and communicating
1910 * with the personality with ->hot_*_disk.
1911 * For now we only support removing
1912 * failed/spare devices. This normally happens automatically,
1913 * but not when the metadata is externally managed.
1914 */
1915 if (slot != -1)
1916 return -EBUSY;
1917 if (rdev->raid_disk == -1)
1918 return -EEXIST;
1919 /* personality does all needed checks */
1920 if (rdev->mddev->pers->hot_add_disk == NULL)
1921 return -EINVAL;
1922 err = rdev->mddev->pers->
1923 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1924 if (err)
1925 return err;
1926 sprintf(nm, "rd%d", rdev->raid_disk);
1927 sysfs_remove_link(&rdev->mddev->kobj, nm);
1928 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1929 md_wakeup_thread(rdev->mddev->thread);
1930 } else {
1931 if (slot >= rdev->mddev->raid_disks)
1932 return -ENOSPC;
1933 rdev->raid_disk = slot;
1934 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001935 clear_bit(Faulty, &rdev->flags);
1936 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08001937 set_bit(In_sync, &rdev->flags);
1938 }
NeilBrown014236d2006-01-06 00:20:55 -08001939 return len;
1940}
1941
1942
1943static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001944__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001945
NeilBrown93c8cad2006-01-06 00:20:56 -08001946static ssize_t
1947offset_show(mdk_rdev_t *rdev, char *page)
1948{
Andrew Morton6961ece2006-01-06 00:20:59 -08001949 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001950}
1951
1952static ssize_t
1953offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1954{
1955 char *e;
1956 unsigned long long offset = simple_strtoull(buf, &e, 10);
1957 if (e==buf || (*e && *e != '\n'))
1958 return -EINVAL;
1959 if (rdev->mddev->pers)
1960 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001961 if (rdev->size && rdev->mddev->external)
1962 /* Must set offset before size, so overlap checks
1963 * can be sane */
1964 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08001965 rdev->data_offset = offset;
1966 return len;
1967}
1968
1969static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001970__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001971
NeilBrown83303b62006-01-06 00:21:06 -08001972static ssize_t
1973rdev_size_show(mdk_rdev_t *rdev, char *page)
1974{
1975 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1976}
1977
NeilBrownc5d79ad2008-02-06 01:39:54 -08001978static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
1979{
1980 /* check if two start/length pairs overlap */
1981 if (s1+l1 <= s2)
1982 return 0;
1983 if (s2+l2 <= s1)
1984 return 0;
1985 return 1;
1986}
1987
NeilBrown83303b62006-01-06 00:21:06 -08001988static ssize_t
1989rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1990{
1991 char *e;
1992 unsigned long long size = simple_strtoull(buf, &e, 10);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001993 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08001994 mddev_t *my_mddev = rdev->mddev;
1995
NeilBrown83303b62006-01-06 00:21:06 -08001996 if (e==buf || (*e && *e != '\n'))
1997 return -EINVAL;
NeilBrown27c529b2008-03-04 14:29:33 -08001998 if (my_mddev->pers)
NeilBrown83303b62006-01-06 00:21:06 -08001999 return -EBUSY;
2000 rdev->size = size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002001 if (size > oldsize && rdev->mddev->external) {
2002 /* need to check that all other rdevs with the same ->bdev
2003 * do not overlap. We need to unlock the mddev to avoid
2004 * a deadlock. We have already changed rdev->size, and if
2005 * we have to change it back, we will have the lock again.
2006 */
2007 mddev_t *mddev;
2008 int overlap = 0;
2009 struct list_head *tmp, *tmp2;
2010
NeilBrown27c529b2008-03-04 14:29:33 -08002011 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002012 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002013 mdk_rdev_t *rdev2;
2014
2015 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002016 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002017 if (test_bit(AllReserved, &rdev2->flags) ||
2018 (rdev->bdev == rdev2->bdev &&
2019 rdev != rdev2 &&
2020 overlaps(rdev->data_offset, rdev->size,
2021 rdev2->data_offset, rdev2->size))) {
2022 overlap = 1;
2023 break;
2024 }
2025 mddev_unlock(mddev);
2026 if (overlap) {
2027 mddev_put(mddev);
2028 break;
2029 }
2030 }
NeilBrown27c529b2008-03-04 14:29:33 -08002031 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002032 if (overlap) {
2033 /* Someone else could have slipped in a size
2034 * change here, but doing so is just silly.
2035 * We put oldsize back because we *know* it is
2036 * safe, and trust userspace not to race with
2037 * itself
2038 */
2039 rdev->size = oldsize;
2040 return -EBUSY;
2041 }
2042 }
NeilBrown27c529b2008-03-04 14:29:33 -08002043 if (size < my_mddev->size || my_mddev->size == 0)
2044 my_mddev->size = size;
NeilBrown83303b62006-01-06 00:21:06 -08002045 return len;
2046}
2047
2048static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002049__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002050
NeilBrown86e6ffd2005-11-08 21:39:24 -08002051static struct attribute *rdev_default_attrs[] = {
2052 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002053 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002054 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002055 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002056 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002057 NULL,
2058};
2059static ssize_t
2060rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2061{
2062 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2063 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002064 mddev_t *mddev = rdev->mddev;
2065 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002066
2067 if (!entry->show)
2068 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002069
2070 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2071 if (!rv) {
2072 if (rdev->mddev == NULL)
2073 rv = -EBUSY;
2074 else
2075 rv = entry->show(rdev, page);
2076 mddev_unlock(mddev);
2077 }
2078 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002079}
2080
2081static ssize_t
2082rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2083 const char *page, size_t length)
2084{
2085 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2086 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002087 ssize_t rv;
2088 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002089
2090 if (!entry->store)
2091 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002092 if (!capable(CAP_SYS_ADMIN))
2093 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002094 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002095 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002096 if (rdev->mddev == NULL)
2097 rv = -EBUSY;
2098 else
2099 rv = entry->store(rdev, page, length);
NeilBrownca388052008-02-06 01:39:55 -08002100 mddev_unlock(rdev->mddev);
2101 }
2102 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002103}
2104
2105static void rdev_free(struct kobject *ko)
2106{
2107 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2108 kfree(rdev);
2109}
2110static struct sysfs_ops rdev_sysfs_ops = {
2111 .show = rdev_attr_show,
2112 .store = rdev_attr_store,
2113};
2114static struct kobj_type rdev_ktype = {
2115 .release = rdev_free,
2116 .sysfs_ops = &rdev_sysfs_ops,
2117 .default_attrs = rdev_default_attrs,
2118};
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120/*
2121 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2122 *
2123 * mark the device faulty if:
2124 *
2125 * - the device is nonexistent (zero size)
2126 * - the device has no valid superblock
2127 *
2128 * a faulty rdev _never_ has rdev->sb set.
2129 */
2130static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2131{
2132 char b[BDEVNAME_SIZE];
2133 int err;
2134 mdk_rdev_t *rdev;
2135 sector_t size;
2136
NeilBrown9ffae0c2006-01-06 00:20:32 -08002137 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (!rdev) {
2139 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2140 return ERR_PTR(-ENOMEM);
2141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 if ((err = alloc_disk_sb(rdev)))
2144 goto abort_free;
2145
NeilBrownc5d79ad2008-02-06 01:39:54 -08002146 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (err)
2148 goto abort_free;
2149
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002150 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002153 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002154 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002155 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002157 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002159 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002160 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2163 if (!size) {
2164 printk(KERN_WARNING
2165 "md: %s has zero or unknown size, marking faulty!\n",
2166 bdevname(rdev->bdev,b));
2167 err = -EINVAL;
2168 goto abort_free;
2169 }
2170
2171 if (super_format >= 0) {
2172 err = super_types[super_format].
2173 load_super(rdev, NULL, super_minor);
2174 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002175 printk(KERN_WARNING
2176 "md: %s does not have a valid v%d.%d "
2177 "superblock, not importing!\n",
2178 bdevname(rdev->bdev,b),
2179 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 goto abort_free;
2181 }
2182 if (err < 0) {
2183 printk(KERN_WARNING
2184 "md: could not read %s's sb, not importing!\n",
2185 bdevname(rdev->bdev,b));
2186 goto abort_free;
2187 }
2188 }
2189 INIT_LIST_HEAD(&rdev->same_set);
2190
2191 return rdev;
2192
2193abort_free:
2194 if (rdev->sb_page) {
2195 if (rdev->bdev)
2196 unlock_rdev(rdev);
2197 free_disk_sb(rdev);
2198 }
2199 kfree(rdev);
2200 return ERR_PTR(err);
2201}
2202
2203/*
2204 * Check a full RAID array for plausibility
2205 */
2206
2207
NeilBrowna757e642005-04-16 15:26:42 -07002208static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 int i;
2211 struct list_head *tmp;
2212 mdk_rdev_t *rdev, *freshest;
2213 char b[BDEVNAME_SIZE];
2214
2215 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002216 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 switch (super_types[mddev->major_version].
2218 load_super(rdev, freshest, mddev->minor_version)) {
2219 case 1:
2220 freshest = rdev;
2221 break;
2222 case 0:
2223 break;
2224 default:
2225 printk( KERN_ERR \
2226 "md: fatal superblock inconsistency in %s"
2227 " -- removing from array\n",
2228 bdevname(rdev->bdev,b));
2229 kick_rdev_from_array(rdev);
2230 }
2231
2232
2233 super_types[mddev->major_version].
2234 validate_super(mddev, freshest);
2235
2236 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002237 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (rdev != freshest)
2239 if (super_types[mddev->major_version].
2240 validate_super(mddev, rdev)) {
2241 printk(KERN_WARNING "md: kicking non-fresh %s"
2242 " from array!\n",
2243 bdevname(rdev->bdev,b));
2244 kick_rdev_from_array(rdev);
2245 continue;
2246 }
2247 if (mddev->level == LEVEL_MULTIPATH) {
2248 rdev->desc_nr = i++;
2249 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002250 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002251 } else if (rdev->raid_disk >= mddev->raid_disks) {
2252 rdev->raid_disk = -1;
2253 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255 }
2256
2257
2258
2259 if (mddev->recovery_cp != MaxSector &&
2260 mddev->level >= 1)
2261 printk(KERN_ERR "md: %s: raid array is not clean"
2262 " -- starting background reconstruction\n",
2263 mdname(mddev));
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
NeilBrowneae17012005-11-08 21:39:23 -08002267static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002268safe_delay_show(mddev_t *mddev, char *page)
2269{
2270 int msec = (mddev->safemode_delay*1000)/HZ;
2271 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2272}
2273static ssize_t
2274safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2275{
2276 int scale=1;
2277 int dot=0;
2278 int i;
2279 unsigned long msec;
2280 char buf[30];
2281 char *e;
2282 /* remove a period, and count digits after it */
2283 if (len >= sizeof(buf))
2284 return -EINVAL;
2285 strlcpy(buf, cbuf, len);
2286 buf[len] = 0;
2287 for (i=0; i<len; i++) {
2288 if (dot) {
2289 if (isdigit(buf[i])) {
2290 buf[i-1] = buf[i];
2291 scale *= 10;
2292 }
2293 buf[i] = 0;
2294 } else if (buf[i] == '.') {
2295 dot=1;
2296 buf[i] = 0;
2297 }
2298 }
2299 msec = simple_strtoul(buf, &e, 10);
2300 if (e == buf || (*e && *e != '\n'))
2301 return -EINVAL;
2302 msec = (msec * 1000) / scale;
2303 if (msec == 0)
2304 mddev->safemode_delay = 0;
2305 else {
2306 mddev->safemode_delay = (msec*HZ)/1000;
2307 if (mddev->safemode_delay == 0)
2308 mddev->safemode_delay = 1;
2309 }
2310 return len;
2311}
2312static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002313__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002314
2315static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002316level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002317{
NeilBrown2604b702006-01-06 00:20:36 -08002318 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002319 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002320 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002321 else if (mddev->clevel[0])
2322 return sprintf(page, "%s\n", mddev->clevel);
2323 else if (mddev->level != LEVEL_NONE)
2324 return sprintf(page, "%d\n", mddev->level);
2325 else
2326 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002327}
2328
NeilBrownd9d166c2006-01-06 00:20:51 -08002329static ssize_t
2330level_store(mddev_t *mddev, const char *buf, size_t len)
2331{
NeilBrown20a49ff2008-02-06 01:39:57 -08002332 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002333 if (mddev->pers)
2334 return -EBUSY;
2335 if (len == 0)
2336 return 0;
2337 if (len >= sizeof(mddev->clevel))
2338 return -ENOSPC;
2339 strncpy(mddev->clevel, buf, len);
2340 if (mddev->clevel[len-1] == '\n')
2341 len--;
2342 mddev->clevel[len] = 0;
2343 mddev->level = LEVEL_NONE;
2344 return rv;
2345}
2346
2347static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002348__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002349
NeilBrownd4dbd022006-06-26 00:27:59 -07002350
2351static ssize_t
2352layout_show(mddev_t *mddev, char *page)
2353{
2354 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002355 if (mddev->reshape_position != MaxSector &&
2356 mddev->layout != mddev->new_layout)
2357 return sprintf(page, "%d (%d)\n",
2358 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002359 return sprintf(page, "%d\n", mddev->layout);
2360}
2361
2362static ssize_t
2363layout_store(mddev_t *mddev, const char *buf, size_t len)
2364{
2365 char *e;
2366 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002367
2368 if (!*buf || (*e && *e != '\n'))
2369 return -EINVAL;
2370
NeilBrown08a02ec2007-05-09 02:35:38 -07002371 if (mddev->pers)
2372 return -EBUSY;
2373 if (mddev->reshape_position != MaxSector)
2374 mddev->new_layout = n;
2375 else
2376 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002377 return len;
2378}
2379static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002380__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002381
2382
NeilBrowneae17012005-11-08 21:39:23 -08002383static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002384raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002385{
NeilBrownbb636542005-11-08 21:39:45 -08002386 if (mddev->raid_disks == 0)
2387 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002388 if (mddev->reshape_position != MaxSector &&
2389 mddev->delta_disks != 0)
2390 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2391 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002392 return sprintf(page, "%d\n", mddev->raid_disks);
2393}
2394
NeilBrownda943b992006-01-06 00:20:54 -08002395static int update_raid_disks(mddev_t *mddev, int raid_disks);
2396
2397static ssize_t
2398raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2399{
NeilBrownda943b992006-01-06 00:20:54 -08002400 char *e;
2401 int rv = 0;
2402 unsigned long n = simple_strtoul(buf, &e, 10);
2403
2404 if (!*buf || (*e && *e != '\n'))
2405 return -EINVAL;
2406
2407 if (mddev->pers)
2408 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002409 else if (mddev->reshape_position != MaxSector) {
2410 int olddisks = mddev->raid_disks - mddev->delta_disks;
2411 mddev->delta_disks = n - olddisks;
2412 mddev->raid_disks = n;
2413 } else
NeilBrownda943b992006-01-06 00:20:54 -08002414 mddev->raid_disks = n;
2415 return rv ? rv : len;
2416}
2417static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002418__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002419
NeilBrown24dd4692005-11-08 21:39:26 -08002420static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002421chunk_size_show(mddev_t *mddev, char *page)
2422{
NeilBrown08a02ec2007-05-09 02:35:38 -07002423 if (mddev->reshape_position != MaxSector &&
2424 mddev->chunk_size != mddev->new_chunk)
2425 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2426 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002427 return sprintf(page, "%d\n", mddev->chunk_size);
2428}
2429
2430static ssize_t
2431chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2432{
2433 /* can only set chunk_size if array is not yet active */
2434 char *e;
2435 unsigned long n = simple_strtoul(buf, &e, 10);
2436
NeilBrown3b343802006-01-06 00:20:47 -08002437 if (!*buf || (*e && *e != '\n'))
2438 return -EINVAL;
2439
NeilBrown08a02ec2007-05-09 02:35:38 -07002440 if (mddev->pers)
2441 return -EBUSY;
2442 else if (mddev->reshape_position != MaxSector)
2443 mddev->new_chunk = n;
2444 else
2445 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002446 return len;
2447}
2448static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002449__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002450
NeilBrowna94213b2006-06-26 00:28:00 -07002451static ssize_t
2452resync_start_show(mddev_t *mddev, char *page)
2453{
2454 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2455}
2456
2457static ssize_t
2458resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2459{
2460 /* can only set chunk_size if array is not yet active */
2461 char *e;
2462 unsigned long long n = simple_strtoull(buf, &e, 10);
2463
2464 if (mddev->pers)
2465 return -EBUSY;
2466 if (!*buf || (*e && *e != '\n'))
2467 return -EINVAL;
2468
2469 mddev->recovery_cp = n;
2470 return len;
2471}
2472static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002473__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002474
NeilBrown9e653b62006-06-26 00:27:58 -07002475/*
2476 * The array state can be:
2477 *
2478 * clear
2479 * No devices, no size, no level
2480 * Equivalent to STOP_ARRAY ioctl
2481 * inactive
2482 * May have some settings, but array is not active
2483 * all IO results in error
2484 * When written, doesn't tear down array, but just stops it
2485 * suspended (not supported yet)
2486 * All IO requests will block. The array can be reconfigured.
2487 * Writing this, if accepted, will block until array is quiessent
2488 * readonly
2489 * no resync can happen. no superblocks get written.
2490 * write requests fail
2491 * read-auto
2492 * like readonly, but behaves like 'clean' on a write request.
2493 *
2494 * clean - no pending writes, but otherwise active.
2495 * When written to inactive array, starts without resync
2496 * If a write request arrives then
2497 * if metadata is known, mark 'dirty' and switch to 'active'.
2498 * if not known, block and switch to write-pending
2499 * If written to an active array that has pending writes, then fails.
2500 * active
2501 * fully active: IO and resync can be happening.
2502 * When written to inactive array, starts with resync
2503 *
2504 * write-pending
2505 * clean, but writes are blocked waiting for 'active' to be written.
2506 *
2507 * active-idle
2508 * like active, but no writes have been seen for a while (100msec).
2509 *
2510 */
2511enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2512 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002513static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002514 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2515 "write-pending", "active-idle", NULL };
2516
2517static int match_word(const char *word, char **list)
2518{
2519 int n;
2520 for (n=0; list[n]; n++)
2521 if (cmd_match(word, list[n]))
2522 break;
2523 return n;
2524}
2525
2526static ssize_t
2527array_state_show(mddev_t *mddev, char *page)
2528{
2529 enum array_state st = inactive;
2530
2531 if (mddev->pers)
2532 switch(mddev->ro) {
2533 case 1:
2534 st = readonly;
2535 break;
2536 case 2:
2537 st = read_auto;
2538 break;
2539 case 0:
2540 if (mddev->in_sync)
2541 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002542 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2543 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002544 else if (mddev->safemode)
2545 st = active_idle;
2546 else
2547 st = active;
2548 }
2549 else {
2550 if (list_empty(&mddev->disks) &&
2551 mddev->raid_disks == 0 &&
2552 mddev->size == 0)
2553 st = clear;
2554 else
2555 st = inactive;
2556 }
2557 return sprintf(page, "%s\n", array_states[st]);
2558}
2559
2560static int do_md_stop(mddev_t * mddev, int ro);
2561static int do_md_run(mddev_t * mddev);
2562static int restart_array(mddev_t *mddev);
2563
2564static ssize_t
2565array_state_store(mddev_t *mddev, const char *buf, size_t len)
2566{
2567 int err = -EINVAL;
2568 enum array_state st = match_word(buf, array_states);
2569 switch(st) {
2570 case bad_word:
2571 break;
2572 case clear:
2573 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002574 if (atomic_read(&mddev->active) > 1)
2575 return -EBUSY;
2576 err = do_md_stop(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002577 break;
2578 case inactive:
2579 /* stopping an active array */
2580 if (mddev->pers) {
2581 if (atomic_read(&mddev->active) > 1)
2582 return -EBUSY;
2583 err = do_md_stop(mddev, 2);
NeilBrowne6910632008-02-06 01:39:51 -08002584 } else
2585 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002586 break;
2587 case suspended:
2588 break; /* not supported yet */
2589 case readonly:
2590 if (mddev->pers)
2591 err = do_md_stop(mddev, 1);
2592 else {
2593 mddev->ro = 1;
2594 err = do_md_run(mddev);
2595 }
2596 break;
2597 case read_auto:
2598 /* stopping an active array */
2599 if (mddev->pers) {
2600 err = do_md_stop(mddev, 1);
2601 if (err == 0)
2602 mddev->ro = 2; /* FIXME mark devices writable */
2603 } else {
2604 mddev->ro = 2;
2605 err = do_md_run(mddev);
2606 }
2607 break;
2608 case clean:
2609 if (mddev->pers) {
2610 restart_array(mddev);
2611 spin_lock_irq(&mddev->write_lock);
2612 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002613 if (mddev->in_sync == 0) {
2614 mddev->in_sync = 1;
2615 if (mddev->persistent)
2616 set_bit(MD_CHANGE_CLEAN,
2617 &mddev->flags);
2618 }
2619 err = 0;
2620 } else
2621 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002622 spin_unlock_irq(&mddev->write_lock);
2623 } else {
2624 mddev->ro = 0;
2625 mddev->recovery_cp = MaxSector;
2626 err = do_md_run(mddev);
2627 }
2628 break;
2629 case active:
2630 if (mddev->pers) {
2631 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002632 if (mddev->external)
2633 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002634 wake_up(&mddev->sb_wait);
2635 err = 0;
2636 } else {
2637 mddev->ro = 0;
2638 err = do_md_run(mddev);
2639 }
2640 break;
2641 case write_pending:
2642 case active_idle:
2643 /* these cannot be set */
2644 break;
2645 }
2646 if (err)
2647 return err;
2648 else
2649 return len;
2650}
NeilBrown80ca3a42006-07-10 04:44:18 -07002651static struct md_sysfs_entry md_array_state =
2652__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002653
NeilBrown6d7ff7382006-01-06 00:21:16 -08002654static ssize_t
2655null_show(mddev_t *mddev, char *page)
2656{
2657 return -EINVAL;
2658}
2659
2660static ssize_t
2661new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2662{
2663 /* buf must be %d:%d\n? giving major and minor numbers */
2664 /* The new device is added to the array.
2665 * If the array has a persistent superblock, we read the
2666 * superblock to initialise info and check validity.
2667 * Otherwise, only checking done is that in bind_rdev_to_array,
2668 * which mainly checks size.
2669 */
2670 char *e;
2671 int major = simple_strtoul(buf, &e, 10);
2672 int minor;
2673 dev_t dev;
2674 mdk_rdev_t *rdev;
2675 int err;
2676
2677 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2678 return -EINVAL;
2679 minor = simple_strtoul(e+1, &e, 10);
2680 if (*e && *e != '\n')
2681 return -EINVAL;
2682 dev = MKDEV(major, minor);
2683 if (major != MAJOR(dev) ||
2684 minor != MINOR(dev))
2685 return -EOVERFLOW;
2686
2687
2688 if (mddev->persistent) {
2689 rdev = md_import_device(dev, mddev->major_version,
2690 mddev->minor_version);
2691 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2692 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2693 mdk_rdev_t, same_set);
2694 err = super_types[mddev->major_version]
2695 .load_super(rdev, rdev0, mddev->minor_version);
2696 if (err < 0)
2697 goto out;
2698 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002699 } else if (mddev->external)
2700 rdev = md_import_device(dev, -2, -1);
2701 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08002702 rdev = md_import_device(dev, -1, -1);
2703
2704 if (IS_ERR(rdev))
2705 return PTR_ERR(rdev);
2706 err = bind_rdev_to_array(rdev, mddev);
2707 out:
2708 if (err)
2709 export_rdev(rdev);
2710 return err ? err : len;
2711}
2712
2713static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002714__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002715
2716static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002717bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2718{
2719 char *end;
2720 unsigned long chunk, end_chunk;
2721
2722 if (!mddev->bitmap)
2723 goto out;
2724 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2725 while (*buf) {
2726 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2727 if (buf == end) break;
2728 if (*end == '-') { /* range */
2729 buf = end + 1;
2730 end_chunk = simple_strtoul(buf, &end, 0);
2731 if (buf == end) break;
2732 }
2733 if (*end && !isspace(*end)) break;
2734 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2735 buf = end;
2736 while (isspace(*buf)) buf++;
2737 }
2738 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2739out:
2740 return len;
2741}
2742
2743static struct md_sysfs_entry md_bitmap =
2744__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2745
2746static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002747size_show(mddev_t *mddev, char *page)
2748{
2749 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2750}
2751
2752static int update_size(mddev_t *mddev, unsigned long size);
2753
2754static ssize_t
2755size_store(mddev_t *mddev, const char *buf, size_t len)
2756{
2757 /* If array is inactive, we can reduce the component size, but
2758 * not increase it (except from 0).
2759 * If array is active, we can try an on-line resize
2760 */
2761 char *e;
2762 int err = 0;
2763 unsigned long long size = simple_strtoull(buf, &e, 10);
2764 if (!*buf || *buf == '\n' ||
2765 (*e && *e != '\n'))
2766 return -EINVAL;
2767
2768 if (mddev->pers) {
2769 err = update_size(mddev, size);
NeilBrown850b2b422006-10-03 01:15:46 -07002770 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002771 } else {
2772 if (mddev->size == 0 ||
2773 mddev->size > size)
2774 mddev->size = size;
2775 else
2776 err = -ENOSPC;
2777 }
2778 return err ? err : len;
2779}
2780
2781static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002782__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002783
NeilBrown8bb93aa2006-01-06 00:20:50 -08002784
2785/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002786 * This is one of
2787 * 'none' for arrays with no metadata (good luck...)
2788 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002789 * or N.M for internally known formats
2790 */
2791static ssize_t
2792metadata_show(mddev_t *mddev, char *page)
2793{
2794 if (mddev->persistent)
2795 return sprintf(page, "%d.%d\n",
2796 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002797 else if (mddev->external)
2798 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002799 else
2800 return sprintf(page, "none\n");
2801}
2802
2803static ssize_t
2804metadata_store(mddev_t *mddev, const char *buf, size_t len)
2805{
2806 int major, minor;
2807 char *e;
2808 if (!list_empty(&mddev->disks))
2809 return -EBUSY;
2810
2811 if (cmd_match(buf, "none")) {
2812 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002813 mddev->external = 0;
2814 mddev->major_version = 0;
2815 mddev->minor_version = 90;
2816 return len;
2817 }
2818 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002819 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002820 if (namelen >= sizeof(mddev->metadata_type))
2821 namelen = sizeof(mddev->metadata_type)-1;
2822 strncpy(mddev->metadata_type, buf+9, namelen);
2823 mddev->metadata_type[namelen] = 0;
2824 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2825 mddev->metadata_type[--namelen] = 0;
2826 mddev->persistent = 0;
2827 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002828 mddev->major_version = 0;
2829 mddev->minor_version = 90;
2830 return len;
2831 }
2832 major = simple_strtoul(buf, &e, 10);
2833 if (e==buf || *e != '.')
2834 return -EINVAL;
2835 buf = e+1;
2836 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002837 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002838 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002839 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002840 return -ENOENT;
2841 mddev->major_version = major;
2842 mddev->minor_version = minor;
2843 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002844 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002845 return len;
2846}
2847
2848static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002849__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002850
NeilBrowna35b0d62006-01-06 00:20:49 -08002851static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002852action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002853{
NeilBrown7eec3142005-11-08 21:39:44 -08002854 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002855 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002856 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002857 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2858 type = "reshape";
2859 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002860 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2861 type = "resync";
2862 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2863 type = "check";
2864 else
2865 type = "repair";
2866 } else
2867 type = "recover";
2868 }
2869 return sprintf(page, "%s\n", type);
2870}
2871
2872static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002873action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002874{
NeilBrown7eec3142005-11-08 21:39:44 -08002875 if (!mddev->pers || !mddev->pers->sync_request)
2876 return -EINVAL;
2877
NeilBrownbce74da2006-01-06 00:20:41 -08002878 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002879 if (mddev->sync_thread) {
2880 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2881 md_unregister_thread(mddev->sync_thread);
2882 mddev->sync_thread = NULL;
2883 mddev->recovery = 0;
2884 }
NeilBrown03c902e2006-01-06 00:20:46 -08002885 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2886 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002887 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002888 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002889 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002890 else if (cmd_match(page, "reshape")) {
2891 int err;
2892 if (mddev->pers->start_reshape == NULL)
2893 return -EINVAL;
2894 err = mddev->pers->start_reshape(mddev);
2895 if (err)
2896 return err;
2897 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002898 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002899 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002900 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002901 return -EINVAL;
2902 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2903 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002904 }
NeilBrown03c902e2006-01-06 00:20:46 -08002905 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002906 md_wakeup_thread(mddev->thread);
2907 return len;
2908}
2909
NeilBrown9d888832005-11-08 21:39:26 -08002910static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002911mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002912{
2913 return sprintf(page, "%llu\n",
2914 (unsigned long long) mddev->resync_mismatches);
2915}
2916
NeilBrown80ca3a42006-07-10 04:44:18 -07002917static struct md_sysfs_entry md_scan_mode =
2918__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002919
NeilBrown96de1e62005-11-08 21:39:39 -08002920
NeilBrown80ca3a42006-07-10 04:44:18 -07002921static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002922
NeilBrown88202a02006-01-06 00:21:36 -08002923static ssize_t
2924sync_min_show(mddev_t *mddev, char *page)
2925{
2926 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2927 mddev->sync_speed_min ? "local": "system");
2928}
2929
2930static ssize_t
2931sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2932{
2933 int min;
2934 char *e;
2935 if (strncmp(buf, "system", 6)==0) {
2936 mddev->sync_speed_min = 0;
2937 return len;
2938 }
2939 min = simple_strtoul(buf, &e, 10);
2940 if (buf == e || (*e && *e != '\n') || min <= 0)
2941 return -EINVAL;
2942 mddev->sync_speed_min = min;
2943 return len;
2944}
2945
2946static struct md_sysfs_entry md_sync_min =
2947__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2948
2949static ssize_t
2950sync_max_show(mddev_t *mddev, char *page)
2951{
2952 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2953 mddev->sync_speed_max ? "local": "system");
2954}
2955
2956static ssize_t
2957sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2958{
2959 int max;
2960 char *e;
2961 if (strncmp(buf, "system", 6)==0) {
2962 mddev->sync_speed_max = 0;
2963 return len;
2964 }
2965 max = simple_strtoul(buf, &e, 10);
2966 if (buf == e || (*e && *e != '\n') || max <= 0)
2967 return -EINVAL;
2968 mddev->sync_speed_max = max;
2969 return len;
2970}
2971
2972static struct md_sysfs_entry md_sync_max =
2973__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2974
Iustin Popd7f3d292007-10-16 23:30:54 -07002975static ssize_t
2976degraded_show(mddev_t *mddev, char *page)
2977{
2978 return sprintf(page, "%d\n", mddev->degraded);
2979}
2980static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08002981
2982static ssize_t
2983sync_speed_show(mddev_t *mddev, char *page)
2984{
2985 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07002986 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08002987 dt = ((jiffies - mddev->resync_mark) / HZ);
2988 if (!dt) dt++;
2989 db = resync - (mddev->resync_mark_cnt);
2990 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2991}
2992
NeilBrown80ca3a42006-07-10 04:44:18 -07002993static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08002994
2995static ssize_t
2996sync_completed_show(mddev_t *mddev, char *page)
2997{
2998 unsigned long max_blocks, resync;
2999
3000 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3001 max_blocks = mddev->resync_max_sectors;
3002 else
3003 max_blocks = mddev->size << 1;
3004
3005 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3006 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3007}
3008
NeilBrown80ca3a42006-07-10 04:44:18 -07003009static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003010
NeilBrowne464eaf2006-03-27 01:18:14 -08003011static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003012max_sync_show(mddev_t *mddev, char *page)
3013{
3014 if (mddev->resync_max == MaxSector)
3015 return sprintf(page, "max\n");
3016 else
3017 return sprintf(page, "%llu\n",
3018 (unsigned long long)mddev->resync_max);
3019}
3020static ssize_t
3021max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3022{
3023 if (strncmp(buf, "max", 3) == 0)
3024 mddev->resync_max = MaxSector;
3025 else {
3026 char *ep;
3027 unsigned long long max = simple_strtoull(buf, &ep, 10);
3028 if (ep == buf || (*ep != 0 && *ep != '\n'))
3029 return -EINVAL;
3030 if (max < mddev->resync_max &&
3031 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3032 return -EBUSY;
3033
3034 /* Must be a multiple of chunk_size */
3035 if (mddev->chunk_size) {
3036 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3037 return -EINVAL;
3038 }
3039 mddev->resync_max = max;
3040 }
3041 wake_up(&mddev->recovery_wait);
3042 return len;
3043}
3044
3045static struct md_sysfs_entry md_max_sync =
3046__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3047
3048static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003049suspend_lo_show(mddev_t *mddev, char *page)
3050{
3051 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3052}
3053
3054static ssize_t
3055suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3056{
3057 char *e;
3058 unsigned long long new = simple_strtoull(buf, &e, 10);
3059
3060 if (mddev->pers->quiesce == NULL)
3061 return -EINVAL;
3062 if (buf == e || (*e && *e != '\n'))
3063 return -EINVAL;
3064 if (new >= mddev->suspend_hi ||
3065 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3066 mddev->suspend_lo = new;
3067 mddev->pers->quiesce(mddev, 2);
3068 return len;
3069 } else
3070 return -EINVAL;
3071}
3072static struct md_sysfs_entry md_suspend_lo =
3073__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3074
3075
3076static ssize_t
3077suspend_hi_show(mddev_t *mddev, char *page)
3078{
3079 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3080}
3081
3082static ssize_t
3083suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3084{
3085 char *e;
3086 unsigned long long new = simple_strtoull(buf, &e, 10);
3087
3088 if (mddev->pers->quiesce == NULL)
3089 return -EINVAL;
3090 if (buf == e || (*e && *e != '\n'))
3091 return -EINVAL;
3092 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3093 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3094 mddev->suspend_hi = new;
3095 mddev->pers->quiesce(mddev, 1);
3096 mddev->pers->quiesce(mddev, 0);
3097 return len;
3098 } else
3099 return -EINVAL;
3100}
3101static struct md_sysfs_entry md_suspend_hi =
3102__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3103
NeilBrown08a02ec2007-05-09 02:35:38 -07003104static ssize_t
3105reshape_position_show(mddev_t *mddev, char *page)
3106{
3107 if (mddev->reshape_position != MaxSector)
3108 return sprintf(page, "%llu\n",
3109 (unsigned long long)mddev->reshape_position);
3110 strcpy(page, "none\n");
3111 return 5;
3112}
3113
3114static ssize_t
3115reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3116{
3117 char *e;
3118 unsigned long long new = simple_strtoull(buf, &e, 10);
3119 if (mddev->pers)
3120 return -EBUSY;
3121 if (buf == e || (*e && *e != '\n'))
3122 return -EINVAL;
3123 mddev->reshape_position = new;
3124 mddev->delta_disks = 0;
3125 mddev->new_level = mddev->level;
3126 mddev->new_layout = mddev->layout;
3127 mddev->new_chunk = mddev->chunk_size;
3128 return len;
3129}
3130
3131static struct md_sysfs_entry md_reshape_position =
3132__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3133 reshape_position_store);
3134
NeilBrowne464eaf2006-03-27 01:18:14 -08003135
NeilBrowneae17012005-11-08 21:39:23 -08003136static struct attribute *md_default_attrs[] = {
3137 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003138 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003139 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003140 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003141 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003142 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003143 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003144 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003145 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003146 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003147 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003148 NULL,
3149};
3150
3151static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003152 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003153 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003154 &md_sync_min.attr,
3155 &md_sync_max.attr,
3156 &md_sync_speed.attr,
3157 &md_sync_completed.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003158 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003159 &md_suspend_lo.attr,
3160 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003161 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003162 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003163 NULL,
3164};
NeilBrown411036f2005-11-08 21:39:40 -08003165static struct attribute_group md_redundancy_group = {
3166 .name = NULL,
3167 .attrs = md_redundancy_attrs,
3168};
3169
NeilBrowneae17012005-11-08 21:39:23 -08003170
3171static ssize_t
3172md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3173{
3174 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3175 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003176 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003177
3178 if (!entry->show)
3179 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003180 rv = mddev_lock(mddev);
3181 if (!rv) {
3182 rv = entry->show(mddev, page);
3183 mddev_unlock(mddev);
3184 }
NeilBrown96de1e62005-11-08 21:39:39 -08003185 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003186}
3187
3188static ssize_t
3189md_attr_store(struct kobject *kobj, struct attribute *attr,
3190 const char *page, size_t length)
3191{
3192 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3193 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003194 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003195
3196 if (!entry->store)
3197 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003198 if (!capable(CAP_SYS_ADMIN))
3199 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003200 rv = mddev_lock(mddev);
3201 if (!rv) {
3202 rv = entry->store(mddev, page, length);
3203 mddev_unlock(mddev);
3204 }
NeilBrown96de1e62005-11-08 21:39:39 -08003205 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003206}
3207
3208static void md_free(struct kobject *ko)
3209{
3210 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3211 kfree(mddev);
3212}
3213
3214static struct sysfs_ops md_sysfs_ops = {
3215 .show = md_attr_show,
3216 .store = md_attr_store,
3217};
3218static struct kobj_type md_ktype = {
3219 .release = md_free,
3220 .sysfs_ops = &md_sysfs_ops,
3221 .default_attrs = md_default_attrs,
3222};
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224int mdp_major = 0;
3225
3226static struct kobject *md_probe(dev_t dev, int *part, void *data)
3227{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003228 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 mddev_t *mddev = mddev_find(dev);
3230 struct gendisk *disk;
3231 int partitioned = (MAJOR(dev) != MD_MAJOR);
3232 int shift = partitioned ? MdpMinorShift : 0;
3233 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003234 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
3236 if (!mddev)
3237 return NULL;
3238
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003239 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003241 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 mddev_put(mddev);
3243 return NULL;
3244 }
3245 disk = alloc_disk(1 << shift);
3246 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003247 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 mddev_put(mddev);
3249 return NULL;
3250 }
3251 disk->major = MAJOR(dev);
3252 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07003253 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07003255 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 disk->fops = &md_fops;
3258 disk->private_data = mddev;
3259 disk->queue = mddev->queue;
3260 add_disk(disk);
3261 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003262 mutex_unlock(&disks_mutex);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003263 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003264 "%s", "md");
3265 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003266 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3267 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003268 else
3269 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return NULL;
3271}
3272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273static void md_safemode_timeout(unsigned long data)
3274{
3275 mddev_t *mddev = (mddev_t *) data;
3276
3277 mddev->safemode = 1;
3278 md_wakeup_thread(mddev->thread);
3279}
3280
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003281static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283static int do_md_run(mddev_t * mddev)
3284{
NeilBrown2604b702006-01-06 00:20:36 -08003285 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 int chunk_size;
3287 struct list_head *tmp;
3288 mdk_rdev_t *rdev;
3289 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003290 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 char b[BDEVNAME_SIZE];
3292
NeilBrowna757e642005-04-16 15:26:42 -07003293 if (list_empty(&mddev->disks))
3294 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
3297 if (mddev->pers)
3298 return -EBUSY;
3299
3300 /*
3301 * Analyze all RAID superblock(s)
3302 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003303 if (!mddev->raid_disks) {
3304 if (!mddev->persistent)
3305 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003306 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
NeilBrown2604b702006-01-06 00:20:36 -08003311 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 if (chunk_size > MAX_CHUNK_SIZE) {
3313 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3314 chunk_size, MAX_CHUNK_SIZE);
3315 return -EINVAL;
3316 }
3317 /*
3318 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3319 */
3320 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003321 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return -EINVAL;
3323 }
3324 if (chunk_size < PAGE_SIZE) {
3325 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3326 chunk_size, PAGE_SIZE);
3327 return -EINVAL;
3328 }
3329
3330 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003331 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003332 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 continue;
3334 if (rdev->size < chunk_size / 1024) {
3335 printk(KERN_WARNING
3336 "md: Dev %s smaller than chunk_size:"
3337 " %lluk < %dk\n",
3338 bdevname(rdev->bdev,b),
3339 (unsigned long long)rdev->size,
3340 chunk_size / 1024);
3341 return -EINVAL;
3342 }
3343 }
3344 }
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003347 if (mddev->level != LEVEL_NONE)
3348 request_module("md-level-%d", mddev->level);
3349 else if (mddev->clevel[0])
3350 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351#endif
3352
3353 /*
3354 * Drop all container device buffers, from now on
3355 * the only valid external interface is through the md
3356 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003358 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003359 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 continue;
3361 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003362 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003363
3364 /* perform some consistency tests on the device.
3365 * We don't want the data to overlap the metadata,
3366 * Internal Bitmap issues has handled elsewhere.
3367 */
3368 if (rdev->data_offset < rdev->sb_offset) {
3369 if (mddev->size &&
3370 rdev->data_offset + mddev->size*2
3371 > rdev->sb_offset*2) {
3372 printk("md: %s: data overlaps metadata\n",
3373 mdname(mddev));
3374 return -EINVAL;
3375 }
3376 } else {
3377 if (rdev->sb_offset*2 + rdev->sb_size/512
3378 > rdev->data_offset) {
3379 printk("md: %s: metadata overlaps data\n",
3380 mdname(mddev));
3381 return -EINVAL;
3382 }
3383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 }
3385
3386 md_probe(mddev->unit, NULL, NULL);
3387 disk = mddev->gendisk;
3388 if (!disk)
3389 return -ENOMEM;
3390
3391 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003392 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003393 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003395 if (mddev->level != LEVEL_NONE)
3396 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3397 mddev->level);
3398 else
3399 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3400 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 return -EINVAL;
3402 }
NeilBrown2604b702006-01-06 00:20:36 -08003403 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003405 mddev->level = pers->level;
3406 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
NeilBrownf6705572006-03-27 01:18:11 -08003408 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003409 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003410 /* This personality cannot handle reshaping... */
3411 mddev->pers = NULL;
3412 module_put(pers->owner);
3413 return -EINVAL;
3414 }
3415
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003416 if (pers->sync_request) {
3417 /* Warn if this is a potentially silly
3418 * configuration.
3419 */
3420 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3421 mdk_rdev_t *rdev2;
3422 struct list_head *tmp2;
3423 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003424 rdev_for_each(rdev, tmp, mddev) {
3425 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003426 if (rdev < rdev2 &&
3427 rdev->bdev->bd_contains ==
3428 rdev2->bdev->bd_contains) {
3429 printk(KERN_WARNING
3430 "%s: WARNING: %s appears to be"
3431 " on the same physical disk as"
3432 " %s.\n",
3433 mdname(mddev),
3434 bdevname(rdev->bdev,b),
3435 bdevname(rdev2->bdev,b2));
3436 warned = 1;
3437 }
3438 }
3439 }
3440 if (warned)
3441 printk(KERN_WARNING
3442 "True protection against single-disk"
3443 " failure might be compromised.\n");
3444 }
3445
NeilBrown657390d2005-08-26 18:34:16 -07003446 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003448 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003449 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
NeilBrownf91de922005-11-08 21:39:36 -08003451 if (start_readonly)
3452 mddev->ro = 2; /* read-only, but switch on first write */
3453
NeilBrownb15c2e52006-01-06 00:20:16 -08003454 err = mddev->pers->run(mddev);
3455 if (!err && mddev->pers->sync_request) {
3456 err = bitmap_create(mddev);
3457 if (err) {
3458 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3459 mdname(mddev), err);
3460 mddev->pers->stop(mddev);
3461 }
3462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 if (err) {
3464 printk(KERN_ERR "md: pers->run() failed ...\n");
3465 module_put(mddev->pers->owner);
3466 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003467 bitmap_destroy(mddev);
3468 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003470 if (mddev->pers->sync_request) {
3471 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3472 printk(KERN_WARNING
3473 "md: cannot register extra attributes for %s\n",
3474 mdname(mddev));
3475 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003476 mddev->ro = 0;
3477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 atomic_set(&mddev->writes_pending,0);
3479 mddev->safemode = 0;
3480 mddev->safemode_timer.function = md_safemode_timeout;
3481 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003482 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003484
NeilBrownd089c6a2008-02-06 01:39:59 -08003485 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003486 if (rdev->raid_disk >= 0) {
3487 char nm[20];
3488 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003489 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3490 printk("md: cannot register %s for %s\n",
3491 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
3494 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3495
NeilBrown850b2b422006-10-03 01:15:46 -07003496 if (mddev->flags)
3497 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
3499 set_capacity(disk, mddev->array_size<<1);
3500
3501 /* If we call blk_queue_make_request here, it will
3502 * re-initialise max_sectors etc which may have been
3503 * refined inside -> run. So just set the bits we need to set.
3504 * Most initialisation happended when we called
3505 * blk_queue_make_request(..., md_fail_request)
3506 * earlier.
3507 */
3508 mddev->queue->queuedata = mddev;
3509 mddev->queue->make_request_fn = mddev->pers->make_request;
3510
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003511 /* If there is a partially-recovered drive we need to
3512 * start recovery here. If we leave it to md_check_recovery,
3513 * it will remove the drives and not do the right thing
3514 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003515 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003516 struct list_head *rtmp;
3517 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003518 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003519 if (rdev->raid_disk >= 0 &&
3520 !test_bit(In_sync, &rdev->flags) &&
3521 !test_bit(Faulty, &rdev->flags))
3522 /* complete an interrupted recovery */
3523 spares++;
3524 if (spares && mddev->pers->sync_request) {
3525 mddev->recovery = 0;
3526 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3527 mddev->sync_thread = md_register_thread(md_do_sync,
3528 mddev,
3529 "%s_resync");
3530 if (!mddev->sync_thread) {
3531 printk(KERN_ERR "%s: could not start resync"
3532 " thread...\n",
3533 mdname(mddev));
3534 /* leave the spares where they are, it shouldn't hurt */
3535 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003536 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003537 }
3538 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003539 md_wakeup_thread(mddev->thread);
3540 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003541
Linus Torvalds44ce62942007-05-09 18:51:36 -07003542 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003543 md_new_event(mddev);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003544 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 return 0;
3546}
3547
3548static int restart_array(mddev_t *mddev)
3549{
3550 struct gendisk *disk = mddev->gendisk;
3551 int err;
3552
3553 /*
3554 * Complain if it has no devices
3555 */
3556 err = -ENXIO;
3557 if (list_empty(&mddev->disks))
3558 goto out;
3559
3560 if (mddev->pers) {
3561 err = -EBUSY;
3562 if (!mddev->ro)
3563 goto out;
3564
3565 mddev->safemode = 0;
3566 mddev->ro = 0;
3567 set_disk_ro(disk, 0);
3568
3569 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3570 mdname(mddev));
3571 /*
3572 * Kick recovery or resync if necessary
3573 */
3574 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3575 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003576 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003578 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
3581out:
3582 return err;
3583}
3584
NeilBrownacc55e22006-06-26 00:27:47 -07003585/* similar to deny_write_access, but accounts for our holding a reference
3586 * to the file ourselves */
3587static int deny_bitmap_write_access(struct file * file)
3588{
3589 struct inode *inode = file->f_mapping->host;
3590
3591 spin_lock(&inode->i_lock);
3592 if (atomic_read(&inode->i_writecount) > 1) {
3593 spin_unlock(&inode->i_lock);
3594 return -ETXTBSY;
3595 }
3596 atomic_set(&inode->i_writecount, -1);
3597 spin_unlock(&inode->i_lock);
3598
3599 return 0;
3600}
3601
3602static void restore_bitmap_write_access(struct file *file)
3603{
3604 struct inode *inode = file->f_mapping->host;
3605
3606 spin_lock(&inode->i_lock);
3607 atomic_set(&inode->i_writecount, 1);
3608 spin_unlock(&inode->i_lock);
3609}
3610
NeilBrown9e653b62006-06-26 00:27:58 -07003611/* mode:
3612 * 0 - completely stop and dis-assemble array
3613 * 1 - switch to readonly
3614 * 2 - stop but do not disassemble array
3615 */
3616static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617{
3618 int err = 0;
3619 struct gendisk *disk = mddev->gendisk;
3620
3621 if (mddev->pers) {
3622 if (atomic_read(&mddev->active)>2) {
3623 printk("md: %s still in use.\n",mdname(mddev));
3624 return -EBUSY;
3625 }
3626
3627 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003628 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3630 md_unregister_thread(mddev->sync_thread);
3631 mddev->sync_thread = NULL;
3632 }
3633
3634 del_timer_sync(&mddev->safemode_timer);
3635
3636 invalidate_partition(disk, 0);
3637
NeilBrown9e653b62006-06-26 00:27:58 -07003638 switch(mode) {
3639 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003641 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 goto out;
3643 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003644 break;
3645 case 0: /* disassemble */
3646 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003647 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003648 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 if (mddev->ro)
3650 set_disk_ro(disk, 0);
3651 blk_queue_make_request(mddev->queue, md_fail_request);
3652 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003653 mddev->queue->merge_bvec_fn = NULL;
3654 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003655 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003656 if (mddev->pers->sync_request)
3657 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 module_put(mddev->pers->owner);
3660 mddev->pers = NULL;
NeilBrown0d4ca602006-12-10 02:20:44 -08003661
3662 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003663 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003664
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 if (mddev->ro)
3666 mddev->ro = 0;
3667 }
NeilBrown850b2b422006-10-03 01:15:46 -07003668 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 /* mark array as shutdown cleanly */
3670 mddev->in_sync = 1;
NeilBrown850b2b422006-10-03 01:15:46 -07003671 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 }
NeilBrown9e653b62006-06-26 00:27:58 -07003673 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003675 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 }
NeilBrown32a76272005-06-21 17:17:14 -07003677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 /*
3679 * Free resources if final stop
3680 */
NeilBrown9e653b62006-06-26 00:27:58 -07003681 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003682 mdk_rdev_t *rdev;
3683 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3686
NeilBrown978f9462006-02-02 14:28:05 -08003687 bitmap_destroy(mddev);
3688 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003689 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003690 fput(mddev->bitmap_file);
3691 mddev->bitmap_file = NULL;
3692 }
3693 mddev->bitmap_offset = 0;
3694
NeilBrownd089c6a2008-02-06 01:39:59 -08003695 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003696 if (rdev->raid_disk >= 0) {
3697 char nm[20];
3698 sprintf(nm, "rd%d", rdev->raid_disk);
3699 sysfs_remove_link(&mddev->kobj, nm);
3700 }
3701
NeilBrown177a99b2008-02-06 01:39:56 -08003702 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003703 flush_scheduled_work();
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 export_array(mddev);
3706
3707 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003708 mddev->size = 0;
3709 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003710 mddev->recovery_cp = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003711 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003712 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003713 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003714 mddev->persistent = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003715
NeilBrowna8a55c32006-06-26 00:27:39 -07003716 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3718 mdname(mddev));
3719 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003720 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721out:
3722 return err;
3723}
3724
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003725#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726static void autorun_array(mddev_t *mddev)
3727{
3728 mdk_rdev_t *rdev;
3729 struct list_head *tmp;
3730 int err;
3731
NeilBrowna757e642005-04-16 15:26:42 -07003732 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
3735 printk(KERN_INFO "md: running: ");
3736
NeilBrownd089c6a2008-02-06 01:39:59 -08003737 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 char b[BDEVNAME_SIZE];
3739 printk("<%s>", bdevname(rdev->bdev,b));
3740 }
3741 printk("\n");
3742
3743 err = do_md_run (mddev);
3744 if (err) {
3745 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3746 do_md_stop (mddev, 0);
3747 }
3748}
3749
3750/*
3751 * lets try to run arrays based on all disks that have arrived
3752 * until now. (those are in pending_raid_disks)
3753 *
3754 * the method: pick the first pending disk, collect all disks with
3755 * the same UUID, remove all from the pending list and put them into
3756 * the 'same_array' list. Then order this list based on superblock
3757 * update time (freshest comes first), kick out 'old' disks and
3758 * compare superblocks. If everything's fine then run it.
3759 *
3760 * If "unit" is allocated, then bump its reference count
3761 */
3762static void autorun_devices(int part)
3763{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 struct list_head *tmp;
3765 mdk_rdev_t *rdev0, *rdev;
3766 mddev_t *mddev;
3767 char b[BDEVNAME_SIZE];
3768
3769 printk(KERN_INFO "md: autorun ...\n");
3770 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003771 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003773 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 rdev0 = list_entry(pending_raid_disks.next,
3775 mdk_rdev_t, same_set);
3776
3777 printk(KERN_INFO "md: considering %s ...\n",
3778 bdevname(rdev0->bdev,b));
3779 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08003780 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 if (super_90_load(rdev, rdev0, 0) >= 0) {
3782 printk(KERN_INFO "md: adding %s ...\n",
3783 bdevname(rdev->bdev,b));
3784 list_move(&rdev->same_set, &candidates);
3785 }
3786 /*
3787 * now we have a set of devices, with all of them having
3788 * mostly sane superblocks. It's time to allocate the
3789 * mddev.
3790 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003791 if (part) {
3792 dev = MKDEV(mdp_major,
3793 rdev0->preferred_minor << MdpMinorShift);
3794 unit = MINOR(dev) >> MdpMinorShift;
3795 } else {
3796 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3797 unit = MINOR(dev);
3798 }
3799 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3801 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3802 break;
3803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
3805 md_probe(dev, NULL, NULL);
3806 mddev = mddev_find(dev);
3807 if (!mddev) {
3808 printk(KERN_ERR
3809 "md: cannot allocate memory for md drive.\n");
3810 break;
3811 }
3812 if (mddev_lock(mddev))
3813 printk(KERN_WARNING "md: %s locked, cannot run\n",
3814 mdname(mddev));
3815 else if (mddev->raid_disks || mddev->major_version
3816 || !list_empty(&mddev->disks)) {
3817 printk(KERN_WARNING
3818 "md: %s already running, cannot run %s\n",
3819 mdname(mddev), bdevname(rdev0->bdev,b));
3820 mddev_unlock(mddev);
3821 } else {
3822 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08003823 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08003824 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 list_del_init(&rdev->same_set);
3826 if (bind_rdev_to_array(rdev, mddev))
3827 export_rdev(rdev);
3828 }
3829 autorun_array(mddev);
3830 mddev_unlock(mddev);
3831 }
3832 /* on success, candidates will be empty, on error
3833 * it won't...
3834 */
NeilBrown73c34432008-02-06 01:39:59 -08003835 rdev_for_each_list(rdev, tmp, candidates)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 export_rdev(rdev);
3837 mddev_put(mddev);
3838 }
3839 printk(KERN_INFO "md: ... autorun DONE.\n");
3840}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003841#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843static int get_version(void __user * arg)
3844{
3845 mdu_version_t ver;
3846
3847 ver.major = MD_MAJOR_VERSION;
3848 ver.minor = MD_MINOR_VERSION;
3849 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3850
3851 if (copy_to_user(arg, &ver, sizeof(ver)))
3852 return -EFAULT;
3853
3854 return 0;
3855}
3856
3857static int get_array_info(mddev_t * mddev, void __user * arg)
3858{
3859 mdu_array_info_t info;
3860 int nr,working,active,failed,spare;
3861 mdk_rdev_t *rdev;
3862 struct list_head *tmp;
3863
3864 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003865 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003867 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 failed++;
3869 else {
3870 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003871 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 active++;
3873 else
3874 spare++;
3875 }
3876 }
3877
3878 info.major_version = mddev->major_version;
3879 info.minor_version = mddev->minor_version;
3880 info.patch_version = MD_PATCHLEVEL_VERSION;
3881 info.ctime = mddev->ctime;
3882 info.level = mddev->level;
3883 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003884 if (info.size != mddev->size) /* overflow */
3885 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 info.nr_disks = nr;
3887 info.raid_disks = mddev->raid_disks;
3888 info.md_minor = mddev->md_minor;
3889 info.not_persistent= !mddev->persistent;
3890
3891 info.utime = mddev->utime;
3892 info.state = 0;
3893 if (mddev->in_sync)
3894 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003895 if (mddev->bitmap && mddev->bitmap_offset)
3896 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 info.active_disks = active;
3898 info.working_disks = working;
3899 info.failed_disks = failed;
3900 info.spare_disks = spare;
3901
3902 info.layout = mddev->layout;
3903 info.chunk_size = mddev->chunk_size;
3904
3905 if (copy_to_user(arg, &info, sizeof(info)))
3906 return -EFAULT;
3907
3908 return 0;
3909}
3910
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003911static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003912{
3913 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3914 char *ptr, *buf = NULL;
3915 int err = -ENOMEM;
3916
NeilBrown2a2275d2007-01-26 00:57:11 -08003917 md_allow_write(mddev);
3918
NeilBrown32a76272005-06-21 17:17:14 -07003919 file = kmalloc(sizeof(*file), GFP_KERNEL);
3920 if (!file)
3921 goto out;
3922
3923 /* bitmap disabled, zero the first byte and copy out */
3924 if (!mddev->bitmap || !mddev->bitmap->file) {
3925 file->pathname[0] = '\0';
3926 goto copy_out;
3927 }
3928
3929 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3930 if (!buf)
3931 goto out;
3932
3933 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3934 if (!ptr)
3935 goto out;
3936
3937 strcpy(file->pathname, ptr);
3938
3939copy_out:
3940 err = 0;
3941 if (copy_to_user(arg, file, sizeof(*file)))
3942 err = -EFAULT;
3943out:
3944 kfree(buf);
3945 kfree(file);
3946 return err;
3947}
3948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949static int get_disk_info(mddev_t * mddev, void __user * arg)
3950{
3951 mdu_disk_info_t info;
3952 unsigned int nr;
3953 mdk_rdev_t *rdev;
3954
3955 if (copy_from_user(&info, arg, sizeof(info)))
3956 return -EFAULT;
3957
3958 nr = info.number;
3959
3960 rdev = find_rdev_nr(mddev, nr);
3961 if (rdev) {
3962 info.major = MAJOR(rdev->bdev->bd_dev);
3963 info.minor = MINOR(rdev->bdev->bd_dev);
3964 info.raid_disk = rdev->raid_disk;
3965 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003966 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003968 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 info.state |= (1<<MD_DISK_ACTIVE);
3970 info.state |= (1<<MD_DISK_SYNC);
3971 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003972 if (test_bit(WriteMostly, &rdev->flags))
3973 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 } else {
3975 info.major = info.minor = 0;
3976 info.raid_disk = -1;
3977 info.state = (1<<MD_DISK_REMOVED);
3978 }
3979
3980 if (copy_to_user(arg, &info, sizeof(info)))
3981 return -EFAULT;
3982
3983 return 0;
3984}
3985
3986static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3987{
3988 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3989 mdk_rdev_t *rdev;
3990 dev_t dev = MKDEV(info->major,info->minor);
3991
3992 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3993 return -EOVERFLOW;
3994
3995 if (!mddev->raid_disks) {
3996 int err;
3997 /* expecting a device which has a superblock */
3998 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3999 if (IS_ERR(rdev)) {
4000 printk(KERN_WARNING
4001 "md: md_import_device returned %ld\n",
4002 PTR_ERR(rdev));
4003 return PTR_ERR(rdev);
4004 }
4005 if (!list_empty(&mddev->disks)) {
4006 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4007 mdk_rdev_t, same_set);
4008 int err = super_types[mddev->major_version]
4009 .load_super(rdev, rdev0, mddev->minor_version);
4010 if (err < 0) {
4011 printk(KERN_WARNING
4012 "md: %s has different UUID to %s\n",
4013 bdevname(rdev->bdev,b),
4014 bdevname(rdev0->bdev,b2));
4015 export_rdev(rdev);
4016 return -EINVAL;
4017 }
4018 }
4019 err = bind_rdev_to_array(rdev, mddev);
4020 if (err)
4021 export_rdev(rdev);
4022 return err;
4023 }
4024
4025 /*
4026 * add_new_disk can be used once the array is assembled
4027 * to add "hot spares". They must already have a superblock
4028 * written
4029 */
4030 if (mddev->pers) {
4031 int err;
4032 if (!mddev->pers->hot_add_disk) {
4033 printk(KERN_WARNING
4034 "%s: personality does not support diskops!\n",
4035 mdname(mddev));
4036 return -EINVAL;
4037 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004038 if (mddev->persistent)
4039 rdev = md_import_device(dev, mddev->major_version,
4040 mddev->minor_version);
4041 else
4042 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 if (IS_ERR(rdev)) {
4044 printk(KERN_WARNING
4045 "md: md_import_device returned %ld\n",
4046 PTR_ERR(rdev));
4047 return PTR_ERR(rdev);
4048 }
NeilBrown41158c72005-06-21 17:17:25 -07004049 /* set save_raid_disk if appropriate */
4050 if (!mddev->persistent) {
4051 if (info->state & (1<<MD_DISK_SYNC) &&
4052 info->raid_disk < mddev->raid_disks)
4053 rdev->raid_disk = info->raid_disk;
4054 else
4055 rdev->raid_disk = -1;
4056 } else
4057 super_types[mddev->major_version].
4058 validate_super(mddev, rdev);
4059 rdev->saved_raid_disk = rdev->raid_disk;
4060
NeilBrownb2d444d2005-11-08 21:39:31 -08004061 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004062 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4063 set_bit(WriteMostly, &rdev->flags);
4064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 rdev->raid_disk = -1;
4066 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004067 if (!err && !mddev->pers->hot_remove_disk) {
4068 /* If there is hot_add_disk but no hot_remove_disk
4069 * then added disks for geometry changes,
4070 * and should be added immediately.
4071 */
4072 super_types[mddev->major_version].
4073 validate_super(mddev, rdev);
4074 err = mddev->pers->hot_add_disk(mddev, rdev);
4075 if (err)
4076 unbind_rdev_from_array(rdev);
4077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 if (err)
4079 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07004080
NeilBrown17571282006-12-10 02:20:52 -08004081 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07004082 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004083 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 return err;
4085 }
4086
4087 /* otherwise, add_new_disk is only allowed
4088 * for major_version==0 superblocks
4089 */
4090 if (mddev->major_version != 0) {
4091 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4092 mdname(mddev));
4093 return -EINVAL;
4094 }
4095
4096 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4097 int err;
4098 rdev = md_import_device (dev, -1, 0);
4099 if (IS_ERR(rdev)) {
4100 printk(KERN_WARNING
4101 "md: error, md_import_device() returned %ld\n",
4102 PTR_ERR(rdev));
4103 return PTR_ERR(rdev);
4104 }
4105 rdev->desc_nr = info->number;
4106 if (info->raid_disk < mddev->raid_disks)
4107 rdev->raid_disk = info->raid_disk;
4108 else
4109 rdev->raid_disk = -1;
4110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004112 if (info->state & (1<<MD_DISK_SYNC))
4113 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004115 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4116 set_bit(WriteMostly, &rdev->flags);
4117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 if (!mddev->persistent) {
4119 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4120 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4121 } else
4122 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4123 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
4124
NeilBrown2bf071b2006-01-06 00:20:55 -08004125 err = bind_rdev_to_array(rdev, mddev);
4126 if (err) {
4127 export_rdev(rdev);
4128 return err;
4129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 }
4131
4132 return 0;
4133}
4134
4135static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4136{
4137 char b[BDEVNAME_SIZE];
4138 mdk_rdev_t *rdev;
4139
4140 if (!mddev->pers)
4141 return -ENODEV;
4142
4143 rdev = find_rdev(mddev, dev);
4144 if (!rdev)
4145 return -ENXIO;
4146
4147 if (rdev->raid_disk >= 0)
4148 goto busy;
4149
4150 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07004151 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004152 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
4154 return 0;
4155busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004156 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 bdevname(rdev->bdev,b), mdname(mddev));
4158 return -EBUSY;
4159}
4160
4161static int hot_add_disk(mddev_t * mddev, dev_t dev)
4162{
4163 char b[BDEVNAME_SIZE];
4164 int err;
4165 unsigned int size;
4166 mdk_rdev_t *rdev;
4167
4168 if (!mddev->pers)
4169 return -ENODEV;
4170
4171 if (mddev->major_version != 0) {
4172 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4173 " version-0 superblocks.\n",
4174 mdname(mddev));
4175 return -EINVAL;
4176 }
4177 if (!mddev->pers->hot_add_disk) {
4178 printk(KERN_WARNING
4179 "%s: personality does not support diskops!\n",
4180 mdname(mddev));
4181 return -EINVAL;
4182 }
4183
4184 rdev = md_import_device (dev, -1, 0);
4185 if (IS_ERR(rdev)) {
4186 printk(KERN_WARNING
4187 "md: error, md_import_device() returned %ld\n",
4188 PTR_ERR(rdev));
4189 return -EINVAL;
4190 }
4191
4192 if (mddev->persistent)
4193 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4194 else
4195 rdev->sb_offset =
4196 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4197
4198 size = calc_dev_size(rdev, mddev->chunk_size);
4199 rdev->size = size;
4200
NeilBrownb2d444d2005-11-08 21:39:31 -08004201 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 printk(KERN_WARNING
4203 "md: can not hot-add faulty %s disk to %s!\n",
4204 bdevname(rdev->bdev,b), mdname(mddev));
4205 err = -EINVAL;
4206 goto abort_export;
4207 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004208 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004210 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004211 err = bind_rdev_to_array(rdev, mddev);
4212 if (err)
4213 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
4215 /*
4216 * The rest should better be atomic, we can have disk failures
4217 * noticed in interrupt contexts ...
4218 */
4219
4220 if (rdev->desc_nr == mddev->max_disks) {
4221 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4222 mdname(mddev));
4223 err = -EBUSY;
4224 goto abort_unbind_export;
4225 }
4226
4227 rdev->raid_disk = -1;
4228
NeilBrown850b2b422006-10-03 01:15:46 -07004229 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
4231 /*
4232 * Kick recovery, maybe this spare has to be added to the
4233 * array immediately.
4234 */
4235 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4236 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004237 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 return 0;
4239
4240abort_unbind_export:
4241 unbind_rdev_from_array(rdev);
4242
4243abort_export:
4244 export_rdev(rdev);
4245 return err;
4246}
4247
NeilBrown32a76272005-06-21 17:17:14 -07004248static int set_bitmap_file(mddev_t *mddev, int fd)
4249{
4250 int err;
4251
NeilBrown36fa3062005-09-09 16:23:45 -07004252 if (mddev->pers) {
4253 if (!mddev->pers->quiesce)
4254 return -EBUSY;
4255 if (mddev->recovery || mddev->sync_thread)
4256 return -EBUSY;
4257 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004258 }
4259
NeilBrown36fa3062005-09-09 16:23:45 -07004260
4261 if (fd >= 0) {
4262 if (mddev->bitmap)
4263 return -EEXIST; /* cannot add when bitmap is present */
4264 mddev->bitmap_file = fget(fd);
4265
4266 if (mddev->bitmap_file == NULL) {
4267 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4268 mdname(mddev));
4269 return -EBADF;
4270 }
4271
4272 err = deny_bitmap_write_access(mddev->bitmap_file);
4273 if (err) {
4274 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4275 mdname(mddev));
4276 fput(mddev->bitmap_file);
4277 mddev->bitmap_file = NULL;
4278 return err;
4279 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004280 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004281 } else if (mddev->bitmap == NULL)
4282 return -ENOENT; /* cannot remove what isn't there */
4283 err = 0;
4284 if (mddev->pers) {
4285 mddev->pers->quiesce(mddev, 1);
4286 if (fd >= 0)
4287 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004288 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004289 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004290 fd = -1; /* make sure to put the file */
4291 }
NeilBrown36fa3062005-09-09 16:23:45 -07004292 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004293 }
4294 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004295 if (mddev->bitmap_file) {
4296 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004297 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004298 }
NeilBrown36fa3062005-09-09 16:23:45 -07004299 mddev->bitmap_file = NULL;
4300 }
4301
NeilBrown32a76272005-06-21 17:17:14 -07004302 return err;
4303}
4304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305/*
4306 * set_array_info is used two different ways
4307 * The original usage is when creating a new array.
4308 * In this usage, raid_disks is > 0 and it together with
4309 * level, size, not_persistent,layout,chunksize determine the
4310 * shape of the array.
4311 * This will always create an array with a type-0.90.0 superblock.
4312 * The newer usage is when assembling an array.
4313 * In this case raid_disks will be 0, and the major_version field is
4314 * use to determine which style super-blocks are to be found on the devices.
4315 * The minor and patch _version numbers are also kept incase the
4316 * super_block handler wishes to interpret them.
4317 */
4318static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4319{
4320
4321 if (info->raid_disks == 0) {
4322 /* just setting version number for superblock loading */
4323 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004324 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 super_types[info->major_version].name == NULL) {
4326 /* maybe try to auto-load a module? */
4327 printk(KERN_INFO
4328 "md: superblock version %d not known\n",
4329 info->major_version);
4330 return -EINVAL;
4331 }
4332 mddev->major_version = info->major_version;
4333 mddev->minor_version = info->minor_version;
4334 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004335 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 return 0;
4337 }
4338 mddev->major_version = MD_MAJOR_VERSION;
4339 mddev->minor_version = MD_MINOR_VERSION;
4340 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4341 mddev->ctime = get_seconds();
4342
4343 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004344 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 mddev->size = info->size;
4346 mddev->raid_disks = info->raid_disks;
4347 /* don't set md_minor, it is determined by which /dev/md* was
4348 * openned
4349 */
4350 if (info->state & (1<<MD_SB_CLEAN))
4351 mddev->recovery_cp = MaxSector;
4352 else
4353 mddev->recovery_cp = 0;
4354 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004355 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
4357 mddev->layout = info->layout;
4358 mddev->chunk_size = info->chunk_size;
4359
4360 mddev->max_disks = MD_SB_DISKS;
4361
NeilBrowne6910632008-02-06 01:39:51 -08004362 if (mddev->persistent)
4363 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07004364 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
NeilBrownb2a27032005-11-28 13:44:12 -08004366 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4367 mddev->bitmap_offset = 0;
4368
NeilBrownf6705572006-03-27 01:18:11 -08004369 mddev->reshape_position = MaxSector;
4370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 /*
4372 * Generate a 128 bit UUID
4373 */
4374 get_random_bytes(mddev->uuid, 16);
4375
NeilBrownf6705572006-03-27 01:18:11 -08004376 mddev->new_level = mddev->level;
4377 mddev->new_chunk = mddev->chunk_size;
4378 mddev->new_layout = mddev->layout;
4379 mddev->delta_disks = 0;
4380
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 return 0;
4382}
4383
NeilBrowna35b0d62006-01-06 00:20:49 -08004384static int update_size(mddev_t *mddev, unsigned long size)
4385{
4386 mdk_rdev_t * rdev;
4387 int rv;
4388 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004389 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004390
4391 if (mddev->pers->resize == NULL)
4392 return -EINVAL;
4393 /* The "size" is the amount of each device that is used.
4394 * This can only make sense for arrays with redundancy.
4395 * linear and raid0 always use whatever space is available
4396 * We can only consider changing the size if no resync
4397 * or reconstruction is happening, and if the new size
4398 * is acceptable. It must fit before the sb_offset or,
4399 * if that is <data_offset, it must fit before the
4400 * size of each device.
4401 * If size is zero, we find the largest size that fits.
4402 */
4403 if (mddev->sync_thread)
4404 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004405 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004406 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004407 avail = rdev->size * 2;
4408
NeilBrowna35b0d62006-01-06 00:20:49 -08004409 if (fit && (size == 0 || size > avail/2))
4410 size = avail/2;
4411 if (avail < ((sector_t)size << 1))
4412 return -ENOSPC;
4413 }
4414 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4415 if (!rv) {
4416 struct block_device *bdev;
4417
4418 bdev = bdget_disk(mddev->gendisk, 0);
4419 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004420 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004421 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004422 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004423 bdput(bdev);
4424 }
4425 }
4426 return rv;
4427}
4428
NeilBrownda943b992006-01-06 00:20:54 -08004429static int update_raid_disks(mddev_t *mddev, int raid_disks)
4430{
4431 int rv;
4432 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004433 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004434 return -EINVAL;
4435 if (raid_disks <= 0 ||
4436 raid_disks >= mddev->max_disks)
4437 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004438 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004439 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004440 mddev->delta_disks = raid_disks - mddev->raid_disks;
4441
4442 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004443 return rv;
4444}
4445
4446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447/*
4448 * update_array_info is used to change the configuration of an
4449 * on-line array.
4450 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4451 * fields in the info are checked against the array.
4452 * Any differences that cannot be handled will cause an error.
4453 * Normally, only one change can be managed at a time.
4454 */
4455static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4456{
4457 int rv = 0;
4458 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004459 int state = 0;
4460
4461 /* calculate expected state,ignoring low bits */
4462 if (mddev->bitmap && mddev->bitmap_offset)
4463 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
4465 if (mddev->major_version != info->major_version ||
4466 mddev->minor_version != info->minor_version ||
4467/* mddev->patch_version != info->patch_version || */
4468 mddev->ctime != info->ctime ||
4469 mddev->level != info->level ||
4470/* mddev->layout != info->layout || */
4471 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004472 mddev->chunk_size != info->chunk_size ||
4473 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4474 ((state^info->state) & 0xfffffe00)
4475 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 return -EINVAL;
4477 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004478 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 if (mddev->raid_disks != info->raid_disks) cnt++;
4480 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004481 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 if (cnt == 0) return 0;
4483 if (cnt > 1) return -EINVAL;
4484
4485 if (mddev->layout != info->layout) {
4486 /* Change layout
4487 * we don't need to do anything at the md level, the
4488 * personality will take care of it all.
4489 */
4490 if (mddev->pers->reconfig == NULL)
4491 return -EINVAL;
4492 else
4493 return mddev->pers->reconfig(mddev, info->layout, -1);
4494 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004495 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004496 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
NeilBrownda943b992006-01-06 00:20:54 -08004498 if (mddev->raid_disks != info->raid_disks)
4499 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
NeilBrown36fa3062005-09-09 16:23:45 -07004501 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4502 if (mddev->pers->quiesce == NULL)
4503 return -EINVAL;
4504 if (mddev->recovery || mddev->sync_thread)
4505 return -EBUSY;
4506 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4507 /* add the bitmap */
4508 if (mddev->bitmap)
4509 return -EEXIST;
4510 if (mddev->default_bitmap_offset == 0)
4511 return -EINVAL;
4512 mddev->bitmap_offset = mddev->default_bitmap_offset;
4513 mddev->pers->quiesce(mddev, 1);
4514 rv = bitmap_create(mddev);
4515 if (rv)
4516 bitmap_destroy(mddev);
4517 mddev->pers->quiesce(mddev, 0);
4518 } else {
4519 /* remove the bitmap */
4520 if (!mddev->bitmap)
4521 return -ENOENT;
4522 if (mddev->bitmap->file)
4523 return -EINVAL;
4524 mddev->pers->quiesce(mddev, 1);
4525 bitmap_destroy(mddev);
4526 mddev->pers->quiesce(mddev, 0);
4527 mddev->bitmap_offset = 0;
4528 }
4529 }
NeilBrown850b2b422006-10-03 01:15:46 -07004530 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 return rv;
4532}
4533
4534static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4535{
4536 mdk_rdev_t *rdev;
4537
4538 if (mddev->pers == NULL)
4539 return -ENODEV;
4540
4541 rdev = find_rdev(mddev, dev);
4542 if (!rdev)
4543 return -ENODEV;
4544
4545 md_error(mddev, rdev);
4546 return 0;
4547}
4548
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004549static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4550{
4551 mddev_t *mddev = bdev->bd_disk->private_data;
4552
4553 geo->heads = 2;
4554 geo->sectors = 4;
4555 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4556 return 0;
4557}
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559static int md_ioctl(struct inode *inode, struct file *file,
4560 unsigned int cmd, unsigned long arg)
4561{
4562 int err = 0;
4563 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 mddev_t *mddev = NULL;
4565
4566 if (!capable(CAP_SYS_ADMIN))
4567 return -EACCES;
4568
4569 /*
4570 * Commands dealing with the RAID driver but not any
4571 * particular array:
4572 */
4573 switch (cmd)
4574 {
4575 case RAID_VERSION:
4576 err = get_version(argp);
4577 goto done;
4578
4579 case PRINT_RAID_DEBUG:
4580 err = 0;
4581 md_print_devices();
4582 goto done;
4583
4584#ifndef MODULE
4585 case RAID_AUTORUN:
4586 err = 0;
4587 autostart_arrays(arg);
4588 goto done;
4589#endif
4590 default:;
4591 }
4592
4593 /*
4594 * Commands creating/starting a new array:
4595 */
4596
4597 mddev = inode->i_bdev->bd_disk->private_data;
4598
4599 if (!mddev) {
4600 BUG();
4601 goto abort;
4602 }
4603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 err = mddev_lock(mddev);
4605 if (err) {
4606 printk(KERN_INFO
4607 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4608 err, cmd);
4609 goto abort;
4610 }
4611
4612 switch (cmd)
4613 {
4614 case SET_ARRAY_INFO:
4615 {
4616 mdu_array_info_t info;
4617 if (!arg)
4618 memset(&info, 0, sizeof(info));
4619 else if (copy_from_user(&info, argp, sizeof(info))) {
4620 err = -EFAULT;
4621 goto abort_unlock;
4622 }
4623 if (mddev->pers) {
4624 err = update_array_info(mddev, &info);
4625 if (err) {
4626 printk(KERN_WARNING "md: couldn't update"
4627 " array info. %d\n", err);
4628 goto abort_unlock;
4629 }
4630 goto done_unlock;
4631 }
4632 if (!list_empty(&mddev->disks)) {
4633 printk(KERN_WARNING
4634 "md: array %s already has disks!\n",
4635 mdname(mddev));
4636 err = -EBUSY;
4637 goto abort_unlock;
4638 }
4639 if (mddev->raid_disks) {
4640 printk(KERN_WARNING
4641 "md: array %s already initialised!\n",
4642 mdname(mddev));
4643 err = -EBUSY;
4644 goto abort_unlock;
4645 }
4646 err = set_array_info(mddev, &info);
4647 if (err) {
4648 printk(KERN_WARNING "md: couldn't set"
4649 " array info. %d\n", err);
4650 goto abort_unlock;
4651 }
4652 }
4653 goto done_unlock;
4654
4655 default:;
4656 }
4657
4658 /*
4659 * Commands querying/configuring an existing array:
4660 */
NeilBrown32a76272005-06-21 17:17:14 -07004661 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004662 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004663 if ((!mddev->raid_disks && !mddev->external)
4664 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4665 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4666 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 err = -ENODEV;
4668 goto abort_unlock;
4669 }
4670
4671 /*
4672 * Commands even a read-only array can execute:
4673 */
4674 switch (cmd)
4675 {
4676 case GET_ARRAY_INFO:
4677 err = get_array_info(mddev, argp);
4678 goto done_unlock;
4679
NeilBrown32a76272005-06-21 17:17:14 -07004680 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004681 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004682 goto done_unlock;
4683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 case GET_DISK_INFO:
4685 err = get_disk_info(mddev, argp);
4686 goto done_unlock;
4687
4688 case RESTART_ARRAY_RW:
4689 err = restart_array(mddev);
4690 goto done_unlock;
4691
4692 case STOP_ARRAY:
4693 err = do_md_stop (mddev, 0);
4694 goto done_unlock;
4695
4696 case STOP_ARRAY_RO:
4697 err = do_md_stop (mddev, 1);
4698 goto done_unlock;
4699
4700 /*
4701 * We have a problem here : there is no easy way to give a CHS
4702 * virtual geometry. We currently pretend that we have a 2 heads
4703 * 4 sectors (with a BIG number of cylinders...). This drives
4704 * dosfs just mad... ;-)
4705 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 }
4707
4708 /*
4709 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004710 * superblock, so we do not allow them on read-only arrays.
4711 * However non-MD ioctls (e.g. get-size) will still come through
4712 * here and hit the 'default' below, so only disallow
4713 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 */
NeilBrownf91de922005-11-08 21:39:36 -08004715 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4716 mddev->ro && mddev->pers) {
4717 if (mddev->ro == 2) {
4718 mddev->ro = 0;
4719 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4720 md_wakeup_thread(mddev->thread);
4721
4722 } else {
4723 err = -EROFS;
4724 goto abort_unlock;
4725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 }
4727
4728 switch (cmd)
4729 {
4730 case ADD_NEW_DISK:
4731 {
4732 mdu_disk_info_t info;
4733 if (copy_from_user(&info, argp, sizeof(info)))
4734 err = -EFAULT;
4735 else
4736 err = add_new_disk(mddev, &info);
4737 goto done_unlock;
4738 }
4739
4740 case HOT_REMOVE_DISK:
4741 err = hot_remove_disk(mddev, new_decode_dev(arg));
4742 goto done_unlock;
4743
4744 case HOT_ADD_DISK:
4745 err = hot_add_disk(mddev, new_decode_dev(arg));
4746 goto done_unlock;
4747
4748 case SET_DISK_FAULTY:
4749 err = set_disk_faulty(mddev, new_decode_dev(arg));
4750 goto done_unlock;
4751
4752 case RUN_ARRAY:
4753 err = do_md_run (mddev);
4754 goto done_unlock;
4755
NeilBrown32a76272005-06-21 17:17:14 -07004756 case SET_BITMAP_FILE:
4757 err = set_bitmap_file(mddev, (int)arg);
4758 goto done_unlock;
4759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 err = -EINVAL;
4762 goto abort_unlock;
4763 }
4764
4765done_unlock:
4766abort_unlock:
4767 mddev_unlock(mddev);
4768
4769 return err;
4770done:
4771 if (err)
4772 MD_BUG();
4773abort:
4774 return err;
4775}
4776
4777static int md_open(struct inode *inode, struct file *file)
4778{
4779 /*
4780 * Succeed if we can lock the mddev, which confirms that
4781 * it isn't being stopped right now.
4782 */
4783 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4784 int err;
4785
NeilBrownd63a5a72006-12-08 02:36:17 -08004786 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 goto out;
4788
4789 err = 0;
4790 mddev_get(mddev);
4791 mddev_unlock(mddev);
4792
4793 check_disk_change(inode->i_bdev);
4794 out:
4795 return err;
4796}
4797
4798static int md_release(struct inode *inode, struct file * file)
4799{
4800 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4801
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004802 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 mddev_put(mddev);
4804
4805 return 0;
4806}
4807
Linus Torvalds44ce62942007-05-09 18:51:36 -07004808static int md_media_changed(struct gendisk *disk)
4809{
4810 mddev_t *mddev = disk->private_data;
4811
4812 return mddev->changed;
4813}
4814
4815static int md_revalidate(struct gendisk *disk)
4816{
4817 mddev_t *mddev = disk->private_data;
4818
4819 mddev->changed = 0;
4820 return 0;
4821}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822static struct block_device_operations md_fops =
4823{
4824 .owner = THIS_MODULE,
4825 .open = md_open,
4826 .release = md_release,
4827 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004828 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07004829 .media_changed = md_media_changed,
4830 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831};
4832
Adrian Bunk75c96f82005-05-05 16:16:09 -07004833static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
4835 mdk_thread_t *thread = arg;
4836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 /*
4838 * md_thread is a 'system-thread', it's priority should be very
4839 * high. We avoid resource deadlocks individually in each
4840 * raid personality. (RAID5 does preallocation) We also use RR and
4841 * the very same RT priority as kswapd, thus we will never get
4842 * into a priority inversion deadlock.
4843 *
4844 * we definitely have to have equal or higher priority than
4845 * bdflush, otherwise bdflush will deadlock if there are too
4846 * many dirty RAID5 blocks.
4847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
NeilBrown6985c432005-10-19 21:23:47 -07004849 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004850 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
NeilBrown93588e22005-11-15 00:09:12 -08004852 /* We need to wait INTERRUPTIBLE so that
4853 * we don't add to the load-average.
4854 * That means we need to be sure no signals are
4855 * pending
4856 */
4857 if (signal_pending(current))
4858 flush_signals(current);
4859
4860 wait_event_interruptible_timeout
4861 (thread->wqueue,
4862 test_bit(THREAD_WAKEUP, &thread->flags)
4863 || kthread_should_stop(),
4864 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
4866 clear_bit(THREAD_WAKEUP, &thread->flags);
4867
NeilBrown787453c2005-11-08 21:39:43 -08004868 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 return 0;
4872}
4873
4874void md_wakeup_thread(mdk_thread_t *thread)
4875{
4876 if (thread) {
4877 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4878 set_bit(THREAD_WAKEUP, &thread->flags);
4879 wake_up(&thread->wqueue);
4880 }
4881}
4882
4883mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4884 const char *name)
4885{
4886 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
NeilBrown9ffae0c2006-01-06 00:20:32 -08004888 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 if (!thread)
4890 return NULL;
4891
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 init_waitqueue_head(&thread->wqueue);
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 thread->run = run;
4895 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004896 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004897 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004898 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 kfree(thread);
4900 return NULL;
4901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 return thread;
4903}
4904
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905void md_unregister_thread(mdk_thread_t *thread)
4906{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004907 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07004908
4909 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 kfree(thread);
4911}
4912
4913void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4914{
4915 if (!mddev) {
4916 MD_BUG();
4917 return;
4918 }
4919
NeilBrownb2d444d2005-11-08 21:39:31 -08004920 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 return;
NeilBrown32a76272005-06-21 17:17:14 -07004922/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4924 mdname(mddev),
4925 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4926 __builtin_return_address(0),__builtin_return_address(1),
4927 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004928*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004929 if (!mddev->pers)
4930 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 if (!mddev->pers->error_handler)
4932 return;
4933 mddev->pers->error_handler(mddev,rdev);
4934 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4935 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4936 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004937 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938}
4939
4940/* seq_file implementation /proc/mdstat */
4941
4942static void status_unused(struct seq_file *seq)
4943{
4944 int i = 0;
4945 mdk_rdev_t *rdev;
4946 struct list_head *tmp;
4947
4948 seq_printf(seq, "unused devices: ");
4949
NeilBrown73c34432008-02-06 01:39:59 -08004950 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 char b[BDEVNAME_SIZE];
4952 i++;
4953 seq_printf(seq, "%s ",
4954 bdevname(rdev->bdev,b));
4955 }
4956 if (!i)
4957 seq_printf(seq, "<none>");
4958
4959 seq_printf(seq, "\n");
4960}
4961
4962
4963static void status_resync(struct seq_file *seq, mddev_t * mddev)
4964{
NeilBrown4588b422006-03-27 01:18:04 -08004965 sector_t max_blocks, resync, res;
4966 unsigned long dt, db, rt;
4967 int scale;
4968 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
4970 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4971
4972 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4973 max_blocks = mddev->resync_max_sectors >> 1;
4974 else
4975 max_blocks = mddev->size;
4976
4977 /*
4978 * Should not happen.
4979 */
4980 if (!max_blocks) {
4981 MD_BUG();
4982 return;
4983 }
NeilBrown4588b422006-03-27 01:18:04 -08004984 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4985 * in a sector_t, and (max_blocks>>scale) will fit in a
4986 * u32, as those are the requirements for sector_div.
4987 * Thus 'scale' must be at least 10
4988 */
4989 scale = 10;
4990 if (sizeof(sector_t) > sizeof(unsigned long)) {
4991 while ( max_blocks/2 > (1ULL<<(scale+32)))
4992 scale++;
4993 }
4994 res = (resync>>scale)*1000;
4995 sector_div(res, (u32)((max_blocks>>scale)+1));
4996
4997 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 {
NeilBrown4588b422006-03-27 01:18:04 -08004999 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 seq_printf(seq, "[");
5001 for (i = 0; i < x; i++)
5002 seq_printf(seq, "=");
5003 seq_printf(seq, ">");
5004 for (i = 0; i < y; i++)
5005 seq_printf(seq, ".");
5006 seq_printf(seq, "] ");
5007 }
NeilBrown4588b422006-03-27 01:18:04 -08005008 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005009 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5010 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005011 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5012 "check" :
5013 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5014 "resync" : "recovery"))),
5015 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005016 (unsigned long long) resync,
5017 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
5019 /*
5020 * We do not want to overflow, so the order of operands and
5021 * the * 100 / 100 trick are important. We do a +1 to be
5022 * safe against division by zero. We only estimate anyway.
5023 *
5024 * dt: time from mark until now
5025 * db: blocks written from mark until now
5026 * rt: remaining time
5027 */
5028 dt = ((jiffies - mddev->resync_mark) / HZ);
5029 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005030 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5031 - mddev->resync_mark_cnt;
5032 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
5034 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5035
NeilBrownff4e8d92006-07-10 04:44:16 -07005036 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037}
5038
5039static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5040{
5041 struct list_head *tmp;
5042 loff_t l = *pos;
5043 mddev_t *mddev;
5044
5045 if (l >= 0x10000)
5046 return NULL;
5047 if (!l--)
5048 /* header */
5049 return (void*)1;
5050
5051 spin_lock(&all_mddevs_lock);
5052 list_for_each(tmp,&all_mddevs)
5053 if (!l--) {
5054 mddev = list_entry(tmp, mddev_t, all_mddevs);
5055 mddev_get(mddev);
5056 spin_unlock(&all_mddevs_lock);
5057 return mddev;
5058 }
5059 spin_unlock(&all_mddevs_lock);
5060 if (!l--)
5061 return (void*)2;/* tail */
5062 return NULL;
5063}
5064
5065static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5066{
5067 struct list_head *tmp;
5068 mddev_t *next_mddev, *mddev = v;
5069
5070 ++*pos;
5071 if (v == (void*)2)
5072 return NULL;
5073
5074 spin_lock(&all_mddevs_lock);
5075 if (v == (void*)1)
5076 tmp = all_mddevs.next;
5077 else
5078 tmp = mddev->all_mddevs.next;
5079 if (tmp != &all_mddevs)
5080 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5081 else {
5082 next_mddev = (void*)2;
5083 *pos = 0x10000;
5084 }
5085 spin_unlock(&all_mddevs_lock);
5086
5087 if (v != (void*)1)
5088 mddev_put(mddev);
5089 return next_mddev;
5090
5091}
5092
5093static void md_seq_stop(struct seq_file *seq, void *v)
5094{
5095 mddev_t *mddev = v;
5096
5097 if (mddev && v != (void*)1 && v != (void*)2)
5098 mddev_put(mddev);
5099}
5100
NeilBrownd7603b72006-01-06 00:20:30 -08005101struct mdstat_info {
5102 int event;
5103};
5104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105static int md_seq_show(struct seq_file *seq, void *v)
5106{
5107 mddev_t *mddev = v;
5108 sector_t size;
5109 struct list_head *tmp2;
5110 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005111 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005112 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
5114 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005115 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 seq_printf(seq, "Personalities : ");
5117 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005118 list_for_each_entry(pers, &pers_list, list)
5119 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120
5121 spin_unlock(&pers_lock);
5122 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005123 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 return 0;
5125 }
5126 if (v == (void*)2) {
5127 status_unused(seq);
5128 return 0;
5129 }
5130
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005131 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005133
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5135 seq_printf(seq, "%s : %sactive", mdname(mddev),
5136 mddev->pers ? "" : "in");
5137 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005138 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005140 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005141 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 seq_printf(seq, " %s", mddev->pers->name);
5143 }
5144
5145 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005146 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 char b[BDEVNAME_SIZE];
5148 seq_printf(seq, " %s[%d]",
5149 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005150 if (test_bit(WriteMostly, &rdev->flags))
5151 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005152 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 seq_printf(seq, "(F)");
5154 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005155 } else if (rdev->raid_disk < 0)
5156 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 size += rdev->size;
5158 }
5159
5160 if (!list_empty(&mddev->disks)) {
5161 if (mddev->pers)
5162 seq_printf(seq, "\n %llu blocks",
5163 (unsigned long long)mddev->array_size);
5164 else
5165 seq_printf(seq, "\n %llu blocks",
5166 (unsigned long long)size);
5167 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005168 if (mddev->persistent) {
5169 if (mddev->major_version != 0 ||
5170 mddev->minor_version != 90) {
5171 seq_printf(seq," super %d.%d",
5172 mddev->major_version,
5173 mddev->minor_version);
5174 }
NeilBrowne6910632008-02-06 01:39:51 -08005175 } else if (mddev->external)
5176 seq_printf(seq, " super external:%s",
5177 mddev->metadata_type);
5178 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005179 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
5181 if (mddev->pers) {
5182 mddev->pers->status (seq, mddev);
5183 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005184 if (mddev->pers->sync_request) {
5185 if (mddev->curr_resync > 2) {
5186 status_resync (seq, mddev);
5187 seq_printf(seq, "\n ");
5188 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5189 seq_printf(seq, "\tresync=DELAYED\n ");
5190 else if (mddev->recovery_cp < MaxSector)
5191 seq_printf(seq, "\tresync=PENDING\n ");
5192 }
NeilBrown32a76272005-06-21 17:17:14 -07005193 } else
5194 seq_printf(seq, "\n ");
5195
5196 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005197 unsigned long chunk_kb;
5198 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005199 spin_lock_irqsave(&bitmap->lock, flags);
5200 chunk_kb = bitmap->chunksize >> 10;
5201 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5202 "%lu%s chunk",
5203 bitmap->pages - bitmap->missing_pages,
5204 bitmap->pages,
5205 (bitmap->pages - bitmap->missing_pages)
5206 << (PAGE_SHIFT - 10),
5207 chunk_kb ? chunk_kb : bitmap->chunksize,
5208 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005209 if (bitmap->file) {
5210 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005211 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005212 }
NeilBrown78d742d2005-06-21 17:17:15 -07005213
NeilBrown32a76272005-06-21 17:17:14 -07005214 seq_printf(seq, "\n");
5215 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 }
5217
5218 seq_printf(seq, "\n");
5219 }
5220 mddev_unlock(mddev);
5221
5222 return 0;
5223}
5224
5225static struct seq_operations md_seq_ops = {
5226 .start = md_seq_start,
5227 .next = md_seq_next,
5228 .stop = md_seq_stop,
5229 .show = md_seq_show,
5230};
5231
5232static int md_seq_open(struct inode *inode, struct file *file)
5233{
5234 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005235 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5236 if (mi == NULL)
5237 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
5239 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005240 if (error)
5241 kfree(mi);
5242 else {
5243 struct seq_file *p = file->private_data;
5244 p->private = mi;
5245 mi->event = atomic_read(&md_event_count);
5246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 return error;
5248}
5249
NeilBrownd7603b72006-01-06 00:20:30 -08005250static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5251{
5252 struct seq_file *m = filp->private_data;
5253 struct mdstat_info *mi = m->private;
5254 int mask;
5255
5256 poll_wait(filp, &md_event_waiters, wait);
5257
5258 /* always allow read */
5259 mask = POLLIN | POLLRDNORM;
5260
5261 if (mi->event != atomic_read(&md_event_count))
5262 mask |= POLLERR | POLLPRI;
5263 return mask;
5264}
5265
Arjan van de Venfa027c22007-02-12 00:55:33 -08005266static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005267 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 .open = md_seq_open,
5269 .read = seq_read,
5270 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005271 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005272 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273};
5274
NeilBrown2604b702006-01-06 00:20:36 -08005275int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005278 list_add_tail(&p->list, &pers_list);
5279 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 spin_unlock(&pers_lock);
5281 return 0;
5282}
5283
NeilBrown2604b702006-01-06 00:20:36 -08005284int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285{
NeilBrown2604b702006-01-06 00:20:36 -08005286 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005288 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 spin_unlock(&pers_lock);
5290 return 0;
5291}
5292
5293static int is_mddev_idle(mddev_t *mddev)
5294{
5295 mdk_rdev_t * rdev;
5296 struct list_head *tmp;
5297 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005298 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
5300 idle = 1;
NeilBrownd089c6a2008-02-06 01:39:59 -08005301 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005303 curr_events = disk_stat_read(disk, sectors[0]) +
5304 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005306 /* sync IO will cause sync_io to increase before the disk_stats
5307 * as sync_io is counted when a request starts, and
5308 * disk_stats is counted when it completes.
5309 * So resync activity will cause curr_events to be smaller than
5310 * when there was no such activity.
5311 * non-sync IO will cause disk_stat to increase without
5312 * increasing sync_io so curr_events will (eventually)
5313 * be larger than it was before. Once it becomes
5314 * substantially larger, the test below will cause
5315 * the array to appear non-idle, and resync will slow
5316 * down.
5317 * If there is a lot of outstanding resync activity when
5318 * we set last_event to curr_events, then all that activity
5319 * completing might cause the array to appear non-idle
5320 * and resync will be slowed down even though there might
5321 * not have been non-resync activity. This will only
5322 * happen once though. 'last_events' will soon reflect
5323 * the state where there is little or no outstanding
5324 * resync requests, and further resync activity will
5325 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005326 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005328 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 rdev->last_events = curr_events;
5330 idle = 0;
5331 }
5332 }
5333 return idle;
5334}
5335
5336void md_done_sync(mddev_t *mddev, int blocks, int ok)
5337{
5338 /* another "blocks" (512byte) blocks have been synced */
5339 atomic_sub(blocks, &mddev->recovery_active);
5340 wake_up(&mddev->recovery_wait);
5341 if (!ok) {
5342 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5343 md_wakeup_thread(mddev->thread);
5344 // stop recovery, signal do_sync ....
5345 }
5346}
5347
5348
NeilBrown06d91a52005-06-21 17:17:12 -07005349/* md_write_start(mddev, bi)
5350 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005351 * in superblock) before writing, schedule a superblock update
5352 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005353 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005354void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355{
NeilBrown06d91a52005-06-21 17:17:12 -07005356 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005357 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005358
NeilBrownf91de922005-11-08 21:39:36 -08005359 BUG_ON(mddev->ro == 1);
5360 if (mddev->ro == 2) {
5361 /* need to switch to read/write */
5362 mddev->ro = 0;
5363 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5364 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005365 md_wakeup_thread(mddev->sync_thread);
NeilBrownf91de922005-11-08 21:39:36 -08005366 }
NeilBrown06d91a52005-06-21 17:17:12 -07005367 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005368 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005369 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005370 if (mddev->in_sync) {
5371 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005372 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005373 md_wakeup_thread(mddev->thread);
5374 }
NeilBrowna9701a32005-11-08 21:39:34 -08005375 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005376 }
NeilBrown850b2b422006-10-03 01:15:46 -07005377 wait_event(mddev->sb_wait, mddev->flags==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378}
5379
5380void md_write_end(mddev_t *mddev)
5381{
5382 if (atomic_dec_and_test(&mddev->writes_pending)) {
5383 if (mddev->safemode == 2)
5384 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005385 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5387 }
5388}
5389
NeilBrown2a2275d2007-01-26 00:57:11 -08005390/* md_allow_write(mddev)
5391 * Calling this ensures that the array is marked 'active' so that writes
5392 * may proceed without blocking. It is important to call this before
5393 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5394 * Must be called with mddev_lock held.
5395 */
5396void md_allow_write(mddev_t *mddev)
5397{
5398 if (!mddev->pers)
5399 return;
5400 if (mddev->ro)
5401 return;
5402
5403 spin_lock_irq(&mddev->write_lock);
5404 if (mddev->in_sync) {
5405 mddev->in_sync = 0;
5406 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5407 if (mddev->safemode_delay &&
5408 mddev->safemode == 0)
5409 mddev->safemode = 1;
5410 spin_unlock_irq(&mddev->write_lock);
5411 md_update_sb(mddev, 0);
5412 } else
5413 spin_unlock_irq(&mddev->write_lock);
5414}
5415EXPORT_SYMBOL_GPL(md_allow_write);
5416
Adrian Bunk75c96f82005-05-05 16:16:09 -07005417static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
5419#define SYNC_MARKS 10
5420#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005421void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422{
5423 mddev_t *mddev2;
5424 unsigned int currspeed = 0,
5425 window;
NeilBrown57afd892005-06-21 17:17:13 -07005426 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 unsigned long mark[SYNC_MARKS];
5428 sector_t mark_cnt[SYNC_MARKS];
5429 int last_mark,m;
5430 struct list_head *tmp;
5431 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005432 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005433 struct list_head *rtmp;
5434 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005435 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
5437 /* just incase thread restarts... */
5438 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5439 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005440 if (mddev->ro) /* never try to sync a read-only array */
5441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
NeilBrown61df9d92006-10-03 01:15:57 -07005443 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5444 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5445 desc = "data-check";
5446 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5447 desc = "requested-resync";
5448 else
5449 desc = "resync";
5450 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5451 desc = "reshape";
5452 else
5453 desc = "recovery";
5454
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 /* we overload curr_resync somewhat here.
5456 * 0 == not engaged in resync at all
5457 * 2 == checking that there is no conflict with another sync
5458 * 1 == like 2, but have yielded to allow conflicting resync to
5459 * commense
5460 * other == active in resync - this many blocks
5461 *
5462 * Before starting a resync we must have set curr_resync to
5463 * 2, and then checked that every "conflicting" array has curr_resync
5464 * less than ours. When we find one that is the same or higher
5465 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5466 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5467 * This will mean we have to start checking from the beginning again.
5468 *
5469 */
5470
5471 do {
5472 mddev->curr_resync = 2;
5473
5474 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005475 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005476 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 goto skip;
5478 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005479 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 if (mddev2 == mddev)
5481 continue;
5482 if (mddev2->curr_resync &&
5483 match_mddev_units(mddev,mddev2)) {
5484 DEFINE_WAIT(wq);
5485 if (mddev < mddev2 && mddev->curr_resync == 2) {
5486 /* arbitrarily yield */
5487 mddev->curr_resync = 1;
5488 wake_up(&resync_wait);
5489 }
5490 if (mddev > mddev2 && mddev->curr_resync == 1)
5491 /* no need to wait here, we can wait the next
5492 * time 'round when curr_resync == 2
5493 */
5494 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005495 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5496 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005497 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005498 printk(KERN_INFO "md: delaying %s of %s"
5499 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005501 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 mddev_put(mddev2);
5503 schedule();
5504 finish_wait(&resync_wait, &wq);
5505 goto try_again;
5506 }
5507 finish_wait(&resync_wait, &wq);
5508 }
5509 }
5510 } while (mddev->curr_resync < 2);
5511
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005512 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005513 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005515 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 */
5517 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005518 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005519 /* we don't use the checkpoint if there's a bitmap */
5520 if (!mddev->bitmap &&
5521 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5522 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005523 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5524 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005525 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 /* recovery follows the physical size of devices */
5527 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005528 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005529 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005530 if (rdev->raid_disk >= 0 &&
5531 !test_bit(Faulty, &rdev->flags) &&
5532 !test_bit(In_sync, &rdev->flags) &&
5533 rdev->recovery_offset < j)
5534 j = rdev->recovery_offset;
5535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
NeilBrown61df9d92006-10-03 01:15:57 -07005537 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5538 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5539 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005540 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005541 "(but not more than %d KB/sec) for %s.\n",
5542 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
5544 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005545
NeilBrown57afd892005-06-21 17:17:13 -07005546 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 for (m = 0; m < SYNC_MARKS; m++) {
5548 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005549 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 }
5551 last_mark = 0;
5552 mddev->resync_mark = mark[last_mark];
5553 mddev->resync_mark_cnt = mark_cnt[last_mark];
5554
5555 /*
5556 * Tune reconstruction:
5557 */
5558 window = 32*(PAGE_SIZE/512);
5559 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5560 window/2,(unsigned long long) max_sectors/2);
5561
5562 atomic_set(&mddev->recovery_active, 0);
5563 init_waitqueue_head(&mddev->recovery_wait);
5564 last_check = 0;
5565
5566 if (j>2) {
5567 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005568 "md: resuming %s of %s from checkpoint.\n",
5569 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 mddev->curr_resync = j;
5571 }
5572
5573 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005574 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
NeilBrown57afd892005-06-21 17:17:13 -07005576 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005577 if (j >= mddev->resync_max) {
5578 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5579 wait_event(mddev->recovery_wait,
5580 mddev->resync_max > j
5581 || kthread_should_stop());
5582 }
5583 if (kthread_should_stop())
5584 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005585 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005586 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005587 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5589 goto out;
5590 }
NeilBrown57afd892005-06-21 17:17:13 -07005591
5592 if (!skipped) { /* actual IO requested */
5593 io_sectors += sectors;
5594 atomic_add(sectors, &mddev->recovery_active);
5595 }
5596
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 j += sectors;
5598 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005599 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005600 if (last_check == 0)
5601 /* this is the earliers that rebuilt will be
5602 * visible in /proc/mdstat
5603 */
5604 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005605
5606 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 continue;
5608
NeilBrown57afd892005-06-21 17:17:13 -07005609 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
5611 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5612 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5613 break;
5614
5615 repeat:
5616 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5617 /* step marks */
5618 int next = (last_mark+1) % SYNC_MARKS;
5619
5620 mddev->resync_mark = mark[next];
5621 mddev->resync_mark_cnt = mark_cnt[next];
5622 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005623 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 last_mark = next;
5625 }
5626
5627
NeilBrownc6207272008-02-06 01:39:52 -08005628 if (kthread_should_stop())
5629 goto interrupted;
5630
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
5632 /*
5633 * this loop exits only if either when we are slower than
5634 * the 'hard' speed limit, or the system was IO-idle for
5635 * a jiffy.
5636 * the system might be non-idle CPU-wise, but we only care
5637 * about not overloading the IO subsystem. (things like an
5638 * e2fsck being done on the RAID array should execute fast)
5639 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005640 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 cond_resched();
5642
NeilBrown57afd892005-06-21 17:17:13 -07005643 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5644 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
NeilBrown88202a02006-01-06 00:21:36 -08005646 if (currspeed > speed_min(mddev)) {
5647 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005649 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 goto repeat;
5651 }
5652 }
5653 }
NeilBrown61df9d92006-10-03 01:15:57 -07005654 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 /*
5656 * this also signals 'finished resyncing' to md_stop
5657 */
5658 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005659 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
5661 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5662
5663 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005664 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
5666 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005667 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005668 mddev->curr_resync > 2) {
5669 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5670 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5671 if (mddev->curr_resync >= mddev->recovery_cp) {
5672 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005673 "md: checkpointing %s of %s.\n",
5674 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005675 mddev->recovery_cp = mddev->curr_resync;
5676 }
5677 } else
5678 mddev->recovery_cp = MaxSector;
5679 } else {
5680 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5681 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005682 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005683 if (rdev->raid_disk >= 0 &&
5684 !test_bit(Faulty, &rdev->flags) &&
5685 !test_bit(In_sync, &rdev->flags) &&
5686 rdev->recovery_offset < mddev->curr_resync)
5687 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 }
NeilBrown17571282006-12-10 02:20:52 -08005690 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 skip:
5693 mddev->curr_resync = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005694 mddev->resync_max = MaxSector;
5695 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 wake_up(&resync_wait);
5697 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5698 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005699 return;
5700
5701 interrupted:
5702 /*
5703 * got a signal, exit.
5704 */
5705 printk(KERN_INFO
5706 "md: md_do_sync() got signal ... exiting\n");
5707 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5708 goto out;
5709
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710}
NeilBrown29269552006-03-27 01:18:10 -08005711EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
5713
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005714static int remove_and_add_spares(mddev_t *mddev)
5715{
5716 mdk_rdev_t *rdev;
5717 struct list_head *rtmp;
5718 int spares = 0;
5719
NeilBrownd089c6a2008-02-06 01:39:59 -08005720 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005721 if (rdev->raid_disk >= 0 &&
NeilBrownc303da62008-02-06 01:39:51 -08005722 !mddev->external &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005723 (test_bit(Faulty, &rdev->flags) ||
5724 ! test_bit(In_sync, &rdev->flags)) &&
5725 atomic_read(&rdev->nr_pending)==0) {
5726 if (mddev->pers->hot_remove_disk(
5727 mddev, rdev->raid_disk)==0) {
5728 char nm[20];
5729 sprintf(nm,"rd%d", rdev->raid_disk);
5730 sysfs_remove_link(&mddev->kobj, nm);
5731 rdev->raid_disk = -1;
5732 }
5733 }
5734
5735 if (mddev->degraded) {
NeilBrownd089c6a2008-02-06 01:39:59 -08005736 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005737 if (rdev->raid_disk < 0
5738 && !test_bit(Faulty, &rdev->flags)) {
5739 rdev->recovery_offset = 0;
5740 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5741 char nm[20];
5742 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005743 if (sysfs_create_link(&mddev->kobj,
5744 &rdev->kobj, nm))
5745 printk(KERN_WARNING
5746 "md: cannot register "
5747 "%s for %s\n",
5748 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005749 spares++;
5750 md_new_event(mddev);
5751 } else
5752 break;
5753 }
5754 }
5755 return spares;
5756}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757/*
5758 * This routine is regularly called by all per-raid-array threads to
5759 * deal with generic issues like resync and super-block update.
5760 * Raid personalities that don't have a thread (linear/raid0) do not
5761 * need this as they never do any recovery or update the superblock.
5762 *
5763 * It does not do any resync itself, but rather "forks" off other threads
5764 * to do that as needed.
5765 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5766 * "->recovery" and create a thread at ->sync_thread.
5767 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5768 * and wakeups up this thread which will reap the thread and finish up.
5769 * This thread also removes any faulty devices (with nr_pending == 0).
5770 *
5771 * The overall approach is:
5772 * 1/ if the superblock needs updating, update it.
5773 * 2/ If a recovery thread is running, don't do anything else.
5774 * 3/ If recovery has finished, clean up, possibly marking spares active.
5775 * 4/ If there are any faulty devices, remove them.
5776 * 5/ If array is degraded, try to add spares devices
5777 * 6/ If array has spares or is not in-sync, start a resync thread.
5778 */
5779void md_check_recovery(mddev_t *mddev)
5780{
5781 mdk_rdev_t *rdev;
5782 struct list_head *rtmp;
5783
5784
NeilBrown5f404022005-06-21 17:17:16 -07005785 if (mddev->bitmap)
5786 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
5788 if (mddev->ro)
5789 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005790
5791 if (signal_pending(current)) {
5792 if (mddev->pers->sync_request) {
5793 printk(KERN_INFO "md: %s in immediate safe mode\n",
5794 mdname(mddev));
5795 mddev->safemode = 2;
5796 }
5797 flush_signals(current);
5798 }
5799
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08005801 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005803 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5804 (mddev->safemode == 1) ||
5805 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5806 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 ))
5808 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005809
NeilBrowndf5b89b2006-03-27 01:18:20 -08005810 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005811 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07005812
NeilBrowna9701a32005-11-08 21:39:34 -08005813 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005814 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5815 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5816 mddev->in_sync = 1;
NeilBrowne6910632008-02-06 01:39:51 -08005817 if (mddev->persistent)
5818 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownfca4d842005-06-21 17:17:11 -07005819 }
5820 if (mddev->safemode == 1)
5821 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005822 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005823
NeilBrown850b2b422006-10-03 01:15:46 -07005824 if (mddev->flags)
5825 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005826
NeilBrown06d91a52005-06-21 17:17:12 -07005827
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5829 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5830 /* resync/recovery still happening */
5831 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5832 goto unlock;
5833 }
5834 if (mddev->sync_thread) {
5835 /* resync has finished, collect result */
5836 md_unregister_thread(mddev->sync_thread);
5837 mddev->sync_thread = NULL;
5838 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5839 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5840 /* success...*/
5841 /* activate any spares */
5842 mddev->pers->spare_active(mddev);
5843 }
NeilBrown850b2b422006-10-03 01:15:46 -07005844 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005845
5846 /* if array is no-longer degraded, then any saved_raid_disk
5847 * information must be scrapped
5848 */
5849 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08005850 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07005851 rdev->saved_raid_disk = -1;
5852
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 mddev->recovery = 0;
5854 /* flag recovery needed just to double check */
5855 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005856 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 goto unlock;
5858 }
NeilBrown24dd4692005-11-08 21:39:26 -08005859 /* Clear some bits that don't mean anything, but
5860 * might be left set
5861 */
5862 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5863 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5864 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5865 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005867 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5868 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 /* no recovery is running.
5870 * remove any failed drives, then
5871 * add spares if possible.
5872 * Spare are also removed and re-added, to allow
5873 * the personality to fail the re-add.
5874 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005876 if (mddev->reshape_position != MaxSector) {
5877 if (mddev->pers->check_reshape(mddev) != 0)
5878 /* Cannot proceed */
5879 goto unlock;
5880 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5881 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08005882 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5883 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5884 } else if (mddev->recovery_cp < MaxSector) {
5885 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5886 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5887 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005889
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 if (mddev->pers->sync_request) {
5891 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005892 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5893 /* We are adding a device or devices to an array
5894 * which has the bitmap stored on all devices.
5895 * So make sure all bitmap pages get written
5896 */
5897 bitmap_write_all(mddev->bitmap);
5898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 mddev->sync_thread = md_register_thread(md_do_sync,
5900 mddev,
5901 "%s_resync");
5902 if (!mddev->sync_thread) {
5903 printk(KERN_ERR "%s: could not start resync"
5904 " thread...\n",
5905 mdname(mddev));
5906 /* leave the spares where they are, it shouldn't hurt */
5907 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005908 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005910 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 }
5912 unlock:
5913 mddev_unlock(mddev);
5914 }
5915}
5916
Adrian Bunk75c96f82005-05-05 16:16:09 -07005917static int md_notify_reboot(struct notifier_block *this,
5918 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919{
5920 struct list_head *tmp;
5921 mddev_t *mddev;
5922
5923 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5924
5925 printk(KERN_INFO "md: stopping all md devices.\n");
5926
NeilBrown29ac4aa2008-02-06 01:39:58 -08005927 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005928 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005930 mddev_unlock(mddev);
5931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 /*
5933 * certain more exotic SCSI devices are known to be
5934 * volatile wrt too early system reboots. While the
5935 * right place to handle this issue is the given
5936 * driver, we do want to have a safe RAID driver ...
5937 */
5938 mdelay(1000*1);
5939 }
5940 return NOTIFY_DONE;
5941}
5942
Adrian Bunk75c96f82005-05-05 16:16:09 -07005943static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 .notifier_call = md_notify_reboot,
5945 .next = NULL,
5946 .priority = INT_MAX, /* before any real devices */
5947};
5948
5949static void md_geninit(void)
5950{
5951 struct proc_dir_entry *p;
5952
5953 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5954
5955 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5956 if (p)
5957 p->proc_fops = &md_seq_fops;
5958}
5959
Adrian Bunk75c96f82005-05-05 16:16:09 -07005960static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 if (register_blkdev(MAJOR_NR, "md"))
5963 return -1;
5964 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5965 unregister_blkdev(MAJOR_NR, "md");
5966 return -1;
5967 }
NeilBrowne8703fe2006-10-03 01:15:59 -07005968 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
5969 md_probe, NULL, NULL);
5970 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 md_probe, NULL, NULL);
5972
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08005974 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975
5976 md_geninit();
5977 return (0);
5978}
5979
5980
5981#ifndef MODULE
5982
5983/*
5984 * Searches all registered partitions for autorun RAID arrays
5985 * at boot time.
5986 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005987
5988static LIST_HEAD(all_detected_devices);
5989struct detected_devices_node {
5990 struct list_head list;
5991 dev_t dev;
5992};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993
5994void md_autodetect_dev(dev_t dev)
5995{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005996 struct detected_devices_node *node_detected_dev;
5997
5998 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
5999 if (node_detected_dev) {
6000 node_detected_dev->dev = dev;
6001 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6002 } else {
6003 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6004 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006}
6007
6008
6009static void autostart_arrays(int part)
6010{
6011 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006012 struct detected_devices_node *node_detected_dev;
6013 dev_t dev;
6014 int i_scanned, i_passed;
6015
6016 i_scanned = 0;
6017 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
6019 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6020
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006021 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6022 i_scanned++;
6023 node_detected_dev = list_entry(all_detected_devices.next,
6024 struct detected_devices_node, list);
6025 list_del(&node_detected_dev->list);
6026 dev = node_detected_dev->dev;
6027 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006028 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 if (IS_ERR(rdev))
6030 continue;
6031
NeilBrownb2d444d2005-11-08 21:39:31 -08006032 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 MD_BUG();
6034 continue;
6035 }
NeilBrownd0fae182008-03-04 14:29:31 -08006036 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006038 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006040
6041 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6042 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
6044 autorun_devices(part);
6045}
6046
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006047#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
6049static __exit void md_exit(void)
6050{
6051 mddev_t *mddev;
6052 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006053
NeilBrowne8703fe2006-10-03 01:15:59 -07006054 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6055 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056
6057 unregister_blkdev(MAJOR_NR,"md");
6058 unregister_blkdev(mdp_major, "mdp");
6059 unregister_reboot_notifier(&md_notifier);
6060 unregister_sysctl_table(raid_table_header);
6061 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006062 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 struct gendisk *disk = mddev->gendisk;
6064 if (!disk)
6065 continue;
6066 export_array(mddev);
6067 del_gendisk(disk);
6068 put_disk(disk);
6069 mddev->gendisk = NULL;
6070 mddev_put(mddev);
6071 }
6072}
6073
Dan Williams685784a2007-07-09 11:56:42 -07006074subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075module_exit(md_exit)
6076
NeilBrownf91de922005-11-08 21:39:36 -08006077static int get_ro(char *buffer, struct kernel_param *kp)
6078{
6079 return sprintf(buffer, "%d", start_readonly);
6080}
6081static int set_ro(const char *val, struct kernel_param *kp)
6082{
6083 char *e;
6084 int num = simple_strtoul(val, &e, 10);
6085 if (*val && (*e == '\0' || *e == '\n')) {
6086 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006087 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006088 }
6089 return -EINVAL;
6090}
6091
NeilBrown80ca3a42006-07-10 04:44:18 -07006092module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6093module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006094
NeilBrownf91de922005-11-08 21:39:36 -08006095
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096EXPORT_SYMBOL(register_md_personality);
6097EXPORT_SYMBOL(unregister_md_personality);
6098EXPORT_SYMBOL(md_error);
6099EXPORT_SYMBOL(md_done_sync);
6100EXPORT_SYMBOL(md_write_start);
6101EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102EXPORT_SYMBOL(md_register_thread);
6103EXPORT_SYMBOL(md_unregister_thread);
6104EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105EXPORT_SYMBOL(md_check_recovery);
6106MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006107MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006108MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);